Perl implementation of Linux under the socket proxy server _perl

Source: Internet
Author: User
Tags auth bind ord in python

You provide a number of Linux open proxy methods, generally used in Python and other languages, some servers may not be installed, but Perl can be said to be the standard Linux language, to give everyone a Perl language socket agent, the code is very small, but also support the password, the effect is good and feel very stable.

#!/usr/bin/perl $auth _enabled = 0; 
$auth _login = "hidden"; 
$auth _pass = "hidden"; 
 
$port = 44269; 
 
Use io::socket::inet; 
$SIG {' chld '} = ' IGNORE '; $bind = Io::socket::inet->new (listen=>10, reuse=>1, localport=> $port) or die "нельзязабиндитьпорт $port \ n" 
 
; 
 
while ($client = $bind->accept ()) {$client->autoflush (); 
if (fork ()) {$client->close ();} 
else {$bind->close (); new_client ($client); exit ();} 
Sub New_client {local $t, $i, $buff, $ord, $success; 
Local $client = $_[0]; 
 
Sysread ($client, $buff, 1); 
 if (ord ($buff) = = 5) {sysread ($client, $buff, 1); 
 
 $t = Ord ($buff); 
 
 Unless (Sysread ($client, $buff, $t) = = $t) {return;} 
 $success = 0; 
 for ($i = 0; $i < $t; $i + +) {$ord = Ord (substr ($buff, $i, 1)); 
  if ($ord = = 0 && $auth _enabled) {syswrite ($client, "\x05\x00", 2); 
  $success + +; 
 Break 
  } elsif ($ord = = 2 && $auth _enabled) {unless (Do_auth ($client)) {return;} 
  $success + +; 
 Break } 
 } if ($success) {$t = Sysread ($client, $buff, 3); if (substr ($buff, 0, 1) = = ' \x05 ') {if (Ord (substr ($buff, 2, 1)) = = 0) {($host, $raw _host) = Socks_get_host ($client) 
  ; 
  if (! $host) {return;} 
  ($port, $raw _port) = Socks_get_port ($client); 
  if (! $port) {return;} 
  $ord = Ord (substr ($buff, 1, 1)); 
  $buff = "\x05\x00\x00". $raw _host. $raw _port; 
  Syswrite ($client, $buff, Length ($buff)); 
  Socks_do ($ord, $client, $host, $port); 
}} else {syswrite ($client, "\x05\xff", 2);}; 
} $client->close (); 
Sub Do_auth {local $buff, $login, $pass; 
 
Local $client = $_[0]; 
Syswrite ($client, "\x05\x02", 2); 
 
Sysread ($client, $buff, 1); 
 if (ord ($buff) = = 1) {Sysread ($client, $buff, 1); 
 Sysread ($client, $login, Ord ($buff)); 
 Sysread ($client, $buff, 1); 
 
 Sysread ($client, $pass, Ord ($buff)); 
 if ($login eq $auth _login && $pass eq $auth _pass) {syswrite ($client, "\x05\x00", 2); 
 return 1; else {syswrite ($client, "\x05\x01", 2);}
} $client->close (); 
return 0; 
Sub Socks_get_host {Local $client = $_[0]; 
Local $t, $ord, $raw _host; 
 
Local $host = ""; 
Sysread ($client, $t, 1); 
$ord = Ord ($t); 
 if ($ord = = 1) {Sysread ($client, $raw _host, 4); @host = $raw _host =~/(.) 
 /g; $host = Ord ($host [0]). "." Ord ($host [1]). ".". Ord ($host [2]). ".". 
Ord ($host [3]); 
 } elsif ($ord = = 3) {Sysread ($client, $raw _host, 1); 
 Sysread ($client, $host, Ord ($raw _host)); 
$raw _host. = $host; 
} elsif ($ord = = 4) {#ipv6-not supported} return ($host, $t. $raw _host); 
Sub Socks_get_port {Local $client = $_[0]; 
Local $raw _port, $port; 
Sysread ($client, $raw _port, 2); $port = Ord (substr ($raw _port, 0, 1)) << 8 | 
Ord (substr ($raw _port, 1, 1)); 
Return ($port, $raw _port); 
 
Sub Socks_do {local ($t, $client, $host, $port) = @_; 
if ($t = = 1) {Socks_connect ($client, $host, $port);} 
elsif ($t = = 2) {socks_bind ($client, $host, $port);} elsif ($t = = 3) {socks_udp_associate ($client, $host, $port);}
else {return 0;} 
return 1; 
Sub Socks_connect {My ($client, $host, $port) = @_; My $target = io::socket::inet->new (peeraddr => $host, Peerport => $port, Proto => ' TCP ', Type => Sock_strea 
 
M); 
 
Unless ($target) {return;} 
$target->autoflush (); 
 while ($client | |. $target) {My $rin = ""; 
 VEC ($rin, Fileno ($client), 1) = 1 if $client; 
 VEC ($rin, Fileno ($target), 1) = 1 if $target; 
 My ($rout, $eout); 
 Select ($rout = $rin, undef, $eout = $rin, 120); 
 if (! $rout &&! $eout) {return;} 
 My $cbuffer = ""; 
 
 My $tbuffer = ""; if ($client && (VEC ($eout, Fileno ($client), 1) | | VEC ($rout, Fileno ($client), 1)) {My $result = Sysread ($clien 
 T, $tbuffer, 1024); 
 if (!defined ($result) | |! $result) {return;} } if ($target && (VEC ($eout, Fileno ($target), 1) | | VEC ($rout, Fileno ($target), 1)) {My $result = Sysread ( 
 $target, $cbuffer, 1024); 
 if (!defined ($result) | |! $result) {return;} } if ($fh && $tbuffER) {print $fh $tbuffer;} 
 while (i $len = Length ($tbuffer)) {My $res = Syswrite ($target, $tbuffer, $len); 
 if ($res > 0) {$tbuffer = substr ($tbuffer, $res);} else {return;} 
 while (my $len = Length ($cbuffer)) {My $res = Syswrite ($client, $cbuffer, $len); 
 if ($res > 0) {$cbuffer = substr ($cbuffer, $res);} else {return;} 
}} sub Socks_bind {My ($client, $host, $port) = @_; 
Sub Socks_udp_associate {My ($client, $host, $port) = @_; }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.