1
This commit is contained in:
parent
fe82e43428
commit
d1fab83969
@ -15,21 +15,65 @@ class ClientSide:
|
||||
self._local_ip = local_ip
|
||||
self._remote_ip = remote_ip
|
||||
self.local_conn = None
|
||||
self.local_read_future = None
|
||||
self.remote_conn = None
|
||||
|
||||
@gen.coroutine
|
||||
def co_connect(self):
|
||||
def co_connect2(self):
|
||||
while True:
|
||||
if not self.local_conn:
|
||||
[local_host, local_port] = self._local_ip.split(':')
|
||||
local_conn = yield TCPClient().connect(local_host, local_port)
|
||||
self.local_conn = yield TCPClient().connect(local_host, local_port)
|
||||
if not self.remote_conn:
|
||||
[remote_host, remote_port] = self._remote_ip.split(':')
|
||||
url = 'ws://%s:%s/websocket' % (remote_host, remote_port)
|
||||
remote_conn = yield websocket_connect(url)
|
||||
self.remote_conn = yield websocket_connect(url)
|
||||
|
||||
if self.local_conn:
|
||||
pass
|
||||
if self.local_conn and not self.local_read_future:
|
||||
print('ok1')
|
||||
self.local_read_future = self.local_conn.read_until(b"\n")
|
||||
print('ok')
|
||||
|
||||
if self.local_read_future:
|
||||
self.local_read_future.fetch_next_chunk()
|
||||
|
||||
@gen.coroutine
|
||||
def co_localConnect(self):
|
||||
[local_host, local_port] = self._local_ip.split(':')
|
||||
self.local_conn = yield TCPClient().connect(local_host, local_port)
|
||||
|
||||
@gen.coroutine
|
||||
def co_remoteConnect(self):
|
||||
[remote_host, remote_port] = self._remote_ip.split(':')
|
||||
url = 'ws://%s:%s/websocket' % (remote_host, remote_port)
|
||||
self.remote_conn = yield websocket_connect(url)
|
||||
data = ''
|
||||
while True:
|
||||
data += yield self.remote_conn.read_message()
|
||||
lines = data.split('\n')
|
||||
if data[-1] == '\n':
|
||||
data = lines[-1]
|
||||
lines = lines[:-1]
|
||||
for line in lines:
|
||||
print(line)
|
||||
msg = json.loads(line)
|
||||
self.dispatchRemoteMsg(msg)
|
||||
|
||||
def dispatchRemoteMsg(self, msg):
|
||||
if msg['cmd'] == 'connect':
|
||||
ioloop.IOLoop.current().spawn_callback(self.co_localConnect)
|
||||
elif msg['cmd'] == 'socketClose':
|
||||
pass
|
||||
elif msg['cmd'] == 'forwardData':
|
||||
pass
|
||||
|
||||
@gen.coroutine
|
||||
def co_connect(self):
|
||||
# ioloop.IOLoop.current().spawn_callback(self.co_localConnect)
|
||||
ioloop.IOLoop.current().spawn_callback(self.co_remoteConnect)
|
||||
while True:
|
||||
yield gen.sleep(0.1)
|
||||
|
||||
def run(self):
|
||||
ioloop.IOLoop.current().run_sync(self.co_connect)
|
||||
|
||||
|
@ -50,6 +50,7 @@ class ServerSide:
|
||||
self.remoteConnIdx += 1
|
||||
self.remotePending[conn.idx] = conn
|
||||
conn.localSocket = local_conn
|
||||
print('zzzzzzzz')
|
||||
self.remotePendingMutex.release()
|
||||
local_conn.SendMsg({
|
||||
'remoteConnIdx' : conn.idx,
|
||||
@ -70,7 +71,7 @@ class ServerSide:
|
||||
for remoteConn in self.remoteConnHash.values():
|
||||
if remoteConn.localSocket == conn:
|
||||
try:
|
||||
remoteConn.steam.close()
|
||||
remoteConn.stream.close()
|
||||
except:
|
||||
pass
|
||||
except:
|
||||
@ -113,7 +114,7 @@ class ServerSide:
|
||||
self.remotePendingMutex.acquire()
|
||||
if msg['remoteConnIdx'] in self.remotePending:
|
||||
local_conn = self.remotePending[msg['remoteConnIdx']]
|
||||
local_conn.steam.write(base64.b64decode(msg['data']))
|
||||
local_conn.stream.write(base64.b64decode(msg['data']))
|
||||
self.remotePendingMutex.release()
|
||||
|
||||
def isConnectOk(self, connIdx):
|
||||
@ -125,8 +126,8 @@ class ServerSide:
|
||||
|
||||
class RemoteServerConnection(object):
|
||||
|
||||
def __init__(self, steam, address):
|
||||
self.steam = steam
|
||||
def __init__(self, stream, address):
|
||||
self.stream = stream
|
||||
self.address = address
|
||||
self.idx = 0
|
||||
self.localSocket = None
|
||||
@ -154,15 +155,15 @@ class RemoteServer(tornado.tcpserver.TCPServer):
|
||||
|
||||
async def handle_stream(self, stream, address):
|
||||
global app
|
||||
conn = RemoteServerConnection(steam, address)
|
||||
conn = RemoteServerConnection(stream, address)
|
||||
if not app.addRemoteConn(conn):
|
||||
stream.close()
|
||||
return
|
||||
yield gen.sleep(2)
|
||||
await gen.sleep(2)
|
||||
if not app.isConnectOk(conn.connIdx):
|
||||
stream.close()
|
||||
return
|
||||
conn.handle_stream()
|
||||
await conn.handle_stream()
|
||||
|
||||
class LocalSocketHandler(tornado.websocket.WebSocketHandler):
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user