Update server.py

This commit is contained in:
Namhyeon Go 2024-03-02 16:01:03 +09:00 committed by GitHub
parent 4993806c22
commit dd0913cb67
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -157,21 +157,8 @@ def parse_first_data(data):
def conn_string(conn, data, addr):
# check is it JSON-RPC 2.0 request
if data.find(b'{') == 0:
type, id, method, rpcdata = jsonrpc2_decode(data.decode(client_encoding))
if type == "call":
if method == "relay_accept":
accepted_relay[id] = conn
connection_speed = rpcdata['connection_speed']
print ("[*] connection speed: %s miliseconds" % (str(connection_speed)))
while conn.fileno() > -1:
time.sleep(1)
del accepted_relay[id]
print ("[*] relay destroyed: %s" % (id))
else:
rpchandler = Extension.get_rpcmethod(method)
if rpchandler:
rpchandler.dispatch(type, id, method, rpcdata)
return
jsonrpc2_server(conn, data)
return
# parse first data (header)
webserver, port, scheme, method, url = parse_first_data(data)
@ -187,6 +174,34 @@ def conn_string(conn, data, addr):
proxy_server(webserver, port, scheme, method, url, conn, addr, data)
def jsonrpc2_server(conn, data):
# get following chunks
conn.settimeout(1)
while True:
try:
chunk = conn.recv(buffer_size)
if not chunk:
break
data += chunk
except:
break
# process JSON-RPC 2 request
type, id, method, rpcdata = jsonrpc2_decode(data.decode(client_encoding))
if type == "call":
if method == "relay_accept":
accepted_relay[id] = conn
connection_speed = rpcdata['connection_speed']
print ("[*] connection speed: %s miliseconds" % (str(connection_speed)))
while conn.fileno() > -1:
time.sleep(1)
del accepted_relay[id]
print ("[*] relay destroyed: %s" % (id))
else:
rpchandler = Extension.get_rpcmethod(method)
if rpchandler:
rpchandler.dispatch(type, id, method, rpcdata)
def proxy_connect(webserver, conn):
hostname = webserver.decode(client_encoding)
certpath = "%s/%s.crt" % (certdir.rstrip('/'), hostname)