-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.py
80 lines (69 loc) · 2.27 KB
/
worker.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
import tempfile
import requests
import zipfile
import pathlib
import base64
import time
import subprocess
import shutil
import os
server_host = os.getenv("MASTER_ONION")
print(f"Server host: {server_host}")
server_port = 80
session = requests.session()
session.proxies = {
'http': 'socks5h://127.0.0.1:9050'
}
def upload_onion(path):
with tempfile.TemporaryDirectory() as d:
with zipfile.ZipFile(f"{d}/onion.zip", "w") as zf:
for f in pathlib.Path(path).iterdir():
zf.write(f, arcname=f.name)
with open(f"{d}/onion.zip", "rb") as f:
body = {
"zipfile": base64.b64encode(f.read()).decode()
}
for _ in range(5):
try:
session.post(f"http://{server_host}:{server_port}/found", json=body)
return
except requests.exceptions.ConnectionError:
time.sleep(10)
print("Failed to connect to master, trying again shortly...")
exit("Failed to connect too many times. Exiting now.")
def get_filters():
for _ in range(5):
try:
res = session.get(f"http://{server_host}:{server_port}/filters")
return res.json()["filters"]
except requests.exceptions.ConnectionError:
time.sleep(10)
print("Failed to connect to master, trying again shortly...")
exit("Failed to connect too many times. Exiting now.")
def generate_onion(filters, timeout=60):
try:
print(f"Searching for up to {timeout} seconds with filter \"{','.join(filters)}\"")
p = subprocess.run(
["mkp224o", "-z", "-s", "-n", "1", *filters],
timeout=timeout,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
)
onion_name = p.stdout.decode().strip()
return onion_name
except subprocess.TimeoutExpired:
print("Timed out.")
return None
def main():
while True:
filters = get_filters()
if not filters:
time.sleep(10)
continue
found_onion = generate_onion(filters)
if found_onion:
print(f"Successfully found onion, uploading... ({found_onion})")
upload_onion(found_onion)
shutil.rmtree(found_onion)
if __name__ == "__main__":
main()