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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
#!/usr/bin/env perl
use lib '../lib';
use IO::Socket::Socks qw(:constants $SOCKS_ERROR);
use IO::Select;
use strict;
# return bind address as ip address like most socks5 proxyes does
$IO::Socket::Socks::SOCKS5_RESOLVE = 1;
# create socks server
my $server = IO::Socket::Socks->new(SocksVersion => 5, SocksDebug => 1, ProxyAddr => 'localhost', ProxyPort => 1080, Listen => 10)
or die $SOCKS_ERROR;
# accept connections
while()
{
my $client = $server->accept();
if($client)
{
my ($cmd, $host, $port) = @{$client->command()};
if($cmd == CMD_CONNECT)
{ # connect
# create socket with requested host
my $socket = IO::Socket::INET->new(PeerHost => $host, PeerPort => $port, Timeout => 10);
if($socket)
{
# success
$client->command_reply(REPLY_SUCCESS, $socket->sockhost, $socket->sockport);
}
else
{
# Host Unreachable
$client->command_reply(REPLY_HOST_UNREACHABLE, $host, $port);
$client->close();
next;
}
my $selector = IO::Select->new($socket, $client);
MAIN_CONNECT:
while()
{
my @ready = $selector->can_read();
foreach my $s (@ready)
{
my $readed = $s->sysread(my $data, 1024);
unless($readed)
{
# error or socket closed
warn 'connection 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);
}
}
}
}
elsif($cmd == CMD_BIND)
{ # bind
# create listen socket
my $socket = IO::Socket::INET->new(Listen => 10);
if($socket)
{
# success
$client->command_reply(REPLY_SUCCESS, $socket->sockhost, $socket->sockport);
}
else
{
# request rejected or failed
$client->command_reply(REPLY_HOST_UNREACHABLE, $host, $port);
$client->close();
next;
}
while()
{
# accept new connection needed proxifycation
my $conn = $socket->accept()
or next;
$socket->close();
if($conn->peerhost ne join('.', unpack('C4', (gethostbyname($host))[4])))
{
# connected host should be same as specified in the client bind request
last;
}
$client->command_reply(REPLY_SUCCESS, $conn->peerhost, $conn->peerport);
my $selector = IO::Select->new($conn, $client);
MAIN_BIND:
while()
{
my @ready = $selector->can_read();
foreach my $s (@ready)
{
my $readed = $s->sysread(my $data, 1024);
unless($readed)
{
# error or socket closed
warn 'connection closed';
$conn->close();
last MAIN_BIND;
}
if($s == $conn)
{
# return to client data readed from remote host
$client->syswrite($data);
}
else
{
# return to remote host data readed from the client
$conn->syswrite($data);
}
}
}
last;
}
}
elsif($cmd == CMD_UDPASSOC)
{ # UDP associate
# who really need it?
# you could send me a patch
warn 'UDP assoc: not implemented';
$client->command_reply(REPLY_GENERAL_FAILURE, $host, $port);
}
else
{
warn 'Unknown command';
}
$client->close();
}
else
{
warn $SOCKS_ERROR;
}
}
sub auth
{ # add `UserAuth => \&auth, RequireAuth => 1' to the server constructor if you want to authenticate user by login and password
my $login = shift;
my $password = shift;
my %allowed_users = (root => 123, oleg => 321, ryan => 213);
return $allowed_users{$login} eq $password;
}
# tested with `curl --socks5'
|