Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
},
Expand Down
10 changes: 5 additions & 5 deletions src/ProxyHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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"
);
}

Expand All @@ -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'] ?? ''));
Expand All @@ -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,
];
}

Expand Down