forked from amphp/http-tunnel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-client-via-socks5.php
65 lines (51 loc) · 2.12 KB
/
http-client-via-socks5.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
use Amp\Http\Client\Connection\DefaultConnectionFactory;
use Amp\Http\Client\Connection\UnlimitedConnectionPool;
use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\HttpException;
use Amp\Http\Client\Request;
use Amp\Http\Http1\Rfc7230;
use Amp\Http\Tunnel\Http1TunnelConnector;
use Amp\Http\Tunnel\Socks5TunnelConnector;
require __DIR__ . '/../vendor/autoload.php';
try {
// If you need authentication, you can set a custom header (using Basic auth here)
// $connector = new Http1TunnelConnector(new SocketAddress('127.0.0.1', 5512), [
// 'proxy-authorization' => 'Basic ' . \base64_encode('user:pass'),
// ]);
// If you have a proxy accepting HTTPS connections, you need to use Https1TunnelConnector instead:
// $connector = new Https1TunnelConnector(new SocketAddress('proxy.example.com', 5512));
$socketConnector = new Socks5TunnelConnector('127.0.0.1:5512');
$client = (new HttpClientBuilder)
->usingPool(new UnlimitedConnectionPool(new DefaultConnectionFactory($socketConnector)))
->build();
$request = new Request('http://amphp.org/');
$response = $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 = $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) {
// If something goes wrong Amp will throw the exception where the promise was yielded.
// The HttpClient::request() method itself will never throw directly, but returns a promise.
echo $error;
}