Xׯd5ddlZddlZddlZddlZddlZddlmZddlmZGddejZ GddeZ Gdd ejZ Gd d e Z Gd d e ZdS)N) TCPServer)BaseRequestHandlerceZdZdZdZdZdS)TCPCommandListenera This listener is to use a SocketServer.TCPServer on the logic for listening commands the advantage of this version is that is basically a server shipped with python so more error prone. It requires the implementation of a Handler class which will contain all the handshaking logic that was implemented on the original SocketServer (below). There's a specific TODO on this version: need to find the way to tell the server to stop once the command execution has completed. ctjddt dD_ddt dD_tdfdd_j jj d_ dS) Nc3lK|]/}tjtjtjzV0dSNrandomchoicestringascii_uppercasedigits.0xs ..\workbench\tcp_utils.py z.TCPCommandListener.__init__...7eeWXV]6+AFM+QRReeeeeec3lK|]/}tjtjtjzV0dSr r rs rrz.TCPCommandListener.__init__../rr) 127.0.0.1rcBt|||jjfSr )HandShakeHandler handshake close_key)rcsoutput_handlerselfs rz-TCPCommandListener.__init__..1s5?OPQSTVWZ^ZhjnjxYy|J@K@KrF) threadingThread__init__joinrangerrr_server server_bindserver_addressportr#r"s``rr(zTCPCommandListener.__init__+s d###WWee\abc\d\deeeeeDNWWee\abc\d\deeeeeDN-0K0K0K0K0KMRSSDLL +A.DIIIrcj|j|jdSr )r+server_activate serve_forever)r#s rrunzTCPCommandListener.run7s0L  """L     rN)__name__ __module__ __qualname____doc__r(r3rrrrs<   / / /!!!!!rrc0eZdZdZdZdZdZdZdZdS)ra This is the command handler needed when using the TCPServer distributed in the python standard library It is basically a port of the logic contained on the initial SocketServer class (below) that was created for the management of the command executions. c|d|_|d|_|jrdnd|_d|_d|_d|_||_tj||||dS)Nrr%TF) _handshake _close_key_wait_for_right_client_client_connected_pinger_closed_by_client_output_handlerrr()r#requestclient_addressserverhandshake_keysr"s rr(zHandShakeHandler.__init__Csk$Q'DO$Q'DO+//"D$$uD"DDL"D)Dg~vFFFFFrc||jkr#|jdd|_dS|jd|jd|_dSNOKFERROR)r;rBsendr=closer>r#rs rauthenticate_clientz$HandShakeHandler.authenticate_clientPskDO## l$)d!!! l    l$drc2|jr |jdtjd|j|_|jdS#t$r-}d|_ t||_ d|_Yd}~dSd}~wwxYwdSN.r%F) r>rBrJr&Timer_pingr?start Exception exit_statusrepr exit_messager#es rrRzHandShakeHandler._pingYs  '' # q$*55   ''' GG!&' ' 'AA B'"BBcd}tjd|j|_|jd|_|jr |jd}|rS|jr| |n6|j r|j |krd|_ d|_n| |n(#t$r}td|fYd}~nd}~wwxYw|jdSdS)NTr%Fz EXCEPTION : )r&rQrRr?rSr>rBrecvr=rMr<r@ process_datarTprint)r# keep_alivedatarYs rhandlezHandShakeHandler.handlejs/ J?1dj11DLL"D  %%|  &&  $  ( $  $ $T * * * * $4?d#:#:%)D "%*D " "   d # # # %%% # $ $ $ $ $ $ $ $%+  %%%%%s A/B:: CCCc0||dSr )rAr#ras rr^zHandShakeHandler.process_datasrN) r4r5r6r7r(rMrRrbr^r8rrrr;sm G G G%%% ' ' '")%)%)%`rrc:eZdZdZd dZd dZdZdZdZd Z d S) SocketServeraY Custom socket server class handling: - optional handshake with connected client - optional server shutdown based on close_key received by the client This class is currently used to manage the command execution through the admin helper script which communicates to this server by using an instance of the SocketClient class below rcLtj|||_||_||_||_|jrdnd|_d|_d|_ d|_ d|_ d|_ d|_ tjtjtj|_dS)NTFrr)r&r'r(hostr.r;r<r=r>r?r@_boundrUrWsocketAF_INET SOCK_STREAM_socketr#rhr.rrs rr(zSocketServer.__init__s d###DIDIDODO+//"D$$uD"DDL"DDKDD=1CDDDLLLrTc||_ |j|j|jf|j|_d|_dS#t $r&}d|_t||_ Yd}~dSd}~wwxYw)NTr%) r.rmbindrh getsocknamerirTrUrVrW)r#r. catch_errorrYs rrpzSocketServer.bindsDI" lDI.///,**,,didkkk """dq''d"sA A B BBc||jkr#|jdd|_dS|jd|jd|_dSrG)r; _connectionrJr=rKr>rLs rrMz SocketServer.authenticate_clientsnDO## D!!!$)d!!! G$$$ $drc2|jr |jdtjd|j|_|jdS#t$r-}d|_ t||_ d|_Yd}~dSd}~wwxYwdSrO) r>rtrJr&rQrRr?rSrTrUrVrWrXs rrRzSocketServer._pings  '' c""" q$*55   ''' GG!&' ' 'rZcX |js||jdd}|jr|jd|j\|_|_d|_ tj d|j |_ |j |j r3 |jd}|r|jr||n|jr||jdkr}d|_d|_ |j|dd\}}|ds'|dd\}}d|_||_n||n9#t4$r,}d|_t7||_d|_ Yd}~nd}~wwxYw|j 3|jdSdS#t8j$r&}d|_t7||_Yd}~dSd}~wwxYw)NFTr%r\r 0)rirpr.r=rmlistenacceptrt_addressr>r&rQrRr?rSr]rMr<findr@rKsplit startswithrUrWr^rTrVrjerror)r#r`rakey return_dataret_coderrYs rr3zSocketServer.runsrB" [$ $)U###j  '3+ A*.,*=*=*?*?'$-"& q$*55  $" +! +#((..D (,(((....?(tyy'A'AQ'F'F)-&).& &&((($(::c1#5#5 ["--c22,$/$5$5c1$=$=/(E%&$"&+$#!!$''' + + + D  $QD %*D " " " " " " +?$" +#  '3+3+3+3+3+l <"""dq''d"sCB3G46C0F'&G4' G1"GG4GG44H)H$$H)cdSr r8rds rr^zSocketServer.process_data#sDrNrr)T) r4r5r6r7r(rprMrRr3r^r8rrrfrfsEEEE(""""%%% ' ' 'D"D"D"L     rrfceZdZdZdZdZdS)CustomCommandListenera) This is the implementationn of a socket server which will be listening for commands executed as admin in local windows, the output is received through a socket it is used only when the output is required and all the received data will be sent to the configured output handler cddtdD|_ddtdD|_t|dd|j|jt|d|j\|_ |_ ||_ dS)Nrc3lK|]/}tjtjtjzV0dSr r rs rrz1CustomCommandListener.__init__..07 i i[\v/E /U!V!V i i i i i irrc3lK|]/}tjtjtjzV0dSr r rs rrz1CustomCommandListener.__init__..1rrrr) r)r*rrrfr(rprmrqrhr._handlerr/s rr(zCustomCommandListener.__init__.s i i`efg`h`h i i iii i i`efg`h`h i i iiidKDNDNSSS$"""#|7799 49& rc0||dSr )rrds rr^z"CustomCommandListener.process_data;s drN)r4r5r6r7r(r^r8rrrr's< ' ' 'rrc.eZdZdZd dZdZdZd dZdS) SocketClientz Custom socket client class handling: - optional handshake with server - optional server shutdown based on close_key sent by the client rcZ||_||_||_||_d|_d|_dS)NF)_host_portr;r< _connected_authenticatedrns rr(zSocketClient.__init__Fs3DJDJDODODODrc4tjtjtj|_ |j|j|jfd|_|jrx|j |jd}|j P|j d}|r-|dk|_ |j sd|_| |j Pn6#tj $r$}|jdkrt|Yd}~nd}~wwxYw|jS)NTr\rHFiM')rjrkrlrmconnectrrrr;rJrr]rKrerrnor_)r# validatedresponserYs rrSzSocketClient.startOs=1CDDDL lDJ 3444do  # ,  DO , , ,)#+**400h#)1T)9$%,#(-dojjlll#+ < E   a  ?sB,CD,D  DcL|jr|j|dSdSr )rrmrJrds rrJzSocketClient.sendps2   $  rrc|jrtjd|jrB|jr;d|j||fz}||j||jd|_dSdS)Nz%s %d %sF) rtimesleepr<rstriprmrJrK)r#rUmsgclosing_messages rrKzSocketClient.closeus    1 ? /t2 /(DO[#+NNO  ! ! # # # L  o . . .    rNr)rr)r4r5r6r7r(rSrJrKr8rrrr?se B         rr)rjr&rr r socketserverrrr'rrrfrobjectrr8rrrsZ,  """"""++++++!!!!!)!!!:`````)```DG G G G G 9#G G G TL0B B B B B 6B B B B B r