phprivoxy/proxy

HTTP/HTTPS 代理构建的核心库。

v0.8.10 2024-07-02 12:51 UTC

This package is auto-updated.

Last update: 2024-09-02 13:08:51 UTC


README

HTTP/HTTPS 代理构建的核心库。

此基于Workerman框架(https://github.com/walkor/workerman)的PHP包,将有助于创建自定义代理服务器。

要求

  • PHP >= 8.1

安装

使用composer(推荐)

composer create phprivoxy/proxy

简单的透明代理示例

$handler = new PHPrivoxy\Proxy\Transparent();
new PHPrivoxy\Core\Server($handler);// By default, it listen all connections on 8080 port.

将浏览器配置为通过IP地址为127.0.0.1、端口号为8080的代理服务器工作。

尝试通过HTTP或HTTPS协议打开任何网站。例如,尝试打开https://php.ac.cnhttps://google.comhttps://microsoft.com

此示例也位于"tests"目录中。

只需运行它

php tests/transparent.php start

简单的SSL MITM(中间人)代理示例

use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

class HttpClientMiddleware implements MiddlewareInterface
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $client = new GuzzleHttp\Client();
        try {
            $response = $client->send($request, ['allow_redirects' => false]);
        } catch (GuzzleHttp\Exception\ConnectException $e) {
            // Do something
        } catch (GuzzleHttp\Exception\BadResponseException $e) {
            return $e->getResponse();
        }

        return $response;
    }
}

$httpClientMiddleware = new HttpClientMiddleware();
$queue = [$httpClientMiddleware];
$psr15handler = new Relay\Relay($queue);
$tcpHandler = new PHPrivoxy\Proxy\MITM($psr15handler);
$processes = 6; // Default 1.

new PHPrivoxy\Core\Server($tcpHandler, $processes);// By default, it listen all connections on 8080 port.

此示例也位于"tests"目录中。

只需运行它

php tests/mitm.php start

首次运行时,在CA子目录中创建一个自签名的SSL根证书。请将此自签名CA证书添加到浏览器受信任的证书!

对于每个网站,PHPrivoxy\Proxy\MITM将在"certificates"子目录中生成自签名证书。

在此示例中,我们使用简单的PSR-15兼容HttpClientMiddleware进行网站下载。您也可以添加自己的PSR-15兼容Middleware到队列中,以供PSR-15处理器使用(此示例中修改了Relay\Relay)。

许可

MIT许可 请参阅LICENSE