forked from thedunston/bash_cli_zt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer.bash
646 lines (401 loc) · 15 KB
/
peer.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
#!/bin/bash
# Script to manage peers
# Oscar's Blog was very helpful in learning to query and edit ZT API
# https://blog.ogarcia.me/zerotier/
source "functions.bash"
if [[ -f "${peerTempFile}" ]]; then
rm "${peerTempFile}"
fi
# Get all networks
allNets "exit 0"
# Get the selected network
theNet=$(cat 'tmp/ztcurrent.txt')
echo ""
echo "############# PEER MANAGEMENT #################"
echo "Please wait..."
clear
echo "######### WHICH PEER NETWORK DO YOU WANT TO MANAGE #######"
echo ""
# Create the network's directory if it
# doesn't exist and members
if [[ ! -d "networks/${theNet}" ]]; then
mkdir -p "networks/${theNet}"
fi
function existingPeerInfo() {
# Get existing peer information
exPeerName="$(grep PEERNAME networks/${theNet}/${themem} | cut -d: -f2 )"
exPeerDesc="$(grep PEERDESC networks/${theNet}/${themem} | cut -d: -f2 )"
themem_info="ID: ${themem} Name: ${exPeerName} Description: ${exPeerDesc}"
}
function delTemp() {
# Delete temp file if it exists
if [[ -f ${tmpPeerFile} ]]; then
rm -f ${tmpPeerFile}
fi
}
function selectMem() {
# Get a list of all the peers
PEERS=$(cat ${tmpPeerFile} |grep -v Peer | column -t -s " " | sed 's/___/ /g')
SELECTION=1
while read -r line; do
echo "$SELECTION) $line"
((SELECTION++))
#echo "[E] Exit"
done <<< "$PEERS"
((SELECTION--))
echo
printf "Select the number next to the peer to ${1} or "E" to not ${1} Peers: "
read -r opt
if [[ ${opt} == "E" ]]; then
peerManage
fi
}
function getAllPeers() {
# Add header to file
echo "Peer IP Name" > ${tmpPeerFile}
# Get all the members
for themem in $(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member"| egrep -o '[a-f0-9]{10}'); do
# Check if the peer is authorized
ifAuth=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member/${themem}" | jq '.authorized')
ifIP=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member/${themem}" | jq -r '.ipAssignments[]')
# ...is so then display it.
if [[ "${ifAuth}" == "true" ]]; then
existingName=$(grep PEERNAME "networks/${theNet}/${themem}" | cut -d: -f2)
echo "${themem} ${ifIP} ${existingName}" >> ${tmpPeerFile}
fi
done
# Check to see if there are any peers
if [[ -f ${tmpPeerFile} || ! -f ${tmpPeerFile} ]]; then
if [[ ! -s ${tmpPeerFile} ]]; then
echo "There are no authorized peers."
read -p "Press ENTER when done."
fi
fi
}
function peerManage() {
# Create members if they do not exist.
for themem in $(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member"| egrep -o '[a-f0-9]{10}'); do
if [[ ! -f "networks/${theNet}/${themem}" ]]; then
touch networks/${theNet}/${themem}
fi
done
clear
delTemp
echo "##################################################"
echo "Network ID and Name: ${net}"
echo "##################################################"
echo "1. List all peers"
echo "2. List all unauthorized peers"
echo "3. List only authorized peers"
echo "4. Deauthorize a peer"
echo "5. 'Delete' a peer"
echo "6. Add/Change a peer's name or description"
echo "7. 'UnDelete' a peer"
echo "[Z] Back to Network Configuration Main Menu"
echo "[E] Exit Program"
read -p " Please select a number value: " todo
case "${todo}" in
1) # List all peers
clear
# Add header to file
echo "Peer IP Name" > ${tmpPeerFile}
for themem in $(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member"| egrep -o '[a-f0-9]{10}'); do
# Check if the member is authorized.
ifIP=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member/${themem}" |jq -r '.ipAssignments[]')
ifAuth=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member/${themem}" |jq '.authorized')
# If the user is authorized, don't show them
if [[ ("${ifAuth}" =~ "false" && "${ifIP}" =~ "127.0.0.100") ]]; then
continue
else
# Get existing peer details
existingPeerInfo
# Write results to the temp file.
echo "${themem} ${ifIP} ${exPeerName}" >> ${tmpPeerFile}
fi
done
if [[ "$(cat ${tmpPeerFile})" =~ [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} ]]; then
cat ${tmpPeerFile} | column -t -s " " | sed 's/___/ /g'
read -p "Hit Enter when done."
else
echo "There are no members."
read -p "Press ENTER when done."
peerManage
fi
;;
2) # List all unauthorized peers
clear
delTemp
# Get all the members
for themem in $(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member"| egrep -o '[a-f0-9]{10}'); do
# Check if the member is authorized.
ifIP=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member/${themem}" |jq '.ipAssignments[0]')
ifAuth=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member/${themem}" |jq '.authorized')
# If the user is authorized, don't show them
if [[ "${ifIP}" =~ "127.0.0.100" || ("${ifAuth}" == "true") ]]; then
continue
else
# Write results to the temp file.
echo "${themem}" >> ${tmpPeerFile}
fi
done
# Authorize a peer
function authPeers() {
selectMem "Authorize"
# Authorize the member
if [[ $(seq 1 $SELECTION) =~ $opt ]]; then
# Get the selection value
thePeer=$(sed -n "${opt}p" <<< "${PEERS}")
themem=$(echo "${thePeer}" | awk ' { print $1 } ')
authed=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" -X POST -d '{"authorized": true}' "${ztAddress}/${theNet}/member/${themem}")
fi
}
# Check to see if there are any peers
if [[ -f ${tmpPeerFile} ]]; then
authPeers
else
echo "There are no unauthorized peers."
read -p "Press ENTER when done."
fi
;;
3) # List authorized Peers
clear
delTemp
# Add header to file
echo "Peer IP Name" > ${tmpPeerFile}
# Get all the members
for themem in $(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member"| egrep -o '[a-f0-9]{10}'); do
# Check if the peer is authorized
ifAuth=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member/${themem}" | jq '.authorized')
ifIP=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member/${themem}" | jq -r '.ipAssignments[]')
# ...is so then display it.
if [[ "${ifAuth}" == "true" ]]; then
# Get existing Peer info details
existingPeerInfo
echo "${themem} ${ifIP} ${exPeerName}" >> ${tmpPeerFile}
fi
done
# Check to see if there are any peers
if [[ -f ${tmpPeerFile} || ! -f ${tmpPeerFile} ]]; then
if [[ ! -s ${tmpPeerFile} ]]; then
echo "There are no authorized peers."
read -p "Press ENTER when done."
else
cat ${tmpPeerFile} | column -t -s " " | sed 's/___/ /g'
echo ""
read -p "Press ENTER when done."
fi
fi
;;
4)
clear
delTemp
# Add header to file
echo "Peer IP Name" > ${tmpPeerFile}
# Get all the members
for themem in $(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member"| egrep -o '[a-f0-9]{10}'); do
# Check if the peer is authorized
ifAuth=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member/${themem}" | jq '.authorized')
ifIP=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member/${themem}" | jq -r '.ipAssignments[]')
# ...is so then display it.
if [[ "${ifAuth}" == "true" ]]; then
# Get existing Peer Info
existingPeerInfo
echo "${themem} ${ifIP} ${exPeerName}" >> ${tmpPeerFile}
fi
done
# unAuthorize a peer
function unAuthPeers() {
# Get a list of all the peers
selectMem "Deauthorize"
# deauthorize the member
if [[ $(seq 1 $SELECTION) =~ $opt ]]; then
# Get the selection value
thePeer=$(sed -n "${opt}p" <<< "${PEERS}")
themem=$(echo "${thePeer}" | awk ' { print $1 } ')
unAuthed=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" -X POST -d '{"authorized": false}' "${ztAddress}/${theNet}/member/${themem}" | jq -r '.authorized')
if [[ "${unAuthed}" == "false" ]]; then
read -p "The peer: ${themem} was set to unauthorized. Hit Enter to continue."
peerManage
else
read -p "The peer: ${themem} was NOT set to unauthorized. Hit Enter to continue."
unAuthPeers
fi
fi
}
# Check to see if there are any peers
if [[ -f ${tmpPeerFile} ]]; then
unAuthPeers
else
echo "There are no unauthorized peers."
read -p "Press ENTER when done."
fi
;;
5) # Delete a member
clear
delTemp
# Get all the members
for themem in $(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member"| egrep -o '[a-f0-9]{10}'); do
# Check if the peer is authorized
ifIP=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member/${themem}" |jq '.ipAssignments[]')
if [[ "${ifIP}" =~ "127.0.0.100" ]]; then
continue
else
# Get existing Peer Info
existingPeerInfo
echo "${themem} ${exPeerName}" >> ${tmpPeerFile}
fi
done
# deAuthorize a peer
function deAuthPeers() {
selectMem "Remove"
# Authorize the member
if [[ $(seq 1 $SELECTION) =~ $opt ]]; then
# Get the selection value
thePeer=$(sed -n "${opt}p" <<< "${PEERS}")
# Get the member ID
themem=$(echo "${thePeer}" | awk ' { print $1 } ')
delPeer=$(curl -s -X POST -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" -d '{"authorized": false}' "${ztAddress}/${theNet}/member/${themem}" | jq '.authorized')
fi
# Peers can't be removed from the network so the IP is changed to 127.0.0.100 as a flag that it should not appear when listing members.
reIP=$(curl -X POST -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" -d '{"ipAssignments":["127.0.0.100"]}' "${ztAddress}/${theNet}/member/${themem}" | jq '(.ipAssignments[] == "127.0.0.100")')
echo "DELPEER: ${delPeer}"
echo "REIP: ${reip}"
if [[ ("${delPeer}" == "false" && "${reIP}" == "true" ) ]]; then
# Delete Peer information
rm -f networks/${theNet}/${themem}
echo "Peer: ${themem} 'removed'"
else
echo "Peer: ${themem} was not removed."
echo "Authorized => ${delPeer}"
echo "IP => ${reIP}"
read -p "Press ENTER when done."
peerManage
fi
}
# Check to see if there are any peers
if [[ -f ${tmpPeerFile} ]]; then
deAuthPeers
else
echo "There are no peers to delete."
read -p "Press ENTER when done."
fi
;;
6)
clear
# Get all peers
getAllPeers
# Bring up Edit menu
selectMem "Edit"
# Authorize the member
if [[ $(seq 1 $SELECTION) =~ $opt ]]; then
# Get the selection value
thePeer=$(sed -n "${opt}p" <<< "${PEERS}")
existingPeerInfo
# Existing Information
if [[ ${exPeerName} != "" ]]; then
echo "Existing Name: ${exPeerName}"
else
exPeerName="empty"
fi
if [[ ${exPeerDesc} != "" ]]; then
echo "Existing Description: ${exPeerDesc}"
else
exPeerDesc="empty"
fi
# Get the member ID
themem=$(echo "${thePeer}" | awk ' { print $1 } ')
echo "${themem_info}"
# Prompt for the user
read -p "Enter the peer name (leave blank for no changes): " peerName
read -p "Enter the Peer Description (leave blank for no changes): " peerDesc
# If no changes then go back to Peer Manage page.
if [[ "${peerName}" == "" && "${peerDesc}" == "" ]]; then
peerManage
fi
echo "New Name: ${peerName}"
echo "New Desc: ${peerDesc}"
read -p "To add the new information above, hit Enter or E to not change." toEdit
# Check if user wants to exit
if [[ "${toEdit}" =~ ^(e|E)$ ]]; then
peerManage
fi
# Add Peer information if change is detected
if [[ "$(grep PEERNAME networks/${theNet}/$themem |cut -d: -f2)" != "${exPeerName}" ]]; then
replaceSpace="$(echo ${peerName} | sed 's/ /___/g')"
echo "PEERNAME:${replaceSpace}" > "networks/${theNet}/$themem"
fi
if [[ "$(grep PEERDESC networks/${theNet}/$themem |cut -d: -f2)" != "${exPeerDesc}" ]]; then
echo "PEERDESC:${peerDesc}" >> "networks/${theNet}/$themem"
fi
if [[ $? -eq 0 ]]; then
read -p "Peer Information added. Press Enter to continue." readEnter
peerManage
fi
fi
;;
7)
clear
delTemp
# Get all the members
for themem in $(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member"| egrep -o '[a-f0-9]{10}'); do
# Check if the peer is authorized
ifIP=$(curl -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" "${ztAddress}/${theNet}/member/${themem}" |jq '.ipAssignments[]')
if [[ "${ifIP}" != "\"127.0.0.100\"" ]]; then
continue
else
# Get existing Peer Info
existingPeerInfo
echo "${themem} ${exPeerName}" >> ${tmpPeerFile}
fi
done
# deAuthorize a peer
function deAuthPeers() {
selectMem "Undelete"
# Authorize the member
if [[ $(seq 1 $SELECTION) =~ $opt ]]; then
# Get the selection value
thePeer=$(sed -n "${opt}p" <<< "${PEERS}")
# Get the member ID
themem=$(echo "${thePeer}" | awk ' { print $1 } ')
delPeer=$(curl -s -X POST -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" -d '{"authorized": false}' "${ztAddress}/${theNet}/member/${themem}" | jq '.authorized')
fi
# Unset the 127.0.0.100 IP.
reIP=$(curl -X POST -s -H "X-ZT1-Auth: $(cat /var/lib/zerotier-one/authtoken.secret)" -d '{"ipAssignments":[]}' "${ztAddress}/${theNet}/member/${themem}" | jq '(.ipAssignments[] == "")')
# Should return nothing if it was successfully undeleted.
if [[ "${reIP}" == "" ]]; then
# UnDelete Peer information
touch networks/${theNet}/${themem}
echo "Peer: ${themem} 'was undeleted'"
read -p "Press ENTER when done."
else
echo "Peer: ${themem} was not undeleted."
read -p "Press ENTER when done."
peerManage
fi
}
# Check to see if there are any peers
if [[ -f ${tmpPeerFile} ]]; then
deAuthPeers
else
echo "There are no peers to undelete."
read -p "Press ENTER when done."
fi
;;
z|Z) # Back to Main configuration
bash ztnetworks.bash
exit 0
;;
e|E)
exit 0
;;
*)
echo "Invalid Option!"
read -p "Press ENTER when done."
peerManage
;;
esac
peerManage
echo ""
}
#fi
peerManage