<s> import threading <EOL> import socket <EOL> import sys , time <EOL> import SocketServer , struct , select <EOL> global bufLen <EOL> global endflag <EOL> global socksPort <EOL> socksPort = <NUM_LIT> <EOL> endflag = [ ] <EOL> bufLen = <NUM_LIT:4> * <NUM_LIT> <EOL> class startThreadSoket ( threading . Thread ) : <EOL> def __init__ ( self , socksPort ) : <EOL> threading . Thread . __init__ ( self ) <EOL> self . socksPort = socksPort <EOL> def run ( self ) : <EOL> socket_bind ( self . socksPort ) <EOL> class control ( threading . Thread ) : <EOL> def __init__ ( self , server_Conn , client_Conn , serverAddr , clientAddr , clientNum ) : <EOL> threading . Thread . __init__ ( self ) <EOL> self . server_Conn = server_Conn <EOL> self . client_Conn = client_Conn <EOL> self . server_Addr = serverAddr <EOL> self . client_Addr = clientAddr <EOL> self . clientNum = clientNum <EOL> def run ( self ) : <EOL> global endflag <EOL> transferDataThreads = [ ] <EOL> thread = <NUM_LIT:2> <EOL> flag = self . clientNum <EOL> endflag . append ( False ) <EOL> y = transfer2Server ( self . server_Conn , self . client_Conn , self . server_Addr , self . client_Addr , flag ) <EOL> y . setDaemon ( True ) <EOL> z = transfer2Client ( self . client_Conn , self . server_Conn , self . client_Addr , self . server_Addr , flag ) <EOL> z . setDaemon ( True ) <EOL> transferDataThreads . append ( y ) <EOL> transferDataThreads . append ( z ) <EOL> for t in transferDataThreads : <EOL> t . start ( ) <EOL> while True : <EOL> alive = True <EOL> for i in range ( int ( thread ) ) : <EOL> alive = alive and transferDataThreads [ i ] . isAlive ( ) <EOL> if not alive : <EOL> time . sleep ( <NUM_LIT:3> ) <EOL> print "<STR_LIT>" % self . clientNum <EOL> break <EOL> break <EOL> class transfer2Server ( threading . Thread ) : <EOL> def __init__ ( self , server_Conn , client_Conn , server_Addr , client_Addr , flag ) : <EOL> threading . Thread . __init__ ( self ) <EOL> self . server_Conn = server_Conn <EOL> self . client_Conn = client_Conn <EOL> self . server_Addr = server_Addr <EOL> self . client_Conn = client_Conn <EOL> self . flag = flag <EOL> self . currentNum = self . flag + <NUM_LIT:1> <EOL> def run ( self ) : <EOL> global bufLen <EOL> global endflag <EOL> servPeerName = self . server_Conn . getpeername ( ) <EOL> clientPeerName = self . client_Conn . getpeername ( ) <EOL> while True and not endflag [ self . flag ] : <EOL> try : <EOL> buf = self . client_Conn . recv ( bufLen ) <EOL> except : <EOL> print "<STR_LIT>" <EOL> for m in endflag : <EOL> m = True <EOL> sys . exit ( ) <EOL> if buf == '<STR_LIT>' or buf == '<STR_LIT>' : <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> self . client_Conn . close ( ) <EOL> endflag [ self . flag ] = True <EOL> break <EOL> try : <EOL> self . server_Conn . send ( buf ) <EOL> print "<STR_LIT>" % ( self . currentNum , clientPeerName , servPeerName , len ( buf ) ) <EOL> except : <EOL> endflag [ self . flag ] = True <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> self . client_Conn . send ( '<STR_LIT>' ) <EOL> self . client_Conn . close ( ) <EOL> break <EOL> class transfer2Client ( threading . Thread ) : <EOL> def __init__ ( self , client_Conn , server_Conn , client_Addr , server_Addr , flag ) : <EOL> threading . Thread . __init__ ( self ) <EOL> self . client_Conn = client_Conn <EOL> self . server_Conn = server_Conn <EOL> self . client_Addr = client_Addr <EOL> self . server_Addr = server_Addr <EOL> self . flag = flag <EOL> self . currentNum = flag + <NUM_LIT:1> <EOL> def run ( self ) : <EOL> global bufLen <EOL> global endflag <EOL> servPeerName = self . server_Conn . getpeername ( ) <EOL> clientPeerName = self . client_Conn . getpeername ( ) <EOL> while True and not endflag [ self . flag ] : <EOL> buf = self . server_Conn . recv ( bufLen ) <EOL> if buf == '<STR_LIT>' : <EOL> print "<STR_LIT>" % ( self . currentNum , clientPeerName ) <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> self . server_Conn . close ( ) <EOL> endflag [ self . flag ] = True <EOL> break <EOL> try : <EOL> self . client_Conn . send ( buf ) <EOL> print "<STR_LIT>" % ( self . currentNum , servPeerName , clientPeerName , len ( buf ) ) <EOL> except : <EOL> endflag [ self . flag ] = True <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> self . server_Conn . close ( ) <EOL> break <EOL> class ThreadingTCPServer ( SocketServer . ThreadingMixIn , SocketServer . TCPServer ) : pass <EOL> class Socks5Server ( SocketServer . StreamRequestHandler ) : <EOL> def handle_tcp ( self , sock , remote ) : <EOL> fdset = [ sock , remote ] <EOL> while True : <EOL> r , w , e = select . select ( fdset , [ ] , [ ] ) <EOL> if sock in r : <EOL> if remote . send ( sock . recv ( <NUM_LIT> ) ) <= <NUM_LIT:0> : break <EOL> if remote in r : <EOL> if sock . send ( remote . recv ( <NUM_LIT> ) ) <= <NUM_LIT:0> : break <EOL> def handle ( self ) : <EOL> try : <EOL> pass <EOL> sock = self . connection <EOL> sock . recv ( <NUM_LIT> ) <EOL> sock . send ( "<STR_LIT>" ) ; <EOL> data = self . rfile . read ( <NUM_LIT:4> ) <EOL> mode = ord ( data [ <NUM_LIT:1> ] ) <EOL> addrtype = ord ( data [ <NUM_LIT:3> ] ) <EOL> if addrtype == <NUM_LIT:1> : <EOL> addr = socket . inet_ntoa ( self . rfile . read ( <NUM_LIT:4> ) ) <EOL> elif addrtype == <NUM_LIT:3> : <EOL> addr = self . rfile . read ( ord ( sock . recv ( <NUM_LIT:1> ) [ <NUM_LIT:0> ] ) ) <EOL> port = struct . unpack ( '<STR_LIT>' , self . rfile . read ( <NUM_LIT:2> ) ) <EOL> reply = "<STR_LIT>" <EOL> try : <EOL> if mode == <NUM_LIT:1> : <EOL> remote = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> remote . connect ( ( addr , port [ <NUM_LIT:0> ] ) ) <EOL> pass <EOL> else : <EOL> reply = "<STR_LIT>" <EOL> local = remote . getsockname ( ) <EOL> reply += socket . inet_aton ( local [ <NUM_LIT:0> ] ) + struct . pack ( "<STR_LIT>" , local [ <NUM_LIT:1> ] ) <EOL> except socket . error : <EOL> reply = '<STR_LIT>' <EOL> sock . send ( reply ) <EOL> if reply [ <NUM_LIT:1> ] == '<STR_LIT:\x00>' : <EOL> if mode == <NUM_LIT:1> : <EOL> self . handle_tcp ( sock , remote ) <EOL> except socket . error : <EOL> pass <EOL> except IndexError : <EOL> pass <EOL> def socket_bind ( socketPort ) : <EOL> socks_port = int ( socketPort ) <EOL> server = ThreadingTCPServer ( ( '<STR_LIT>' , socks_port ) , Socks5Server ) <EOL> print '<STR_LIT>' % socks_port + '<STR_LIT>' <EOL> server . serve_forever ( ) <EOL> def usage ( ) : <EOL> print """<STR_LIT>""" % ( sys . argv [ <NUM_LIT:0> ] , sys . argv [ <NUM_LIT:0> ] , sys . argv [ <NUM_LIT:0> ] , sys . argv [ <NUM_LIT:0> ] , sys . argv [ <NUM_LIT:0> ] , sys . argv [ <NUM_LIT:0> ] ) <EOL> def main ( ) : <EOL> global socksPort <EOL> global endflag <EOL> try : <EOL> if len ( sys . argv ) >= <NUM_LIT:3> : <EOL> if sys . argv [ <NUM_LIT:2> ] == '<STR_LIT:3>' : <EOL> if len ( sys . argv ) == <NUM_LIT:4> : <EOL> socksPort = int ( sys . argv [ <NUM_LIT:3> ] ) <EOL> socket_bind ( socksPort ) <EOL> elif sys . argv [ <NUM_LIT:2> ] == '<STR_LIT:1>' and len ( sys . argv ) == <NUM_LIT:5> : <EOL> socksProxy = startThreadSoket ( socksPort ) <EOL> socksProxy . setDaemon ( True ) <EOL> socksProxy . start ( ) <EOL> reproket ( '<STR_LIT:localhost>' , socksPort , sys . argv [ <NUM_LIT:3> ] , sys . argv [ <NUM_LIT:4> ] ) <EOL> elif sys . argv [ <NUM_LIT:2> ] == '<STR_LIT:2>' : <EOL> if len ( sys . argv ) == <NUM_LIT:7> : <EOL> reproket ( sys . argv [ <NUM_LIT:3> ] , sys . argv [ <NUM_LIT:4> ] , sys . argv [ <NUM_LIT:5> ] , sys . argv [ <NUM_LIT:6> ] ) <EOL> else : <EOL> usage ( ) <EOL> else : <EOL> usage ( ) <EOL> except KeyboardInterrupt : <EOL> print "<STR_LIT>" <EOL> for m in endflag : <EOL> m = True <EOL> def reproket ( transmitIP , transmitPort , clientIP , clientPort ) : <EOL> serverAddr = ( transmitIP , int ( transmitPort ) ) <EOL> clientAddr = ( clientIP , int ( clientPort ) ) <EOL> serverLink = [ ] <EOL> clientLink = [ ] <EOL> socketServer = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> socketServer . connect ( serverAddr ) <EOL> socketClient = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> try : <EOL> socketClient . connect ( clientAddr ) <EOL> except : <EOL> print "<STR_LIT>" <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> sys . exit ( ) <EOL> print "<STR_LIT>" <EOL> serverLink . append ( socketServer ) <EOL> clientLink . append ( socketClient ) <EOL> controlThreads = [ ] <EOL> clientNum = <NUM_LIT:0> <EOL> while True : <EOL> try : <EOL> newLinkFlag = clientLink [ clientNum ] . recv ( bufLen ) <EOL> except : <EOL> print "<STR_LIT>" % ( clientNum + <NUM_LIT:1> ) <EOL> break <EOL> if newLinkFlag == '<STR_LIT>' : <EOL> nextClientLink = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> nextClientLink . connect ( clientAddr ) <EOL> print "<STR_LIT>" % ( clientNum + <NUM_LIT:1> ) <EOL> nextServerLink = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> nextServerLink . connect ( serverAddr ) <EOL> print "<STR_LIT>" % ( clientNum + <NUM_LIT:1> ) <EOL> temp = control ( serverLink [ clientNum ] , clientLink [ clientNum ] , serverAddr , clientAddr , clientNum ) <EOL> temp . setDaemon ( True ) <EOL> controlThreads . append ( temp ) <EOL> controlThreads [ clientNum ] . start ( ) <EOL> clientLink . append ( nextClientLink ) <EOL> serverLink . append ( nextServerLink ) <EOL> clientNum += <NUM_LIT:1> <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
