1
This commit is contained in:
parent
0368ce1295
commit
796a91ec3e
@ -3,6 +3,7 @@ import os
|
|||||||
import json
|
import json
|
||||||
import random
|
import random
|
||||||
import time
|
import time
|
||||||
|
import base64
|
||||||
import threading
|
import threading
|
||||||
from tornado import ioloop
|
from tornado import ioloop
|
||||||
from tornado import gen
|
from tornado import gen
|
||||||
@ -17,30 +18,30 @@ class ClientSide:
|
|||||||
self.local_conn = None
|
self.local_conn = None
|
||||||
self.local_read_future = None
|
self.local_read_future = None
|
||||||
self.remote_conn = None
|
self.remote_conn = None
|
||||||
|
self.conn_hash = {}
|
||||||
|
|
||||||
@gen.coroutine
|
@gen.coroutine
|
||||||
def co_connect2(self):
|
def co_localConnect(self, idx):
|
||||||
while True:
|
print(idx, flush=True)
|
||||||
if not self.local_conn:
|
|
||||||
[local_host, local_port] = self._local_ip.split(':')
|
|
||||||
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)
|
|
||||||
self.remote_conn = yield websocket_connect(url)
|
|
||||||
|
|
||||||
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(':')
|
[local_host, local_port] = self._local_ip.split(':')
|
||||||
self.local_conn = yield TCPClient().connect(local_host, local_port)
|
local_conn = yield TCPClient().connect(local_host, local_port)
|
||||||
|
self.conn_hash[idx] = local_conn
|
||||||
|
self.sendRemoteMsg({
|
||||||
|
'cmd' : 'connectOk',
|
||||||
|
'remoteConnIdx' : idx,
|
||||||
|
})
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
data = yield local_conn.read_until_close()
|
||||||
|
if data:
|
||||||
|
self.sendRemoteMsg({
|
||||||
|
'cmd' : 'forwardData',
|
||||||
|
'remoteConnIdx' : idx,
|
||||||
|
'data' : str(base64.b64encode(data))
|
||||||
|
})
|
||||||
|
except:
|
||||||
|
print('qqqqq', flush=True)
|
||||||
|
break
|
||||||
|
|
||||||
@gen.coroutine
|
@gen.coroutine
|
||||||
def co_remoteConnect(self):
|
def co_remoteConnect(self):
|
||||||
@ -48,26 +49,39 @@ class ClientSide:
|
|||||||
url = 'ws://%s:%s/websocket' % (remote_host, remote_port)
|
url = 'ws://%s:%s/websocket' % (remote_host, remote_port)
|
||||||
self.remote_conn = yield websocket_connect(url)
|
self.remote_conn = yield websocket_connect(url)
|
||||||
data = ''
|
data = ''
|
||||||
while True:
|
try:
|
||||||
data += yield self.remote_conn.read_message()
|
while True:
|
||||||
if not data:
|
data += yield self.remote_conn.read_message()
|
||||||
continue
|
print(data, flush=True)
|
||||||
lines = data.split('\n')
|
if not data:
|
||||||
if data[-1] == '\n':
|
continue
|
||||||
data = lines[-1]
|
print(data, flush=True)
|
||||||
lines = lines[:-1]
|
lines = data.split('\n')
|
||||||
for line in lines:
|
if data[-1] == '\n':
|
||||||
print(line)
|
data = lines[-1]
|
||||||
msg = json.loads(line)
|
lines = lines[:-1]
|
||||||
self.dispatchRemoteMsg(msg)
|
for line in lines:
|
||||||
|
msg = json.loads(line)
|
||||||
|
self.dispatchRemoteMsg(msg)
|
||||||
|
except:
|
||||||
|
print('wwwwwwwwww', flush=True)
|
||||||
|
|
||||||
|
|
||||||
def dispatchRemoteMsg(self, msg):
|
def dispatchRemoteMsg(self, msg):
|
||||||
if msg['cmd'] == 'connect':
|
if msg['cmd'] == 'connect':
|
||||||
ioloop.IOLoop.current().spawn_callback(self.co_localConnect)
|
ioloop.IOLoop.current().spawn_callback(self.co_localConnect, msg['remoteConnIdx'])
|
||||||
elif msg['cmd'] == 'socketClose':
|
elif msg['cmd'] == 'socketClose':
|
||||||
pass
|
pass
|
||||||
elif msg['cmd'] == 'forwardData':
|
elif msg['cmd'] == 'forwardData':
|
||||||
pass
|
if msg['remoteConnIdx'] in self.conn_hash:
|
||||||
|
conn = self.conn_hash[msg['remoteConnIdx']]
|
||||||
|
data = base64.b64decode(msg['data'][2:-1])
|
||||||
|
print(data, flush=True)
|
||||||
|
conn.write(data)
|
||||||
|
|
||||||
|
def sendRemoteMsg(self, msg):
|
||||||
|
data = json.dumps(msg) + '\n'
|
||||||
|
self.remote_conn.write_message(data)
|
||||||
|
|
||||||
@gen.coroutine
|
@gen.coroutine
|
||||||
def co_connect(self):
|
def co_connect(self):
|
||||||
|
@ -3,6 +3,7 @@ import os
|
|||||||
import json
|
import json
|
||||||
import random
|
import random
|
||||||
import time
|
import time
|
||||||
|
import base64
|
||||||
import threading
|
import threading
|
||||||
import tornado.ioloop
|
import tornado.ioloop
|
||||||
import tornado.web
|
import tornado.web
|
||||||
@ -55,6 +56,7 @@ class ServerSide:
|
|||||||
'remoteConnIdx' : conn.idx,
|
'remoteConnIdx' : conn.idx,
|
||||||
'cmd' : 'connect'
|
'cmd' : 'connect'
|
||||||
})
|
})
|
||||||
|
print('addRemoteConn')
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def addLocalConn(self, conn):
|
def addLocalConn(self, conn):
|
||||||
@ -83,7 +85,7 @@ class ServerSide:
|
|||||||
def removeRemoteConn(self, conn):
|
def removeRemoteConn(self, conn):
|
||||||
self.remoteConnHashMutex.acquire()
|
self.remoteConnHashMutex.acquire()
|
||||||
if conn.idx in self.remoteConnHash:
|
if conn.idx in self.remoteConnHash:
|
||||||
del self.remoteConnHash[conn]
|
del self.remoteConnHash[conn.idx]
|
||||||
self.remoteConnHashMutex.release()
|
self.remoteConnHashMutex.release()
|
||||||
|
|
||||||
def randLocalConn(self):
|
def randLocalConn(self):
|
||||||
@ -110,11 +112,14 @@ class ServerSide:
|
|||||||
self.remoteConnHashMutex.release()
|
self.remoteConnHashMutex.release()
|
||||||
|
|
||||||
def onLocalForwardData(self, msg):
|
def onLocalForwardData(self, msg):
|
||||||
self.remotePendingMutex.acquire()
|
# print(msg)
|
||||||
if msg['remoteConnIdx'] in self.remotePending:
|
self.remoteConnHashMutex.acquire()
|
||||||
local_conn = self.remotePending[msg['remoteConnIdx']]
|
if msg['remoteConnIdx'] in self.remoteConnHash:
|
||||||
local_conn.stream.write(base64.b64decode(msg['data']))
|
local_conn = self.remoteConnHash[msg['remoteConnIdx']]
|
||||||
self.remotePendingMutex.release()
|
data = base64.b64decode(msg['data'][2:-1])
|
||||||
|
print(data)
|
||||||
|
local_conn.stream.write(data)
|
||||||
|
self.remoteConnHashMutex.release()
|
||||||
|
|
||||||
def isConnectOk(self, connIdx):
|
def isConnectOk(self, connIdx):
|
||||||
isOk = False
|
isOk = False
|
||||||
@ -138,7 +143,8 @@ class RemoteServerConnection(object):
|
|||||||
data = await self.stream.read_until(b"\n")
|
data = await self.stream.read_until(b"\n")
|
||||||
self.localSocket.sendMsg({
|
self.localSocket.sendMsg({
|
||||||
'cmd' : 'forwardData',
|
'cmd' : 'forwardData',
|
||||||
'data' : base64.b64encode(data)
|
'remoteConnIdx' : self.idx,
|
||||||
|
'data' : str(base64.b64encode(data))
|
||||||
})
|
})
|
||||||
except tornado.iostream.StreamClosedError:
|
except tornado.iostream.StreamClosedError:
|
||||||
break
|
break
|
||||||
@ -153,6 +159,7 @@ class RemoteServerConnection(object):
|
|||||||
class RemoteServer(tornado.tcpserver.TCPServer):
|
class RemoteServer(tornado.tcpserver.TCPServer):
|
||||||
|
|
||||||
async def handle_stream(self, stream, address):
|
async def handle_stream(self, stream, address):
|
||||||
|
print('zzzz')
|
||||||
global app
|
global app
|
||||||
conn = RemoteServerConnection(stream, address)
|
conn = RemoteServerConnection(stream, address)
|
||||||
if not app.addRemoteConn(conn):
|
if not app.addRemoteConn(conn):
|
||||||
@ -179,6 +186,7 @@ class LocalSocketHandler(tornado.websocket.WebSocketHandler):
|
|||||||
def on_close(self):
|
def on_close(self):
|
||||||
global app
|
global app
|
||||||
app.removeLocalConn(self)
|
app.removeLocalConn(self)
|
||||||
|
print('on_close')
|
||||||
|
|
||||||
def parsePacket(self):
|
def parsePacket(self):
|
||||||
if len(self._recvBuf) <= 0:
|
if len(self._recvBuf) <= 0:
|
||||||
@ -196,10 +204,12 @@ class LocalSocketHandler(tornado.websocket.WebSocketHandler):
|
|||||||
if msg['cmd'] == 'connectOk':
|
if msg['cmd'] == 'connectOk':
|
||||||
app.onLocalConnectOk(msg)
|
app.onLocalConnectOk(msg)
|
||||||
elif msg['cmd'] == 'forwardData':
|
elif msg['cmd'] == 'forwardData':
|
||||||
app.onLocalForward(msg)
|
app.onLocalForwardData(msg)
|
||||||
elif msg['cmd'] == 'socketClose':
|
elif msg['cmd'] == 'socketClose':
|
||||||
app.removeRemoteConn(msg['remoteConnIdx'])
|
pass
|
||||||
|
# app.removeRemoteConn(msg['remoteConnIdx'])
|
||||||
|
|
||||||
def sendMsg(self, msg):
|
def sendMsg(self, msg):
|
||||||
data = json.dumps(msg) + '\n'
|
data = json.dumps(msg) + '\n'
|
||||||
|
print(data)
|
||||||
self.write_message(data)
|
self.write_message(data)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user