100多行PHP代码实现socks5代理服务器,这次是使用swoole纯异步来写,使用状态机来处理数据。目前用它访问开源中国木有压力,但访问网易新闻就压力山大。我发现我用别的语言写得代理,访问网易新闻都压力大。嘎嘎,学艺不精。
对swoole理解不深,不知道怎么处理socket shutdown只关闭读/写这样,还有就是连接超时,读写超时这种怎么处理。在网上看到作者说要用定时器,感觉好麻烦,所以,这次的代理,虽然个人用,一般不会有什么问题,但离产品级的代理,还有段路要走。
如果要利用多核,就使用process模式,设置worker个数为cpu数量即可。
<?php class Client { public $connected = true; public $data = ''; public $remote = null; public $status = 0; } class Server { public $clients = []; public function start() { $server = new swoole_server('0.0.0.0', 8388, SWOOLE_BASE, SWOOLE_SOCK_TCP); $server->set([ 'max_conn' => 1000, 'daemonize' => 1, 'reactor_num' => 1, 'worker_num' => 1, 'dispatch_mode' => 2, 'buffer_output_size' => 128 * 1024 * 1024, 'open_cpu_affinity' => 1, 'open_tcp_nodelay' => 1, 'log_file' => 'socks5_server.log', ]); $server->on('connect', [$this, 'onConnect']); $server->on('receive', [$this, 'onReceive']); $server->on('close', [$this, 'onClose']); $server->start(); } public function onConnect($server, $fd, $fromID) { $this->clients[$fd] = new Client(); } public function onReceive($server, $fd, $fromID, $data) { ($this->clients[$fd])->data .= $data; $this->parse($server, $fd); } public function onClose($server, $fd, $fromID) { $client = $this->clients[$fd]; $client->connected = false; } private function parse($server, $fd) { $client = $this->clients[$fd]; switch ($client->status) { case 0: { if (strlen($client->data) >= 2) { $request = unpack('c*', substr($client->data, 0, 2)); if ($request[1] !== 0x05) { echo '协议不正确:' . $request[1], PHP_EOL; $server->close($fd); break; } $nmethods = $request[2]; if (strlen($client->data) >= 2 + $nmethods) { $client->data = substr($client->data, 2 + $nmethods); $server->send($fd, "\x05\x00"); $client->status = 1; } } } case 1: { if (strlen($client->data) < 5) break; $request = unpack('c*', $client->data); $aType = $request[4]; if ($aType === 0x03) { // domain $domainLen = $request[5]; if (strlen($client->data) < 5 + $domainLen + 2) { break; } $domain = substr($client->data, 5, $domainLen); $port = unpack('n', substr($client->data, 5 + $domainLen, 2))[1]; $client->data = substr($client->data, 5 + $domainLen + 2); } else if ($aType === 0x01) { // ipv4 $domain = long2ip(unpack('N', substr($client->data, 4, 4))[1]); $port = unpack('n', substr($client->data, 8, 2))[1]; $client->data = substr($client->data, 10); } else { echo '不支持的atype:' . $aType, PHP_EOL; $server->close($fd); break; } $remote = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); $remote->on('connect', function($cli) use($client, $server, $fd, $remote) { $server->send($fd, "\x05\x00\x00\x01\x00\x00\x00\x00\x00\x00"); $client->status = 2; $client->remote = $remote; }); $remote->on("error", function(swoole_client $cli) use($server, $fd) { //$server->send($fd, ""); // todo 连接不上remote echo 'connect to remote error.', PHP_EOL; $server->close($fd); }); $remote->on('receive', function($cli, $data) use($server, $fd, $client) { if (!$client->connected) { echo 'connection has been closed.', PHP_EOL; return; } $server->send($fd, $data); }); $remote->on('close', function($cli) use($server, $fd, $client) { $client->remote = null; }); if ($aType === 0x03) { swoole_async_dns_lookup($domain, function($host, $ip) use($remote, $port, $server, $fd) { //todo 当host为空时的处理。貌似不存在的域名都解析成了本机的外网ip,奇怪 if (empty($ip) || empty($host)) { echo "host:{$host}, ip:{$ip}\n"; $server->close($fd); return; } $remote->connect($ip, $port); }); } else { $remote->connect($domain, $port); } } case 2: { if (strlen($client->data) === 0) { break; } if ($client->remote === null) { echo 'remote connection has been closed.', PHP_EOL; break; } $sendByteCount = $client->remote->send($client->data); if ($sendByteCount === false || $sendByteCount < strlen($client->data)) { echo 'data length:' , strlen($client->data), ' send byte count:', $sendByteCount, PHP_EOL; echo $client->data, PHP_EOL; $server->close($fd); } $client->data = ''; } } } } (new Server())->start();

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Dreamweaver CS6
视觉化网页开发工具

WebStorm Mac版
好用的JavaScript开发工具