Browse Source

close remote fd

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

13
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,6 +69,7 @@ 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):
try:
fdset = [sock, remote] fdset = [sock, remote]
counter = 0 counter = 0
while True: while True:
@ -77,20 +78,24 @@ class Socks5Server(SocketServer.StreamRequestHandler):
r_data = sock.recv(4096) r_data = sock.recv(4096)
if counter == 1: if counter == 1:
try: try:
lock_print("Connecting " + r_data[5:5 + ord(r_data[4])]) lock_print(
"Connecting " + r_data[5:5 + ord(r_data[4])])
except Exception: except Exception:
pass pass
if counter < 2: if counter < 2:
counter += 1 counter += 1
if remote.send(self.encrypt(r_data)) <= 0: break if remote.send(self.encrypt(r_data)) <= 0:
break
if remote in r: if remote in r:
if sock.send(self.decrypt(remote.recv(4096))) <= 0: if sock.send(self.decrypt(remote.recv(4096))) <= 0:
break 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:

9
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):
try:
fdset = [sock, remote] fdset = [sock, remote]
while True: while True:
r, w, e = select.select(fdset, [], []) r, w, e = select.select(fdset, [], [])
if sock in r: if sock in r:
if remote.send(self.decrypt(sock.recv(4096))) <= 0: break if remote.send(self.decrypt(sock.recv(4096))) <= 0:
break
if remote in r: if remote in r:
if sock.send(self.encrypt(remote.recv(4096))) <= 0: break 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