1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
use IO::Socket::Socks qw/:constants $SOCKS_ERROR/;
use IO::Socket;
use IO::Select;
use strict;
sub make_socks_server {
my ($version, $login, $password, %delay) = @_;
my $serv = IO::Socket::Socks->new(Listen => 3, SocksVersion => $version, RequireAuth => ($login && $password), UserAuth => sub {
$login = '' unless defined $login;
$password = '' unless defined $password;
$_[0] = '' unless defined $_[0];
$_[1] = '' unless defined $_[1];
return $_[0] eq $login && $_[1] eq $password;
}) or die $@;
my $child = fork();
die 'fork: ', $! unless defined $child;
if ($child == 0) {
while (1) {
if ($delay{accept}) {
sleep $delay{accept};
}
my $client = $serv->accept()
or next;
my $subchild = fork();
die 'subfork: ', $! unless defined $subchild;
if ($subchild == 0) {
my ($cmd, $host, $port) = @{$client->command()};
if($cmd == CMD_CONNECT)
{ # connect
my $socket = IO::Socket::INET->new(PeerHost => $host, PeerPort => $port, Timeout => 10);
if ($delay{reply}) {
sleep $delay{reply};
}
if($socket)
{
# request granted
$client->command_reply($version == 4 ? REQUEST_GRANTED : REPLY_SUCCESS, $socket->sockhost, $socket->sockport);
}
else
{
# request rejected or failed
$client->command_reply($version == 4 ? REQUEST_FAILED : REPLY_HOST_UNREACHABLE, $host, $port);
$client->close();
exit;
}
my $selector = IO::Select->new($socket, $client);
MAIN_CONNECT:
while(1)
{
my @ready = $selector->can_read();
foreach my $s (@ready)
{
my $readed = $s->sysread(my $data, 1024);
unless($readed)
{
# error or socket closed
$socket->close();
last MAIN_CONNECT;
}
if($s == $socket)
{
# return to client data readed from remote host
$client->syswrite($data);
}
else
{
# return to remote host data readed from the client
$socket->syswrite($data);
}
}
}
}
exit;
}
}
}
return ($child, $serv->sockhost eq "0.0.0.0" ? "127.0.0.1" : $serv->sockhost, $serv->sockport);
}
sub make_http_server {
my $serv = IO::Socket::INET->new(Listen => 3)
or die $@;
my $child = fork();
die 'fork: ', $! unless defined $child;
if ($child == 0) {
while (1) {
my $client = $serv->accept()
or next;
my $subchild = fork();
die 'subfork: ', $! unless defined $subchild;
if ($subchild == 0) {
my $buf;
while (1) {
$client->sysread($buf, 1024, length $buf)
or last;
if (rindex($buf, "\015\012\015\012") != -1) {
last;
}
}
my ($path) = $buf =~ /GET\s+(\S+)/
or exit;
my $response;
if ($path eq '/') {
$response = 'ROOT';
}
elsif ($path eq '/index') {
$response = 'INDEX';
}
else {
$response = 'UNKNOWN';
}
$client->syswrite(
join(
"\015\012",
"HTTP/1.1 200 OK",
"Connection: close",
"Content-Type: text/html",
"\015\012"
) . $response
);
exit;
}
}
exit;
}
return ($child, $serv->sockhost eq "0.0.0.0" ? "127.0.0.1" : $serv->sockhost, $serv->sockport);
}
1;
|