-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextsv.py
99 lines (76 loc) · 2.2 KB
/
extsv.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
from flask import Flask, request, stream_with_context, Response
from threading import Thread
import base64
from collections import deque
import socket
from uuid import uuid4
import traceback
MLEN = 65000
app = Flask(__name__)
connections: dict = {}
def sock(host):
remote = socket.socket()
remote.connect((host[0], host[1]))
print("Connecting to", host)
return remote
@stream_with_context
def handle_get(uid):
while True:
try:
data = connections[uid]["sock"].recv(MLEN)
except:
data = b""
if data:
yield base64.b64encode(data).decode()+"\r"
else:
del connections[uid]
break
@stream_with_context
def handle_post(uid):
data = request.data
try:
connections[uid]["sock"].sendall(base64.b64decode(data))
except Exception as e:
del connections[uid]
traceback.print_exc()
raise e
def proxy(uid):
print("proxy started")
while True:
try:
data = connections[uid]["sock"].recv(MLEN)
except:
data = b""
if data:
connections[uid]["queue"].append(base64.b64encode(data).decode())
else:
del connections[uid]
break
@app.route("/")
def index():
return "Server random :3"
@app.route("/connections", methods=["POST"])
def make_conn():
uid = str(uuid4())
data = request.form
host = data["host"].split(":")
host[1] = int(host[1])
connections[uid] = {"sock": sock(host),}
return uid
@app.route("/connections/<uid>", methods=["GET", "POST", "DELETE"])
def connection(uid):
if not uid in connections.keys():
return "NOT FOUND", 404
if request.method == "POST":
handle_post(uid)
return "OK", 200
elif request.method == "GET":
return Response(handle_get(uid))
elif request.method == "DELETE":
if uid in connections.keys():
del connections[uid]
return "OK", 200
else:
return "NOT FOUND", 404
if __name__ == "__main__":
app.run()