<s> import asyncore_25 as asyncore <EOL> import asynchat_25 as asynchat <EOL> import socket <EOL> import string <EOL> import sys <EOL> import os <EOL> import md5 <EOL> class stdin_channel ( asyncore . file_dispatcher ) : <EOL> def handle_read ( self ) : <EOL> data = self . recv ( <NUM_LIT> ) <EOL> if not data : <EOL> print '<STR_LIT>' <EOL> self . sock_channel . close ( ) <EOL> try : <EOL> self . close ( ) <EOL> except : <EOL> pass <EOL> data = string . replace ( data , '<STR_LIT:\n>' , '<STR_LIT:\r\n>' ) <EOL> self . sock_channel . push ( data ) <EOL> def writable ( self ) : <EOL> return <NUM_LIT:0> <EOL> def log ( self , * ignore ) : <EOL> pass <EOL> class monitor_client ( asynchat . async_chat ) : <EOL> def __init__ ( self , password , addr = ( '<STR_LIT>' , <NUM_LIT> ) , socket_type = socket . AF_INET ) : <EOL> asynchat . async_chat . __init__ ( self ) <EOL> self . create_socket ( socket_type , socket . SOCK_STREAM ) <EOL> self . terminator = '<STR_LIT:\r\n>' <EOL> self . connect ( addr ) <EOL> self . sent_auth = <NUM_LIT:0> <EOL> self . timestamp = '<STR_LIT>' <EOL> self . password = password <EOL> def collect_incoming_data ( self , data ) : <EOL> if not self . sent_auth : <EOL> self . timestamp = self . timestamp + data <EOL> else : <EOL> sys . stdout . write ( data ) <EOL> sys . stdout . flush ( ) <EOL> def found_terminator ( self ) : <EOL> if not self . sent_auth : <EOL> self . push ( hex_digest ( self . timestamp + self . password ) + '<STR_LIT:\r\n>' ) <EOL> self . sent_auth = <NUM_LIT:1> <EOL> else : <EOL> print <EOL> def handle_close ( self ) : <EOL> map ( lambda x : x . close ( ) , asyncore . socket_map . values ( ) ) <EOL> def log ( self , * ignore ) : <EOL> pass <EOL> class encrypted_monitor_client ( monitor_client ) : <EOL> "<STR_LIT>" <EOL> def init_cipher ( self , cipher , key ) : <EOL> self . outgoing = cipher . new ( key ) <EOL> self . incoming = cipher . new ( key ) <EOL> def push ( self , data ) : <EOL> return monitor_client . push ( self , self . outgoing . encrypt ( data ) ) <EOL> def recv ( self , block_size ) : <EOL> data = monitor_client . recv ( self , block_size ) <EOL> if data : <EOL> return self . incoming . decrypt ( data ) <EOL> else : <EOL> return data <EOL> def hex_digest ( s ) : <EOL> m = md5 . md5 ( ) <EOL> m . update ( s ) <EOL> return string . join ( <EOL> map ( lambda x : hex ( ord ( x ) ) [ <NUM_LIT:2> : ] , map ( None , m . digest ( ) ) ) , <EOL> '<STR_LIT>' , <EOL> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> if len ( sys . argv ) == <NUM_LIT:1> : <EOL> print '<STR_LIT>' % sys . argv [ <NUM_LIT:0> ] <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> if ( '<STR_LIT>' in sys . argv ) : <EOL> encrypt = <NUM_LIT:1> <EOL> sys . argv . remove ( '<STR_LIT>' ) <EOL> else : <EOL> encrypt = <NUM_LIT:0> <EOL> sys . stderr . write ( '<STR_LIT>' ) <EOL> sys . stderr . flush ( ) <EOL> try : <EOL> os . system ( '<STR_LIT>' ) <EOL> p = raw_input ( ) <EOL> print <EOL> finally : <EOL> os . system ( '<STR_LIT>' ) <EOL> stdin = stdin_channel ( <NUM_LIT:0> ) <EOL> if len ( sys . argv ) > <NUM_LIT:1> : <EOL> if encrypt : <EOL> client = encrypted_monitor_client ( p , ( sys . argv [ <NUM_LIT:1> ] , string . atoi ( sys . argv [ <NUM_LIT:2> ] ) ) ) <EOL> import sapphire <EOL> client . init_cipher ( sapphire , p ) <EOL> else : <EOL> client = monitor_client ( p , ( sys . argv [ <NUM_LIT:1> ] , string . atoi ( sys . argv [ <NUM_LIT:2> ] ) ) ) <EOL> else : <EOL> client = monitor_client ( p ) <EOL> stdin . sock_channel = client <EOL> asyncore . loop ( ) </s>
