-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
95 lines (85 loc) · 3.7 KB
/
main.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
import subprocess
import threading
import requests
import psutil
import time
class Tor:
def write_torrc():
with open("torrc", "w") as file:
file.write(
"BandwidthRate 2 GBytes\n"
"BandwidthBurst 2 GBytes\n"
"PerConnBWRate 2 GByte\n"
"ClientTransportPlugin obfs4 exec obfs4/obfs4proxy\n"
"ConnLimit 65535\n"
"ConstrainedSockets 1\n"
"ControlPort auto\n"
"CookieAuthentication 1\n"
"LogMessageDomains 1\n"
"HardwareAccel 1\n"
"ClientOnly 1\n"
"SocksPort auto IsolateClientAddr IsolateSOCKSAuth IsolateClientProtocol IsolateDestPort IsolateDestAddr KeepAliveIsolateSOCKSAuth PreferSOCKSNoAuth\n"
"TokenBucketRefillInterval 100 msec\n"
"UseGuardFraction 1\n"
"NumEntryGuards 1\n" # higher = more secure, but less performance
"NumDirectoryGuards 1\n"
"SafeSocks 1\n"
"TestSocks 1\n"
"WarnUnsafeSocks 1\n"
"FastFirstHopPK 1\n" # gets ready slower but more secure
"DNSPort auto IsolateClientAddr IsolateSOCKSAuth IsolateClientProtocol IsolateDestPort IsolateDestAddr KeepAliveIsolateSOCKSAuth PreferSOCKSNoAuth\n"
"AllowSingleHopCircuits 1\n"
"OptimisticData 1\n"
"ServerDNSRandomizeCase 1\n"
"GeoIPFile data/geoip\n"
"GeoIPv6File data/geoip6\n"
)
def start():
global bootstrapped, socks_port, dns_port, control_port
process = subprocess.Popen("tor -f torrc", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
while True:
output = process.stdout.readline().decode().strip()
if output:
print(output)
if "Socks listener listening on port" in output:
socks_port = output.split()[-1].split(":")[-1].replace(".", "")
elif "DNS listener listening on port" in output:
dns_port = output.split()[-1].split(":")[-1].replace(".", "")
elif "Control listener listening on port" in output:
control_port = output.split()[-1].split(":")[-1].replace(".", "")
elif "Bootstrapped 100% (done): Done" in output:
bootstrapped = True
print("\033[32m\nTor started\033[0m")
print(f"Control port: {control_port}")
print(f"Socks port: {socks_port}")
print(f"DNS port: {dns_port}\n")
def stop():
global bootstrapped
for process in ["tor", "tor.exe"]:
for proc in psutil.process_iter(['pid', 'name']):
if process.lower() in proc.info['name'].lower():
print(f"Terminating PID: {proc.pid}")
process = psutil.Process(proc.pid)
process.terminate()
bootstrapped = False
return True
return False
if __name__ == "__main__":
bootstrapped = False
socks_port = None
dns_port = None
control_port = None
Tor.write_torrc()
thread = threading.Thread(target=Tor.start)
thread.start()
while True:
if bootstrapped is True:
proxies = {
'http': f'socks5h://127.0.0.1:{socks_port}',
'https': f'socks5h://127.0.0.1:{socks_port}'
}
response = requests.get("http://api.ipify.org", proxies=proxies)
print("Proxy IP:", response.text)
break
else:
time.sleep(5)