blob: 921465f7e492480f4adec3257394a91250c1c18b [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001#!/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
9. /lib/functions.sh
10
11DDNS_PACKAGE_DIR="/usr/share/ddns"
12URL="https://raw.githubusercontent.com/openwrt/packages/master/net/ddns-scripts/files"
13
14usage() {
15 local code="$1"
16 local msg="$2"
17
18 echo "$msg"
19 echo ""
20 echo "Usage: $(basename "$0") <command> <action> <service>"
21 echo ""
22 echo "Supported ddns <command>:"
23 echo " service: Command for custom ddns service providers"
24 echo ""
25 echo "Supported ddns 'service' command <action>:"
26 echo " update: Update local custom ddns service list"
27 echo " list-available: List all available custom service providers"
28 echo " list-installed: List all installed custom service providers"
29 echo " install <service>: Install custom service provider"
30 echo " remove <service>: Remove custom service provider"
31 echo " purge: Remove local custom ddns services"
32
33 exit "$code"
34}
35
36action_update() {
37 local cacert
38
39 config_load ddns
40 config_get url global 'url' "${URL}${DDNS_PACKAGE_DIR}"
41 config_get cacert global 'cacert' "IGNORE"
42 url="${url}/list"
43
44 mkdir -p "${DDNS_PACKAGE_DIR}"
45
46 if [ "$cacert" = "IGNORE" ]; then
47 uclient-fetch \
48 --no-check-certificate \
49 "$url" \
50 -O "${DDNS_PACKAGE_DIR}/list"
51 elif [ -f "$cacert" ]; then
52 uclient-fetch \
53 --ca-certificate="${cacert}" \
54 "$url" \
55 -O "${DDNS_PACKAGE_DIR}/list"
56 elif [ -n "$cacert" ]; then
57 echo "Certification file not found ($cacert)"
58 exit 5
59 fi
60}
61
62action_list_available() {
63 if [ -f "${DDNS_PACKAGE_DIR}/list" ]; then
64 cat "${DDNS_PACKAGE_DIR}/list"
65 else
66 echo "No custom service list file found. Please download first"
67 exit 3
68 fi
69}
70
71action_list_installed() {
72 if [ -d "${DDNS_PACKAGE_DIR}/custom" ]; then
73 ls "${DDNS_PACKAGE_DIR}/custom"
74 else
75 echo "No custom services installed"
76 exit 4
77 fi
78}
79
80action_install() {
81 local service="$1"
82
83 local url cacert
84
85 config_load ddns
86 config_get url global 'url' "${URL}${DDNS_PACKAGE_DIR}/default"
87 config_get cacert global 'cacert' "IGNORE"
88 url="${url}/${service}.json"
89
90 if [ -z "$service" ]; then
91 usage "4" "No custom service specified"
92 fi
93
94 mkdir -p "${DDNS_PACKAGE_DIR}/custom"
95
96 if [ "$cacert" = "IGNORE" ]; then
97 uclient-fetch \
98 --no-check-certificate \
99 "${url}" \
100 -O "${DDNS_PACKAGE_DIR}/custom/${service}.json"
101 elif [ -f "$cacert" ]; then
102 uclient-fetch \
103 --ca-certifcate="${cacert}" \
104 "${url}" \
105 -O "${DDNS_PACKAGE_DIR}/custom/${service}.json"
106 elif [ -n "$cacert" ]; then
107 echo "Certification file not found ($cacert)"
108 exit 5
109 fi
110}
111
112action_remove() {
113 local service="$1"
114 if [ -z "$service" ]; then
115 usage "4" "No custom service specified"
116 fi
117
118 rm "${DDNS_PACKAGE_DIR}/custom/${service}.json"
119}
120
121action_purge() {
122 rm -rf "${DDNS_PACKAGE_DIR}/custom"
123 rm -rf "${DDNS_PACKAGE_DIR}/list"
124}
125
126sub_service() {
127 local action="$1"
128 local service="$2"
129
130 case "$action" in
131 update)
132 action_update
133 ;;
134 list-available)
135 action_list_available
136 ;;
137 list-installed)
138 action_list_installed
139 ;;
140 purge)
141 action_purge
142 ;;
143 install)
144 action_install "$service"
145 ;;
146 remove)
147 action_remove "$service"
148 ;;
149 *)
150 usage "2" "Action not supported"
151 ;;
152 esac
153}
154
155main() {
156 local cmd="$1"
157 local action="$2"
158 local service="$3"
159
160 [ "$#" -eq 0 ] && usage "1"
161
162 case "${cmd}" in
163 service)
164 sub_service "${action}" "${service}"
165 ;;
166 *)
167 usage "1" "Command not supported"
168 ;;
169 esac
170}
171
172main "$@"