-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTorInit.py
92 lines (68 loc) · 2.41 KB
/
TorInit.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
import re
import sys
import time
import logging
import subprocess
from stem.util import system, term
from stem import process as tor_process
class TorInit():
def __init__(self, socks_port=9050, control_port=9051):
self.socks_port = socks_port
self.control_port = control_port
# self.check_tor_command = "lsof -n -i :9050"
self.check_tor_command = "lsof -i :9050"
self.command2 = ['awk', '{print $2}']
def _checkTorStatus(self):
"""
if Torproxy is exist, will kill tor
"""
proc1 = subprocess.Popen(self.check_tor_command.split(), stdout=subprocess.PIPE)
proc2 = subprocess.Popen(self.command2, stdin=proc1.stdout, stdout=subprocess.PIPE)
port_status = proc2.communicate()[0]
logging.info(port_status)
if port_status:
port_status = set([pid for pid in re.sub(r'\D', ' ', str(port_status)).split() if pid.isdigit()])
logging.info(port_status)
for pid in port_status:
logging.info(pid)
subprocess.Popen("kill {}".format(pid).split(), stdout=subprocess.PIPE).communicate()
logging.info('kill tor pid {} success'.format(pid))
def startTorProxy(self):
"""
start tor and open SocksPort proxy, after _checkTorStatus function
"""
limit_try = 0
while True:
try:
self._checkTorStatus()
tor_process.launch_tor_with_config(
config={
'SocksPort': str(self.socks_port),
'ControlPort': str(self.control_port),
},
init_msg_handler=self.printBootstrapLines,
timeout=300
)
logging.info('Start tor success, socks_port : {}'.format(self.socks_port))
break
except Exception as e:
logging.info(e)
if limit_try == 10:
logging.info('Init tor fail.')
break
limit_try += 1
logging.info('Start tor fail, limit try : {}'.format(limit_try))
continue
def printBootstrapLines(self, line):
if "Bootstrapped " in line:
line = term.format(line, term.Color.GREEN)
self.displayMsg(line)
def displayMsg(self, msg_context, msg_type=None):
'''
Display the processing message of the process.
'''
if msg_type:
message = '[{0}] {1} \n'.format(msg_type, msg_context)
else:
message = '{0} \n'.format(msg_context)
sys.stdout.write(message)