| 1 | #!/usr/bin/env perl | ||
| 2 | # PODNAME: tcpproxy.pl | ||
| 3 | # ABSTRACT: Simple TCP proxy for debugging connections | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use Term::ANSIColor qw( colored ); | ||
| 9 | use AnyEvent::Handle; | ||
| 10 | use AnyEvent::Socket; | ||
| 11 | use Carp qw( croak ); | ||
| 12 | use DDC; | ||
| 13 | |||
| 14 | sub logmsg { | ||
| 15 | print colored($_[0],'yellow')."\n"; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub in { | ||
| 19 | print colored(' IN ','yellow'); | ||
| 20 | print colored('[','blue'); | ||
| 21 | pc($_[0]); | ||
| 22 | print colored(']','blue')."\n"; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub out { | ||
| 26 | print colored('OUT ','yellow'); | ||
| 27 | print colored('[','blue'); | ||
| 28 | pc($_[0]); | ||
| 29 | print colored(']','blue')."\n"; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub create_proxy { | ||
| 33 | my ( $port, $remote_host, $remote_port ) = @_; | ||
| 34 | |||
| 35 | my %handles; | ||
| 36 | |||
| 37 | my $ip = '127.0.0.1'; | ||
| 38 | |||
| 39 | if ($port =~ /:/) { | ||
| 40 | ( $ip, $port ) = split(/:/,$port); | ||
| 41 | } | ||
| 42 | |||
| 43 | logmsg("starting proxy on $ip:$port"); | ||
| 44 | |||
| 45 | return tcp_server $ip, $port, sub { | ||
| 46 | my ( $client_fh, $client_host, $client_port ) = @_; | ||
| 47 | |||
| 48 | logmsg("received connection from $client_host:$client_port"); | ||
| 49 | |||
| 50 | my $client_h = AnyEvent::Handle->new( | ||
| 51 | fh => $client_fh, | ||
| 52 | ); | ||
| 53 | |||
| 54 | $handles{$client_h} = $client_h; | ||
| 55 | |||
| 56 | tcp_connect $remote_host, $remote_port, sub { | ||
| 57 | unless(@_) { | ||
| 58 | logmsg("connection failed: $!"); | ||
| 59 | $client_h->destroy; | ||
| 60 | return; | ||
| 61 | } | ||
| 62 | my ( $host_fh ) = @_; | ||
| 63 | |||
| 64 | my $host_h = AnyEvent::Handle->new( | ||
| 65 | fh => $host_fh, | ||
| 66 | ); | ||
| 67 | |||
| 68 | $handles{$host_h} = $host_h; | ||
| 69 | |||
| 70 | $client_h->on_read(sub { | ||
| 71 | my $buffer = $client_h->rbuf; | ||
| 72 | $client_h->rbuf = ''; | ||
| 73 | out($buffer); | ||
| 74 | $host_h->push_write($buffer); | ||
| 75 | }); | ||
| 76 | |||
| 77 | $client_h->on_error(sub { | ||
| 78 | my ( undef, undef, $msg ) = @_; | ||
| 79 | logmsg("transmission error: $msg"); | ||
| 80 | $client_h->destroy; | ||
| 81 | $host_h->destroy; | ||
| 82 | delete @handles{$client_h, $host_h}; | ||
| 83 | }); | ||
| 84 | |||
| 85 | $client_h->on_eof(sub { | ||
| 86 | logmsg("client closed connection"); | ||
| 87 | $client_h->destroy; | ||
| 88 | $host_h->destroy; | ||
| 89 | delete @handles{$client_h, $host_h}; | ||
| 90 | }); | ||
| 91 | |||
| 92 | $host_h->on_read(sub { | ||
| 93 | my $buffer = $host_h->rbuf; | ||
| 94 | $host_h->rbuf = ''; | ||
| 95 | in($buffer); | ||
| 96 | $client_h->push_write($buffer); | ||
| 97 | }); | ||
| 98 | |||
| 99 | $host_h->on_error(sub { | ||
| 100 | my ( undef, undef, $msg ) = @_; | ||
| 101 | logmsg("transmission error: $msg"); | ||
| 102 | $host_h->destroy; | ||
| 103 | $client_h->destroy; | ||
| 104 | delete @handles{$client_h, $host_h}; | ||
| 105 | }); | ||
| 106 | |||
| 107 | $host_h->on_eof(sub { | ||
| 108 | logmsg("host closed connection"); | ||
| 109 | $host_h->destroy; | ||
| 110 | $client_h->destroy; | ||
| 111 | delete @handles{$client_h, $host_h}; | ||
| 112 | }); | ||
| 113 | }; | ||
| 114 | }; | ||
| 115 | } | ||
| 116 | |||
| 117 | unless(@ARGV == 3) { | ||
| 118 | print <<"END_USAGE"; | ||
| 119 | usage: $0 [<ip:>localport] [remotehost] [remoteport] | ||
| 120 | |||
| 121 | END_USAGE | ||
| 122 | exit 0 | ||
| 123 | } | ||
| 124 | |||
| 125 | my ( $port, $remote_host, $remote_port ) = @ARGV; | ||
| 126 | |||
| 127 | my $cond = AnyEvent->condvar; | ||
| 128 | |||
| 129 | my $proxy = create_proxy($port, $remote_host, $remote_port); | ||
| 130 | |||
| 131 | $cond->recv; | ||
| 132 | |||
| 133 | __END__ |