Perl實(shí)現(xiàn)的Linux下socket代理服務(wù)器
來(lái)源:易賢網(wǎng) 閱讀:714 次 日期:2014-09-25 11:59:35
溫馨提示:易賢網(wǎng)小編為您整理了“Perl實(shí)現(xiàn)的Linux下socket代理服務(wù)器”,方便廣大網(wǎng)友查閱!

大家提供了許多l(xiāng)inux開(kāi)代理的方法,一般用到python等語(yǔ)言,一些服務(wù)器可能不會(huì)安裝,然而perl可以說(shuō)是linux標(biāo)配的語(yǔ)言,給大家一款Perl語(yǔ)言的socket代理,代碼非常少,而且還支持密碼,效果還是不錯(cuò),感覺(jué)很穩(wěn)定。

?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 #!/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) = @_; }

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看腳本欄目
易賢網(wǎng)手機(jī)網(wǎng)站地址:Perl實(shí)現(xiàn)的Linux下socket代理服務(wù)器
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽(tīng)報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)