merged from plus/shadowsocks
This commit is contained in:
parent
7d8005891a
commit
31fd15702d
2 changed files with 67 additions and 8 deletions
33
local.py
33
local.py
|
@ -34,6 +34,32 @@ import threading
|
|||
import time
|
||||
import SocketServer
|
||||
|
||||
def socket_create_connection(address, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
|
||||
source_address=None):
|
||||
"""python 2.7 socket.create_connection"""
|
||||
host, port = address
|
||||
err = None
|
||||
for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
|
||||
af, socktype, proto, canonname, sa = res
|
||||
sock = None
|
||||
try:
|
||||
sock = socket.socket(af, socktype, proto)
|
||||
if timeout is not socket._GLOBAL_DEFAULT_TIMEOUT:
|
||||
sock.settimeout(timeout)
|
||||
if source_address:
|
||||
sock.bind(source_address)
|
||||
sock.connect(sa)
|
||||
return sock
|
||||
|
||||
except socket.error as _:
|
||||
err = _
|
||||
if sock is not None:
|
||||
sock.close()
|
||||
if err is not None:
|
||||
raise err
|
||||
else:
|
||||
raise error("getaddrinfo returns an empty list")
|
||||
|
||||
def get_table(key):
|
||||
m = hashlib.md5()
|
||||
m.update(key)
|
||||
|
@ -95,11 +121,10 @@ class Socks5Server(SocketServer.StreamRequestHandler):
|
|||
def handle(self):
|
||||
try:
|
||||
sock = self.connection
|
||||
remote = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
|
||||
remote.connect((SERVER, REMOTE_PORT))
|
||||
remote = socket_create_connection((SERVER, REMOTE_PORT))
|
||||
self.handle_tcp(sock, remote)
|
||||
except socket.error as e:
|
||||
lock_print('socket error: ' + str(e))
|
||||
except socket.error:
|
||||
lock_print('socket error')
|
||||
|
||||
|
||||
def main():
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue