服务器之家

服务器之家 > 正文

Perl实现的Linux下socket代理服务器

时间:2020-06-19 10:37     来源/作者:脚本之家

大家提供了许多linux开代理的方法,一般用到python等语言,一些服务器可能不会安装,然而perl可以说是linux标配的语言,给大家一款Perl语言的socket代理,代码非常少,而且还支持密码,效果还是不错,感觉很稳定。

?
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
168
169
170
171
172
173
174
175
176
177
#!/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) = @_;
}

相关文章

热门资讯

歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意 2019-07-07
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
返回顶部