You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.4 KiB

  1. #!/bin/sh
  2. # /usr/lib/ddns/luci_dns_helper.sh
  3. #
  4. #.Distributed under the terms of the GNU General Public License (GPL) version 2.0
  5. #
  6. # Written in August 2014 by
  7. #.Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
  8. # This script is used by luci-app-ddns
  9. # - getting registered IP
  10. # - check if possible to get local IP
  11. # - verifing given DNS- or Proxy-Server
  12. #
  13. # variables in small chars are read from /etc/config/ddns as parameter given here
  14. # variables in big chars are defined inside these scripts as gloval vars
  15. # variables in big chars beginning with "__" are local defined inside functions only
  16. # set -vx #script debugger
  17. [ $# -lt 2 ] && exit 1
  18. . /usr/lib/ddns/dynamic_dns_functions.sh # global vars are also defined here
  19. # preset some variables, wrong or not set in dynamic_dns_functions.sh
  20. SECTION_ID="lucihelper"
  21. LOGFILE="$LOGDIR/$SECTION_ID.log"
  22. DATFILE="$RUNDIR/$SECTION_ID.$$.dat" # save stdout data of WGet and other extern programs called
  23. ERRFILE="$RUNDIR/$SECTION_ID.$$.err" # save stderr output of WGet and other extern programs called
  24. VERBOSE_MODE=0 # no console logging
  25. # global variables normally set by reading DDNS UCI configuration
  26. use_syslog=0 # no syslog
  27. use_logfile=0 # by default no logfile, can be changed here
  28. __RET=0
  29. case "$1" in
  30. get_registered_ip)
  31. local IP
  32. lookup_host=$2 # FQDN of host registered at DDNS
  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. write_log 7 "-----> get_registered_ip IP"
  38. get_registered_ip IP
  39. __RET=$?
  40. [ $__RET -ne 0 ] && IP=""
  41. echo -n "$IP" # suppress LF
  42. ;;
  43. verify_dns)
  44. # $2 : dns-server to verify # no need for force_dnstcp because
  45. # verify with nc (netcat) uses tcp anyway
  46. use_ipv6=${3:-"0"} # Use IPv6 - default IPv4
  47. force_ipversion=${4:-"0"} # Force IP Version - default 0 - No
  48. write_log 7 "-----> verify_dns '$2'"
  49. verify_dns "$2"
  50. __RET=$?
  51. ;;
  52. verify_proxy)
  53. # $2 : proxy string to verify
  54. use_ipv6=${3:-"0"} # Use IPv6 - default IPv4
  55. force_ipversion=${4:-"0"} # Force IP Version - default 0 - No
  56. write_log 7 "-----> verify_proxy '$2'"
  57. verify_proxy "$2"
  58. __RET=$?
  59. ;;
  60. get_local_ip)
  61. local IP
  62. use_ipv6="$2" # Use IPv6
  63. ip_source="$3" # IP source
  64. ip_network="$4" # set if source = "network" otherwise "-"
  65. ip_url="$5" # set if source = "web" otherwise "-"
  66. ip_interface="$6" # set if source = "interface" itherwiase "-"
  67. ip_script="$7" # set if source = "script" otherwise "-"
  68. proxy="$8" # proxy if set
  69. force_ipversion="0" # not needed but must be set
  70. use_https="0" # not needed but must be set
  71. [ -n "$proxy" -a "$ip_source" = "web" ] && {
  72. # proxy defined, used for ip_source=web
  73. export HTTP_PROXY="http://$proxy"
  74. export HTTPS_PROXY="http://$proxy"
  75. export http_proxy="http://$proxy"
  76. export https_proxy="http://$proxy"
  77. }
  78. # don't need IP only the return code
  79. [ "$ip_source" = "web" -o "$ip_source" = "script" ] && {
  80. # we wait only 3 seconds for an
  81. # answer from "web" or "script"
  82. write_log 7 "-----> timeout 3 -- get_local_ip IP"
  83. timeout 3 -- get_local_ip IP
  84. } || {
  85. write_log 7 "-----> get_local_ip IP"
  86. get_local_ip IP
  87. }
  88. __RET=$?
  89. ;;
  90. *)
  91. __RET=255
  92. ;;
  93. esac
  94. # remove out and err file
  95. [ -f $DATFILE ] && rm -f $DATFILE
  96. [ -f $ERRFILE ] && rm -f $ERRFILE
  97. return $__RET