|
@ -69,6 +69,11 @@ def send_all(sock, data): |
|
|
class ThreadingTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): |
|
|
class ThreadingTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): |
|
|
allow_reuse_address = True |
|
|
allow_reuse_address = True |
|
|
|
|
|
|
|
|
|
|
|
def get_request(self): |
|
|
|
|
|
connection = self.socket.accept() |
|
|
|
|
|
connection[0].settimeout(config_timeout) |
|
|
|
|
|
return connection |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Socks5Server(SocketServer.StreamRequestHandler): |
|
|
class Socks5Server(SocketServer.StreamRequestHandler): |
|
|
@staticmethod |
|
|
@staticmethod |
|
@ -101,7 +106,10 @@ class Socks5Server(SocketServer.StreamRequestHandler): |
|
|
fdset = [sock, remote] |
|
|
fdset = [sock, remote] |
|
|
while True: |
|
|
while True: |
|
|
should_break = False |
|
|
should_break = False |
|
|
r, w, e = select.select(fdset, [], []) |
|
|
r, w, e = select.select(fdset, [], [], config_timeout) |
|
|
|
|
|
if not r: |
|
|
|
|
|
logging.warn('read time out') |
|
|
|
|
|
break |
|
|
if sock in r: |
|
|
if sock in r: |
|
|
if not connected and config_fast_open: |
|
|
if not connected and config_fast_open: |
|
|
data = sock.recv(4096) |
|
|
data = sock.recv(4096) |
|
@ -243,7 +251,9 @@ class Socks5Server(SocketServer.StreamRequestHandler): |
|
|
addr_to_send, a_server, a_port) |
|
|
addr_to_send, a_server, a_port) |
|
|
else: |
|
|
else: |
|
|
logging.info('connecting %s:%d' % (addr, port[0])) |
|
|
logging.info('connecting %s:%d' % (addr, port[0])) |
|
|
remote = socket.create_connection((a_server, a_port)) |
|
|
remote = socket.create_connection((a_server, a_port), |
|
|
|
|
|
timeout=config_timeout) |
|
|
|
|
|
remote.settimeout(config_timeout) |
|
|
remote.setsockopt(socket.IPPROTO_TCP, |
|
|
remote.setsockopt(socket.IPPROTO_TCP, |
|
|
socket.TCP_NODELAY, 1) |
|
|
socket.TCP_NODELAY, 1) |
|
|
Socks5Server.handle_tcp(sock, remote, encryptor, |
|
|
Socks5Server.handle_tcp(sock, remote, encryptor, |
|
@ -258,7 +268,7 @@ class Socks5Server(SocketServer.StreamRequestHandler): |
|
|
|
|
|
|
|
|
def main(): |
|
|
def main(): |
|
|
global config_server, config_server_port, config_password, config_method,\ |
|
|
global config_server, config_server_port, config_password, config_method,\ |
|
|
config_fast_open |
|
|
config_fast_open, config_timeout |
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG, |
|
|
logging.basicConfig(level=logging.DEBUG, |
|
|
format='%(asctime)s %(levelname)-8s %(message)s', |
|
|
format='%(asctime)s %(levelname)-8s %(message)s', |
|
@ -350,6 +360,7 @@ def main(): |
|
|
config_method, int(config_timeout), True).start() |
|
|
config_method, int(config_timeout), True).start() |
|
|
server = ThreadingTCPServer((config_local_address, config_local_port), |
|
|
server = ThreadingTCPServer((config_local_address, config_local_port), |
|
|
Socks5Server) |
|
|
Socks5Server) |
|
|
|
|
|
server.timeout = int(config_timeout) |
|
|
logging.info("starting local at %s:%d" % |
|
|
logging.info("starting local at %s:%d" % |
|
|
tuple(server.server_address[:2])) |
|
|
tuple(server.server_address[:2])) |
|
|
server.serve_forever() |
|
|
server.serve_forever() |
|
|