-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipwatcher.py
More file actions
179 lines (146 loc) · 7.26 KB
/
Copy pathipwatcher.py
File metadata and controls
179 lines (146 loc) · 7.26 KB
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
#!/usr/bin/env python3
# coding: utf-8
# Version 0.1
import os
import time
import requests
from colored import fore, back, style
from multiprocessing import Process, Lock
def check_connection_VPN(previous_ip, previous_interface):
current_ip = str.rstrip(os.popen('curl -s ifconfig.io').read())
current_interface = os.popen('ip tuntap show | tail -n 1 | cut -c1-4').read()
if current_ip != previous_ip and current_interface != previous_interface:
os.popen('sudo service network-manager stop')
banner('ip_changed',current_ip, current_interface)
raise SystemExit
time.sleep(5)
def check_tor_connection(current_ip):
r=requests.get(url='https://check.torproject.org/?lang=en')
r2=requests.get(url='https://www.dan.me.uk/tornodes')
if 'Sorry. You are not using Tor.' in r.text or 'Forbidden' not in r2.text:
os.popen('sudo service network-manager stop')
banner('ip_changed',current_ip)
raise SystemExit
def tor_ip_changed(current_ip, previous_ip):
if current_ip != previous_ip and current_ip != '':
banner('tor_changed', current_ip)
time.sleep(2)
def change_tor_ip():
os.popen('sudo torghost -r >/dev/null')
def banner(type, ip=None, interface=None):
if type == 'tor' or type == 'tor_changing':
os.system('clear')
print ("Your " + style.BOLD + "current IP " + style.RESET + "is : " + fore.LIGHT_GREEN + style.BOLD + ip + style.RESET)
print("If your IP is not a TOR exit node, you will be noticed and the script will turn off your network manager")
print(fore.LIGHT_GREEN + style.BOLD + "Running..." + style.RESET)
if type == 'not_tor':
os.system('clear')
print("!!The script will use your last VPN interface")
print(fore.LIGHT_GREEN + style.BOLD + "Checking VPN network interface and IP..." + style.RESET)
print ("Your " + style.BOLD + "current IP " + style.RESET + "is : " + fore.LIGHT_GREEN + style.BOLD + ip + style.RESET)
print ("Your " + style.BOLD + "VPN network interface " + style.RESET + "is : " + fore.LIGHT_GREEN + style.BOLD + str(interface) + style.RESET)
print("If your IP changes, you will be noticed and the script will turn off your network manager")
print(fore.LIGHT_GREEN + style.BOLD + "Running..." + style.RESET)
if type == 'ip_changed':
t = time.localtime()
print(fore.RED + style.BOLD + "Your IP just changed at " + style.BOLD + str(time.strftime("%H:%M:%S", t)) + " to " + style.RESET + fore.LIGHT_GREEN + ip + style.RESET)
print("Turning off network interface ...")
print(fore.RED + style.BOLD + "Network interface DOWN" + style.RESET)
print('To restart your network-manager type: ' + fore.BLUE + 'sudo service network-manager start' )
if type == 'tor_changed':
print(fore.LIGHT_GREEN + style.BOLD + "TOR IP changing..." + style.RESET)
print("Your " + style.BOLD + "new TOR IP " + style.RESET + "is : " + fore.LIGHT_GREEN + style.BOLD + ip + style.RESET)
print(fore.LIGHT_GREEN + style.BOLD + "Running..." + style.RESET)
def check_ip_type():
print("Are you using TOR? [yY/nN]")
option = str(input())
if option == 'y' or option == 'Y':
r=requests.get(url='https://check.torproject.org/?lang=en')
r2=requests.get(url='https://www.dan.me.uk/tornodes')
if 'Sorry. You are not using Tor.' in r.text and 'Forbidden' not in r2.text:
print(fore.RED + style.BOLD + "You should first connect to a TOR node before running this script" + style.RESET)
print ("Bye")
raise SystemExit
else:
print("Would you like to change your TOR IP every ~7 seconds? [yY/nN]")
print(fore.RED + style.BOLD + "You will need to install and use torghost first" + style.RESET)
print(fore.RED + style.BOLD + "https://github.com/SusmithKrishnan/torghost.git" + style.RESET)
option = str(input())
if option == 'y' or option == 'Y':
return 'tor_changing'
if option == 'n' or option == 'N':
return 'tor'
else:
print('Wrong input')
if option == 'n' or option == 'N':
return 'not_tor'
else:
print('Wrong input')
def options(current_ip):
os.system('clear')
print ("Your current IP is : " + fore.LIGHT_GREEN + style.BOLD + current_ip + style.RESET)
print ("What do you want to do?")
print (" 1) Check my current IP while working")
print (" 2) Exit")
option = input()
if option == '1':
return check_ip_type()
if option == '2':
raise SystemExit
else:
print('Wrong input')
def main():
global ip_type
t = 0
current_ip = str.rstrip(os.popen('curl -s ifconfig.io').read())
current_interface = os.popen('ip tuntap show | tail -n 1 | cut -c1-4').read()
if os.geteuid() != 0:
exit("You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting.")
raise SystemExit
ip_type = options(current_ip)
banner(ip_type, current_ip, current_interface)
while True:
try:
if ip_type == 'tor_changing':
t1 = Process(check_tor_connection(current_ip))
t1.start()
previous_ip = current_ip
current_ip = str.rstrip(os.popen('curl -s ifconfig.io').read())
tor_ip_changed(current_ip, previous_ip)
t2 = Process(target=change_tor_ip())
t = t+1
if t ==3:
t2.start()
t2.join()
t2.terminate()
t = 0
if ip_type == 'tor':
check_tor_connection(current_ip)
previous_ip = current_ip
current_ip = str.rstrip(os.popen('curl -s ifconfig.io').read())
tor_ip_changed(current_ip, previous_ip)
else:
check_connection_VPN(current_ip, current_interface)
except requests.exceptions.ConnectTimeout:
t = time.localtime()
print(fore.RED + style.BOLD + "Got a timeout from the TOR IP check at " + style.BOLD + str(time.strftime("%H:%M:%S", t)) + style.RESET)
print("Turning off network interface ...")
os.popen('sudo service network-manager stop')
print(fore.RED + style.BOLD + "Network interface DOWN" + style.RESET)
print('To restart your network-manager type: ' + fore.BLUE + 'sudo service network-manager start' )
raise SystemExit
except requests.exceptions.RequestException:
t = time.localtime()
print(fore.RED + style.BOLD + "Unable to verify your TOR IP at " + style.BOLD + str(time.strftime("%H:%M:%S", t)) + style.RESET)
print("Turning off network interface ...")
os.popen('sudo service network-manager stop')
print(fore.RED + style.BOLD + "Network interface DOWN" + style.RESET)
print('To restart your network-manager type: ' + fore.BLUE + 'sudo service network-manager start' )
raise SystemExit
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
if ip_type == 'tor_changing':
os.popen('sudo killall torghost >/dev/null')
raise SystemExit