Skip to content

Commit 2e49795

Browse files
committed
Improve example code to be more consistent
1 parent 9d0e726 commit 2e49795

10 files changed

+140
-60
lines changed

README.md

Lines changed: 96 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ to google.com via a local SOCKS proxy server:
8484
```php
8585
$loop = React\EventLoop\Factory::create();
8686
$connector = new React\Socket\Connector($loop);
87-
$client = new Clue\React\Socks\Client('127.0.0.1:1080', $connector);
87+
$proxy = new Clue\React\Socks\Client('127.0.0.1:1080', $connector);
8888

89-
$client->connect('tcp://www.google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {
89+
$proxy->connect('tcp://www.google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {
9090
$connection->write("GET / HTTP/1.0\r\n\r\n");
9191

9292
$connection->on('data', function ($chunk) {
@@ -130,13 +130,13 @@ like this:
130130

131131
```php
132132
$connector = new React\Socket\Connector($loop);
133-
$client = new Clue\React\Socks\Client('127.0.0.1:1080', $connector);
133+
$proxy = new Clue\React\Socks\Client('127.0.0.1:1080', $connector);
134134
```
135135

136136
You can omit the port if you're using the default SOCKS port 1080:
137137

138138
```php
139-
$client = new Clue\React\Socks\Client('127.0.0.1', $connector);
139+
$proxy = new Clue\React\Socks\Client('127.0.0.1', $connector);
140140
```
141141

142142
If you need custom connector settings (DNS resolution, TLS parameters, timeouts,
@@ -151,7 +151,7 @@ $connector = new React\Socket\Connector($loop, array(
151151
)
152152
));
153153

154-
$client = new Clue\React\Socks\Client('my-socks-server.local:1080', $connector);
154+
$proxy = new Clue\React\Socks\Client('my-socks-server.local:1080', $connector);
155155
```
156156

157157
This is one of the two main classes in this package.
@@ -169,9 +169,9 @@ This makes it fairly simple to add SOCKS proxy support to pretty much any
169169
higher-level component:
170170

171171
```diff
172-
- $client = new SomeClient($connector);
172+
- $acme = new AcmeApi($connector);
173173
+ $proxy = new Clue\React\Socks\Client('127.0.0.1:1080', $connector);
174-
+ $client = new SomeClient($proxy);
174+
+ $acme = new AcmeApi($proxy);
175175
```
176176

177177
#### Plain TCP connections
@@ -183,7 +183,12 @@ As documented above, you can simply invoke its `connect()` method to establish
183183
a streaming plain TCP/IP connection and use any higher level protocol like so:
184184

185185
```php
186-
$client->connect('tcp://www.google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {
186+
$proxy = new Clue\React\Socks\Client(
187+
'127.0.0.1:1080',
188+
new React\Socket\Connector($loop)
189+
);
190+
191+
$proxy->connect('tcp://www.google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {
187192
echo 'connected to www.google.com:80';
188193
$connection->write("GET / HTTP/1.0\r\n\r\n");
189194

@@ -197,8 +202,13 @@ You can either use the `Client` directly or you may want to wrap this connector
197202
in ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector):
198203

199204
```php
205+
$proxy = new Clue\React\Socks\Client(
206+
'127.0.0.1:1080',
207+
new React\Socket\Connector($loop)
208+
);
209+
200210
$connector = new React\Socket\Connector($loop, array(
201-
'tcp' => $client,
211+
'tcp' => $proxy,
202212
'dns' => false
203213
));
204214

