1
This commit is contained in:
parent
1172fc16fd
commit
fe82e43428
@ -4,11 +4,9 @@ import json
|
|||||||
import random
|
import random
|
||||||
import time
|
import time
|
||||||
import threading
|
import threading
|
||||||
import tornado.ioloop
|
from tornado import ioloop
|
||||||
import tornado.web
|
from tornado import gen
|
||||||
import tornado.gen
|
from tornado.websocket import websocket_connect
|
||||||
import tornado.websocket
|
|
||||||
import tornado.tcpserver
|
|
||||||
from tornado.tcpclient import TCPClient
|
from tornado.tcpclient import TCPClient
|
||||||
|
|
||||||
class ClientSide:
|
class ClientSide:
|
||||||
@ -16,11 +14,22 @@ class ClientSide:
|
|||||||
def __init__(self, local_ip, remote_ip):
|
def __init__(self, local_ip, remote_ip):
|
||||||
self._local_ip = local_ip
|
self._local_ip = local_ip
|
||||||
self._remote_ip = remote_ip
|
self._remote_ip = remote_ip
|
||||||
|
self.local_conn = None
|
||||||
|
self.remote_conn = None
|
||||||
|
|
||||||
@gen.coroutine
|
@gen.coroutine
|
||||||
def co_connect():
|
def co_connect(self):
|
||||||
[host, port] = self._local_ip.split(':')
|
while True:
|
||||||
stream = yield TCPClient().connect(host, port)
|
if not self.local_conn:
|
||||||
|
[local_host, local_port] = self._local_ip.split(':')
|
||||||
|
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)
|
||||||
|
|
||||||
|
if self.local_conn:
|
||||||
|
pass
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
ioloop.IOLoop.current().run_sync(self,connec)
|
ioloop.IOLoop.current().run_sync(self.co_connect)
|
||||||
|
@ -14,6 +14,7 @@ app = None
|
|||||||
class ServerSide:
|
class ServerSide:
|
||||||
|
|
||||||
def __init__(self, local_port, remote_port):
|
def __init__(self, local_port, remote_port):
|
||||||
|
global app
|
||||||
self._local_port = int(local_port)
|
self._local_port = int(local_port)
|
||||||
self._remote_port = int(remote_port)
|
self._remote_port = int(remote_port)
|
||||||
app = self
|
app = self
|
||||||
@ -131,6 +132,7 @@ class RemoteServerConnection(object):
|
|||||||
self.localSocket = None
|
self.localSocket = None
|
||||||
|
|
||||||
async def handle_stream(self):
|
async def handle_stream(self):
|
||||||
|
global app
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
data = await self.stream.read_until(b"\n")
|
data = await self.stream.read_until(b"\n")
|
||||||
@ -151,6 +153,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):
|
||||||
|
global app
|
||||||
conn = RemoteServerConnection(steam, address)
|
conn = RemoteServerConnection(steam, address)
|
||||||
if not app.addRemoteConn(conn):
|
if not app.addRemoteConn(conn):
|
||||||
stream.close()
|
stream.close()
|
||||||
@ -164,14 +167,17 @@ class RemoteServer(tornado.tcpserver.TCPServer):
|
|||||||
class LocalSocketHandler(tornado.websocket.WebSocketHandler):
|
class LocalSocketHandler(tornado.websocket.WebSocketHandler):
|
||||||
|
|
||||||
def open(self):
|
def open(self):
|
||||||
|
global app
|
||||||
self._recvBuf = ''
|
self._recvBuf = ''
|
||||||
app.addLocalConn(self)
|
app.addLocalConn(self)
|
||||||
|
|
||||||
def on_message(self, message):
|
def on_message(self, message):
|
||||||
|
global app
|
||||||
self._recvBuf += message
|
self._recvBuf += message
|
||||||
self.parsePacket()
|
self.parsePacket()
|
||||||
|
|
||||||
def on_close(self):
|
def on_close(self):
|
||||||
|
global app
|
||||||
app.removeLocalConn(self)
|
app.removeLocalConn(self)
|
||||||
|
|
||||||
def parsePacket(self):
|
def parsePacket(self):
|
||||||
@ -186,6 +192,7 @@ class LocalSocketHandler(tornado.websocket.WebSocketHandler):
|
|||||||
self.dispatchMsg(msg)
|
self.dispatchMsg(msg)
|
||||||
|
|
||||||
def dispatchMsg(self, msg):
|
def dispatchMsg(self, msg):
|
||||||
|
global app
|
||||||
if msg['cmd'] == 'connectOk':
|
if msg['cmd'] == 'connectOk':
|
||||||
app.onLocalConnectOk(msg)
|
app.onLocalConnectOk(msg)
|
||||||
elif msg['cmd'] == 'forwardData':
|
elif msg['cmd'] == 'forwardData':
|
||||||
@ -196,4 +203,3 @@ class LocalSocketHandler(tornado.websocket.WebSocketHandler):
|
|||||||
def sendMsg(self, msg):
|
def sendMsg(self, msg):
|
||||||
data = json.dumps(msg) + '\n'
|
data = json.dumps(msg) + '\n'
|
||||||
self.write_message(data)
|
self.write_message(data)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user