diff --git a/README.md b/README.md index 5c2c6ea..1339c74 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,8 @@ Asynchronous http client for PHP based on workerman. - Support Http proxy. +- Support Socks5 proxy. (include authenticated) + - Parallel Request. (use 'revolt/event-loop') # Installation @@ -106,7 +108,9 @@ $worker->onWorkerStart = function(){ $http->request('https://example.com/', [ 'method' => 'GET', 'proxy' => 'http://127.0.0.1:1080', + // 'proxy' => 'http://user:pass@127.0.0.1:1080', // 'proxy' => 'socks5://127.0.0.1:1081', + // 'proxy' => 'socks5://username:password@127.0.0.1:1081', 'success' => function ($response) { echo $response->getBody(); }, diff --git a/src/ProxyHelper.php b/src/ProxyHelper.php index 652179c..23ffd24 100644 --- a/src/ProxyHelper.php +++ b/src/ProxyHelper.php @@ -16,6 +16,8 @@ public static function setConnectionProxy(AsyncTcpConnection $connection, array if ($proxy['scheme'] === 'socks5') { $connection->proxySocks5 = $proxy['connect_endpoint']; + $connection->proxySocks5Username = $proxy['username']; + $connection->proxySocks5Password = $proxy['password']; return; } @@ -52,7 +54,7 @@ public static function parseProxy(string $proxyString): ?array $proxy = parse_url($proxyString); if (!$proxy || empty($proxy['host'])) { throw new InvalidArgumentException( - "Invalid proxy url: $proxyString. Expected formats like http://user:pass@host:port or socks5://host:port" + "Invalid proxy url: $proxyString. Expected formats like http://user:pass@host:port, socks5://host:port or socks5://user:pass@host:port" ); } @@ -67,10 +69,6 @@ public static function parseProxy(string $proxyString): ?array throw new InvalidArgumentException("Unsupported proxy scheme: $scheme"); } - if ($scheme === 'socks5' && (isset($proxy['user']) || isset($proxy['pass']))) { - throw new InvalidArgumentException('Authenticated socks5 proxies are not supported by this client.'); - } - $host = $proxy['host']; $port = $proxy['port'] ?? self::defaultPortForScheme($scheme); $user = rawurldecode((string)($proxy['user'] ?? '')); @@ -82,6 +80,8 @@ public static function parseProxy(string $proxyString): ?array 'port' => $port, 'connect_endpoint' => self::buildEndpoint(self::resolveHost($host), $port), 'authorization_header' => ($user !== '' || $pass !== '') ? 'Basic ' . base64_encode($user . ':' . $pass) : '', + 'username' => $user, + 'password' => $pass, ]; }