| 1 | |||
| 2 | ############################################################################ | ||
| 3 | # Connection object | ||
| 4 | # derived from Net::Inspect::L7::HTTP | ||
| 5 | # just pipes all input into conn->in(...) | ||
| 6 | ############################################################################ | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | |||
| 11 | package App::HTTP_Proxy_IMP::Conn; | ||
| 12 | use base 'Net::Inspect::L7::HTTP'; | ||
| 13 | use App::HTTP_Proxy_IMP::Debug; | ||
| 14 | use Scalar::Util 'weaken'; | ||
| 15 | use fields ( | ||
| 16 | # all connections | ||
| 17 | 'pcapdir', # dir for writing pcaps | ||
| 18 | 'mitm', # IO::Socket::SSL::Intercept object for SSL interception | ||
| 19 | 'capath', # path to file|direcory with CA certificates | ||
| 20 | 'imp_factory', # App::HTTP_Proxy_IMP::IMP factory | ||
| 21 | # per connection | ||
| 22 | 'spool', # any data which cannot be processed yet? | ||
| 23 | 'pcapw', # Net::PcapWriter object | ||
| 24 | 'intunnel', # true if connections is inside intercepted SSL tunnel | ||
| 25 | 'relay', # weak reference to managing relay | ||
| 26 | ); | ||
| 27 | |||
| 28 | sub new { | ||
| 29 | my ($class,$upper_flow,%args) = @_; | ||
| 30 | my $self = $class->SUPER::new($upper_flow); | ||
| 31 | %$self = ( %$self, %args ) if %args; | ||
| 32 | if ( ref($class)) { # from factory | ||
| 33 | $self->{pcapdir} ||= $class->{pcapdir}; | ||
| 34 | $self->{imp_factory} ||= $class->{imp_factory}; | ||
| 35 | $self->{mitm} ||= $class->{mitm}; | ||
| 36 | $self->{capath} ||= $class->{capath}; | ||
| 37 | } | ||
| 38 | return $self; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub DESTROY { | ||
| 42 | my $self = shift or return; | ||
| 43 | $self->xdebug("connection done"); | ||
| 44 | $self->SUPER::DESTROY(); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub clone { | ||
| 48 | my $self = shift; | ||
| 49 | return $self->new_connection( | ||
| 50 | $self->{meta}, | ||
| 51 | $self->{relay} | ||
| 52 | ); | ||
| 53 | } | ||
| 54 | |||
| 55 | sub new_connection { | ||
| 56 | my ($self,$meta,$relay) = @_; | ||
| 57 | my $obj = $self->SUPER::new_connection($meta); | ||
| 58 | |||
| 59 | if ( my $pcapdir = $self->{pcapdir} ) { | ||
| 60 | open( my $fh,'>', sprintf("%s/%d.%d.pcap",$pcapdir,$$,$obj->{connid})) | ||
| 61 | or die "cannot open pcap file: $!"; | ||
| 62 | $fh->autoflush; | ||
| 63 | my $w = Net::PcapWriter->new($fh); | ||
| 64 | my $c = $w->tcp_conn( | ||
| 65 | $meta->{daddr}, $meta->{dport}, | ||
| 66 | $meta->{saddr}, $meta->{sport} | ||
| 67 | ); | ||
| 68 | $obj->{pcapw} = [$c,$w], | ||
| 69 | } | ||
| 70 | |||
| 71 | weaken( $obj->{relay} = $relay ); | ||
| 72 | return $obj; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub in { | ||
| 76 | my $self = shift; | ||
| 77 | return $self->SUPER::in(@_) if ! $self->{pcapw}; | ||
| 78 | my ($dir,$data,$eof,$time) = @_; | ||
| 79 | if ( defined ( my $bytes = eval { $self->SUPER::in(@_) } )) { | ||
| 80 | $self->{pcapw}[0]->write($dir,substr($data,0,$bytes)); | ||
| 81 | return $bytes; | ||
| 82 | } else { | ||
| 83 | # save final data | ||
| 84 | $self->{pcapw}[0]->write($dir,$data); | ||
| 85 | die $@ if $@; | ||
| 86 | return; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | |||
| 91 | sub id { | ||
| 92 | my $self = shift; | ||
| 93 | return "$$.$self->{connid}"; | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | 1; |