aboutsummaryrefslogtreecommitdiff
path: root/net/ddns-scripts/files/usr/bin/ddns.sh
blob: 921465f7e492480f4adec3257394a91250c1c18b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/bin/sh
#
# Copyright (C) 2020 TDT AG <development@tdt.de>
#
# This is free software, licensed under the GNU General Public License v2.
# See https://www.gnu.org/licenses/gpl-2.0.txt for more information.
#

. /lib/functions.sh

DDNS_PACKAGE_DIR="/usr/share/ddns"
URL="https://raw.githubusercontent.com/openwrt/packages/master/net/ddns-scripts/files"

usage() {
	local code="$1"
	local msg="$2"

	echo "$msg"
	echo ""
	echo "Usage: $(basename "$0") <command> <action> <service>"
	echo ""
	echo "Supported ddns <command>:"
	echo "  service:  Command for custom ddns service providers"
	echo ""
	echo "Supported ddns 'service' command <action>:"
	echo "  update:             Update local custom ddns service list"
	echo "  list-available:     List all available custom service providers"
	echo "  list-installed:     List all installed custom service providers"
	echo "  install <service>:  Install custom service provider"
	echo "  remove <service>:   Remove custom service provider"
	echo "  purge:              Remove local custom ddns services"

	exit "$code"
}

action_update() {
	local cacert

	config_load ddns
	config_get url global 'url' "${URL}${DDNS_PACKAGE_DIR}"
	config_get cacert global 'cacert' "IGNORE"
	url="${url}/list"

	mkdir -p "${DDNS_PACKAGE_DIR}"

	if [ "$cacert" = "IGNORE" ]; then
		uclient-fetch \
			--no-check-certificate \
			"$url" \
			-O "${DDNS_PACKAGE_DIR}/list"
	elif [ -f "$cacert" ]; then
		uclient-fetch \
			--ca-certificate="${cacert}" \
			"$url" \
			-O "${DDNS_PACKAGE_DIR}/list"
	elif [ -n "$cacert" ]; then
		echo "Certification file not found ($cacert)"
		exit 5
	fi
}

action_list_available() {
	if [ -f "${DDNS_PACKAGE_DIR}/list" ]; then
		cat "${DDNS_PACKAGE_DIR}/list"
	else
		echo "No custom service list file found. Please download first"
		exit 3
	fi
}

action_list_installed() {
	if [ -d "${DDNS_PACKAGE_DIR}/custom" ]; then
		ls "${DDNS_PACKAGE_DIR}/custom"
	else
		echo "No custom services installed"
		exit 4
	fi
}

action_install() {
	local service="$1"

	local url cacert

	config_load ddns
	config_get url global 'url' "${URL}${DDNS_PACKAGE_DIR}/default"
	config_get cacert global 'cacert' "IGNORE"
	url="${url}/${service}.json"

	if [ -z "$service" ]; then
		usage "4" "No custom service specified"
	fi

	mkdir -p "${DDNS_PACKAGE_DIR}/custom"

	if [ "$cacert" = "IGNORE" ]; then
		uclient-fetch \
			--no-check-certificate \
			"${url}" \
			-O "${DDNS_PACKAGE_DIR}/custom/${service}.json"
	elif [ -f "$cacert" ]; then
		uclient-fetch \
			--ca-certifcate="${cacert}" \
			"${url}" \
			-O "${DDNS_PACKAGE_DIR}/custom/${service}.json"
	elif [ -n "$cacert" ]; then
		echo "Certification file not found ($cacert)"
		exit 5
	fi
}

action_remove() {
	local service="$1"
	if [ -z "$service" ]; then
		usage "4" "No custom service specified"
	fi

	rm "${DDNS_PACKAGE_DIR}/custom/${service}.json"
}

action_purge() {
	rm -rf "${DDNS_PACKAGE_DIR}/custom"
	rm -rf "${DDNS_PACKAGE_DIR}/list"
}

sub_service() {
	local action="$1"
	local service="$2"

	case "$action" in
		update)
			action_update
			;;
		list-available)
			action_list_available
			;;
		list-installed)
			action_list_installed
			;;
		purge)
			action_purge
			;;
		install)
			action_install "$service"
			;;
		remove)
			action_remove "$service"
			;;
		*)
			usage "2" "Action not supported"
			;;
	esac
}

main() {
	local cmd="$1"
	local action="$2"
	local service="$3"

	[ "$#" -eq 0 ] && usage "1"

	case "${cmd}" in
		service)
			sub_service "${action}" "${service}"
			;;
		*)
			usage "1" "Command not supported"
			;;
	esac
}

main "$@"