本篇文章为大家展示了使用PHP怎么编写一个socks5代理服务器,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
Class Pipe extends Threaded
{
private $client;
private $remote;
public function __construct($client, $remote)
{
$this->client = $client;
$this->remote = $remote;
}
public function run()
{
for ( ; ; ) {
$data = stream_socket_recvfrom($this->client, 4096);
if ($data === false || strlen($data) === 0) {
break;
}
$sendBytes = stream_socket_sendto($this->remote, $data);
if ($sendBytes <= 0) {
break;
}
}
stream_socket_shutdown($this->client, STREAM_SHUT_RD);
stream_socket_shutdown($this->remote, STREAM_SHUT_WR);
}
}
Class Client extends Threaded
{
public $fd;
public function __construct($fd)
{
$this->fd = $fd;
}
public function run()
{
$data = stream_socket_recvfrom($this->fd, 2);
$data = unpack('c*', $data);
if ($data[1] !== 0x05) {
stream_socket_shutdown($this->fd, STREAM_SHUT_RDWR);
echo '协议不正确.', PHP_EOL;
return;
}
$nmethods = $data[2];
$data = stream_socket_recvfrom($this->fd, $nmethods);
stream_socket_sendto($this->fd, "\x05\x00");
$data = stream_socket_recvfrom($this->fd, 4);
$data = unpack('c*', $data);
$addressType = $data[4];
if ($addressType === 0x03) { // domain
$domainLength = unpack('c', stream_socket_recvfrom($this->fd, 1))[1];
$data = stream_socket_recvfrom($this->fd, $domainLength + 2);
$domain = substr($data, 0, $domainLength);
$port = unpack("n", substr($data, -2))[1];
} else {
stream_socket_shutdown($this->fd, STREAM_SHUT_RDWR);
echo '请使用远程dns解析.', PHP_EOL;
}
stream_socket_sendto($this->fd, "\x05\x00\x00\x01\x00\x00\x00\x00\x00\x00");
echo "{$domain}:{$port}", PHP_EOL;
$remote = stream_socket_client("tcp://{$domain}:{$port}");
if ($remote === false) {
stream_socket_shutdown($this->fd, STREAM_SHUT_RDWR);
return;
}
$pool = $this->worker->pipePool;
$pipe1 = new Pipe($remote, $this->fd);
$pipe2 = new Pipe($this->fd, $remote);
$pool->submit($pipe1);
$pool->submit($pipe2);
}
}
class ProxyWorker extends Worker
{
public $pipePool;
public function __construct($pipePool)
{
$this->pipePool = $pipePool;
}
}
$server = stream_socket_server('tcp://0.0.0.0:1080', $errno, $errstr);
if ($server === false)
exit($errstr);
$pipePool = new Pool(200, Worker::class);
$pool = new Pool(50, 'ProxyWorker', [$pipePool]);
for( ; ; ) {
$fd = @stream_socket_accept($server, 60);
if ($fd === false)
continue;
$pool->submit(new Client($fd));
}
上述内容就是使用PHP怎么编写一个socks5代理服务器,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。