diff --git a/proxy.pl b/proxy.pl new file mode 100644 index 0000000..39c7014 --- /dev/null +++ b/proxy.pl @@ -0,0 +1,51 @@ +#!/usr/bin/env perl + +use v5.38.2; +use strict; +use warnings; + +use IO::Socket::INET; + +my $server = IO::Socket::INET->new( + LocalPort => 19132, + LocalHost => '0.0.0.0', + Proto => 'udp', +); + +my %sockets; + +my $buf = ''; +while ($server->recv($buf, 1024)) { + my $host = $server->peerhost; + my $port = $server->peerport; + my $source = "$host:$port"; + generate_socket($source, $server) if !defined $sockets{$source}; + my $final_socket = $sockets{$source}; + $final_socket->send($buf); + $buf = ''; +} + +sub generate_socket($source, $server) { + $sockets{$source} = connect_final(); + my $pid = fork; + if (!$pid) { + my $buf = ''; + while ($sockets{$source}->recv($buf, 1024)) { + $server->send($buf); + $buf = ''; + } + } +} + +sub connect_udp($host, $port) { + my $socket = IO::Socket::INET->new( + PeerPort => $port, + PeerHost => $host, + Proto => 'udp', + ); + return $socket; +} + +sub connect_final { + return connect_udp('192.168.2.1', 19132); +}