Skip to content

Commit c64bc0d

Browse files
authored
Merge pull request #98 from clue-labs/readme
Improve code examples and documentation
2 parents 5cc99a1 + 2e49795 commit c64bc0d

11 files changed

+214
-97
lines changed

README.md

Lines changed: 149 additions & 49 deletions
Large diffs are not rendered by default.

examples/01-https-request.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222
}
2323

2424
$loop = React\EventLoop\Factory::create();
25-
$client = new Clue\React\Socks\Client($url, new React\Socket\Connector($loop));
25+
$proxy = new Clue\React\Socks\Client(
26+
$url,
27+
new React\Socket\Connector($loop)
28+
);
2629

2730
$connector = new React\Socket\Connector($loop, array(
28-
'tcp' => $client,
31+
'tcp' => $proxy,
2932
'timeout' => 3.0,
3033
'dns' => false
3134
));
@@ -37,5 +40,5 @@
3740
}, function (Exception $e) {
3841
echo 'Error: ' . $e->getMessage() . PHP_EOL;
3942
});
40-
43+
4144
$loop->run();

examples/02-optional-proxy-https-request.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
$connector = null;
2121
$url = getenv('socks_proxy');
2222
if ($url !== false) {
23-
$connector = new React\Socket\Connector($loop);
24-
$client = new Clue\React\Socks\Client($url, $connector);
23+
$proxy = new Clue\React\Socks\Client(
24+
$url,
25+
new React\Socket\Connector($loop)
26+
);
2527
$connector = new React\Socket\Connector($loop, array(
26-
'tcp' => $client,
28+
'tcp' => $proxy,
2729
'timeout' => 3.0,
2830
'dns' => false
2931
));

examples/11-proxy-raw-http-protocol.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,22 @@
2626

2727
$loop = React\EventLoop\Factory::create();
2828

29-
$client = new Clue\React\Socks\Client($url, new React\Socket\Connector($loop));
29+
$proxy = new Clue\React\Socks\Client(
30+
$url,
31+
new React\Socket\Connector($loop)
32+
);
3033
$connector = new React\Socket\Connector($loop, array(
31-
'tcp' => $client,
34+
'tcp' => $proxy,
3235
'timeout' => 3.0,
3336
'dns' => false
3437
));
3538

3639
echo 'Demo SOCKS client connecting to SOCKS server ' . $url . PHP_EOL;
3740

38-
$connector->connect('tcp://www.google.com:80')->then(function (React\Socket\ConnectionInterface $stream) {
41+
$connector->connect('tcp://www.google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {
3942
echo 'connected' . PHP_EOL;
40-
$stream->write("GET / HTTP/1.0\r\n\r\n");
41-
$stream->on('data', function ($data) {
43+
$connection->write("GET / HTTP/1.0\r\n\r\n");
44+
$connection->on('data', function ($data) {
4245
echo $data;
4346
});
4447
}, function (Exception $e) {

examples/12-optional-proxy-raw-http-protocol.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@
2828

2929
$url = getenv('socks_proxy');
3030
if ($url !== false) {
31-
$client = new Clue\React\Socks\Client($url, $connector);
31+
$proxy = new Clue\React\Socks\Client($url, $connector);
3232
$connector = new React\Socket\Connector($loop, array(
33-
'tcp' => $client,
33+
'tcp' => $proxy,
3434
'timeout' => 3.0,
3535
'dns' => false
3636
));
3737
}
3838

3939
echo 'Demo SOCKS client connecting to SOCKS server ' . $url . PHP_EOL;
4040

41-
$connector->connect('tcp://www.google.com:80')->then(function (React\Socket\ConnectionInterface $stream) {
41+
$connector->connect('tcp://www.google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {
4242
echo 'connected' . PHP_EOL;
43-
$stream->write("GET / HTTP/1.0\r\n\r\n");
44-
$stream->on('data', function ($data) {
43+
$connection->write("GET / HTTP/1.0\r\n\r\n");
44+
$connection->on('data', function ($data) {
4545
echo $data;
4646
});
4747
}, function (Exception $e) {

examples/13-proxy-raw-https-protocol.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,22 @@
2626

2727
$loop = React\EventLoop\Factory::create();
2828

29-
$client = new Clue\React\Socks\Client($url, new React\Socket\Connector($loop));
29+
$proxy = new Clue\React\Socks\Client(
30+
$url,
31+
new React\Socket\Connector($loop)
32+
);
3033
$connector = new React\Socket\Connector($loop, array(
31-
'tcp' => $client,
34+
'tcp' => $proxy,
3235
'timeout' => 3.0,
3336
'dns' => false
3437
));
3538

3639
echo 'Demo SOCKS client connecting to SOCKS server ' . $url . PHP_EOL;
3740

38-
$connector->connect('tls://www.google.com:443')->then(function (React\Socket\ConnectionInterface $stream) {
41+
$connector->connect('tls://www.google.com:443')->then(function (React\Socket\ConnectionInterface $connection) {
3942
echo 'connected' . PHP_EOL;
40-
$stream->write("GET / HTTP/1.0\r\n\r\n");
41-
$stream->on('data', function ($data) {
43+
$connection->write("GET / HTTP/1.0\r\n\r\n");
44+
$connection->on('data', function ($data) {
4245
echo $data;
4346
});
4447
}, function (Exception $e) {

examples/14-optional-proxy-raw-https-protocol.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@
2828

2929
$url = getenv('socks_proxy');
3030
if ($url !== false) {
31-
$client = new Clue\React\Socks\Client($url, $connector);
31+
$proxy = new Clue\React\Socks\Client($url, $connector);
3232
$connector = new React\Socket\Connector($loop, array(
33-
'tcp' => $client,
33+
'tcp' => $proxy,
3434
'timeout' => 3.0,
3535
'dns' => false
3636
));
3737
}
3838

3939
echo 'Demo SOCKS client connecting to SOCKS server ' . $url . PHP_EOL;
4040

41-
$connector->connect('tls://www.google.com:443')->then(function (React\Socket\ConnectionInterface $stream) {
41+
$connector->connect('tls://www.google.com:443')->then(function (React\Socket\ConnectionInterface $connection) {
4242
echo 'connected' . PHP_EOL;
43-
$stream->write("GET / HTTP/1.0\r\n\r\n");
44-
$stream->on('data', function ($data) {
43+
$connection->write("GET / HTTP/1.0\r\n\r\n");
44+
$connection->on('data', function ($data) {
4545
echo $data;
4646
});
4747
}, function (Exception $e) {

examples/15-proxy-chaining.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040

4141
echo 'Demo SOCKS client connecting to SOCKS proxy server chain ' . implode(' -> ', $path) . PHP_EOL;
4242

43-
$connector->connect('tls://www.google.com:443')->then(function (React\Socket\ConnectionInterface $stream) {
43+
$connector->connect('tls://www.google.com:443')->then(function (React\Socket\ConnectionInterface $connection) {
4444
echo 'connected' . PHP_EOL;
45-
$stream->write("GET / HTTP/1.0\r\n\r\n");
46-
$stream->on('data', function ($data) {
45+
$connection->write("GET / HTTP/1.0\r\n\r\n");
46+
$connection->on('data', function ($data) {
4747
echo $data;
4848
});
4949
}, function (Exception $e) {

examples/16-local-dns.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,35 @@
33
// A simple example which requests https://www.google.com/ through a SOCKS proxy with local DNS resolution.
44
// The proxy can be given as first argument and defaults to localhost:1080 otherwise.
55
//
6-
// Not already running a SOCKS proxy server? See also example #21 or try this:
6+
// Not already running a SOCKS proxy server? See also example #21 or try this:
77
// $ ssh -D 1080 localhost
88
//
99
// For illustration purposes only. If you want to send HTTP requests in a real
1010
// world project, take a look at example #01, example #02 and https://github.com/reactphp/http#client-usage.
1111

1212
require __DIR__ . '/../vendor/autoload.php';
1313

14-
$proxy = isset($argv[1]) ? $argv[1] : '127.0.0.1:1080';
14+
$url = isset($argv[1]) ? $argv[1] : '127.0.0.1:1080';
1515

1616
$loop = React\EventLoop\Factory::create();
1717

1818
// set up DNS server to use (Google's public DNS)
19-
$client = new Clue\React\Socks\Client($proxy, new React\Socket\Connector($loop));
19+
$proxy = new Clue\React\Socks\Client(
20+
$url,
21+
new React\Socket\Connector($loop)
22+
);
2023
$connector = new React\Socket\Connector($loop, array(
21-
'tcp' => $client,
24+
'tcp' => $proxy,
2225
'timeout' => 3.0,
2326
'dns' => '8.8.8.8'
2427
));
2528

2629
echo 'Demo SOCKS client connecting to SOCKS server ' . $proxy . PHP_EOL;
2730

28-
$connector->connect('tls://www.google.com:443')->then(function (React\Socket\ConnectionInterface $stream) {
31+
$connector->connect('tls://www.google.com:443')->then(function (React\Socket\ConnectionInterface $connection) {
2932
echo 'connected' . PHP_EOL;
30-
$stream->write("GET / HTTP/1.0\r\n\r\n");
31-
$stream->on('data', function ($data) {
33+
$connection->write("GET / HTTP/1.0\r\n\r\n");
34+
$connection->on('data', function ($data) {
3235
echo $data;
3336
});
3437
}, function (Exception $e) {

examples/32-server-proxy-chaining-from-random-pool.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
// forward to socks server listening on 127.0.0.1:9051-9053
2828
// this connector randomly picks one of the the attached connectors from the pool
2929
$connector = new React\Socket\Connector($loop);
30-
$clients = array();
30+
$proxies = array();
3131
foreach ($pool as $proxy) {
32-
$clients []= new Clue\React\Socks\Client($proxy, $connector);
32+
$proxies []= new Clue\React\Socks\Client($proxy, $connector);
3333
}
34-
$connector = new ConnectionManager\Extra\Multiple\ConnectionManagerRandom($clients);
34+
$connector = new ConnectionManager\Extra\Multiple\ConnectionManagerRandom($proxies);
3535

3636
// start the SOCKS proxy server using our connection manager for outgoing connections
3737
$server = new Clue\React\Socks\Server($loop, $socket, $connector);

0 commit comments

Comments
 (0)