Browse Source

close remote fd

1.4
clowwindy 13 years ago
parent
commit
93890e9ccc
  1. 43
      local.py
  2. 19
      server.py

43
local.py

@ -52,7 +52,7 @@ my_lock = threading.Lock()
def lock_print(msg): def lock_print(msg):
my_lock.acquire() my_lock.acquire()
try: try:
print "[%s]%s" % (time.ctime(), msg) print "[%s] %s" % (time.ctime(), msg)
finally: finally:
my_lock.release() my_lock.release()
@ -69,28 +69,33 @@ class Socks5Server(SocketServer.StreamRequestHandler):
return data.translate(decrypt_table) return data.translate(decrypt_table)
def handle_tcp(self, sock, remote): def handle_tcp(self, sock, remote):
fdset = [sock, remote] try:
counter = 0 fdset = [sock, remote]
while True: counter = 0
r, w, e = select.select(fdset, [], []) while True:
if sock in r: r, w, e = select.select(fdset, [], [])
r_data = sock.recv(4096) if sock in r:
if counter == 1: r_data = sock.recv(4096)
try: if counter == 1:
lock_print("Connecting " + r_data[5:5 + ord(r_data[4])]) try:
except Exception: lock_print(
pass "Connecting " + r_data[5:5 + ord(r_data[4])])
if counter < 2: except Exception:
counter += 1 pass
if remote.send(self.encrypt(r_data)) <= 0: break if counter < 2:
if remote in r: counter += 1
if sock.send(self.decrypt(remote.recv(4096))) <= 0: if remote.send(self.encrypt(r_data)) <= 0:
break break
if remote in r:
if sock.send(self.decrypt(remote.recv(4096))) <= 0:
break
finally:
remote.close()
def handle(self): def handle(self):
try: try:
sock = self.connection sock = self.connection
remote = socket.socket() remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
remote.connect((SERVER, REMOTE_PORT)) remote.connect((SERVER, REMOTE_PORT))
self.handle_tcp(sock, remote) self.handle_tcp(sock, remote)
except socket.error: except socket.error:

19
server.py

@ -47,13 +47,18 @@ class ThreadingTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
class Socks5Server(SocketServer.StreamRequestHandler): class Socks5Server(SocketServer.StreamRequestHandler):
def handle_tcp(self, sock, remote): def handle_tcp(self, sock, remote):
fdset = [sock, remote] try:
while True: fdset = [sock, remote]
r, w, e = select.select(fdset, [], []) while True:
if sock in r: r, w, e = select.select(fdset, [], [])
if remote.send(self.decrypt(sock.recv(4096))) <= 0: break if sock in r:
if remote in r: if remote.send(self.decrypt(sock.recv(4096))) <= 0:
if sock.send(self.encrypt(remote.recv(4096))) <= 0: break break
if remote in r:
if sock.send(self.encrypt(remote.recv(4096))) <= 0:
break
finally:
remote.close()
def encrypt(self, data): def encrypt(self, data):
return data.translate(encrypt_table) return data.translate(encrypt_table)

Loading…
Cancel
Save