freedombone-freedns 2.5KB

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