t/README: add missing value for GIT_TEST_DEFAULT_REF_FORMAT
[git/gitster.git] / t / socks4-proxy.pl
blob4c3a35c0083526c430f2aebc496751ad5ed15a2c
1 use strict;
2 use IO::Select;
3 use IO::Socket::UNIX;
4 use IO::Socket::INET;
6 my $path = shift;
8 unlink($path);
9 my $server = IO::Socket::UNIX->new(Listen => 1, Local => $path)
10 or die "unable to listen on $path: $!";
12 $| = 1;
13 print "ready\n";
15 while (my $client = $server->accept()) {
16 sysread $client, my $buf, 8;
17 my ($version, $cmd, $port, $ip) = unpack 'CCnN', $buf;
18 next unless $version == 4; # socks4
19 next unless $cmd == 1; # TCP stream connection
21 # skip NUL-terminated id
22 while (sysread $client, my $char, 1) {
23 last unless ord($char);
26 # version(0), reply(5a == granted), port (ignored), ip (ignored)
27 syswrite $client, "\x00\x5a\x00\x00\x00\x00\x00\x00";
29 my $remote = IO::Socket::INET->new(PeerHost => $ip, PeerPort => $port)
30 or die "unable to connect to $ip/$port: $!";
32 my $io = IO::Select->new($client, $remote);
33 while ($io->count) {
34 for my $fh ($io->can_read(0)) {
35 for my $pair ([$client, $remote], [$remote, $client]) {
36 my ($from, $to) = @$pair;
37 next unless $fh == $from;
39 my $r = sysread $from, my $buf, 1024;
40 if (!defined $r || $r <= 0) {
41 $io->remove($from);
42 next;
44 syswrite $to, $buf;