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):
my_lock.acquire()
try:
print "[%s]%s" % (time.ctime(), msg)
print "[%s] %s" % (time.ctime(), msg)
finally:
my_lock.release()
@ -69,6 +69,7 @@ class Socks5Server(SocketServer.StreamRequestHandler):
return data.translate(decrypt_table)
def handle_tcp(self, sock, remote):
try:
fdset = [sock, remote]
counter = 0
while True:
@ -77,20 +78,24 @@ class Socks5Server(SocketServer.StreamRequestHandler):
r_data = sock.recv(4096)
if counter == 1:
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:
pass
if counter < 2:
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 sock.send(self.decrypt(remote.recv(4096))) <= 0:
break
finally:
remote.close()
def handle(self):
try:
sock = self.connection
remote = socket.socket()
remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
remote.connect((SERVER, REMOTE_PORT))
self.handle_tcp(sock, remote)
except socket.error:

9
server.py

@ -47,13 +47,18 @@ class ThreadingTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
class Socks5Server(SocketServer.StreamRequestHandler):
def handle_tcp(self, sock, remote):
try:
fdset = [sock, remote]
while True:
r, w, e = select.select(fdset, [], [])
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 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):
return data.translate(encrypt_table)

Loading…
Cancel
Save