-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.py
executable file
·192 lines (151 loc) · 5.63 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import os
import json
import urllib.request
import zipfile
import io
import sys
import gzip
import platform
import time
import threading
import subprocess
import secrets
import random
from config import PORT, TLS_DOMAIN, AUTHTOKEN, MODES
from http.server import HTTPServer, BaseHTTPRequestHandler
class GETHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(tls_link.encode("utf-8"))
def stay_alive():
server_address = ("0.0.0.0", 8080)
http_server = HTTPServer(server_address, GETHandler)
http_server_thread = threading.Thread(target=http_server.serve_forever)
http_server_thread.start()
def get_ngrok():
(
os.system("killall ngrok > /dev/null 2>&1")
if uname.system != "Windows"
else os.system("taskkill /f /im ngrok.exe 2> nul")
)
ngrok_file = open(
"ngrok.exe" if uname.system == "Windows" else "ngrok",
"wb",
)
link = ""
if uname.system == "Windows":
if uname.machine == "x86_64" or uname.machine == "AMD64":
link = (
"https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-windows-amd64.zip"
)
elif uname.system == "Linux":
if uname.machine == "aarch64" or uname.machine == "arm64":
link = (
"https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-linux-arm64.zip"
)
if uname.machine == "x86_64":
link = (
"https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-linux-amd64.zip"
)
if "armv" in uname.machine:
link = "https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-linux-arm.zip"
elif uname.system == "Darwin":
if uname.machine == "x86_64":
link = (
"https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-darwin-amd64.zip"
)
elif uname.machine == "arm64":
link = (
" https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-darwin-arm64.zip"
)
else:
sys.stderr.write("Machine not supported")
sys.exit()
elif uname.system == "FreeBSD":
pass
else:
sys.stderr.write("Machine not supported")
sys.exit()
if link:
ngrok = urllib.request.urlopen(link).read()
if link.endswith(".tgz"):
tar_file = io.BytesIO(ngrok)
binary = gzip.GzipFile(fileobj=tar_file)
ngrok_file.write(binary.read())
if link.endswith(".zip"):
with zipfile.ZipFile(io.BytesIO(ngrok)) as zipped:
for zipinfo in zipped.infolist():
with zipped.open(zipinfo) as binary:
ngrok_file.write(binary.read())
ngrok_file.close()
os.system("chmod +x ngrok") if uname.system != "Windows" else None
def expose_server():
token_cmd = f"./ngrok authtoken {AUTHTOKEN} > /dev/null"
ngrok_cmd = f"./ngrok tcp {PORT} --log=stdout > ngrok.log &"
if uname.system == "Windows":
ngrok_path = os.path.join(os.getcwd(), "ngrok.exe")
token_cmd = f'"{ngrok_path}" authtoken {AUTHTOKEN} > nul'
ngrok_cmd = f'"{ngrok_path}" tcp {PORT} --log=stdout > ngrok.log &'
tries = 5
os.system(token_cmd)
ngrok_thread = threading.Thread(target=os.system, args=[ngrok_cmd])
ngrok_thread.start()
while tries != 0:
try:
url = json.loads(
urllib.request.urlopen("http://localhost:4040/api/tunnels")
.read()
.decode("utf-8")
)["tunnels"][0]["public_url"]
url = url.replace("tcp://", "")
return url.split(":")[0], url.split(":")[1]
except Exception:
time.sleep(1)
tries -= 1
raise Exception("Timeout")
if __name__ == "__main__":
uname = platform.uname()
python_exec = sys.executable
if platform.system() == "Windows":
pass
file_dir = os.path.dirname(__file__)
os.chdir(file_dir if file_dir != "" else ".")
if not os.path.exists("ngrok"):
get_ngrok()
url, port = expose_server()
secret = secrets.token_hex(16)
if MODES["tls"]:
domain_hex = TLS_DOMAIN.encode().hex()
if len(domain_hex) >= 30:
raise Exception("TLS domain is too long: " + TLS_DOMAIN)
else:
secret = ""
for i in range(32):
secret += random.choice("abcdef0123456789")
open("secret", "w").write(secret)
tls_secret = "ee" + secret + domain_hex
tls_params = {"server": url, "port": port, "secret": tls_secret}
tls_params_encodeded = urllib.parse.urlencode(tls_params, safe=":")
tls_link = "tg://proxy?{}".format(tls_params_encodeded)
print("TLS: ")
print("in-app:", tls_link)
print("external:", "https://t.me/proxy?{}".format(tls_params_encodeded), "\n")
sec_params = {"server": url, "port": port, "secret": "dd" + secret}
sec_params_encodeded = urllib.parse.urlencode(sec_params, safe=":")
sec_link = "tg://proxy?{}".format(sec_params_encodeded)
print("Secure: ")
print("in-app:", sec_link)
print("external:", "https://t.me/proxy?{}".format(sec_params_encodeded), "\n")
print(f"host:port -> {url}:{port}\nsecret -> {secret}")
if os.getenv("REPL_ID") or os.getenv("RUN_HTTP"):
stay_alive()
(
os.system(f"{sys.executable} mtprotoproxy.py > /dev/null 2>&1")
if uname.system != "Windows"
else subprocess.run(
f"\"{python_exec}\" \"{file_dir+'/'+'mtprotoproxy.py'}\" > nul 2>&1",
shell=True,
)
)