|
1
|
|
|
import socket |
|
2
|
|
|
import threading |
|
3
|
|
|
import random |
|
4
|
|
|
import tempfile |
|
5
|
|
|
import os |
|
6
|
|
|
import subprocess |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class MultiTorProxy: |
|
10
|
|
|
def __init__(self, listen_port=53000, listen_address='localhost', max_connections=10, tor_instaces=7, |
|
11
|
|
|
tor_start_port=53001, recv_buffer_size=4096): |
|
12
|
|
|
self.listen_port = listen_port |
|
13
|
|
|
self.listen_address = listen_address |
|
14
|
|
|
self.max_connections = max_connections |
|
15
|
|
|
self.recv_buffer_size = recv_buffer_size |
|
16
|
|
|
|
|
17
|
|
|
self.tor_instaces = tor_instaces |
|
18
|
|
|
self.tor_start_port = tor_start_port |
|
19
|
|
|
self.tor_ports = [] |
|
20
|
|
|
|
|
21
|
|
|
self.temp_directory = tempfile.TemporaryDirectory() |
|
22
|
|
|
os.chmod(self.temp_directory.name, 0o700) |
|
23
|
|
|
|
|
24
|
|
|
self.tor_settings = {"SocksListenAddress": '127.0.0.1', "CookieAuthentication": '0'} |
|
25
|
|
|
|
|
26
|
|
|
def __del__(self): |
|
27
|
|
|
self.temp_directory.cleanup() |
|
28
|
|
|
|
|
29
|
|
|
def start(self): |
|
30
|
|
|
self._start_tor_instances() |
|
31
|
|
|
threading.Thread(target=self._main_loop).start() |
|
32
|
|
|
|
|
33
|
|
|
def update_tor_settings(self, tor_settings): |
|
34
|
|
|
self.tor_settings.update(tor_settings) |
|
35
|
|
|
|
|
36
|
|
|
def _start_tor_instances(self): |
|
37
|
|
|
for i in range(0, self.tor_instaces): |
|
38
|
|
|
threading.Thread(target=self._start_tor_instance, args=(i,)).start() |
|
39
|
|
|
|
|
40
|
|
|
def _start_tor_instance(self, instance_number): |
|
41
|
|
|
instance_data_directory = os.path.join(self.temp_directory.name, str(instance_number)) |
|
42
|
|
|
if not os.path.exists(instance_data_directory): |
|
43
|
|
|
os.makedirs(instance_data_directory) |
|
44
|
|
|
os.chmod(instance_data_directory, 0o700) |
|
45
|
|
|
|
|
46
|
|
|
tor_instance_settings = self.tor_settings.copy() |
|
47
|
|
|
|
|
48
|
|
|
tor_instance_settings.update({"DataDirectory": instance_data_directory, |
|
49
|
|
|
"SocksPort": self.tor_start_port + instance_number}) |
|
50
|
|
|
|
|
51
|
|
|
self.tor_ports.append(tor_instance_settings["SocksPort"]) |
|
52
|
|
|
|
|
53
|
|
|
tor_settongs_file_path = os.path.join(instance_data_directory, 'tor_settings') |
|
54
|
|
|
|
|
55
|
|
|
with open(tor_settongs_file_path, "w") as file: |
|
56
|
|
|
for key in tor_instance_settings: |
|
57
|
|
|
file.write("%s %s\n" % (key, str(tor_instance_settings[key]))) |
|
58
|
|
|
|
|
59
|
|
|
os.chmod(tor_settongs_file_path, 0o600) |
|
60
|
|
|
|
|
61
|
|
|
while True: |
|
62
|
|
|
try: |
|
63
|
|
|
subprocess.call(["tor", "-f", tor_settongs_file_path]) |
|
64
|
|
|
except: |
|
65
|
|
|
pass |
|
66
|
|
|
|
|
67
|
|
|
def _main_loop(self): |
|
68
|
|
|
try: |
|
69
|
|
|
dock_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
70
|
|
|
dock_socket.bind((self.listen_address, self.listen_port)) |
|
71
|
|
|
dock_socket.listen(self.max_connections) |
|
72
|
|
|
while True: |
|
73
|
|
|
self._on_accept(dock_socket.accept()[0]) |
|
74
|
|
|
finally: |
|
75
|
|
|
threading.Thread(target=self._main_loop).start() |
|
76
|
|
|
|
|
77
|
|
|
def _forward(self, source, destination): |
|
78
|
|
|
while True: |
|
79
|
|
|
try: |
|
80
|
|
|
data = source.recv(self.recv_buffer_size) |
|
81
|
|
|
except Exception: |
|
82
|
|
|
self._on_close(source, destination) |
|
83
|
|
|
break |
|
84
|
|
|
|
|
85
|
|
|
if data: |
|
86
|
|
|
try: |
|
87
|
|
|
destination.sendall(data) |
|
88
|
|
|
except Exception: |
|
89
|
|
|
self._on_close(source, destination) |
|
90
|
|
|
break |
|
91
|
|
|
else: |
|
92
|
|
|
self._on_close(source, destination) |
|
93
|
|
|
break |
|
94
|
|
|
|
|
95
|
|
|
def _on_accept(self, client_socket): |
|
96
|
|
|
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
97
|
|
|
|
|
98
|
|
|
tor_port = random.choice(self.tor_ports) |
|
99
|
|
|
server_socket.connect(('localhost', tor_port)) |
|
100
|
|
|
|
|
101
|
|
|
threading.Thread(target=self._forward, args=(client_socket, server_socket)).start() |
|
102
|
|
|
threading.Thread(target=self._forward, args=(server_socket, client_socket)).start() |
|
103
|
|
|
|
|
104
|
|
|
def _on_close(self, source, destination): |
|
105
|
|
|
try: |
|
106
|
|
|
source.shutdown(socket.SHUT_RD) |
|
107
|
|
|
except Exception: |
|
108
|
|
|
pass |
|
109
|
|
|
|
|
110
|
|
|
try: |
|
111
|
|
|
destination.shutdown(socket.SHUT_WR) |
|
112
|
|
|
except Exception: |
|
113
|
|
|
pass |