搜索
查看: 285|回复: 0

linux socket 代理

[复制链接]

1839

主题

2255

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
11913
发表于 2014-6-4 21:59:58 | 显示全部楼层 |阅读模式
大家提供了许多linux开代理的方法,一般用到python等语言,一些服务器可能不会安装,然而perl可以说是linux标配的语言,给大家一款Perl语言的socket代理,代码非常少,而且还支持密码,效果还是不错,感觉很稳定。

  1. #!/usr/bin/perl

  2. $auth_enabled = 0;
  3. $auth_login = "hidden";
  4. $auth_pass = "hidden";
  5. $port = 44269;

  6. use IO::Socket::INET;

  7. $SIG{'CHLD'} = 'IGNORE';
  8. $bind = IO::Socket::INET->new(Listen=>10, Reuse=>1, LocalPort=>$port) or  die "Нельзя забиндить порт $port\n";

  9. while($client = $bind->accept()) {
  10. $client->autoflush();

  11. if(fork()){ $client->close(); }
  12. else { $bind->close(); new_client($client); exit(); }
  13. }

  14. sub new_client {
  15. local $t, $i, $buff, $ord, $success;
  16. local $client = $_[0];
  17. sysread($client, $buff, 1);

  18. if(ord($buff) == 5) {
  19.   sysread($client, $buff, 1);
  20.   $t = ord($buff);

  21.   unless(sysread($client, $buff, $t) == $t) { return; }

  22.   $success = 0;
  23.   for($i = 0; $i < $t; $i++) {
  24.     $ord = ord(substr($buff, $i, 1));
  25.     if($ord == 0 && !$auth_enabled) {
  26.       syswrite($client, "\x05\x00", 2);
  27.       $success++;
  28.       break;
  29.     }
  30.     elsif($ord == 2 && $auth_enabled) {
  31.       unless(do_auth($client)){ return; }
  32.       $success++;
  33.       break;
  34.     }
  35.   }

  36.   if($success) {
  37.     $t = sysread($client, $buff, 3);

  38.     if(substr($buff, 0, 1) == '\x05') {
  39.       if(ord(substr($buff, 2, 1)) == 0) {
  40.       ($host, $raw_host) = socks_get_host($client);
  41.       if(!$host) {  return; }
  42.       ($port, $raw_port) = socks_get_port($client);
  43.       if(!$port) { return; }
  44.       $ord = ord(substr($buff, 1, 1));
  45.       $buff = "\x05\x00\x00".$raw_host.$raw_port;
  46.       syswrite($client, $buff, length($buff));
  47.       socks_do($ord, $client, $host, $port);
  48.       }
  49.     }
  50.   } else { syswrite($client, "\x05\xFF", 2); };
  51. }
  52. $client->close();
  53. }

  54. sub do_auth {
  55. local $buff, $login, $pass;
  56. local $client = $_[0];

  57. syswrite($client, "\x05\x02", 2);
  58. sysread($client, $buff, 1);

  59. if(ord($buff) == 1) {
  60.   sysread($client, $buff, 1);
  61.   sysread($client, $login, ord($buff));
  62.   sysread($client, $buff, 1);
  63.   sysread($client, $pass, ord($buff));

  64.   if($login eq $auth_login && $pass eq $auth_pass) {
  65.     syswrite($client, "\x05\x00", 2);
  66.     return 1;
  67.   } else { syswrite($client, "\x05\x01", 2); }
  68. }

  69. $client->close();
  70. return 0;
  71. }

  72. sub socks_get_host {
  73. local $client = $_[0];
  74. local $t, $ord, $raw_host;
  75. local $host = "";

  76. sysread($client, $t, 1);
  77. $ord = ord($t);
  78. if($ord == 1) {
  79.   sysread($client, $raw_host, 4);
  80.   @host = $raw_host =~ /(.)/g;
  81.   $host = ord($host[0]).".".ord($host[1]).".".ord($host[2]).".".ord($host[3]);
  82. } elsif($ord == 3) {
  83.   sysread($client, $raw_host, 1);
  84.   sysread($client, $host, ord($raw_host));
  85.   $raw_host .= $host;
  86. } elsif($ord == 4) {
  87.   #ipv6 - not supported
  88. }

  89. return ($host, $t.$raw_host);
  90. }

  91. sub socks_get_port {
  92. local $client = $_[0];
  93. local $raw_port, $port;
  94. sysread($client, $raw_port, 2);
  95. $port = ord(substr($raw_port, 0, 1)) << 8 | ord(substr($raw_port, 1, 1));
  96. return ($port, $raw_port);
  97. }

  98. sub socks_do {
  99. local($t, $client, $host, $port) = @_;

  100. if($t == 1) { socks_connect($client, $host, $port); }
  101. elsif($t == 2) { socks_bind($client, $host, $port); }
  102. elsif($t == 3) { socks_udp_associate($client, $host, $port); }
  103. else { return 0; }

  104. return 1;
  105. }

  106. sub socks_connect {
  107. my($client, $host, $port) = @_;
  108. my $target = IO::Socket::INET->new(PeerAddr => $host, PeerPort => $port, Proto => 'tcp', Type => SOCK_STREAM);

  109. unless($target) { return; }

  110. $target->autoflush();
  111. while($client || $target) {
  112.   my $rin = "";
  113.   vec($rin, fileno($client), 1) = 1 if $client;
  114.   vec($rin, fileno($target), 1) = 1 if $target;
  115.   my($rout, $eout);
  116.   select($rout = $rin, undef, $eout = $rin, 120);
  117.   if (!$rout  &&  !$eout) { return; }
  118.   my $cbuffer = "";
  119.   my $tbuffer = "";

  120.   if ($client && (vec($eout, fileno($client), 1) || vec($rout, fileno($client), 1))) {
  121.     my $result = sysread($client, $tbuffer, 1024);
  122.     if (!defined($result) || !$result) { return; }
  123.   }

  124.   if ($target  &&  (vec($eout, fileno($target), 1)  || vec($rout, fileno($target), 1))) {
  125.     my $result = sysread($target, $cbuffer, 1024);
  126.     if (!defined($result) || !$result) { return; }
  127.     }

  128.   if ($fh  &&  $tbuffer) { print $fh $tbuffer; }

  129.   while (my $len = length($tbuffer)) {
  130.     my $res = syswrite($target, $tbuffer, $len);
  131.     if ($res > 0) { $tbuffer = substr($tbuffer, $res); } else { return; }
  132.   }

  133.   while (my $len = length($cbuffer)) {
  134.     my $res = syswrite($client, $cbuffer, $len);
  135.     if ($res > 0) { $cbuffer = substr($cbuffer, $res); } else { return; }
  136.   }
  137. }
  138. }

  139. sub socks_bind {
  140. my($client, $host, $port) = @_;
  141. }

  142. sub socks_udp_associate {
  143. my($client, $host, $port) = @_;
  144. }
复制代码
过段时间可能会取消签到功能了
您需要登录后才可以回帖 登录 | Join BUC

本版积分规则

Powered by Discuz!

© 2012-2015 Baiker Union of China.

快速回复 返回顶部 返回列表