ϪfdZddlZddlZddlZddlZddlmZmZmZddl m Z GddejZ GddejZ Gd d ejZGd d ejZGd dejZy)z) Implementation of the SOCKSv4 protocol. N)deferprotocolreactor)logc*eZdZdZdZdZdZdZy)SOCKSv4Outgoingc||_yN)socksselfr s 9/usr/lib/python3/dist-packages/twisted/protocols/socks.py__init__zSOCKSv4Outgoing.__init__s  c|jj}|jjdd|j|j ||j_y)NZr)portip) transportgetPeerr makeReplyrhost otherConn)r peers rconnectionMadezSOCKSv4Outgoing.connectionMadesC~~%%' RtyyA# rcL|jjjyr r rloseConnectionr reasons rconnectionLostzSOCKSv4Outgoing.connectionLost ++-rc:|jj|yr r writer datas r dataReceivedzSOCKSv4Outgoing.dataReceived! rcr|jj|||jj|yr r rrr%r&s rr%zSOCKSv4Outgoing.write$& tT" T"rN)__name__ __module__ __qualname__rrr!r(r%rrrrs$ .#rrc$eZdZdZdZdZdZy)SOCKSv4Incomingc4||_||j_yr )r rr s rrzSOCKSv4Incoming.__init__*s # rcL|jjjyr rrs rr!zSOCKSv4Incoming.connectionLost.r"rc:|jj|yr r$r&s rr(zSOCKSv4Incoming.dataReceived1r)rcr|jj|||jj|yr r+r&s rr%zSOCKSv4Incoming.write4r,rN)r-r.r/rr!r(r%r0rrr2r2)s$.#rr2cZeZdZdZdefdZdZdZdZdZ dZ d Z d Z dd Z d Zd Zy)SOCKSv4a An implementation of the SOCKSv4 protocol. @type logging: L{str} or L{None} @ivar logging: If not L{None}, the name of the logfile to which connection information will be written. @type reactor: object providing L{twisted.internet.interfaces.IReactorTCP} @ivar reactor: The reactor used to create connections. @type buf: L{str} @ivar buf: Part of a SOCKSv4 connection request. @type otherConn: C{SOCKSv4Incoming}, C{SOCKSv4Outgoing} or L{None} @ivar otherConn: Until the connection has been established, C{otherConn} is L{None}. After that, it is the proxy-to-destination protocol instance along which the client's connection is being forwarded. Nc ||_||_yr )loggingr)r r:rs rrzSOCKSv4.__init__Ms  rc d|_d|_y)Nr)bufr)r s rrzSOCKSv4.connectionMadeQsrc|jr|jj|y|j|z|_|j}d|jddvr,|jdd|jddc}|_tjd|dd\}}}|jj dd\}|_|dddk(r|dddk7rd|jvr||_y|jj dd\}|_|j j|} | j|j||||| j|fd ytj|dd}|j|||||yy) z Called whenever data is received. @type data: L{bytes} @param data: Part or all of a SOCKSv4 packet. N!BBHsc$|jdSN[rresultr s rz&SOCKSv4.dataReceived..pst~~b7Ir) rr%r<structunpacksplitrresolve addCallback_dataReceived2 addErrbacksocket inet_ntoa) r r'completeBufferheadversioncoderuserserverds rr(zSOCKSv4.dataReceivedUsa >> NN  & 88d? dhhqrl "!XXbq\488AB>'1#= LL((0 d114$M IJ))$q)4   gtT B) #rc|dk(s Jd|z|j||||s|jdy|dk(r-|j||t|}|j |fdn@|dk(r-|j dt ||}|j|fd ntd ||jd k(sJd t|jzy) a4 The second half of the SOCKS connection setup. For a SOCKSv4 packet this is after the server address has been extracted from the header. For a SOCKSv4a packet this is after the host name has been resolved. @type server: L{str} @param server: The IP address of the destination, represented as a dotted quad. @type user: L{str} @param user: The username associated with the connection. @type version: L{int} @param version: The SOCKS protocol version number. @type code: L{int} @param code: The command code. 1 means establish a TCP/IP stream connection, and 2 means establish a TCP/IP port binding. @type port: L{int} @param port: The port number associated with the connection. rAzBad version code: %srFNrBc$|jdSrErGrHs rrJz(SOCKSv4._dataReceived2..s4>>"3Errc6|jdd|d|dS)NrrrBrG)xr s rrJz(SOCKSv4._dataReceived2..st~~b!QqT1Q4/PrzBad Connect Code: rz hmm, still stuff in buffer... %s) authorizer connectClassrrQ listenClassSOCKSv4IncomingFactoryrO RuntimeErrorr<repr)r rYrXrVrWrrZs rrPzSOCKSv4._dataReceived2ws.!|=3g==|~~dFD$7 NN2   19!!&$FA LLTE F QY  $:D&IA MMP Q!3D6:; ;xx3S BT$((^ SSrcf|jr%|jjjyyr )rrrrs rr!zSOCKSv4.connectionLosts$ >> NN $ $ 3 3 5 rc Htjd|d|d|d|d y)Nzcode z connection to :z (user z ) authorizedrB)rmsg)r rWrYrrXs rr`zSOCKSv4.authorizes! VT4 ) rcXtjt|g|j||Sr )r ClientCreatorr connectTCP)r rrklassargss rrazSOCKSv4.connectClasss'%%gu|j d |r|j d |jy) Naz%s %s:%d %s %s:%d <>cdt|zS)Nz%02X)ord)r_s rrJzSOCKSv4.log..sfs1vor . )r:rrropenr%timectimerrstringjoinmaplenreclose)r protor'r their_peerfpcs rrz SOCKSv4.logsN|| ~~%%'^^--557 s #  "   4-'C/C   3BibctA GGFKK$=q A3G#M N GGR#a&[A%+ , tAwrs   65#h''#( #h'' # \h\~ .X%% .X--r