-
Notifications
You must be signed in to change notification settings - Fork 10
/
start.py
131 lines (100 loc) · 3.49 KB
/
start.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
import argparse
import os
import re
import signal
from subprocess import Popen, check_call
from jinja2 import Template
PRIVOXY_BASE_PORT = 8118
SOCKS_BASE_PORT = 9050
HAPROXY_CONFIG_PATH = "/etc/haproxy.conf"
HAPROXY_USERNAME_FILE = "/run/secrets/haproxy_username"
HAPROXY_PASSWORD_FILE = "/run/secrets/haproxy_password"
def run_tor_process(tor_id, additional_args, host="127.0.0.1"):
port = SOCKS_BASE_PORT + tor_id
command = [
"tor",
"--SocksPort",
"{}:{}".format(host, port),
"--DataDirectory",
"/var/lib/tor/{}".format(tor_id),
"--PidFile",
"/var/run/tor/{}.pid".format(tor_id),
"--RunAsDaemon",
"1",
"--ClientOnly",
"1",
"--ExitRelay",
"0",
]
if additional_args:
command += additional_args
process = Popen(command, shell=False)
process.wait()
def prepare_privoxy_config(
default_config_path,
new_config_path,
listen_port,
socks_port,
listen_host="0.0.0.0",
socks_host="127.0.0.1",
):
with open(default_config_path, "r") as in_file:
content = in_file.read()
new_address = "listen-address {}:{}".format(listen_host, listen_port)
modified_content = re.sub(
"^listen-address.*$", new_address, content, flags=re.MULTILINE
)
modified_content += "\n" + "forward-socks5 / {}:{} .".format(socks_host, socks_port)
with open(new_config_path, "w") as out_file:
out_file.write(modified_content)
def run_privoxy_process(tor_id):
os.makedirs("/var/run/privoxy", exist_ok=True)
default_config_path = "/etc/privoxy/config"
new_config_path = "/etc/privoxy/config{}".format(tor_id)
prepare_privoxy_config(
default_config_path,
new_config_path,
PRIVOXY_BASE_PORT + tor_id,
SOCKS_BASE_PORT + tor_id,
)
pidfile = "/var/run/privoxy/{}".format(tor_id)
privoxy_process = Popen(
["privoxy", "--no-daemon", "--pidfile", pidfile, new_config_path]
)
def read_file(path):
try:
with open(path, "r") as in_file:
return in_file.read()
except Exception as e:
print(e)
return None
def update_haproxy_config(config_path, n_processes, username, password):
with open(config_path, "r") as in_file:
template = Template(in_file.read())
config = template.render(
http_ports=[PRIVOXY_BASE_PORT + i for i in range(n_processes)],
socks_ports=[SOCKS_BASE_PORT + i for i in range(n_processes)],
haproxy_username=username if username else "haproxy",
haproxy_password=password if password else "password",
)
with open(config_path, "w") as out_file:
out_file.write(config)
if __name__ == "__main__":
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument("--Tors", type=int, default=1)
args = arg_parser.parse_known_args()
parsed_args, additional_args = args
print(parsed_args, additional_args)
signal.signal(signal.SIGCHLD, signal.SIG_IGN)
# Start Tor processes
for tor_id in range(parsed_args.Tors):
run_tor_process(tor_id, additional_args)
# Start privoxy for each Tor process
for tor_id in range(parsed_args.Tors):
run_privoxy_process(tor_id)
haproxy_username = read_file(HAPROXY_USERNAME_FILE)
haproxy_password = read_file(HAPROXY_PASSWORD_FILE)
update_haproxy_config(
HAPROXY_CONFIG_PATH, parsed_args.Tors, haproxy_username, haproxy_password
)
check_call(["haproxy", "-f", HAPROXY_CONFIG_PATH, "-db"])