@@ -238,8 +248,13 @@ ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector) or the
238248
low-level [`SecureConnector`](https://github.com/reactphp/socket#secureconnector):
239249

240250
```php
251+
$proxy = new Clue\React\Socks\Client(
252+
'127.0.0.1:1080',
253+
new React\Socket\Connector($loop)
254+
);
255+
241256
$connector = new React\Socket\Connector($loop, array(
242-
'tcp' => $client,
257+
'tcp' => $proxy,
243258
'dns' => false
244259
));
245260

@@ -274,7 +289,7 @@ to the constructor like this:
274289

275290
```php
276291
$connector = new React\Socket\Connector($loop, array(
277-
'tcp' => $client,
292+
'tcp' => $proxy,
278293
'tls' => array(
279294
'verify_peer' => false,
280295
'verify_peer_name' => false
@@ -294,7 +309,7 @@ This allows you to send both plain HTTP and TLS-encrypted HTTPS requests like th
294309

295310
```php
296311
$proxy = new Clue\React\Socks\Client(
297-
'socks://127.0.0.1:1080',
312+
'127.0.0.1:1080',
298313
new React\Socket\Connector($loop)
299314
);
300315

@@ -391,16 +406,16 @@ URI scheme acts as an alias for the default `socks://` URI scheme.
391406

392407
```php
393408
// all three forms are equivalent
394-
$client = new Clue\React\Socks\Client('127.0.0.1', $connector);
395-
$client = new Clue\React\Socks\Client('socks://127.0.0.1', $connector);
396-
$client = new Clue\React\Socks\Client('socks5://127.0.0.1', $connector);
409+
$proxy = new Clue\React\Socks\Client('127.0.0.1', $connector);
410+
$proxy = new Clue\React\Socks\Client('socks://127.0.0.1', $connector);
411+
$proxy = new Clue\React\Socks\Client('socks5://127.0.0.1', $connector);
397412
```
398413

399414
If want to explicitly set the protocol version to SOCKS4(a), you can use the URI
400415
scheme `socks4://` as part of the SOCKS URI:
401416

402417
```php
403-
$client = new Clue\React\Socks\Client('socks4://127.0.0.1', $connector);
418+
$proxy = new Clue\React\Socks\Client('socks4://127.0.0.1', $connector);
404419
```
405420

406421
#### DNS resolution
@@ -429,11 +444,16 @@ However, wrapping the `Client` in ReactPHP's
429444
[`Connector`](https://github.com/reactphp/socket#connector) actually
430445
performs local DNS resolution unless explicitly defined otherwise.
431446
Given that remote DNS resolution is assumed to be the preferred mode, all
432-
other examples explicitly disable DNS resoltion like this:
447+
other examples explicitly disable DNS resolution like this:
433448

434449
```php
450+
$proxy = new Clue\React\Socks\Client(
451+
'127.0.0.1:1080',
452+
new React\Socket\Connector($loop)
453+
);
454+
435455
$connector = new React\Socket\Connector($loop, array(
436-
'tcp' => $client,
456+
'tcp' => $proxy,
437457
'dns' => false
438458
));
439459
```
@@ -442,9 +462,14 @@ If you want to explicitly use *local DNS resolution* (such as when explicitly
442462
using SOCKS4), you can use the following code:
443463

444464
```php
465+
$proxy = new Clue\React\Socks\Client(
466+
'127.0.0.1:1080',
467+
new React\Socket\Connector($loop)
468+
);
469+
445470
// set up Connector which uses Google's public DNS (8.8.8.8)
446471
$connector = new React\Socket\Connector($loop, array(
447-
'tcp' => $client,
472+
'tcp' => $proxy,
448473
'dns' => '8.8.8.8'
449474
));
450475
```
@@ -475,7 +500,7 @@ so this methods should not be used on a network where you have to worry about ea
475500
You can simply pass the authentication information as part of the SOCKS URI:
476501

477502
```php
478-
$client = new Clue\React\Socks\Client('username:[email protected]', $connector);
503+
$proxy = new Clue\React\Socks\Client('username:[email protected]', $connector);
479504
```
480505

481506
Note that both the username and password must be percent-encoded if they contain
@@ -485,7 +510,7 @@ special characters:
485510
$user = 'he:llo';
486511
$pass = 'p@ss';
487512

488-
$client = new Clue\React\Socks\Client(
513+
$proxy = new Clue\React\Socks\Client(
489514
rawurlencode($user) . ':' . rawurlencode($pass) . '@127.0.0.1',
490515
$connector
491516
);
@@ -589,8 +614,13 @@ It provides the same `connect()` method, but will automatically reject the
589614
underlying connection attempt if it takes too long:
590615

591616
```php
617+
$proxy = new Clue\React\Socks\Client(
618+
'127.0.0.1:1080',
619+
new React\Socket\Connector($loop)
620+
);
621+
592622
$connector = new React\Socket\Connector($loop, array(
593-
'tcp' => $client,
623+
'tcp' => $proxy,
594624
'dns' => false,
595625
'timeout' => 3.0
596626
));
@@ -632,12 +662,12 @@ You can use the `sockss://` URI scheme or use an explicit
632662
[SOCKS protocol version](#protocol-version) like this:
633663

634664
```php
635-
$client = new Clue\React\Socks\Client(
665+
$proxy = new Clue\React\Socks\Client(
636666
'sockss://127.0.0.1:1080',
637667
new React\Socket\Connector($loop)
638668
);
639669

640-
$client = new Clue\React\Socks\Client(
670+
$proxy = new Clue\React\Socks\Client(
641671
'socks4s://127.0.0.1:1080',
642672
new React\Socket\Connector($loop)
643673
);
@@ -649,7 +679,7 @@ Similarly, you can also combine this with [authentication](#authentication)
649679
like this:
650680

651681
```php
652-
$client = new Clue\React\Socks\Client(
682+
$proxy = new Clue\React\Socks\Client(
653683
'sockss://user:[email protected]:1080',
654684
new React\Socket\Connector($loop)
655685
);
@@ -684,12 +714,12 @@ You can use the `socks+unix://` URI scheme or use an explicit
684714
[SOCKS protocol version](#protocol-version) like this:
685715

686716
```php
687-
$client = new Clue\React\Socks\Client(
717+
$proxy = new Clue\React\Socks\Client(
688718
'socks+unix:///tmp/proxy.sock'
689719
new React\Socket\Connector($loop)
690720
);
691721

692-
$client = new Clue\React\Socks\Client(
722+
$proxy = new Clue\React\Socks\Client(
693723
'socks4+unix:///tmp/proxy.sock',
694724
new React\Socket\Connector($loop)
695725
);
@@ -699,7 +729,7 @@ Similarly, you can also combine this with [authentication](#authentication)
699729
like this:
700730

701731
```php
702-
$client = new Clue\React\Socks\Client(
732+
$proxy = new Clue\React\Socks\Client(
703733
'socks+unix://user:pass@/tmp/proxy.sock',
704734
new React\Socket\Connector($loop)
705735
);
@@ -865,10 +895,10 @@ $loop = React\EventLoop\Factory::create();
865895

866896
// set next SOCKS server example.com:1080 as target
867897
$connector = new React\Socket\Connector($loop);
868-
$client = new Clue\React\Socks\Client('user:[email protected]:1080', $connector);
898+
$proxy = new Clue\React\Socks\Client('user:[email protected]:1080', $connector);
869899

870900
// start a new server which forwards all connections to the other SOCKS server
871-
$server = new Clue\React\Socks\Server($loop, $client);
901+
$server = new Clue\React\Socks\Server($loop, $proxy);
872902

873903
// listen on localhost:1080
874904
$socket = new React\Socket\Server('127.0.0.1:1080', $loop);
@@ -1005,7 +1035,18 @@ $ ssh -D 1080 example.com
10051035
Now you can simply use this SSH SOCKS server like this:
10061036

10071037
```PHP
1008-
$client = new Clue\React\Socks\Client('127.0.0.1:1080', $connector);
1038+
$proxy = new Clue\React\Socks\Client(
1039+
'127.0.0.1:1080',
1040+
new React\Socket\Connector($loop)
1041+
);
1042+
1043+
$proxy->connect('tcp://www.google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {
1044+
$connection->write("GET / HTTP/1.0\r\n\r\n");
1045+
1046+
$connection->on('data', function ($chunk) {
1047+
echo $chunk;
1048+
});
1049+
});
10091050
```
10101051

10111052
Note that the above will allow all users on the local system to connect over
@@ -1021,7 +1062,18 @@ $ ssh -D/tmp/proxy.sock example.com
10211062
Now you can simply use this SSH SOCKS server like this:
10221063

10231064
```PHP
1024-
$client = new Clue\React\Socks\Client('socks+unix:///tmp/proxy.sock', $connector);
1065+
$proxy = new Clue\React\Socks\Client(
1066+
'socks+unix:///tmp/proxy.sock',
1067+
new React\Socket\Connector($loop)
1068+
);
1069+
1070+
$proxy->connect('tcp://www.google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {
1071+
$connection->write("GET / HTTP/1.0\r\n\r\n");
1072+
1073+
$connection->on('data', function ($chunk) {
1074+
echo $chunk;
1075+
});
1076+
});
10251077
```
10261078

10271079
> As an alternative to requiring this manual setup, you may also want to look
@@ -1038,7 +1090,18 @@ It presents a SOCKS5 and SOCKS4(a) interface on TCP port 9050 by default
10381090
which allows you to tunnel any traffic through the anonymity network:
10391091

10401092
```php
1041-
$client = new Clue\React\Socks\Client('127.0.0.1:9050', $connector);
1093+
$proxy = new Clue\React\Socks\Client(
1094+
'127.0.0.1:9050',
1095+
new React\Socket\Connector($loop)
1096+
);
1097+
1098+
$proxy->connect('tcp://www.google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {
1099+
$connection->write("GET / HTTP/1.0\r\n\r\n");
1100+
1101+
$connection->on('data', function ($chunk) {
1102+
echo $chunk;
1103+
});
1104+
});
10421105
```
10431106

10441107
In most common scenarios you probably want to stick to 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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
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
));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
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
));

0 commit comments

Comments
 (0)