aboutsummaryrefslogtreecommitdiff
path: root/net/zerotier/files/etc/init.d/zerotier
blob: 84002f9526f7956c90392b2205abbc56834592b9 (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
#!/bin/sh /etc/rc.common

START=90

USE_PROCD=1

PROG=/usr/bin/zerotier-one
CONFIG_PATH=/var/lib/zerotier-one

section_enabled() {
	config_get_bool enabled "$1" 'enabled' 0
	[ $enabled -ne 0 ]
}

start_instance() {
	local cfg="$1"
	local port secret config_path local_conf copy_config_path path
	local args=""

	if ! section_enabled "$cfg"; then
		echo "disabled in /etc/config/zerotier"
		return 1
	fi

	config_get config_path $cfg 'config_path'
	config_get port $cfg 'port'
	config_get secret $cfg 'secret'
	config_get local_conf $cfg 'local_conf'
	config_get_bool copy_config_path $cfg 'copy_config_path' 0

	path=${CONFIG_PATH}_$cfg

	# Remove existing link or folder
	rm -rf $path

	# Create link or copy files from CONFIG_PATH to config_path
	if [ -n "$config_path" -a "$config_path" != "$path" ]; then
		if [ ! -d "$config_path" ]; then
			echo "ZeroTier config_path does not exist: $config_path" 1>&2
			return
		fi

		# ensure that the target exists
		mkdir -p $(dirname $path)

		if [ "$copy_config_path" = "1" ]; then
			cp -r $config_path $path
		else
			ln -s $config_path $path
		fi
	fi

	mkdir -p $path/networks.d

	# link latest default config path to latest config path
	rm -f $CONFIG_PATH
	ln -s $path $CONFIG_PATH

	if [ -n "$port" ]; then
		args="$args -p${port}"
	fi

	if [ -z "$secret" ]; then
		echo "Generate secret - please wait..."
		local sf="/tmp/zt.$cfg.secret"

		zerotier-idtool generate "$sf" > /dev/null
		[ $? -ne 0 ] && return 1

		secret="$(cat $sf)"
		rm "$sf"

		uci set zerotier.$cfg.secret="$secret"
		uci commit zerotier
	fi

	if [ -n "$secret" ]; then
		echo "$secret" > $path/identity.secret
		# make sure there is not previous identity.public
		rm -f $path/identity.public
	fi

	if [ -f "$local_conf" ]; then
		ln -s "$local_conf" $path/local.conf
	fi

	add_join() {
		# an (empty) config file will cause ZT to join a network
		touch $path/networks.d/$1.conf
	}

	config_list_foreach $cfg 'join' add_join

	procd_open_instance
	procd_set_param command $PROG $args $path
	procd_set_param stderr 1
	procd_set_param respawn
	procd_close_instance
}

start_service() {
	config_load 'zerotier'
	config_foreach start_instance 'zerotier'
}

stop_instance() {
	local cfg="$1"

	# Remove existing link or folder
	rm -rf ${CONFIG_PATH}_${cfg}
}

stop_service() {
	config_load 'zerotier'
	config_foreach stop_instance 'zerotier'
	rm -f ${CONFIG_PATH}
}

reload_service() {
	stop
	start
}

service_triggers() {
	procd_add_reload_trigger 'zerotier'
}