Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/t/socks4-proxy.pl # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / t / socks4-proxy.pl` 48 lines ยท 1167 bytes Raw Blame History 1 use strict; 2 use IO::Select; 3 use IO::Socket::UNIX; 4 use IO::Socket::INET; 5 6 my $path = shift; 7 8 unlink($path); 9 my $server = IO::Socket::UNIX->new(Listen => 1, Local => $path) 10 or die "unable to listen on $path: $!"; 11 12 $| = 1; 13 print "ready\n"; 14 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 20 21 # skip NUL-terminated id 22 while (sysread $client, my $char, 1) { 23 last unless ord($char); 24 } 25 26 # version(0), reply(5a == granted), port (ignored), ip (ignored) 27 syswrite $client, "\x00\x5a\x00\x00\x00\x00\x00\x00"; 28 29 my $remote = IO::Socket::INET->new(PeerHost => $ip, PeerPort => $port) 30 or die "unable to connect to $ip/$port: $!"; 31 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; 38 39 my $r = sysread $from, my $buf, 1024; 40 if (!defined $r || $r <= 0) { 41 $io->remove($from); 42 next; 43 } 44 syswrite $to, $buf; 45 } 46 } 47 } 48 }