class HttpConn: """all data having same source/dest ip/port in one http connection.""" STATUS_BEGIN = 0 STATUS_RUNNING = 1 STATUS_CLOSED = 2 STATUS_ERROR = -1 def __init__(self, tcp_pac): self.source_ip = tcp_pac.source self.source_port = tcp_pac.source_port self.dest_ip = tcp_pac.dest self.dest_port = tcp_pac.dest_port self.status = HttpConn.STATUS_BEGIN # start parser thread self.processor = HttpPrinter((self.source_ip, self.source_port), (self.dest_ip, self.dest_port)) self.http_parser = HttpParser(self.processor) self.tcp_pac_list = [] self.append(tcp_pac) def append(self, tcp_pac): print("xiaozhen, HttpConn") print([tcp_pac,self.source_ip, self.source_port, self.dest_ip, self.dest_port]) print("xiaozhen, HttpConn end ") if len(tcp_pac.body) == 0: return #Store the tcp infos, will be used to display self.tcp_pac_list.append([tcp_pac,self.source_ip, self.source_port, self.dest_ip, self.dest_port]) if self.status == HttpConn.STATUS_ERROR or self.status == HttpConn.STATUS_CLOSED: # not http conn or conn already closed. return if self.status == HttpConn.STATUS_BEGIN: if tcp_pac.body: if utils.is_request(tcp_pac.body): self.status = HttpConn.STATUS_RUNNING if tcp_pac.source == self.source_ip: http_type = HttpType.REQUEST else: http_type = HttpType.RESPONSE if self.status == HttpConn.STATUS_RUNNING and tcp_pac.body: self.http_parser.send(http_type, tcp_pac.body) if tcp_pac.pac_type == -1: # end of connection if self.status == HttpConn.STATUS_RUNNING: self.status = HttpConn.STATUS_CLOSED else: self.status = HttpConn.STATUS_ERROR def finish(self): self.http_parser.finish()
def _worker(worker_socket, client_ip, client_port, output_file): try: handler = ConnectionHandler(worker_socket) handler.init_connect() processor = HttpPrinter((client_ip, client_port), handler.remote_host) http_parser = HttpParser(processor) handler.proxy_data(http_parser) handler.close() http_parser.finish() except Exception: import traceback traceback.print_exc()
class TcpConnection(object): def __init__(self, packet): """ :type packet: TcpPack """ self.up_stream = Stream() self.down_stream = Stream() self.client_key = packet.source_key() self.is_http = None self.processor = HttpPrinter((packet.source, packet.source_port), (packet.dest, packet.dest_port)) self.http_parser = HttpParser(self.processor) self.on_packet(packet) def on_packet(self, packet): """ :type packet: TcpPack """ if self.is_http is None and packet.body: self.is_http = is_request(packet.body) if self.is_http == False: return if packet.source_key() == self.client_key: send_stream = self.up_stream confirm_stream = self.down_stream pac_type = HttpType.RESPONSE else: send_stream = self.down_stream confirm_stream = self.up_stream pac_type = HttpType.REQUEST if len(packet.body) > 0: send_stream.append_packet(packet) if packet.syn: pass if packet.ack: packets = confirm_stream.retrieve_packet(packet.ack_seq) if packets: for packet in packets: self.http_parser.send(pac_type, packet.body) if packet.fin: send_stream.status = 1 def closed(self): return self.up_stream.status == 1 and self.down_stream.status == 1 def finish(self): self.http_parser.finish()