-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtorrent-functions.sh
executable file
·1232 lines (1043 loc) · 35.8 KB
/
rtorrent-functions.sh
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
TRUE=0
FALSE=1
STOP_START_RTORRENT_ON_VPN_CONNECTION=$FALSE
PIA_INFO_FILE="/tmp/pia_vpn.info"
VPN_INTERFACE="tun0"
CURL="/usr/bin/curl"
CURL_TIMEOUT=4
IP="/usr/sbin/ip"
IFCONFIG="/sbin/ifconfig"
TRACEROUTE="/usr/sbin/traceroute"
JQ="/usr/bin/jq"
SYSTEMCTL="/usr/bin/systemctl"
DIG="/usr/bin/dig"
XMLRPC="/usr/bin/xmlrpc"
PGREP="/usr/bin/pgrep"
XMLRPC_CON="localhost:80/RPC2"
BAD_PORT="6000"
BAD_IP="127.0.0.1"
START_VPN_CMD="$SYSTEMCTL start openvpn"
STOP_VPN_CMD="$SYSTEMCTL stop openvpn"
START_RTORNT_CMD="$SYSTEMCTL start rtorrent"
STOP_RTORNT_CMD="$SYSTEMCTL stop rtorrent"
# If you use a credentials file for openvpn, you can point to it here.
# if not comment `CREDENTIALS=` and uncomment the `PIA_USER=` & `PIA_PASS=` and set them appropiatly.
CREDENTIALS='/etc/openvpn/user.txt'
#PIA_USER=piauserxxxxxxx
#PIA_PASS=pispassxxxxxx
# There are several ways to get your public ip address.
# using opendns servers, google dns servers, or ipinfo.io, set your predered below
# "opendns" "googledns" "ipinfo"
GET_PUBLIC_IP_METHOD="opendns"
# This is used for pgrep only, match rtorrent but not rtorrent_somecrap
#TRP_REGEX="rtorrent .*"
TRP_REGEX="rtorrent .*|^rtorrent$"
# Below is used for install
BIN_DIR="/usr/bin"
LIB_DIR="/usr/lib/rtorrent-utils"
LIB_NAME="rtorrent-functions.sh"
LN_NAMES=("rtorrent_recondition" "rtorrent_status" "rtorrent_bindPIAport" "rtorrent_overview" "rtorrent_helper" "rtorrent_vpn_up" "rtorrent_vpn_down")
# ******************************************************************************************
#
# OTHER CONFIG ITEMS FOR THIS TO WORK
#
# rtorrent cfg for this to work
# # `network.port_range.set` will not take effect unless `network.bind_address.set` is called after it.
# method.insert = get_vpn_ip_address, simple|private, "execute.capture=bash,-c,\"sudo /usr/bin/rtorrent_helper get_vpn_ip_address\""
# method.insert = get_public_port, simple|private, "execute.capture=bash,-c,\"sudo /usr/bin/rtorrent_helper get_public_port\""
# method.insert = get_public_ip_address, simple|private, "execute.capture=bash,-c,\"sudo /usr/bin/rtorrent_helper get_public_ip_address\""
# schedule2 = vpn_ip_tick, 3, 1800, "network.bind_address.set=(get_vpn_ip_address)"
# schedule2 = public_port_tick, 0, 900, "network.port_range.set=(get_public_port)"
# schedule2 = public_ip_tick, 2, 1800, "network.local_address.set=(get_public_ip_address)"
#
# put into openvpn config file for this to work
#
# script-security 2
# up /usr/bin/rtorrent_vpn_up
# down /usr/bin/rtorrent_vpn_down
#
# /etc/sudoers needs access to these scripts and systemctl
# rtorrent,www-data =(root) NOPASSWD: /usr/lib/rtorrent-utils/*, /usr/sbin/traceroute, /usr/bin/systemctl
#
# Crontab also needs to be set
# # Run every hour
# 0 * * * * "/usr/bin/rtorrent_recondition"
#
# ******************************************************************************************
#
# Notes
#
# use below to monitor port for traffic (ie check port forward)
# sudo tcpdump -ni any port 53868
#
# In rtorrent we use network.port_range for the incomming port, as it can be set and changed.
# It may be better to use the port (network.listen.port) but as that can't be set even in config
# we would need to use iptables to do portforwarding. That may bring in other issues of
# network.bind_address which is currently vpn, might need to create a virtual netowrk interface.
#
# ******************************************************************************************
if [ -f "$CREDENTIALS" ]; then
PIA_USER=$(sed '1q;d' $CREDENTIALS 2>/dev/null)
PIA_PASS=$(sed '2q;d' $CREDENTIALS 2>/dev/null)
fi
if [ -t 0 ]; then
TERMINAL=$TRUE
else
TERMINAL=$FALSE
fi
# ******************************************************************************************
#
# Helper functions
#
# ******************************************************************************************
function log() {
if [ "$TERMINAL" == "$TRUE" ]; then
logger -s $0" "$*
else
logger $0" "$*
fi
}
function error() {
#>&2 echo "Error: "$0" "$*
if [ "$TERMINAL" == "$TRUE" ]; then
logger -s "Error: "$0" "$*
else
logger "Error: "$0" "$*
fi
}
function warning() {
#>&2 echo "Error: "$0" "$*
if [ "$TERMINAL" == "$TRUE" ]; then
logger -s "Warning: "$0" "$*
else
logger "Warning: "$0" "$*
fi
}
# Print if we are in terminal, but print to stderror since stdout is for function returns
function term_print() {
if [ "$TERMINAL" == "$TRUE" ]; then
>&2 echo "$*"
fi
}
# ******************************************************************************************
#
# PIA Portforward functions
#
# ******************************************************************************************
function is_valid_ip() {
if [[ $1 =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo $TRUE
return $TRUE
else
echo $FALSE
return $FALSE
fi
}
function get_PIA_VPN_IP() {
#vpnIP=$($IFCONFIG $VPN_INTERFACE 2>/dev/null | grep "inet " | awk -F' ' '{print $2}')
vpnIP=$($IP -o -4 a s $VPN_INTERFACE | awk -F"[ /]+" '{print $4}')
if [ -z "$vpnIP" ]; then
echo $BAD_IP
return $FALSE
fi
if [ $(is_valid_ip $vpnIP) == "$FALSE" ]; then
echo $BAD_IP
return $FALSE
fi
echo $vpnIP
return $TRUE
}
function get_PIA_portforward() {
if [ -f ${PIA_INFO_FILE} ]; then
port=$(cat $PIA_INFO_FILE | grep 'port:' | cut -d: -f2)
echo $port
return $TRUE
fi
echo $BAD_PORT
return $FALSE
}
function get_public_IP() {
if [ "$GET_PUBLIC_IP_METHOD" == "googledns" ]; then
publicIP=$($DIG dig TXT +short -b $(get_PIA_VPN_IP) o-o.myaddr.l.google.com @ns1.google.com 2>/dev/null | tr -d '"' )
elif [ "$GET_PUBLIC_IP_METHOD" == "ipinfo" ]; then
publicIP=$($CURL -s -m $CURL_TIMEOUT http://ipinfo.io/ip)
else
publicIP=$($DIG +short -b $(get_PIA_VPN_IP) myip.opendns.com @resolver1.opendns.com 2>/dev/null )
fi
if [ -z "$publicIP" ]; then
echo $BAD_IP
return $FALSE
fi
if [ $(is_valid_ip $publicIP) == "$FALSE" ]; then
echo $BAD_IP
return $FALSE
fi
echo $publicIP
return $TRUE
}
function is_VPN_up()
{
adapter_check=$( $IP a s $VPN_INTERFACE 2>&1 )
if [ $? == 1 ]; then
echo $FALSE
return $FALSE
fi
if [[ "$adapter_check" == "Device \"$VPN_INTERFACE\" does not exist." ]]; then
echo $FALSE
return $FALSE
fi
if RTN=`sudo $TRACEROUTE -i $VPN_INTERFACE -m 1 nl.privateinternetaccess.com` ; then
if echo "$RTN" | grep '\* \* \*' > /dev/null; then
echo $FALSE
return $FALSE
fi
else
echo $FALSE
return $FALSE
fi
echo $TRUE
return $TRUE
}
function get_PIA_gateway()
{
PIAgateway=$($IP route s t all | grep -m 1 "0.0.0.0/1 via .* dev ${VPN_INTERFACE}" | cut -d ' ' -f3)
if [ -z "$PIAgateway" ]; then
echo $BAD_IP
return $FALSE
fi
echo $PIAgateway
return $TRUE
}
function get_PIA_usertoken()
{
generateTokenResponse=$($CURL --interface "${VPN_INTERFACE}" --silent --insecure -u "${PIA_USER}:${PIA_PASS}" "https://10.0.0.1/authv3/generateToken")
if [ "$(echo "$generateTokenResponse" | jq -r '.status')" != "OK" ]; then
error "Could not get a PIA user token. Please check your account credentials."
echo $FALSE
return $FALSE
fi
pia_token="$(echo "$generateTokenResponse" | jq -r '.token')"
if [ -z "$pia_token" ]; then
echo $FALSE
return $FALSE
fi
echo $pia_token
return $TRUE
}
function create_PIA_portforward()
{
pia_publicIP=$(get_public_IP)
pia_token=$(get_PIA_usertoken)
pia_gateway=$(get_PIA_gateway)
pia_vpnIP=$(get_PIA_VPN_IP)
payload_and_signature=$($CURL --insecure --silent --max-time 5 --get --data-urlencode "token=${pia_token}" "https://${pia_gateway}:19999/getSignature")
# Check if the payload and the signature are OK.
# If they are not OK, just stop the script.
if [ "$(echo "$payload_and_signature" | $JQ -r '.status')" != "OK" ]; then
error "The payload_and_signature variable does not contain an OK status."
error $payload_and_signature
echo $FALSE
return $FALSE;
fi
# We need to get the signature out of the previous response.
# The signature will allow the us to bind the port on the server.
signature="$(echo "$payload_and_signature" | $JQ -r '.signature')"
# The payload has a base64 format. We need to extract it from the
# previous response and also get the following information out:
# - port: This is the port you got access to
# - expires_at: this is the date+time when the port expires
payload="$(echo "$payload_and_signature" | $JQ -r '.payload')"
port="$(echo "$payload" | base64 -d | $JQ -r '.port')"
# The port normally expires after 2 months. If you consider
# 2 months is not enough for your setup, please open a ticket.
expires_at="$(echo "$payload" | base64 -d | $JQ -r '.expires_at')"
$(umask 077; touch $PIA_INFO_FILE)
echo "token:$pia_token" > $PIA_INFO_FILE
echo "gateway:$pia_gateway" >> $PIA_INFO_FILE
echo "publicIP:$pia_publicIP" >> $PIA_INFO_FILE
echo "vpnIP:$pia_vpnIP" >> $PIA_INFO_FILE
echo "signature:$signature" >> $PIA_INFO_FILE
echo "payload:$payload" >> $PIA_INFO_FILE
echo "expires:$expires_at" >> $PIA_INFO_FILE
echo "port:$port" >> $PIA_INFO_FILE
echo $port
return $TRUE
}
function check_PIA_portfwd_cfg() {
# Not best check, all we can do is check file exists and token is out of date.
if [ -f ${PIA_INFO_FILE} ]; then
expires=$(cat $PIA_INFO_FILE | grep 'expires:' | cut -d: -f2,3,4,5)
port=$(cat $PIA_INFO_FILE | grep 'port:' | cut -d: -f2)
public_ip=$(cat $PIA_INFO_FILE | grep 'publicIP:' | cut -d: -f2)
gateway=$(cat $PIA_INFO_FILE | grep 'gateway:' | cut -d: -f2)
vpnip=$(cat $PIA_INFO_FILE | grep 'vpnIP:' | cut -d: -f2)
else
echo $FALSE
return $FALSE
fi
if [ "$public_ip" != "`get_public_IP`" ]; then
warning "PIA External IP changed"
echo $FALSE
return $FALSE
fi
if [ "$gateway" != "`get_PIA_gateway`" ]; then
warning "PIA Gateway changed"
echo $FALSE
return $FALSE
fi
if [ "$vpnip" != "`get_PIA_VPN_IP`" ]; then
warning "PIA VPN IP changed"
echo $FALSE
return $FALSE
fi
# Should probably to 15 mins before time expires
if [ $(date +"%s") -gt $(date --date "$expires" +"%s") ]; then
warning "PIA port forward token out of date"
echo $FALSE
return $FALSE
fi
echo $port
return $TRUE
}
function bind_PIA_portforward() {
if [ -f ${PIA_INFO_FILE} ]; then
#token=$(cat $PIA_INFO_FILE | grep 'token:' | cut -d: -f2)
gateway=$(cat $PIA_INFO_FILE | grep 'gateway:' | cut -d: -f2)
signature=$(cat $PIA_INFO_FILE | grep 'signature:' | cut -d: -f2)
payload=$(cat $PIA_INFO_FILE | grep 'payload:' | cut -d: -f2)
port=$(cat $PIA_INFO_FILE | grep 'port:' | cut -d: -f2)
else
error "$PIA_INFO_FILE doesn't exist"
echo $FALSE
return $FALSE
fi
bind_port_response=$($CURL --insecure --silent --max-time 5 --get --data-urlencode "payload=${payload}" --data-urlencode "signature=${signature}" "https://${gateway}:19999/bindPort")
# Good replys are
# { "status": "OK", "message": "port scheduled for add" }
# { "status": "OK", "message": "timer refreshed" }
#error "return $bind_port_response"
if [ -z "$bind_port_response" ]; then
error "No reply from PIA on bind request"
echo $FALSE
return $FALSE
fi
if [ "`echo "$bind_port_response" | $JQ -r '.status'`" != "OK" ]; then
error "Failed PIA bind request"
echo $FALSE
return $FALSE
fi
echo $port
return $TRUE
}
function test_PIA_portforward() {
if [ -f ${PIA_INFO_FILE} ]; then
port=$(cat $PIA_INFO_FILE | grep 'port:' | cut -d: -f2)
public_ip=$(cat $PIA_INFO_FILE | grep 'publicIP:' | cut -d: -f2)
else
echo $FALSE
return $FALSE
fi
ext_status=$($CURL -s -m $CURL_TIMEOUT "https://proxy6.net/port-check" -H "X-Requested-With: XMLHttpRequest" --data-raw "ip=$public_ip&port=$port&hash=&form_id=form-port-check")
ext_status=$(echo $ext_status | $JQ -r '.result."'$port'"' )
if [ "$ext_status" == "true" ]; then
echo $TRUE
return $TRUE
elif [ "$ext_status" == "false" ]; then
echo $FALSE
return $FALSE
fi
# Note below Can also get the below reply
# {"status":"failure","message":"System temporarily offline due to heavy load."}
ext_status=$($CURL -s -m $CURL_TIMEOUT -X POST -d "remoteAddress=$public_ip&portNumber=$port" "https://ports.yougetsignal.com/check-port.php" | awk 'BEGIN { RS=" ";FS="\"" } /alt/{print $2}')
if [ "$ext_status" == "Open" ]; then
echo $TRUE
return $TRUE
elif [ "$ext_status" == "Closed" ]; then
echo $FALSE
return $FALSE
fi
#Got a reply we didn;t understand from ports.yougetsignal.com, so check different URL
# [{"port":"25757","response_time":"0ms","status":"Open"}]
# [{"port":"25759","response_time":"-","status":"Close"}]
ext_status=$($CURL -s -m $CURL_TIMEOUT --data-raw "domain=$public_ip&port=$port" "https://codebeautify.org/iptools/openPortChecker")
ext_status=$(echo $ext_status | tr -d '[]' | $JQ -r '.status' )
if [ "$ext_status" == "Open" ]; then
echo $TRUE
return $TRUE
elif [ "$ext_status" == "Closed" ]; then
echo $FALSE
return $FALSE
fi
# Still no definitive answer,
ext_status=$($CURL -s -m $CURL_TIMEOUT "https://www.ipfingerprints.com/scripts/getPortsInfo.php" --data-raw "remoteHost=$public_ip&start_port=$port&end_port=$port&normalScan=Yes3scan_type=connect&ping_3ype=none" -s | sed -n "s/^.*bold.*> \(.*\) <.*span.*$/\1/p")
# Should return open closed filtered unfiltered open|filtered closed|filtered
if [ "$ext_status" == *"open"* ] || [ "$ext_status" == "filtered" ]; then
echo $TRUE
return $TRUE
elif [ "$ext_status" == *"closed"* ]; then
echo $FALSE
return $FALSE
fi
echo $FALSE
return $FALSE
}
# ******************************************************************************************
#
# rTorrent functions
#
# ******************************************************************************************
function is_rTorrent_running()
{
#if $PGREP -f $TR_BIN &>/dev/null ; then
if $PGREP -f "$TRP_REGEX" &>/dev/null ; then
echo $TRUE
return $TRUE
else
echo $FALSE
return $FALSE
fi
}
function get_bindIP_from_rtorrent() {
bound_to=$(get_rtorrent_cfg_string "network.bind_address")
echo $bound_to
if [ "$bound_to" == "$FALSE" ]; then
return $FALSE
fi
return $TRUE
}
function get_port_from_rtorrent() {
port_forward=$(get_rtorrent_cfg_string "network.port_range")
port_forward=$(echo $port_forward | sed -r 's|([0-9]*)-.*|\1|')
echo $port_forward
if [ "$port_forward" == "$FALSE" ]; then
return $FALSE
fi
return $TRUE
}
function get_publicIP_from_rtorrent() {
public_ip=$(get_rtorrent_cfg_string "network.local_address")
echo $public_ip
if [ "$public_ip" == "$FALSE" ]; then
return $FALSE
fi
return $TRUE
}
function write_bindIP_to_rtorrent() {
new_bind=$1
current_bind=$(get_bindIP_from_rtorrent)
if [ "$current_bind" != "$new_bind" ]; then
rtn=$(set_rtorrent_cfg_string "network.bind_address.set" "$new_bind")
echo $rtn
return $rtn
fi
echo $TRUE
return $TRUE
}
function write_port_to_rtorrent() {
new_port=$1
current_port=$(get_port_from_rtorrent)
if [ "$current_port" != "$new_port" ]; then
rtn=$(set_rtorrent_cfg_string "network.port_range.set" "$new_port-$new_port")
echo $rtn
return $rtn
fi
echo $TRUE
return $TRUE
}
function write_publicIP_to_rtorrent() {
new_public_ip=$1
current_public_ip=$(get_bindIP_from_rtorrent)
if [ "$current_public_ip" != "$new_public_ip" ]; then
rtn=$(set_rtorrent_cfg_string "network.local_address.set" "$new_public_ip")
echo $rtn
return $rtn
fi
echo $TRUE
return $TRUE
}
function get_rtorrent_cfg_string() {
string=$($XMLRPC $XMLRPC_CON $1 2>/dev/null | grep "String" | cut -d"'" -f2)
if [ -z "$string" ]; then
echo $FALSE;
return $FALSE
fi
echo $string
return $TRUE
}
function set_rtorrent_cfg_string() {
#return=$($XMLRPC $XMLRPC_CON $1 "" $2 2>/dev/null | grep "Integer" | cut -d"'" -f2)
# Doesn;t always return Integer
# returns the above if it didn;t change
return=$($XMLRPC $XMLRPC_CON $1 "" $2 2>/dev/null)
if [[ -z "$return" || "$?" != "0" ]]; then
#term_print "Return from $1 $2 = $return"
echo $FALSE;
return $FALSE
fi
echo $TRUE
return $TRUE
}
# ******************************************************************************************
#
# Main run functions
#
# ******************************************************************************************
function recondition_rTorrent() {
# Check VPN
if [ "$(is_VPN_up)" == "$FALSE" ]; then
term_print "$(printf "%-33s : \033[1m%-16s\033[0m" "VPN is" "Bad")"
log "VPN is down, Restarting!"
$STOP_VPN_CMD
$START_VPN_CMD
sleep 5
# VPN Failed to start
if [ "$(is_VPN_up)" == "$FALSE" ]; then
if [ "$(is_rTorrent_running)" == "$TRUE" ]; then
error "Restarting VPN failed!, stopping rtorrent"
$STOP_RTORNT_CMD
else
error "Restarting VPN failed!"
fi
echo $FALSE
return $FALSE
fi
else
term_print "$(printf "%-33s : \033[1m%-16s\033[0m" "VPN is" "Running")"
fi
#Check rTorrennt
if [ "$(is_rTorrent_running)" == "$FALSE" ]; then
term_print "$(printf "%-33s : \033[1m%-16s\033[0m" "rTorrent is" "Bad")"
log "rTorrent is down, Starting!"
#$STOP_RTORNT_CMD
$START_RTORNT_CMD
sleep 5
if [ "$(is_rTorrent_running)" == "$FALSE" ]; then
error "Restarting rTorrent failed!"
echo $FALSE
return $FALSE
fi
else
term_print "$(printf "%-33s : \033[1m%-16s\033[0m" "rTorrent is" "Running")"
fi
#if [ "$(check_PIA_portfwd_cfg)" == "$FALSE" ]; then
# term_print "$(printf "%-21s %8s : \033[1m%-16s\033[0m" "PIA Port Forward" "$(check_PIA_portfwd_cfg)" "Bad")"
if ! port=$(check_PIA_portfwd_cfg); then
term_print "$(printf "%-24s %8s : \033[1m%-16s\033[0m" "PIA Port Forward" "$port" "Bad")"
warning "PIA Port Forward configuration is bad, resetting"
port=$(create_PIA_portforward)
if [ "$port" == "$FALSE" ]; then
error "Making PIA portforward call"
echo $FALSE
return $FALSE
fi
else
term_print "$(printf "%-24s %8s : \033[1m%-16s\033[0m" "PIA Port Forward" "$port" "Good")"
fi
# Always want to bind port on this
if [ "$(bind_PIA_portforward)" == "$FALSE" ]; then
term_print "$(printf "%-24s %8s : \033[1m%-16s\033[0m" "PIA Bind to port" "$(get_PIA_portforward)" "Failed")"
echo $FALSE
return $FALSE
else
term_print "$(printf "%-24s %8s : \033[1m%-16s\033[0m" "PIA Bind to port" "$(get_PIA_portforward)" "Passed")"
fi
# write_bindIP_to_rtorrent MUST be called after write_port_to_rtorrent, so stuck with this order
if [ $(get_PIA_portforward) != "$(get_port_from_rtorrent)" ]; then
term_print "$(printf "%-24s %8s : \033[1m%-16s\033[0m" "rTorrent port forward" "$(get_PIA_portforward)" "Bad")"
log "rTorrent config Port is incorrect, $(get_PIA_portforward) vs $(get_port_from_rtorrent)"
if [ "$(write_port_to_rtorrent $(get_PIA_portforward) )" == "$FALSE" ]; then
error "updating rTorrent config Port failed"
echo $FALSE
return $FALSE
else
term_print "Updated rTorrent Port"
fi
else
term_print "$(printf "%-24s %8s : \033[1m%-16s\033[0m" "rTorrent port forward" "$(get_PIA_portforward)" "Good")"
fi
if [ $(get_PIA_VPN_IP) != "$(get_bindIP_from_rtorrent)" ]; then
term_print "$(printf "%-17s %15s : \033[1m%-16s\033[0m" "rTorrent VPN IP" "$(get_PIA_VPN_IP)" "Bad")"
log "rTorrent config bindIP is incorrect"
if [ "$(write_bindIP_to_rtorrent $(get_PIA_VPN_IP) )" == "$FALSE" ]; then
error "updating rTorrent config bindIP failed"
echo $FALSE
return $FALSE
else
term_print "Updated rTorrent bindIP"
fi
else
term_print "$(printf "%-17s %15s : \033[1m%-16s\033[0m" "rTorrent VPN IP" "$(get_PIA_VPN_IP)" "Good")"
fi
if [ $(get_public_IP) != "$(get_publicIP_from_rtorrent)" ]; then
term_print "$(printf "%-17s %15s : \033[1m%-16s\033[0m" "rTorrent PublicIP" "$(get_public_IP)" "Bad")"
log "rTorrent config PublicIP is incorrect"
if [ "$(write_publicIP_to_rtorrent $(get_public_IP) )" == "$FALSE" ]; then
error "updating rTorrent config PublicIP failed"
echo $FALSE
return $FALSE
else
term_print "Updated rTorrent PublicIP"
fi
else
term_print "$(printf "%-17s %15s : \033[1m%-16s\033[0m" "rTorrent PublicIP" "$(get_public_IP)" "Good")"
fi
# Always want to bind port on this
#if [ "$(bind_PIA_portforward)" == "$FALSE" ]; then
# term_print "$(printf "%-24s %8s : \033[1m%-16s\033[0m" "PIA Bind to port" "$(get_PIA_portforward)" "Failed")"
# echo $FALSE
# return $FALSE
#else
# term_print "$(printf "%-24s %8s : \033[1m%-16s\033[0m" "PIA Bind to port" "$(get_PIA_portforward)" "Passed")"
#fi
if [ "$(test_PIA_portforward)" == "$FALSE" ]; then
term_print "$(printf "%-33s : \033[1m%-16s\033[0m" "rTorrent incomming connections" "Failed")"
else
term_print "$(printf "%-33s : \033[1m%-16s\033[0m" "rTorrent incomming connections" "Passed")"
fi
echo $TRUE
return $TRUE
}
function status_rTorrent() {
status=$TRUE
if [ -f ${PIA_INFO_FILE} ]; then
pia_cache_expires=$(cat $PIA_INFO_FILE | grep 'expires:' | cut -d: -f2)
pia_cache_port=$(cat $PIA_INFO_FILE | grep 'port:' | cut -d: -f2)
pia_cache_publicip=$(cat $PIA_INFO_FILE | grep 'publicIP:' | cut -d: -f2)
pia_cache_vpnip=$(cat $PIA_INFO_FILE | grep 'vpnIP:' | cut -d: -f2)
else
status=$FALSE
pia_cache_port=$BAD_IP
pia_cache_publicip=$BAD_IP
pia_cache_vpnip=$BAD_IP
fi
if [ "$1" != "json" ]; then
term_print '---------------------------------------------------------------'
term_print "$(printf "%-12s | %-15s | %-15s | %-15s" "Test" "PIA VPN" "System" "rTorrent" )"
term_print '---------------------------------------------------------------'
vpn_running_test=$(is_VPN_up)
tor_running_test=$(is_rTorrent_running)
term_print "$(printf "%-12s | %-15s | %-15s | %-15s" "Running" `if [ "$vpn_running_test" == "$TRUE" ];then echo Yes;else echo No;fi` "N/A" `if [ "$tor_running_test" == "$TRUE" ];then echo Yes;else echo No;fi` )"
system_vpnip=$(get_PIA_VPN_IP)
torrent_vpnip=$(get_bindIP_from_rtorrent)
term_print "$(printf "%-12s | %-15s | %-15s | %-15s" "VPN IP" "$pia_cache_vpnip" "$system_vpnip" "$torrent_vpnip" )"
system_publicip=$(get_public_IP)
torrent_publicip=$(get_publicIP_from_rtorrent)
term_print "$(printf "%-12s | %-15s | %-15s | %-15s" "Public IP" "$pia_cache_publicip" "$system_publicip" "$torrent_publicip" )"
torrent_port=$(get_port_from_rtorrent)
term_print "$(printf "%-12s | %-15s | %-15s | %-15s" "Port forward" "$pia_cache_port" "N/A" "$torrent_port" )"
external_port_test=$(test_PIA_portforward)
port_bind_test=$(bind_PIA_portforward)
term_print "$(printf "%-12s | %-15s | %-15s | %-15s" "Ext Access" `if [ "$port_bind_test" == "$FALSE" ];then echo Failed;else echo Passed;fi` "N/A" `if [ "$external_port_test" == "$FALSE" ];then echo Failed;else echo Passed; fi` )"
term_print '---------------------------------------------------------------'
else
# These are duplicate of above, but here to control json output
vpn_running_test=$(is_VPN_up)
tor_running_test=$(is_rTorrent_running)
system_vpnip=$(get_PIA_VPN_IP)
torrent_vpnip=$(get_bindIP_from_rtorrent)
system_publicip=$(get_public_IP)
torrent_publicip=$(get_publicIP_from_rtorrent)
torrent_port=$(get_port_from_rtorrent)
external_port_test=$(test_PIA_portforward)
port_bind_test=$(bind_PIA_portforward)
fi #JSON format
if [ "$vpn_running_test" == "$FALSE" ]; then
error "VPN is down"
status=$FALSE
fi
if [ "$tor_running_test" == "$FALSE" ]; then
error "rTorrent is not runing"
status=$FALSE
fi
if [[ "$pia_cache_publicip" != "$system_publicip" || "$system_publicip" != "$torrent_publicip" ]]; then
error "Public IP test bad"
status=$FALSE
fi
if [[ "$pia_cache_vpnip" != "$system_vpnip" || "$system_vpnip" != "$torrent_vpnip" ]]; then
error "VPN IP test bad"
status=$FALSE
fi
if [[ "$pia_cache_port" != "$torrent_port" || "$port_bind_test" == "$FALSE" || "$external_port_test" == "$FALSE" ]]; then
error "VPN Port test bad"
status=$FALSE
fi
if [ $(check_PIA_portfwd_cfg) == "$FALSE" ]; then
# That will print error, so no need
status=$FALSE
fi
if [ "$1" = "json" ]; then
if [ "$vpn_running_test" == "$TRUE" ]; then
vpn_status="Up"
else
vpn_status="Down"
fi
if [ "$tor_running_test" == "$TRUE" ]; then
tor_status="Up"
else
tor_status="Down"
fi
if [ "$status" == "$TRUE" ]; then
ext_status="Good"
else
ext_status="Bad"
fi
echo "{ \"vpnstatus\":\"$vpn_status\"," \
"\"torstatus\":\"$tor_status\"," \
"\"vpnip\":\"$pia_cache_vpnip\"," \
"\"vpnport\":\"$pia_cache_port\"," \
"\"cfgip\":\"$torrent_vpnip\"," \
"\"cfgport\":\"$torrent_port\"," \
"\"extip\":\"$torrent_publicip\"," \
"\"extport\":\"$torrent_port\"," \
"\"extaccesstest\":\"$ext_status\" }"
else
if [ "$status" == "$FALSE" ]; then
term_print '---------------------------------------------------------------'
fi
fi
return $status
}
function overview_rTorrent() {
# Big output so run at same level. (the while/do/done)
lc=0
tlc=0
up=0
down=0
active_torrents=0
t_size=0
peers_connected=0
inerror=0
total_torrents=0
while read line; do
if [ $(( $lc % 6 )) -eq 0 ]; then tlc=0; fi
if [ $tlc -eq 1 ]; then
up=$(($up+$line))
l_up=$line
elif [ $tlc -eq 2 ]; then
down=$(($down+$line))
elif [ $tlc -eq 3 ]; then
peers_connected=$(($peers_connected+$line))
if [ $l_up -gt 0 ] || [ $line -gt 0 ]; then
active_torrents=$(($active_torrents+1))
fi
elif [ $tlc -eq 4 ]; then
t_size=$(($t_size+$line))
elif [ $tlc -eq 5 ]; then
if [ "$line" != "''" ]; then
inerror=$(($inerror+1))
fi
elif [ $tlc -eq 5 ]; then
total_torrents=$(($total_torrents+1))
fi
lc=$((lc+1))
tlc=$((tlc+1))
done << EOF
$($XMLRPC $XMLRPC_CON d.multicall2 '' main d.up.rate= d.down.rate= d.peers_connected= d.size_bytes= d.message= | awk -F: '/Index/ {print $2}')
EOF
upload_speed_kB=$(awk "BEGIN {printf \"%.1f\", ($up / 1024)}")
download_speed_kB=$(awk "BEGIN {printf \"%.1f\", ($down / 1024)}")
size_TB=$(awk "BEGIN {printf \"%.2f\", ($t_size / 1099511627776)}")
if [ "$1" = "pp" ]; then
printf " \033[1m%-17s \033[0m%-16s \033[1m%-17s \033[0m%-16s\n" "Up:" "$upload_speed_kB kB/s" "Down:" "$download_speed_kB kB/s"
printf " \033[1m%-17s \033[0m%-16s \033[1m%-17s \033[0m%-16s\n" "Active:" $active_torrents "Peers:" $peers_connected
printf " \033[1m%-17s \033[0m%-16s \033[1m%-17s \033[0m%-16s\n" "Total:" $total_torrents "Size:" "$size_TB TB"
else
echo "Total up: $upload_speed_kB kB/s"
echo "Total down: $download_speed_kB kB/s"
echo "Total Active: $active_torrents"
echo "Total in error: $inerror"
echo "Peers connected: $peers_connected"
echo "Total: $total_torrents"
echo "Total size: $size_TB TB"
fi
}
function install() {
if [ "$1" == "dev" ]; then
for i in ${!LN_NAMES[@]}; do
if [ ! -L "./${LN_NAMES[$i]}" ] ; then
ln -s "$0" "./${LN_NAMES[$i]}"
fi
done
return
fi
if [ ! -d "$LIB_DIR" ]; then
mkdir "$LIB_DIR"
fi
cp "$0" "$LIB_DIR/$LIB_NAME"
for i in ${!LN_NAMES[@]}; do
if [ ! -L "$LIB_DIR/${LN_NAMES[$i]}" ] ; then
ln -s "$LIB_DIR/$LIB_NAME" "$LIB_DIR/${LN_NAMES[$i]}"
fi
if [ ! -L "$BIN_DIR/${LN_NAMES[$i]}" ] ; then
ln -s "$LIB_DIR/${LN_NAMES[$i]}" "$BIN_DIR/${LN_NAMES[$i]}"
fi
done
term_print "$0 installed! the following commands are now available"
for i in ${!LN_NAMES[@]}; do
term_print "$BIN_DIR/${LN_NAMES[$i]}"
done
output=""
if ! command -v $CURL &>/dev/null; then output+="Please check 'curl' is installed\n"; fi
if ! command -v $IP &>/dev/null; then output+="Please check 'ip' is installed\n"; fi
if ! command -v $IFCONFIG &>/dev/null; then output+="Please check 'ifconfig' is installed\n"; fi
if ! command -v $TRACEROUTE &>/dev/null; then output+="Please check 'traceroute' is installed\n"; fi
if ! command -v $JQ &>/dev/null; then output+="Please check 'jq' is installed\n"; fi
if ! command -v $DIG &>/dev/null; then output+="Please check 'dig' is installed\n"; fi
if [ "$PIA_USER" == "" ]; then output+="Please set PIA_USER variable\n";fi
if [ "$PIA_PASS" == "" ]; then output+="Please set PIA_PASS variable\n";fi
if [ "$output" == "" ]; then
echo "System test passed"
else
printf "Errors:-\n%b" "${output}"
fi
}
function vpn_call_rTorrent() {
# Need to go over this logic, too many duplicate
# STOP_START_RTORRENT_ON_VPN_CONNECTION = SSTORVPN
# 1 Call UP from boot, don't do anything.
# 2 Call UP on VPN bounce.
# a) If torrent is running reconfigure torrent.
# b) If torrent is down & SSTORVPN=yes start torrent. (How to detect this from #1????????) use uptime
# 3 Call DOWN if SSTORVPN=yes, stop rtorrent.
# 4 call
# Called from boot, do nothing, let system start rtorrent
uptimesec=$(awk 'BEGIN { FS="."}{ print $1}' /proc/uptime)
if [ $uptimesec -lt 120 ]; then
log "Called from systemboot, not doing anything"
return $TRUE
fi
bindIP = $BAD_IP
pubIP = $BAD_IP
port = $BAD_PORT
# is up, get new information.
if [ "$1" == "up" ]; then
if [ "$(is_rTorrent_running)" == "$FALSE" ]; then
if [ "$STOP_START_RTORRENT_ON_VPN_CONNECTION" == "$TRUE" ]; then
log "Starting rTorrent"
$START_RTORNT_CMD
fi
# Let rTorrent fix it's own connection on startup, since we just started it.
return $TRUE
else
# chances are this is all changed, but chack first.
if ! port=$(check_PIA_portfwd_cfg); then
if ! port=$(create_PIA_portforward); then
error "Making PIA portforward call"
echo $FALSE
return $FALSE
fi
if [ -f ${PIA_INFO_FILE} ]; then
port=$(cat $PIA_INFO_FILE | grep 'port:' | cut -d: -f2)
pubIP=$(cat $PIA_INFO_FILE | grep 'publicIP:' | cut -d: -f2)
bindIP=$(cat $PIA_INFO_FILE | grep 'vpnIP:' | cut -d: -f2)
else