繁体   English   中英

具有使用代理的 AMPHP HTTP 客户端

[英]AMPHP HTTP Client with usage proxies

我正在尝试将 AMPHP HTTP-Client 与代理一起使用,但我无法使其工作。

我正在使用他们 GitHub 中的示例。 https://github.com/amphp/http-tunnel/blob/master/examples/http-client-via-proxy.php

我必须下载 10 个 URL 并为每个 URL 使用不同的代理。 当前的问题是它返回这种错误:

TLS negotiation failed: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:
  error:1408F10B:SSL routines:ssl3_get_record:wrong version number

我们的代理服务器使用证书 (.crx) 进行操作。 我不需要检查 SSL 是否有效,我只想跳过验证,所以我认为这些行可以满足我的需要(跳过验证),但它们没有......

$clientTlsContext = new ClientTlsContext('');
$clientTlsContext->withoutPeerVerification();
$clientTlsContext->withSecurityLevel(0);

这适用于 curl:

curl_setopt($curlResource, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curlResource, CURLOPT_SSL_VERIFYHOST, 0);

这是我的代码:

class AMPHPDownloaderTest
{
    /**
     * @param ConfigWithCallback[] $configsWithCallback
     */
    public static function downSerps($configsWithCallback): void
    {
        Loop::run(static function () use ($configsWithCallback) {
            try {
                $clientTlsContext = new ClientTlsContext('');
                $clientTlsContext->withoutPeerVerification();
                $clientTlsContext->withSecurityLevel(0);

                $connector = new Https1TunnelConnector(new SocketAddress('proxyi2.infatica.io', 44123), $clientTlsContext);

                $client = (new HttpClientBuilder)
                    ->usingPool(new UnlimitedConnectionPool(new DefaultConnectionFactory($connector)))
                    ->build();

                $request = new Request('http://amphp.org/');

                /** @var Response $response */
                $response = yield $client->request($request);

                $request = $response->getRequest();

                \printf(
                    "%s %s HTTP/%s\r\n",
                    $request->getMethod(),
                    $request->getUri(),
                    \implode('+', $request->getProtocolVersions())
                );

                print Rfc7230::formatHeaders($request->getHeaders()) . "\r\n\r\n";

                \printf(
                    "HTTP/%s %d %s\r\n",
                    $response->getProtocolVersion(),
                    $response->getStatus(),
                    $response->getReason()
                );

                print Rfc7230::formatHeaders($response->getHeaders()) . "\r\n\r\n";

                $body = yield $response->getBody()->buffer();
                $bodyLength = \strlen($body);

                if ($bodyLength < 250) {
                    print $body . "\r\n";
                } else {
                    print \substr($body, 0, 250) . "\r\n\r\n";
                    print($bodyLength - 250) . " more bytes\r\n";
                }
            } catch (HttpException $error) {
                echo $error;
            }
        });
    }
}

当与 Http1TunnelConnector 而不是 Https1TunnelConnector 一起使用时,它会引发此错误:

Amp\Socket\TlsException: TLS negotiation failed: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:
error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed

您基本上是在做正确的事情,但是ClientTlsContext是不可变的,并且始终返回一个新实例,该实例在您的代码示例中被丢弃。

$clientTlsContext = (new ClientTlsContext(''))
    ->withoutPeerVerification()
    ->withSecurityLevel(0);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM