def __init__(self, packet): """initialize :class:`TcpConnection` Args: packet (TcpPack): packet that need to be appended """ self.up_stream = Stream() self.down_stream = Stream() self.client_key = packet.source_key() self.is_http = None self.http_parser = HttpParser() self.on_packet(packet)
class TcpConnection(object): """pcap tcp parser parse a pcap file and creates :class:`TcpPacket` objects from that by the help of packetparse project """ def __init__(self, packet): """initialize :class:`TcpConnection` Args: packet (TcpPack): packet that need to be appended """ self.up_stream = Stream() self.down_stream = Stream() self.client_key = packet.source_key() self.is_http = None self.http_parser = HttpParser() self.on_packet(packet) def on_packet(self, packet): """parse a :class:`TcpPack` object. change attributes(ack, sin, fin , ...) of a :class:`TcpPack` object Args: packet (TcpPack): packet that need to be appended Returns: a tcp request header """ all_packets = None if self.is_http is None and packet.body: self.is_http = is_request(packet.body) if self.is_http is 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: tcp = self.http_parser.send(pac_type, packet.body) if tcp is not None: all_packets = tcp if packet.fin: send_stream.status = 1 return all_packets def closed(self): """check if this is the end of a conversation Returns: True if the connection is closed , False otherwise """ return self.up_stream.status == 1 and self.down_stream.status == 1 def finish(self): """if the connection was :func:`closed` then it is finished""" self.http_parser.finish()