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.

172 lines
3.5 KiB

  1. #!/bin/sh
  2. #
  3. # Copyright (C) 2020 TDT AG <development@tdt.de>
  4. #
  5. # This is free software, licensed under the GNU General Public License v2.
  6. # See https://www.gnu.org/licenses/gpl-2.0.txt for more information.
  7. #
  8. . /lib/functions.sh
  9. DDNS_PACKAGE_DIR="/usr/share/ddns"
  10. URL="https://raw.githubusercontent.com/openwrt/packages/master/net/ddns-scripts/files"
  11. usage() {
  12. local code="$1"
  13. local msg="$2"
  14. echo "$msg"
  15. echo ""
  16. echo "Usage: $(basename "$0") <command> <action> <service>"
  17. echo ""
  18. echo "Supported ddns <command>:"
  19. echo " service: Command for custom ddns service providers"
  20. echo ""
  21. echo "Supported ddns 'service' command <action>:"
  22. echo " update: Update local custom ddns service list"
  23. echo " list-available: List all available custom service providers"
  24. echo " list-installed: List all installed custom service providers"
  25. echo " install <service>: Install custom service provider"
  26. echo " remove <service>: Remove custom service provider"
  27. echo " purge: Remove local custom ddns services"
  28. exit "$code"
  29. }
  30. action_update() {
  31. local cacert
  32. config_load ddns
  33. config_get url global 'url' "${URL}${DDNS_PACKAGE_DIR}"
  34. config_get cacert global 'cacert' "IGNORE"
  35. url="${url}/list"
  36. mkdir -p "${DDNS_PACKAGE_DIR}"
  37. if [ "$cacert" = "IGNORE" ]; then
  38. uclient-fetch \
  39. --no-check-certificate \
  40. "$url" \
  41. -O "${DDNS_PACKAGE_DIR}/list"
  42. elif [ -f "$cacert" ]; then
  43. uclient-fetch \
  44. --ca-certificate="${cacert}" \
  45. "$url" \
  46. -O "${DDNS_PACKAGE_DIR}/list"
  47. elif [ -n "$cacert" ]; then
  48. echo "Certification file not found ($cacert)"
  49. exit 5
  50. fi
  51. }
  52. action_list_available() {
  53. if [ -f "${DDNS_PACKAGE_DIR}/list" ]; then
  54. cat "${DDNS_PACKAGE_DIR}/list"
  55. else
  56. echo "No custom service list file found. Please download first"
  57. exit 3
  58. fi
  59. }
  60. action_list_installed() {
  61. if [ -d "${DDNS_PACKAGE_DIR}/custom" ]; then
  62. ls "${DDNS_PACKAGE_DIR}/custom"
  63. else
  64. echo "No custom services installed"
  65. exit 4
  66. fi
  67. }
  68. action_install() {
  69. local service="$1"
  70. local url cacert
  71. config_load ddns
  72. config_get url global 'url' "${URL}${DDNS_PACKAGE_DIR}/default"
  73. config_get cacert global 'cacert' "IGNORE"
  74. url="${url}/${service}.json"
  75. if [ -z "$service" ]; then
  76. usage "4" "No custom service specified"
  77. fi
  78. mkdir -p "${DDNS_PACKAGE_DIR}/custom"
  79. if [ "$cacert" = "IGNORE" ]; then
  80. uclient-fetch \
  81. --no-check-certificate \
  82. "${url}" \
  83. -O "${DDNS_PACKAGE_DIR}/custom/${service}.json"
  84. elif [ -f "$cacert" ]; then
  85. uclient-fetch \
  86. --ca-certifcate="${cacert}" \
  87. "${url}" \
  88. -O "${DDNS_PACKAGE_DIR}/custom/${service}.json"
  89. elif [ -n "$cacert" ]; then
  90. echo "Certification file not found ($cacert)"
  91. exit 5
  92. fi
  93. }
  94. action_remove() {
  95. local service="$1"
  96. if [ -z "$service" ]; then
  97. usage "4" "No custom service specified"
  98. fi
  99. rm "${DDNS_PACKAGE_DIR}/custom/${service}.json"
  100. }
  101. action_purge() {
  102. rm -rf "${DDNS_PACKAGE_DIR}/custom"
  103. rm -rf "${DDNS_PACKAGE_DIR}/list"
  104. }
  105. sub_service() {
  106. local action="$1"
  107. local service="$2"
  108. case "$action" in
  109. update)
  110. action_update
  111. ;;
  112. list-available)
  113. action_list_available
  114. ;;
  115. list-installed)
  116. action_list_installed
  117. ;;
  118. purge)
  119. action_purge
  120. ;;
  121. install)
  122. action_install "$service"
  123. ;;
  124. remove)
  125. action_remove "$service"
  126. ;;
  127. *)
  128. usage "2" "Action not supported"
  129. ;;
  130. esac
  131. }
  132. main() {
  133. local cmd="$1"
  134. local action="$2"
  135. local service="$3"
  136. [ "$#" -eq 0 ] && usage "1"
  137. case "${cmd}" in
  138. service)
  139. sub_service "${action}" "${service}"
  140. ;;
  141. *)
  142. usage "1" "Command not supported"
  143. ;;
  144. esac
  145. }
  146. main "$@"