2020-12-22 19:32:02 +00:00
|
|
|
import threading
|
|
|
|
import socket
|
|
|
|
import time
|
|
|
|
import select
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
class SingleThreadProxy(object):
|
|
|
|
max_timeout_for_remote_connection = 60
|
|
|
|
|
|
|
|
class Forward(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.forward = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
|
|
|
|
def start(self, host, port):
|
2020-12-31 20:19:32 +00:00
|
|
|
# noinspection PyBroadException
|
2020-12-22 19:32:02 +00:00
|
|
|
try:
|
|
|
|
self.forward.connect((host, port))
|
|
|
|
return self.forward
|
2020-12-31 20:19:32 +00:00
|
|
|
except Exception:
|
2020-12-22 19:32:02 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
def __init__(self, port, tgtport, host="127.0.0.1", tgthost="127.0.0.1",
|
|
|
|
buffer_size=4096, delay=0.0001, state=None):
|
|
|
|
self.input_list = []
|
|
|
|
self.channel = {}
|
|
|
|
self.sidmap = {}
|
|
|
|
self.state = state or {}
|
|
|
|
|
|
|
|
# set max number of open files
|
|
|
|
# noinspection PyBroadException
|
|
|
|
try:
|
|
|
|
if sys.platform == 'win32':
|
|
|
|
import ctypes
|
|
|
|
ctypes.windll.msvcrt._setmaxstdio(max(2048, ctypes.windll.msvcrt._getmaxstdio())) # noqa
|
|
|
|
else:
|
|
|
|
import resource
|
|
|
|
soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
|
|
|
|
resource.setrlimit(resource.RLIMIT_NOFILE, (max(4096, soft), hard))
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
|
|
self.server.bind((host, port))
|
|
|
|
self.server.listen(100)
|
|
|
|
self.tgthost, self.tgtport = tgthost, tgtport
|
|
|
|
self.buffer_size, self.delay = buffer_size, delay
|
|
|
|
self._proxy_daemon_thread = threading.Thread(target=self.main_loop)
|
|
|
|
self._proxy_daemon_thread.setDaemon(True)
|
|
|
|
self._proxy_daemon_thread.start()
|
|
|
|
|
|
|
|
def main_loop(self):
|
|
|
|
self.input_list.append(self.server)
|
|
|
|
while 1:
|
|
|
|
time.sleep(self.delay)
|
2020-12-31 20:19:32 +00:00
|
|
|
# noinspection PyBroadException
|
2020-12-22 19:32:02 +00:00
|
|
|
try:
|
2020-12-31 20:19:32 +00:00
|
|
|
inputready, outputready, exceptready = select.select(self.input_list, [], [])
|
|
|
|
except Exception:
|
2020-12-22 19:32:02 +00:00
|
|
|
continue
|
2020-12-31 20:19:32 +00:00
|
|
|
for s in inputready:
|
|
|
|
if s == self.server:
|
|
|
|
# noinspection PyBroadException
|
2020-12-22 19:32:02 +00:00
|
|
|
try:
|
|
|
|
self.on_accept()
|
2020-12-31 20:19:32 +00:00
|
|
|
except Exception:
|
2020-12-22 19:32:02 +00:00
|
|
|
pass
|
|
|
|
break
|
|
|
|
|
2020-12-31 20:19:32 +00:00
|
|
|
# noinspection PyBroadException
|
2020-12-22 19:32:02 +00:00
|
|
|
try:
|
2020-12-31 20:19:32 +00:00
|
|
|
data = s.recv(self.buffer_size)
|
|
|
|
except ConnectionResetError:
|
|
|
|
# this will trigger on_close
|
|
|
|
data = []
|
|
|
|
except Exception:
|
2020-12-22 19:32:02 +00:00
|
|
|
continue
|
2020-12-31 20:19:32 +00:00
|
|
|
|
|
|
|
if len(data) == 0:
|
|
|
|
# noinspection PyBroadException
|
2020-12-22 19:32:02 +00:00
|
|
|
try:
|
2020-12-31 20:19:32 +00:00
|
|
|
self.on_close(s)
|
|
|
|
except Exception:
|
2020-12-22 19:32:02 +00:00
|
|
|
pass
|
|
|
|
break
|
|
|
|
else:
|
2020-12-31 20:19:32 +00:00
|
|
|
# noinspection PyBroadException
|
2020-12-22 19:32:02 +00:00
|
|
|
try:
|
2020-12-31 20:19:32 +00:00
|
|
|
self.on_recv(s, data)
|
|
|
|
except Exception:
|
2020-12-22 19:32:02 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def on_accept(self):
|
|
|
|
clientsock, clientaddr = self.server.accept()
|
2020-12-31 20:19:32 +00:00
|
|
|
forward = None
|
2020-12-22 19:32:02 +00:00
|
|
|
for i in range(self.max_timeout_for_remote_connection):
|
|
|
|
forward = self.Forward().start(self.tgthost, self.tgtport)
|
|
|
|
if forward:
|
|
|
|
break
|
|
|
|
# print('waiting for remote...')
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
if forward:
|
2020-12-31 20:19:32 +00:00
|
|
|
# print("{0} has connected".format(clientaddr))
|
2020-12-22 19:32:02 +00:00
|
|
|
self.input_list.append(clientsock)
|
|
|
|
self.input_list.append(forward)
|
|
|
|
self.channel[clientsock] = forward
|
|
|
|
self.channel[forward] = clientsock
|
2020-12-31 20:19:32 +00:00
|
|
|
sidbase = "{0}_{1}_{2}_{3}".format(self.tgthost, self.tgtport, clientaddr[0], clientaddr[1])
|
|
|
|
self.sidmap[clientsock] = (sidbase, 1)
|
|
|
|
self.sidmap[forward] = (sidbase, -1)
|
2020-12-22 19:32:02 +00:00
|
|
|
else:
|
2020-12-31 20:19:32 +00:00
|
|
|
# print("Can't establish connection with remote server.\n"
|
|
|
|
# "Closing connection with client side{0}".format(clientaddr))
|
2020-12-22 19:32:02 +00:00
|
|
|
clientsock.close()
|
|
|
|
|
2020-12-31 20:19:32 +00:00
|
|
|
def on_close(self, s):
|
2020-12-22 19:32:02 +00:00
|
|
|
# logger.info("{0} has disconnected".format(self.s.getpeername()))
|
2020-12-31 20:19:32 +00:00
|
|
|
# print("has disconnected")
|
2020-12-22 19:32:02 +00:00
|
|
|
|
2020-12-31 20:19:32 +00:00
|
|
|
self.input_list.remove(s)
|
|
|
|
self.input_list.remove(self.channel[s])
|
|
|
|
out = self.channel[s]
|
2020-12-22 19:32:02 +00:00
|
|
|
self.channel[out].close()
|
2020-12-31 20:19:32 +00:00
|
|
|
self.channel[s].close()
|
2020-12-22 19:32:02 +00:00
|
|
|
del self.channel[out]
|
2020-12-31 20:19:32 +00:00
|
|
|
del self.channel[s]
|
2020-12-22 19:32:02 +00:00
|
|
|
del self.sidmap[out]
|
2020-12-31 20:19:32 +00:00
|
|
|
del self.sidmap[s]
|
2020-12-22 19:32:02 +00:00
|
|
|
|
2020-12-31 20:19:32 +00:00
|
|
|
def on_recv(self, s, data):
|
|
|
|
_sidbase = self.sidmap[s][0]
|
|
|
|
_c_or_s = self.sidmap[s][1]
|
2020-12-22 19:32:02 +00:00
|
|
|
# logger.debug(ctrl_less(data.strip()))
|
2020-12-31 20:19:32 +00:00
|
|
|
self.channel[s].send(data)
|