-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauto_restart.py
309 lines (236 loc) · 9.4 KB
/
auto_restart.py
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
import subprocess
import re, time, os, sys, msvcrt
import bencode
import codecs
import psutil
from pprint import pformat
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import smtplib
# import paramparse
#
from Misc import processArguments
def check_interface(interface_names):
output = subprocess.check_output("ipconfig /all")
lines = output.splitlines()
lines = filter(lambda x: x, lines)
print('interface_names: {}\n\n'.format(interface_names))
print('output: {}'.format(output))
# print('lines:\n {}'.format(pformat(lines)))
ip_address = ''
# mac_address = ''
name = ''
description = ''
for line in lines:
# -------------
# Interface Name
line = line.strip().lower()
print('line: {}'.format(line))
# print('ip_address: {}'.format(ip_address))
is_interface_name = re.match(r'^[a-zA-Z0-9].*:$', line)
# print('is_interface_name: {}'.format(is_interface_name))
# is_interface_name = 1
if is_interface_name:
# Check if there's previews values, if so - yield them
if name and ip_address:
if name in interface_names:
return ip_address
ip_address = ''
# mac_address = ''
name = line.rstrip(':')
print('name: {}'.format(name))
if ':' not in line:
continue
value = line.split(':')[-1]
value = value.strip()
is_description = line.startswith('description')
if is_description:
description = value
print('\ndescription: {}\n'.format(description))
if description and ip_address:
if description in interface_names:
return ip_address
# name = description
# -------------
# IP Address
# is_ip_address = re.match(r'ipv4 address|autoconfiguration ipv4 address|ip address', line)
is_ip_address = line.startswith('ipv4 address') or \
line.startswith('autoconfiguration ipv4 address') or \
line.startswith('ip address')
print('is_ip_address: {}'.format(is_ip_address))
# is_ip_address = not ip_address and is_ip_address
if is_ip_address:
ip_address = value
ip_address = ip_address.replace('(preferred)', '')
ip_address = ip_address.strip()
print('ip_address: {}'.format(ip_address))
# print('line: ', line)
# print('ip_address: ', ip_address)
# -------------
# MAC Address
# is_mac_address = not ip_address and re.match(r'physical address', line)
#
# if is_mac_address:
# mac_address = value
# mac_address = mac_address.replace('-', ':')
# mac_address = mac_address.strip()
if ip_address:
if name and name in interface_names:
return ip_address
if description and description in interface_names:
return ip_address
return None
if __name__ == '__main__':
params = {
'interface_names': ['PPP adapter PureVPN', 'TAP-Windows Adapter V9'],
'utorrent_mode': 1,
'restart_time': 86400,
'wait_time': 10800,
'post_wait_time': 10,
'check_vpn_gap': 30,
'max_vpn_wait_time': 600,
'proc_kill_type': 0,
'vpn_path': 'C:/Users/Tommy/Desktop/purevpn.lnk',
'tor_path': 'C:/Users/Tommy/Desktop/uTorrent.lnk',
'settings_path': 'C:/Users/Tommy/AppData/Roaming/uTorrent/settings.dat',
'vpn_proc': 'PureVPN.exe',
'tor_proc': 'uTorrent.exe',
'syatem_name': 'GT1K',
'email_auth': [],
}
# paramparse.process_dict(params)
processArguments(sys.argv[1:], params)
utorrent_mode = params['utorrent_mode']
interface_names = params['interface_names']
restart_time = params['restart_time']
wait_time = params['wait_time']
post_wait_time = params['post_wait_time']
check_vpn_gap = params['check_vpn_gap']
vpn_path = params['vpn_path']
tor_path = params['tor_path']
settings_path = params['settings_path']
vpn_proc = params['vpn_proc']
tor_proc = params['tor_proc']
email_auth = params['email_auth']
max_vpn_wait_time = params['max_vpn_wait_time']
proc_kill_type = params['proc_kill_type']
global_start_t = time.time()
restart_now = 0
prev_ip_address = ''
restarted = 1
interface_names = [k.lower() for k in interface_names]
while True:
os.startfile(vpn_path)
ip_address = None
print('waiting for vpn to start')
vpn_wait_start_t = time.time()
while True:
ip_address = check_interface(interface_names)
if ip_address is not None:
break
current_t = time.time()
if (restart_time > 0 and current_t - global_start_t > restart_time) or \
(current_t - vpn_wait_start_t > max_vpn_wait_time):
restart_now = 1
break
try:
time.sleep(1)
except KeyboardInterrupt:
exit()
if restart_now:
break
print('vpn started with ip_address: {}'.format(ip_address))
if ip_address != prev_ip_address:
prev_ip_address = ip_address
if email_auth:
fromaddr = email_auth[0]
toaddr = email_auth[1]
syatem_name = email_auth[3]
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "{} IP Update".format(syatem_name)
body = ''
if restarted:
restarted = 0
body += 'System restarted\n'
body += "{} IP has been updated to: {}".format(syatem_name, ip_address)
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(fromaddr, email_auth[2])
text = msg.as_string()
print('Sending email to {} from {}'.format(toaddr, fromaddr))
server.sendmail(fromaddr, toaddr, text)
if utorrent_mode:
f = codecs.open(settings_path, "rb").read()
d = bencode.bdecode(f)
d['net.bind_ip'] = ip_address
d['net.outgoing_ip'] = ip_address
f_out = bencode.bencode(d)
codecs.open(settings_path, "wb").write(f_out)
else:
settings_lines = open(settings_path, "r").readlines()
with open(settings_path, "w") as fid:
for _line in settings_lines:
if _line.startswith('Connection\InterfaceAddress'):
_line = 'Connection\InterfaceAddress={}\n'.format(ip_address)
fid.write(_line)
os.startfile(tor_path)
print('Waiting for {} seconds. Press any key to continue'.format(wait_time))
for i in range(wait_time):
if (i + 1) % check_vpn_gap == 0:
ip_address = check_interface(interface_names)
if ip_address is None:
print('\nvpn disconnection detected')
break
if msvcrt.kbhit():
inp = msvcrt.getch()
print('\ncontinuing')
break
time.sleep(1)
sys.stdout.write('\r{}'.format(i + 1))
sys.stdout.flush()
sys.stdout.write('\n')
sys.stdout.flush()
if proc_kill_type == 0:
tor_killed = 0
for proc in psutil.process_iter():
if proc.name() == tor_proc:
proc.terminate()
tor_killed = 1
break
if not tor_killed:
raise IOError('Tor process {} not found'.format(tor_proc))
vpn_killed = 0
for proc in psutil.process_iter():
if proc.name() == vpn_proc:
proc.kill()
vpn_killed = 1
break
if not vpn_killed:
raise IOError('VPN process {} not found'.format(vpn_proc))
else:
os.system('TASKKILL /IM {}'.format(tor_proc))
os.system('TASKKILL /IM {}'.format(vpn_proc))
if time.time() - global_start_t > restart_time > 0:
restart_now = 1
break
if restart_now:
break
print('Waiting for {} seconds. Press any key to continue'.format(post_wait_time))
for i in range(post_wait_time):
if msvcrt.kbhit():
inp = msvcrt.getch()
print('\ncontinuing')
break
time.sleep(1)
sys.stdout.write('\r{}'.format(i + 1))
sys.stdout.flush()
sys.stdout.write('\n')
sys.stdout.flush()
if restart_now:
print("Restarting...")
os.system("shutdown -t 0 -r -f")