freedombone-freedns 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # freedns update command for use in cron or a daemon
  10. # License
  11. # =======
  12. #
  13. # Copyright (C) 2016-2018 Bob Mottram <bob@freedombone.net>
  14. #
  15. # This program is free software: you can redistribute it and/or modify
  16. # it under the terms of the GNU Affero General Public License as published by
  17. # the Free Software Foundation, either version 3 of the License, or
  18. # (at your option) any later version.
  19. #
  20. # This program is distributed in the hope that it will be useful,
  21. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. # GNU Affero General Public License for more details.
  24. #
  25. # You should have received a copy of the GNU Affero General Public License
  26. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. PROJECT_NAME='freedombone'
  28. export TEXTDOMAIN=${PROJECT_NAME}-freedns
  29. export TEXTDOMAINDIR="/usr/share/locale"
  30. VERBOSE=
  31. CONFIGURATION_FILE=$HOME/${PROJECT_NAME}.cfg
  32. FREEDNS_WGET='wget --read-timeout=0.0 --waitretry=5 --tries=4 https://freedns.afraid.org/dynamic/update.php?'
  33. if [[ "$1" == "--verbose" || "$1" == "-v" ]]; then
  34. VERBOSE=1
  35. fi
  36. if [ ! -f "$CONFIGURATION_FILE" ]; then
  37. exit 0
  38. fi
  39. function item_in_array {
  40. local e
  41. for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
  42. return 1
  43. }
  44. detected_codes=()
  45. codelines=$(grep "_CODE=" "$CONFIGURATION_FILE" | uniq)
  46. while read -r line; do
  47. code=$(echo "$line" | awk -F '=' '{print $2}')
  48. # shellcheck disable=SC2068
  49. if ! item_in_array "$code" ${detected_codes[@]}; then
  50. detected_codes+=("$code")
  51. fi
  52. done <<< "$codelines"
  53. if [ ! -d "$HOME/.freedns-update" ]; then
  54. mkdir "$HOME/.freedns-update"
  55. fi
  56. cd "$HOME/.freedns-update" || exit 2467824684
  57. # shellcheck disable=SC2068
  58. for code in ${detected_codes[@]}
  59. do
  60. if [ $VERBOSE ]; then
  61. echo $"command: $FREEDNS_WGET${code}="
  62. # shellcheck disable=SC1066,SC1067
  63. $FREEDNS_WGET${code}=
  64. else
  65. if [ -f /tmp/freedns ]; then
  66. rm /tmp/freedns
  67. fi
  68. # shellcheck disable=SC1066,SC1067,SC1007
  69. $FREEDNS_WGET${code}= >> /tmp/freedns 2>&1
  70. fi
  71. done
  72. if [ -f /tmp/freedns ]; then
  73. rm /tmp/freedns
  74. fi
  75. exit 0