Perl实现的Linux下socket代理服务器
作者:junjie 时间:2023-04-28 10:57:41
大家提供了许多linux开代理的方法,一般用到python等语言,一些服务器可能不会安装,然而perl可以说是linux标配的语言,给大家一款Perl语言的socket代理,代码非常少,而且还支持密码,效果还是不错,感觉很稳定。
#!/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_STREAM);
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($client, $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 (my $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) = @_;
}
标签:Perl,socket,代理服务器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python线程编程之Thread详解
2022-04-10 17:37:01
![](https://img.aspxhome.com/file/2023/7/123527_0s.png)
微信小程序使用自定义组件导航实现当前页面高亮
2024-04-22 12:50:16
![](https://img.aspxhome.com/file/2023/6/136026_0s.png)
python3实现基于用户的协同过滤
2023-11-17 01:39:47
关于Python Selenium自动化导出新版WOS(web of science)检索结果的问题
2022-12-15 02:20:56
![](https://img.aspxhome.com/file/2023/4/112994_0s.jpg)
在sqlserver2005中安装sql server 2000的示例数据库northwind的方法
2024-01-18 11:00:12
浅谈Python数据类型判断及列表脚本操作
2021-02-16 07:56:14
![](https://img.aspxhome.com/file/2023/9/124359_0s.jpg)
关联的 script 标签
2009-11-02 10:40:00
Python提示[Errno 32]Broken pipe导致线程crash错误解决方法
2022-12-02 02:20:42
MySQL选错索引的原因以及解决方案
2024-01-19 21:34:34
![](https://img.aspxhome.com/file/2023/8/102388_0s.png)
python实现simhash算法实例
2023-11-02 23:26:30
![](https://img.aspxhome.com/file/2023/1/97751_0s.jpg)
Python基础之操作MySQL数据库
2024-01-23 01:50:14
![](https://img.aspxhome.com/file/2023/7/117317_0s.png)
Python应用实现双指数函数及拟合代码实例
2023-04-14 18:17:14
![](https://img.aspxhome.com/file/2023/0/95270_0s.png)
Python小白垃圾回收机制入门
2022-05-20 04:38:42
基于Python实现音乐播放器的实现示例代码
2023-11-26 23:58:50
MySQL的时间差函数TIMESTAMPDIFF、DATEDIFF的用法
2024-01-25 04:39:33
php 仿Comsenz安装效果代码打包提供下载
2024-05-11 09:46:37
![](https://img.aspxhome.com/file/2023/6/125866_0s.jpg)
Python第三方库face_recognition在windows上的安装过程
2023-07-27 02:51:29
如何使用python的ctypes调用医保中心的dll动态库下载医保中心的账单
2022-08-02 13:14:35
![](https://img.aspxhome.com/file/2023/8/95678_0s.jpg)
Go语言使用select{}阻塞main函数介绍
2024-04-30 10:06:32
![](https://img.aspxhome.com/file/2023/2/130692_0s.png)
SQL 将一列拆分成多列的三种方法
2024-01-23 15:30:18
![](https://img.aspxhome.com/file/2023/4/98004_0s.jpg)