-
Notifications
You must be signed in to change notification settings - Fork 31
/
ztnetworks.bash
312 lines (188 loc) · 4.89 KB
/
ztnetworks.bash
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/bin/bash
# Script to manage the networks for self-hosted ZT Controllers
source "functions.bash"
# Check if user ID is 0
if [[ $EUID -ne 0 ]]; then
echo "################################"
echo "This script must be run as root"
echo "################################"
exit 1
fi
# Temp File
tmpfile='/tmp/znetwork.tmp'
function allDone() {
read -p "${1}. Press Enter to finish"
${2}
}
# Check that the number is valid entered
function checkNum() {
if ! [[ ${1} =~ ^[0-9]$ ]]; then
allDone "A numeric selection is required" "mainMenu"
fi
}
# Create the network
function createNet() {
read -p "Please enter a description for the network: " netDesc
genIP
# Create the network
CONTROLLER_ID=$(zerotier-cli info | cut -d' ' -f 3)
newNet=$(curl -s -X POST \
-H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" \
-d '{"name": "'"${netDesc}"'"}' \
"${ztAddress}/${CONTROLLER_ID}______" | jq '(.nwid)')
if [[ "${newNet}" =~ ^\"[0-9a-f]{16}\" ]]; then
echo "Network created. ID: ${newNet}."
read -p "Press ENTER when done."
get_mask ${ipnet} ${newNet}
else
allDone "Network was not created" mainMenu
fi
}
# Updates the network's IP Assignment
function updateNetIP() {
allNets
theNet=$(echo "${net}" | awk ' { print $1 } ')
genIP
if [[ "${ipnet}" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
get_mask ${ipnet} ${theNet}
else
allDone "IP was not created" "mainMenu"
fi
}
# Update the network's description
function updateDesc() {
allNets
theNet=$(echo "${net}" | awk ' { print $1 } ')
read -p "Please enter the updated description for the network: " netDesc
json=$(jq -n --arg desc "${netDesc}" '{ name: $desc }')
chk_jq
# Update the network description
updateNet=$(curl -s -X POST \
-H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" \
-d "$json" \
"${ztAddress}/${theNet}" | jq -r '(.name)')
if [[ "${updateNet}" == "${netDesc}" ]]; then
allDone "${updateNet} description has been updated" mainMenu
else
allDone "${updateNet} description was not updated" mainMenu
fi
}
function mainMenu() {
# Remove temp file
if [[ -f ${tmpfile} ]]; then
rm -f ${tmpfile}
fi
clear
echo "################################"
echo " ZeroTier Manager Controller "
echo "################################"
echo ""
echo "1. Create a new ZT Network on this controller"
echo "2. Delete a ZT Network on this controller"
echo "3. Peer Management"
echo "4. Edit Flow Rules for Network"
echo "5. List all networks"
echo "6. Manage Routes"
echo "7. Update Network Description"
echo "8. Update Network IP Assignment"
echo "[A]dvanced Options (edit /var/lib/zerotier/local.conf"
echo "[E]xit"
read -p "Please select a numeric value: " todo
case "${todo}" in
1)
clear
createNet
;;
2)
clear
echo "Please wait..."
if [[ -f ${tmpfile} ]]; then
rm -f ${tmpfile}
fi
allNets
delnet=$(echo "${net}" | awk ' { print $1 } ')
read -p "Are you sure you want to delete the network => ${net} [y|N]:" todelete
if [[ "${todelete}" == "y" || "${todelete}" == "yes" ]]; then
# Delete the network
curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" -X DELETE "${ztAddress}/${delnet}"
# Query to see if it was deleted
thenDelete=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" -X DELETE "${ztAddress}/${delnet}")
if [[ "${thenDelete}" == "{}" ]]; then
allDone "The network => ${net} was deleted" "mainMenu"
else
allDone "The network => ${net} was not deleted" "mainMenu"
fi
fi
;;
3)
clear
bash peer.bash
;;
4) # Edit rules file
clear
cd flowRules
# List networks
allNets "mainMenu:flows"
# The network the user selected
editNet=$(echo "${net}" | awk ' { print $1 } ')
# If the rules file exists
if [[ -f ${editNet}.config ]]; then
# Edit it
nano ${editNet}.config
else
# If not, copy the config
cp default.flows ${editNet}.config
nano ${editNet}.config
fi
function cleanup() {
rm -f ztconfig-${editNet}.tmp
rm -f ${editNet}.config.json
}
clear
read -p "Would you like to commit the changes? Y|n: " goflow
if [[ "${goflow}" =~ ^(y|Y)$ ]]; then
bash ztrules.bash ${editNet}.config
cleanup
cd ..
echo ""
read -p "Rules committed. Be sure to test. Press Enter when done."
mainMenu
else
echo ""
read -p "Changes will not be committed. Press enter when done."
cd ..
mainMenu
fi
;;
5)
clear
bash listnets.bash
;;
6)
clear
bash ztroutes.bash
;;
7) # Update network description
clear
updateDesc
;;
8) # Update Network IP Assignment
clear
updateNetIP
;;
a|A)
clear
bash advancedZT.bash
;;
e|E)
echo "Exiting..."
exit 0
;;
*)
echo "Invalid option."
read -p "Press ENTER when done."
;;
esac
mainMenu
}
mainMenu