1782d1f038492dd53ce6cffe594930dd84b0ddb5
[project/luci.git] / applications / luci-ddns / root / usr / lib / ddns / dynamic_dns_lucihelper.sh
1 #!/bin/sh
2 # /usr/lib/ddns/luci_dns_helper.sh
3 #
4 # Written by Christian Schoenebeck in August 2014 to support:
5 # this script is used by luci-app-ddns
6 # - getting registered IP
7 # - check if possible to get local IP
8 # - verifing given DNS- or Proxy-Server
9 #
10 # variables in small chars are read from /etc/config/ddns
11 # variables in big chars are defined inside these scripts as gloval vars
12 # variables in big chars beginning with "__" are local defined inside functions only
13 # set -vx       #script debugger
14
15 [ $# -lt 2 ] && exit 1
16
17 . /usr/lib/ddns/dynamic_dns_functions.sh        # global vars are also defined here
18
19 # set -vx       #script debugger
20
21 # preset some variables wrong or not set in dynamic_dns_functions.sh
22 SECTION_ID="dynamic_dns_lucihelper"
23 LOGFILE="$LOGDIR/$SECTION_ID.log"
24 LUCI_HELPER="ACTIV"     # supress verbose and critical logging
25 # global variables normally set by reading DDNS UCI configuration
26 use_logfile=0
27 use_syslog=0
28
29 case "$1" in
30         get_registered_ip)
31                 local IP
32                 domain=$2                       # Hostname/Domain
33                 use_ipv6=${3:-"0"}              # Use IPv6 - default IPv4
34                 force_ipversion=${4:-"0"}       # Force IP Version - default 0 - No
35                 force_dnstcp=${5:-"0"}          # Force TCP on DNS - default 0 - No
36                 dns_server=${6:-""}             # DNS server - default No DNS
37                 get_registered_ip IP
38                 [ $? -ne 0 ] && IP=""
39                 echo -n "$IP"                   # suppress LF
40                 ;;
41         verify_dns)
42                 # $2 == dns-server to verify    # no need for force_dnstcp because
43                                                 # verify with nc (netcat) uses tcp anyway
44                 use_ipv6=${3:-"0"}              # Use IPv6 - default IPv4
45                 force_ipversion=${4:-"0"}       # Force IP Version - default 0 - No
46                 verify_dns "$2"
47                 ;;
48         verify_proxy)
49                 # $2 == proxy string to verify
50                 use_ipv6=${3:-"0"}              # Use IPv6 - default IPv4
51                 force_ipversion=${4:-"0"}       # Force IP Version - default 0 - No
52                 verify_proxy "$2"
53                 ;;
54         get_local_ip)
55                 local IP
56                 use_ipv6="$2"                   # Use IPv6
57                 ip_source="$3"                  # IP source
58                 ip_network="$4"                 # set if source = "network" otherwise "-"
59                 ip_url="$5"                     # set if source = "web" otherwise "-"
60                 ip_interface="$6"               # set if source = "interface" itherwiase "-"
61                 ip_script="$7"                  # set if source = "script" otherwise "-"
62                 proxy="$8"                      # proxy if set
63                 force_ipversion="0"             # not needed but must be set
64                 use_https="0"                   # not needed but must be set
65                 [ -n "$proxy" -a "$ip_source" == "web" ] && {
66                         # proxy defined, used for ip_source=web
67                         export HTTP_PROXY="http://$proxy"
68                         export HTTPS_PROXY="http://$proxy"
69                         export http_proxy="http://$proxy"
70                         export https_proxy="http://$proxy"
71                 }
72                 # don't need IP only the return code
73                 [ "$ip_source" == "web" -o  "$ip_source" == "script"] && {              
74                         # we wait only 3 seconds for an 
75                         # answer from "web" or "script"
76                         __timeout 3 -- get_local_ip IP
77                 } || get_local_ip IP
78                 ;;
79         *)      
80                 return 1
81                 ;;
82 esac