<s> import socket <EOL> class NTLM_Proxy_Exception ( Exception ) : <EOL> pass <EOL> class NTLM_Proxy : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ipaddress , port , domain , protoFactory , socketFactory ) : <EOL> self . ipaddress = ipaddress <EOL> self . port = port <EOL> self . domain = domain <EOL> self . socketFactory = socketFactory <EOL> self . protoFactory = protoFactory <EOL> self . socket = False <EOL> self . bufferin = '<STR_LIT>' <EOL> def _openConnection ( self ) : <EOL> """<STR_LIT>""" <EOL> self . close ( ) <EOL> self . socket = self . socketFactory . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> self . socket . settimeout ( <NUM_LIT:5> ) <EOL> self . socket . connect ( ( self . ipaddress , self . port ) ) <EOL> def _readsocket ( self , length ) : <EOL> """<STR_LIT>""" <EOL> if not self . socket : <EOL> raise NTLM_Proxy_Exception ( "<STR_LIT>" ) <EOL> data = self . bufferin <EOL> while len ( data ) < length : <EOL> data += self . socket . recv ( <NUM_LIT> ) <EOL> data , self . bufferin = data [ : length ] , data [ length : ] <EOL> return data <EOL> def _transaction ( self , msg ) : <EOL> if not self . socket : <EOL> raise NTLM_Proxy_Exception ( "<STR_LIT>" ) <EOL> self . socket . send ( msg ) <EOL> data = self . _readsocket ( self . proto . minimumData ) <EOL> data += self . _readsocket ( self . proto . getTransportLength ( data ) ) <EOL> return data <EOL> def close ( self ) : <EOL> if self . socket : <EOL> self . socket . close ( ) <EOL> self . socket = False <EOL> self . bufferin = '<STR_LIT>' <EOL> def negotiate ( self , ntlm_negotiate ) : <EOL> """<STR_LIT>""" <EOL> self . _openConnection ( ) <EOL> self . proto = self . protoFactory ( ) <EOL> msg = self . proto . make_negotiate_protocol_req ( ) <EOL> if msg : <EOL> msg = self . _transaction ( msg ) <EOL> self . proto . parse_negotiate_protocol_resp ( msg ) <EOL> msg = self . proto . make_session_setup_req ( ntlm_negotiate , True ) <EOL> msg = self . _transaction ( msg ) <EOL> result , challenge = self . proto . parse_session_setup_resp ( msg ) <EOL> if not result : <EOL> return False <EOL> return challenge <EOL> def authenticate ( self , ntlm_authenticate ) : <EOL> """<STR_LIT>""" <EOL> msg = self . proto . make_session_setup_req ( ntlm_authenticate , False ) <EOL> msg = self . _transaction ( msg ) <EOL> return self . proto . parse_session_setup_resp ( msg ) [ <NUM_LIT:0> ] </s>
