Repository navigation
Expand file tree
/
Copy pathweb_control.py
More file actions
745 lines (620 loc) · 29.5 KB
/
Copy pathweb_control.py
File metadata and controls
745 lines (620 loc) · 29.5 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
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
#!/usr/bin/env python
# coding:utf-8
import platform
from local import env_info
import urllib.parse
import json
import os
import re
import subprocess
import cgi
import urllib.request, urllib.error, urllib.parse
import sys
import datetime
import locale
import time
import codecs
from xlog import getLogger
xlog = getLogger("gae_proxy")
from .config import config
from .appids_manager import appid_manager
from .google_ip import google_ip
from .google_ip_range import ip_range
from .connect_manager import https_manager
from .scan_ip_log import scan_ip_log
import configparser
from . import connect_control
from . import ip_utils
from . import check_local_network
from . import check_ip
from . import cert_util
import simple_http_server
from . import test_appid
os.environ['HTTPS_PROXY'] = ''
current_path = os.path.dirname(os.path.abspath(__file__))
root_path = os.path.abspath(os.path.join(current_path, os.pardir, os.pardir))
web_ui_path = os.path.join(current_path, os.path.pardir, "web_ui")
import yaml
class User_special(object):
def __init__(self):
self.appid = ''
self.password = ''
self.proxy_enable = "0"
self.proxy_type = "HTTP"
self.proxy_host = ""
self.proxy_port = ""
self.proxy_user = ""
self.proxy_passwd = ""
self.host_appengine_mode = "gae"
self.auto_adjust_scan_ip_thread_num = 1
self.scan_ip_thread_num = 0
self.use_ipv6 = 0
class User_config(object):
user_special = User_special()
def __init__(self):
self.load()
def load(self):
configparser.RawConfigParser.OPTCRE = re.compile(r'(?P<option>[^=\s][^=]*)\s*(?P<vi>[=])\s*(?P<value>.*)$')
self.DEFAULT_CONFIG = configparser.ConfigParser()
DEFAULT_CONFIG_FILENAME = os.path.abspath( os.path.join(current_path, 'proxy.ini'))
self.USER_CONFIG = configparser.ConfigParser()
CONFIG_USER_FILENAME = os.path.abspath( os.path.join(root_path, 'data', 'gae_proxy', 'config.ini'))
try:
if os.path.isfile(DEFAULT_CONFIG_FILENAME):
self.DEFAULT_CONFIG.readfp(codecs.open(DEFAULT_CONFIG_FILENAME, "r", "utf8"))
self.user_special.scan_ip_thread_num = self.DEFAULT_CONFIG.getint('google_ip', 'max_scan_ip_thread_num')
else:
return
if os.path.isfile(CONFIG_USER_FILENAME):
self.USER_CONFIG.read(CONFIG_USER_FILENAME)
else:
return
try:
self.user_special.appid = self.USER_CONFIG.get('gae', 'appid')
self.user_special.password = self.USER_CONFIG.get('gae', 'password')
except:
pass
try:
self.user_special.host_appengine_mode = self.USER_CONFIG.get('hosts', 'appengine.google.com')
except:
pass
try:
self.user_special.scan_ip_thread_num = config.CONFIG.getint('google_ip', 'max_scan_ip_thread_num')
except:
self.user_special.scan_ip_thread_num = self.DEFAULT_CONFIG.getint('google_ip', 'max_scan_ip_thread_num')
try:
self.user_special.auto_adjust_scan_ip_thread_num = config.CONFIG.getint('google_ip', 'auto_adjust_scan_ip_thread_num')
except:
pass
try:
self.user_special.use_ipv6 = config.CONFIG.getint('google_ip', 'use_ipv6')
except:
pass
self.user_special.proxy_enable = self.USER_CONFIG.get('proxy', 'enable')
self.user_special.proxy_type = self.USER_CONFIG.get('proxy', 'type')
self.user_special.proxy_host = self.USER_CONFIG.get('proxy', 'host')
self.user_special.proxy_port = self.USER_CONFIG.get('proxy', 'port')
self.user_special.proxy_user = self.USER_CONFIG.get('proxy', 'user')
self.user_special.proxy_passwd = self.USER_CONFIG.get('proxy', 'passwd')
except Exception as e:
xlog.warn("User_config.load except:%s", e)
def save(self):
CONFIG_USER_FILENAME = os.path.abspath( os.path.join(root_path, 'data', 'gae_proxy', 'config.ini'))
try:
f = open(CONFIG_USER_FILENAME, 'w')
if self.user_special.appid != "":
f.write("[gae]\n")
f.write("appid = %s\n" % self.user_special.appid)
f.write("password = %s\n\n" % self.user_special.password)
f.write("[proxy]\n")
f.write("enable = %s\n" % self.user_special.proxy_enable)
f.write("type = %s\n" % self.user_special.proxy_type)
f.write("host = %s\n" % self.user_special.proxy_host)
f.write("port = %s\n" % self.user_special.proxy_port)
f.write("user = %s\n" % self.user_special.proxy_user)
f.write("passwd = %s\n\n" % self.user_special.proxy_passwd)
if self.user_special.host_appengine_mode != "gae":
f.write("[hosts]\n")
f.write("appengine.google.com = %s\n" % self.user_special.host_appengine_mode)
f.write("www.google.com = %s\n\n" % self.user_special.host_appengine_mode)
f.write("[google_ip]\n")
if int(self.user_special.auto_adjust_scan_ip_thread_num) != self.DEFAULT_CONFIG.getint('google_ip', 'auto_adjust_scan_ip_thread_num'):
f.write("auto_adjust_scan_ip_thread_num = %d\n\n" % int(self.user_special.auto_adjust_scan_ip_thread_num))
if int(self.user_special.scan_ip_thread_num) != self.DEFAULT_CONFIG.getint('google_ip', 'max_scan_ip_thread_num'):
f.write("max_scan_ip_thread_num = %d\n\n" % int(self.user_special.scan_ip_thread_num))
if int(self.user_special.use_ipv6) != self.DEFAULT_CONFIG.getint('google_ip', 'use_ipv6'):
f.write("use_ipv6 = %d\n\n" % int(self.user_special.use_ipv6))
f.close()
except:
xlog.warn("launcher.config save user config fail:%s", CONFIG_USER_FILENAME)
user_config = User_config()
def http_request(url, method="GET"):
proxy_handler = urllib.request.ProxyHandler({})
opener = urllib.request.build_opener(proxy_handler)
try:
req = opener.open(url)
except Exception as e:
xlog.exception("web_control http_request:%s fail:%s", url, e)
return
deploy_proc = None
class ControlHandler(simple_http_server.HttpServerHandler):
def __init__(self, client_address, headers, command, path, rfile, wfile):
self.client_address = client_address
self.headers = headers
self.command = command
self.path = path
self.rfile = rfile
self.wfile = wfile
def do_CONNECT(self):
self.wfile.write(b'HTTP/1.1 403\r\nConnection: close\r\n\r\n')
def do_GET(self):
path = urllib.parse.urlparse(self.path).path
if path == "/log":
return self.req_log_handler()
elif path == "/status":
return self.req_status_handler()
else:
xlog.debug('GAEProxy Web_control %s %s %s ', self.address_string(), self.command, self.path)
if path == '/deploy':
return self.req_deploy_handler()
elif path == "/config":
return self.req_config_handler()
elif path == "/ip_list":
return self.req_ip_list_handler()
elif path == "/scan_ip":
return self.req_scan_ip_handler()
elif path == "/ssl_pool":
return self.req_ssl_pool_handler()
elif path == "/download_cert":
return self.req_download_cert_handler()
elif path == "/is_ready":
return self.req_is_ready_handler()
elif path == "/test_ip":
return self.req_test_ip_handler()
elif path == "/check_ip":
return self.req_check_ip_handler()
elif path == "/quit":
connect_control.keep_running = False
data = "Quit"
self.wfile.write(('HTTP/1.1 200\r\nContent-Type: %s\r\nContent-Length: %s\r\n\r\n' % ('text/plain', len(data))).encode())
self.wfile.write(data)
#sys.exit(0)
#quit()
#os._exit(0)
return
elif path.startswith("/wizard/"):
file_path = os.path.abspath(os.path.join(web_ui_path, '/'.join(path.split('/')[1:])))
if not os.path.isfile(file_path):
self.wfile.write(b'HTTP/1.1 404 Not Found\r\n\r\n')
xlog.warn('%s %s %s wizard file %s not found', self.address_string(), self.command, self.path, file_path)
return
if file_path.endswith('.html'):
mimetype = 'text/html'
elif file_path.endswith('.png'):
mimetype = 'image/png'
elif file_path.endswith('.jpg') or file_path.endswith('.jpeg'):
mimetype = 'image/jpeg'
else:
mimetype = 'application/octet-stream'
self.send_file(file_path, mimetype)
return
else:
xlog.warn('Control Req %s %s %s ', self.address_string(), self.command, self.path)
# check for '..', which will leak file
if re.search(r'(\.{2})', self.path) is not None:
self.wfile.write(b'HTTP/1.1 404\r\n\r\n')
xlog.warn('%s %s %s haking', self.address_string(), self.command, self.path )
return
filename = os.path.normpath('./' + path)
if self.path.startswith(('http://', 'https://')):
data = b'HTTP/1.1 200\r\nCache-Control: max-age=86400\r\nExpires:Oct, 01 Aug 2100 00:00:00 GMT\r\nConnection: close\r\n'
data += b'\r\n'
self.wfile.write(data)
xlog.info('%s "%s %s HTTP/1.1" 200 -', self.address_string(), self.command, self.path)
elif os.path.isfile(filename):
if filename.endswith('.pac'):
mimetype = 'text/plain'
else:
mimetype = 'application/octet-stream'
#self.send_file(filename, mimetype)
else:
self.wfile.write(b'HTTP/1.1 404\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\n404 Not Found')
xlog.info('%s "%s %s HTTP/1.1" 404 -', self.address_string(), self.command, self.path)
def do_POST(self):
try:
refer = self.headers.get('Referer')
netloc = urllib.parse.urlparse(refer).netloc
if not netloc.startswith("127.0.0.1") and not netloc.startswitch("localhost"):
xlog.warn("web control ref:%s refuse", netloc)
return
except:
pass
xlog.debug ('GAEProxy web_control %s %s %s ', self.address_string(), self.command, self.path)
try:
ctype, pdict = cgi.parse_header(self.headers.get('content-type'))
if ctype == 'multipart/form-data':
self.postvars = cgi.parse_multipart(self.rfile, pdict)
elif ctype == 'application/x-www-form-urlencoded':
length = int(self.headers.get('content-length'))
self.postvars = urllib.parse.parse_qs(self.rfile.read(length).decode('iso-8859-1'), keep_blank_values=1)
else:
self.postvars = {}
except:
self.postvars = {}
path = urllib.parse.urlparse(self.path).path
if path == '/deploy':
return self.req_deploy_handler()
elif path == "/config":
return self.req_config_handler()
elif path == "/scan_ip":
return self.req_scan_ip_handler()
elif path.startswith("/importip"):
return self.req_importip_handler()
else:
self.wfile.write(b'HTTP/1.1 404\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\n404 Not Found')
xlog.info('%s "%s %s HTTP/1.1" 404 -', self.address_string(), self.command, self.path)
def req_log_handler(self):
req = urllib.parse.urlparse(self.path).query
reqs = urllib.parse.parse_qs(req, keep_blank_values=True)
data = ''
cmd = "get_last"
if reqs["cmd"]:
cmd = reqs["cmd"][0]
if cmd == "set_buffer_size" :
if not reqs["buffer_size"]:
data = '{"res":"fail", "reason":"size not set"}'
mimetype = 'text/plain'
self.send_response(mimetype, data)
return
buffer_size = reqs["buffer_size"][0]
xlog.set_buffer_size(buffer_size)
elif cmd == "get_last":
max_line = int(reqs["max_line"][0])
data = xlog.get_last_lines(max_line)
elif cmd == "get_new":
last_no = int(reqs["last_no"][0])
data = xlog.get_new_lines(last_no)
else:
xlog.error('PAC %s %s %s ', self.address_string(), self.command, self.path)
mimetype = 'text/plain'
self.send_response(mimetype, data)
def get_launcher_version(self):
data_path = os.path.abspath( os.path.join(root_path, 'data', 'launcher', 'config.yaml'))
try:
config = yaml.load(open(data_path, 'r'))
return config["modules"]["launcher"]["current_version"]
#print yaml.dump(config)
except yaml.YAMLError as exc:
print("Error in configuration file:", exc)
return "unknown"
@staticmethod
def xxnet_version():
readme_file = os.path.join(root_path, "README.md")
try:
fd = codecs.open(readme_file,'r','utf-8')
lines = fd.readlines()
import re
p = re.compile(r'https://codeload.github.com/XX-net/XX-Net/zip/([0-9]+)\.([0-9]+)\.([0-9]+)') #zip/([0-9]+).([0-9]+).([0-9]+)
#m = p.match(content)
for line in lines:
m = p.match(line)
if m:
version = m.group(1) + "." + m.group(2) + "." + m.group(3)
return version
except Exception as e:
xlog.exception("xxnet_version fail")
return "get_version_fail"
def get_os_language(self):
if hasattr(self, "lang_code"):
return self.lang_code
try:
lang_code, code_page = locale.getdefaultlocale()
#('en_GB', 'cp1252'), en_US,
self.lang_code = lang_code
return lang_code
except:
#Mac fail to run this
pass
if sys.platform == "darwin":
try:
oot = os.pipe()
p = subprocess.Popen(["/usr/bin/defaults", 'read', 'NSGlobalDomain', 'AppleLanguages'],stdout=oot[1])
p.communicate()
lang_code = os.read(oot[0],10000)
self.lang_code = lang_code
return lang_code
except:
pass
lang_code = 'Unknown'
return lang_code
def req_status_handler(self):
if "user-agent" in self.headers:
user_agent = self.headers["user-agent"]
else:
user_agent = ""
good_ip_num = google_ip.good_ip_num
if good_ip_num > len(google_ip.gws_ip_list):
good_ip_num = len(google_ip.gws_ip_list)
res_arr = {
"sys_platform": "%s, %s" % (platform.machine(), platform.platform()),
"os_system": platform.system(),
"os_version": platform.version(),
"os_release": platform.release(),
"architecture": platform.architecture(),
"os_detail": env_info.os_detail(),
"language": self.get_os_language(),
"browser": user_agent,
"xxnet_version": self.xxnet_version(),
"python_version": platform.python_version(),
"proxy_listen": config.LISTEN_IP + ":" + str(config.LISTEN_PORT),
"pac_url": config.pac_url,
"use_ipv6": config.CONFIG.getint("google_ip", "use_ipv6"),
"gae_appid": "|".join(config.GAE_APPIDS),
"working_appid": "|".join(appid_manager.working_appid_list),
"out_of_quota_appids": "|".join(appid_manager.out_of_quota_appids),
"not_exist_appids": "|".join(appid_manager.not_exist_appids),
"network_state": check_local_network.network_stat,
"ip_num": len(google_ip.gws_ip_list),
"good_ip_num": good_ip_num,
"connected_link_new": len(https_manager.new_conn_pool.pool),
"connected_link_used": len(https_manager.gae_conn_pool.pool),
"scan_ip_thread_num": google_ip.scan_thread_count,
"ip_quality": google_ip.ip_quality(),
"block_stat": connect_control.block_stat(),
"high_prior_connecting_num": connect_control.high_prior_connecting_num,
"low_prior_connecting_num": connect_control.low_prior_connecting_num,
"high_prior_lock": len(connect_control.high_prior_lock),
"low_prior_lock": len(connect_control.low_prior_lock),
}
data = json.dumps(res_arr, indent=0, sort_keys=True)
self.send_response('text/html', data)
def req_config_handler(self):
req = urllib.parse.urlparse(self.path).query
reqs = urllib.parse.parse_qs(req, keep_blank_values=True)
data = ''
appid_updated = False
try:
if reqs['cmd'] == ['get_config']:
data = json.dumps(user_config.user_special, default=lambda o: o.__dict__)
elif reqs['cmd'] == ['set_config']:
appids = self.postvars['appid'][0]
if appids != user_config.user_special.appid:
if appids and google_ip.good_ip_num:
fail_appid_list = test_appid.test_appids(appids)
if len(fail_appid_list):
fail_appid = "|".join(fail_appid_list)
return self.send_response('text/html', '{"res":"fail", "reason":"appid fail:%s"}' % fail_appid)
appid_updated = True
user_config.user_special.appid = appids
user_config.user_special.proxy_enable = self.postvars['proxy_enable'][0]
user_config.user_special.proxy_type = self.postvars['proxy_type'][0]
user_config.user_special.proxy_host = self.postvars['proxy_host'][0]
user_config.user_special.proxy_port = self.postvars['proxy_port'][0]
try:
user_config.user_special.proxy_port = int(user_config.user_special.proxy_port)
except:
user_config.user_special.proxy_port = 0
user_config.user_special.proxy_user = self.postvars['proxy_user'][0]
user_config.user_special.proxy_passwd = self.postvars['proxy_passwd'][0]
user_config.user_special.host_appengine_mode = self.postvars['host_appengine_mode'][0]
use_ipv6 = int(self.postvars['use_ipv6'][0])
if user_config.user_special.use_ipv6 != use_ipv6:
if use_ipv6:
if not check_local_network.check_ipv6():
xlog.warn("IPv6 was enabled, but check failed.")
return self.send_response('text/html', '{"res":"fail", "reason":"IPv6 fail"}')
user_config.user_special.use_ipv6 = use_ipv6
user_config.save()
config.load()
appid_manager.reset_appid()
from local import connect_manager
connect_manager.load_proxy_config()
connect_manager.https_manager.load_config()
if appid_updated:
connect_manager.https_manager.clean_old_connection()
google_ip.reset()
check_ip.load_proxy_config()
data = '{"res":"success"}'
self.send_response('text/html', data)
#http_request("http://127.0.0.1:8085/init_module?module=gae_proxy&cmd=restart")
return
except Exception as e:
xlog.exception("req_config_handler except:%s", e)
data = '{"res":"fail", "except":"%s"}' % e
self.send_response('text/html', data)
def req_deploy_handler(self):
global deploy_proc
req = urllib.parse.urlparse(self.path).query
reqs = urllib.parse.parse_qs(req, keep_blank_values=True)
data = ''
log_path = os.path.abspath(os.path.join(current_path, os.pardir, "server", 'upload.log'))
time_now = datetime.datetime.today().strftime('%H:%M:%S-%a/%d/%b/%Y')
if reqs['cmd'] == ['deploy']:
appid = self.postvars['appid'][0]
if deploy_proc and deploy_proc.poll() == None:
xlog.warn("deploy is running, request denied.")
data = '{"res":"deploy is running", "time":"%s"}' % time_now
else:
try:
if os.path.isfile(log_path):
os.remove(log_path)
script_path = os.path.abspath(os.path.join(current_path, os.pardir, "server", 'uploader.py'))
deploy_proc = subprocess.Popen([sys.executable, script_path, appid])
xlog.info("deploy begin.")
data = '{"res":"success", "time":"%s"}' % time_now
except Exception as e:
data = '{"res":"%s", "time":"%s"}' % (e, time_now)
elif reqs['cmd'] == ['cancel']:
if deploy_proc and deploy_proc.poll() == None:
deploy_proc.kill()
data = '{"res":"deploy is killed", "time":"%s"}' % time_now
else:
data = '{"res":"deploy is not running", "time":"%s"}' % time_now
elif reqs['cmd'] == ['get_log']:
if deploy_proc and os.path.isfile(log_path):
with open(log_path, "r") as f:
content = f.read()
else:
content = ""
status = 'init'
if deploy_proc:
if deploy_proc.poll() == None:
status = 'running'
else:
status = 'finished'
data = json.dumps({'status': status, 'log': content, 'time': time_now})
self.send_response('text/html', data)
def req_importip_handler(self):
req = urllib.parse.urlparse(self.path).query
reqs = urllib.parse.parse_qs(req, keep_blank_values=True)
data = ''
if reqs['cmd'] == ['importip']:
count = 0
ip_list = self.postvars['ipList'][0]
addresses = ip_list.split('|')
for ip in addresses:
if not ip_utils.check_ip_valid(ip):
continue
if google_ip.add_ip(ip, 100, "google.com", "gws"):
count += 1
data = '{"res":"%s"}' % count
google_ip.save_ip_list(force=True)
elif reqs['cmd'] == ['exportip']:
data = '{"res":"'
for ip in google_ip.gws_ip_list:
if google_ip.ip_dict[ip]['fail_times'] > 0:
continue
data += "%s|" % ip
data = data[0: len(data) - 1]
data += '"}'
self.send_response('text/html', data)
def req_test_ip_handler(self):
req = urllib.parse.urlparse(self.path).query
reqs = urllib.parse.parse_qs(req, keep_blank_values=True)
ip = reqs['ip'][0]
result = check_ip.test_gae_ip(ip)
if not result:
data = "{'res':'fail'}"
else:
data = json.dumps("{'ip':'%s', 'handshake':'%s', 'server':'%s', 'domain':'%s'}" %
(ip, result.handshake_time, result.server_type, result.domain))
self.send_response('text/html', data)
def req_ip_list_handler(self):
time_now = time.time()
data = "<html><body><div style='float: left; white-space:nowrap;font-family: monospace;'>"
data += "time:%d pointer:%d<br>\r\n" % (time_now, google_ip.gws_ip_pointer)
data += "<table><tr><th>N</th><th>IP</th><th>HS</th><th>Fails</th><th>links</th><th>get_time</th><th>success_time</th><th>fail_time</th>"
data += "<th>data_active</th><th>transfered_data</th><th>Trans</th><th>history</th></tr>\n"
i = 1
for ip in google_ip.gws_ip_list:
handshake_time = google_ip.ip_dict[ip]["handshake_time"]
fail_times = google_ip.ip_dict[ip]["fail_times"]
links = google_ip.ip_dict[ip]["links"]
get_time = google_ip.ip_dict[ip]["get_time"]
if get_time:
get_time = time_now - get_time
success_time = google_ip.ip_dict[ip]["success_time"]
if success_time:
success_time = time_now - success_time
fail_time = google_ip.ip_dict[ip]["fail_time"]
if fail_time:
fail_time = time_now - fail_time
data_active = google_ip.ip_dict[ip]["data_active"]
if data_active:
active_time = time_now - data_active
else:
active_time = 0
transfered_data = google_ip.ip_dict[ip]["transfered_data"]
transfered_quota = transfered_data - (active_time * config.ip_traffic_quota)
history = google_ip.ip_dict[ip]["history"]
t0 = 0
str_out = ''
for item in history:
t = item[0]
v = item[1]
if t0 == 0:
t0 = t
time_per = int((t - t0) * 1000)
t0 = t
str_out += "%d(%s) " % (time_per, v)
data += "<tr><td>%d</td><td>%s</td><td>%d</td><td>%d</td><td>%d</td><td>%d</td><td>%d</td><td>%d</td>" \
"<td>%d</td><td>%d</td><td>%d</td><td>%s</td></tr>\n" % \
(i, ip, handshake_time, fail_times, links, get_time, success_time, fail_time,
active_time, transfered_data, transfered_quota, str_out)
i += 1
data += "</table></div></body></html>"
mimetype = 'text/html'
self.send_response(mimetype, data)
def req_scan_ip_handler(self):
req = urllib.parse.urlparse(self.path).query
reqs = urllib.parse.parse_qs(req, keep_blank_values=True)
data = ""
if reqs['cmd'] == ['get_range']:
data = ip_range.load_range_content()
elif reqs['cmd'] == ['set_range']:
content = self.postvars['ip_range'][0]
ip_range.update_range_content(content)
ip_range.load_ip_range()
data = '{"res":"success"}'
elif reqs['cmd'] == ['set_auto_adjust_scan_ip_thread_num']:
user_config.user_special.auto_adjust_scan_ip_thread_num = int(self.postvars['auto_adjust_scan_ip_thread_num'][0])
user_config.save()
google_ip.auto_adjust_scan_ip_thread_num = user_config.user_special.auto_adjust_scan_ip_thread_num
data = '{"res":"success"}'
elif reqs['cmd'] == ['set_scan_thread_num']:
user_config.user_special.scan_ip_thread_num = int(self.postvars['scan_ip_thread_num'][0])
user_config.save()
scan_ip_thread_num = int(self.postvars['scan_ip_thread_num'][0])
google_ip.adjust_scan_thread_num(scan_ip_thread_num)
data = '{"res":"success"}'
elif reqs['cmd'] == ['get_scan_ip_log']:
data = scan_ip_log.get_log_content()
mimetype = 'text/plain'
self.send_response(mimetype, data)
def req_ssl_pool_handler(self):
data = "New conn:\n"
data += https_manager.new_conn_pool.to_string()
data += "\nGAE conn:\n"
data += https_manager.gae_conn_pool.to_string()
for host in https_manager.host_conn_pool:
data += "\nHost:%s\n" % host
data += https_manager.host_conn_pool[host].to_string()
mimetype = 'text/plain'
self.send_response(mimetype, data)
def req_download_cert_handler(self):
filename = cert_util.CertUtil.ca_keyfile
with open(filename, 'rb') as fp:
data = fp.read()
mimetype = "text/plain"
self.wfile.write(('HTTP/1.1 200\r\nContent-Disposition: attachment; filename=CA.crt\r\nContent-Type: %s\r\nContent-Length: %s\r\n\r\n' % (mimetype, len(data))).encode())
self.wfile.write(data)
def req_is_ready_handler(self):
data = "%s" % config.cert_import_ready
mimetype = 'text/plain'
self.send_response(mimetype, data)
def req_check_ip_handler(self):
req = urllib.parse.urlparse(self.path).query
reqs = urllib.parse.parse_qs(req, keep_blank_values=True)
data = ""
if reqs['cmd'] == ['get_process']:
all_ip_num = len(google_ip.ip_dict)
left_num = google_ip.scan_exist_ip_queue.qsize()
good_num = google_ip.good_ip_num
data = json.dumps(dict(all_ip_num=all_ip_num, left_num=left_num, good_num=good_num))
self.send_response('text/plain', data)
elif reqs['cmd'] == ['start']:
left_num = google_ip.scan_exist_ip_queue.qsize()
if left_num:
self.send_response('text/plain', '{"res":"fail", "reason":"running"}')
else:
google_ip.start_scan_all_exist_ip()
self.send_response('text/plain', '{"res":"success"}')
elif reqs['cmd'] == ['stop']:
left_num = google_ip.scan_exist_ip_queue.qsize()
if not left_num:
self.send_response('text/plain', '{"res":"fail", "reason":"not running"}')
else:
google_ip.stop_scan_all_exist_ip()
self.send_response('text/plain', '{"res":"success"}')
else:
return self.send_not_exist()