<s> '''<STR_LIT>''' <EOL> from datetime import datetime <EOL> import dpkt <EOL> import logging <EOL> from pcaputil import ms_from_dpkt_time , ms_from_dpkt_time_diff <EOL> from pagetracker import PageTracker <EOL> import http <EOL> import settings <EOL> class Entry ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , request , response ) : <EOL> self . request = request <EOL> self . response = response <EOL> self . pageref = None <EOL> self . ts_start = ms_from_dpkt_time ( request . ts_connect ) <EOL> if request . ts_connect is None : <EOL> self . startedDateTime = None <EOL> else : <EOL> self . startedDateTime = datetime . utcfromtimestamp ( request . ts_connect ) <EOL> self . time_blocked = - <NUM_LIT:1> <EOL> self . time_dnsing = - <NUM_LIT:1> <EOL> self . time_connecting = ( <EOL> ms_from_dpkt_time_diff ( request . ts_start , request . ts_connect ) ) <EOL> self . time_sending = ( <EOL> ms_from_dpkt_time_diff ( request . ts_end , request . ts_start ) ) <EOL> if response is not None : <EOL> self . time_waiting = ( <EOL> ms_from_dpkt_time_diff ( response . ts_start , request . ts_end ) ) <EOL> self . time_receiving = ( <EOL> ms_from_dpkt_time_diff ( response . ts_end , response . ts_start ) ) <EOL> endedDateTime = datetime . utcfromtimestamp ( response . ts_end ) <EOL> self . total_time = ms_from_dpkt_time_diff ( response . ts_end , request . ts_connect ) <EOL> else : <EOL> self . time_waiting = - <NUM_LIT:1> <EOL> self . time_receiving = - <NUM_LIT:1> <EOL> self . total_time = - <NUM_LIT:1> <EOL> def json_repr ( self ) : <EOL> '''<STR_LIT>''' <EOL> d = { <EOL> '<STR_LIT:time>' : self . total_time , <EOL> '<STR_LIT>' : self . request , <EOL> '<STR_LIT>' : self . response , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : self . time_blocked , <EOL> '<STR_LIT>' : self . time_dnsing , <EOL> '<STR_LIT>' : self . time_connecting , <EOL> '<STR_LIT>' : self . time_sending , <EOL> '<STR_LIT>' : self . time_waiting , <EOL> '<STR_LIT>' : self . time_receiving <EOL> } , <EOL> '<STR_LIT>' : { } , <EOL> } <EOL> if self . startedDateTime : <EOL> d [ '<STR_LIT>' ] = self . startedDateTime . isoformat ( ) + '<STR_LIT>' <EOL> if self . pageref : <EOL> d [ '<STR_LIT>' ] = self . pageref <EOL> return d <EOL> def add_dns ( self , dns_query ) : <EOL> '''<STR_LIT>''' <EOL> self . time_dnsing = ms_from_dpkt_time ( dns_query . duration ( ) ) <EOL> class UserAgentTracker ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self ) : <EOL> self . data = { } <EOL> def add ( self , ua_string ) : <EOL> '''<STR_LIT>''' <EOL> if ua_string in self . data : <EOL> self . data [ ua_string ] += <NUM_LIT:1> <EOL> else : <EOL> self . data [ ua_string ] = <NUM_LIT:1> <EOL> def dominant_user_agent ( self ) : <EOL> '''<STR_LIT>''' <EOL> if not len ( self . data ) : <EOL> return None <EOL> elif len ( self . data ) == <NUM_LIT:1> : <EOL> return self . data . keys ( ) [ <NUM_LIT:0> ] <EOL> else : <EOL> return max ( self . data . iteritems ( ) , key = lambda v : v [ <NUM_LIT:1> ] ) [ <NUM_LIT:0> ] <EOL> class HttpSession ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , packetdispatcher ) : <EOL> '''<STR_LIT>''' <EOL> self . flows = [ ] <EOL> for flow in packetdispatcher . tcp . flows ( ) : <EOL> try : <EOL> self . flows . append ( http . Flow ( flow ) ) <EOL> except http . Error as error : <EOL> logging . warning ( error ) <EOL> except dpkt . dpkt . Error as error : <EOL> logging . warning ( error ) <EOL> pairs = reduce ( lambda p , f : p + f . pairs , self . flows , [ ] ) <EOL> self . user_agents = UserAgentTracker ( ) <EOL> if settings . process_pages : <EOL> self . page_tracker = PageTracker ( ) <EOL> else : <EOL> self . page_tracker = None <EOL> self . entries = [ ] <EOL> pairs . sort ( <EOL> key = lambda pair : pair . request . ts_connect <EOL> ) <EOL> for msg in pairs : <EOL> entry = Entry ( msg . request , msg . response ) <EOL> if '<STR_LIT>' in msg . request . msg . headers : <EOL> self . user_agents . add ( msg . request . msg . headers [ '<STR_LIT>' ] ) <EOL> if self . page_tracker : <EOL> entry . pageref = self . page_tracker . getref ( entry ) <EOL> if entry . response or settings . keep_unfulfilled_requests : <EOL> self . entries . append ( entry ) <EOL> self . user_agent = self . user_agents . dominant_user_agent ( ) <EOL> names_mentioned = set ( ) <EOL> dns = packetdispatcher . udp . dns <EOL> for entry in self . entries : <EOL> name = entry . request . host <EOL> if name not in names_mentioned : <EOL> if name in dns . by_hostname : <EOL> entry . add_dns ( dns . by_hostname [ name ] [ - <NUM_LIT:1> ] ) <EOL> names_mentioned . add ( name ) <EOL> def json_repr ( self ) : <EOL> '''<STR_LIT>''' <EOL> d = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:version>' : '<STR_LIT>' <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:name>' : self . user_agent , <EOL> '<STR_LIT:version>' : '<STR_LIT>' <EOL> } , <EOL> '<STR_LIT>' : sorted ( self . entries , key = lambda x : x . ts_start ) <EOL> } <EOL> } <EOL> if self . page_tracker : <EOL> d [ '<STR_LIT>' ] [ '<STR_LIT>' ] = self . page_tracker <EOL> return d </s>
