Skip to content

Commit 8d3feee

Browse files
committed
Simplify usage by making Connector optional
1 parent fd0691e commit 8d3feee

11 files changed

Lines changed: 83 additions & 146 deletions

README.md

Lines changed: 35 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ Once [installed](#install), you can use the following code to create a connectio
8484
to google.com via a local SOCKS proxy server:
8585

8686
```php
87-
$connector = new React\Socket\Connector();
88-
$proxy = new Clue\React\Socks\Client('127.0.0.1:1080', $connector);
87+
$proxy = new Clue\React\Socks\Client('127.0.0.1:1080');
8988

9089
$proxy->connect('tcp://www.google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {
9190
$connection->write("GET / HTTP/1.0\r\n\r\n");
@@ -116,22 +115,17 @@ See also the [examples](examples).
116115
### Client
117116

118117
The `Client` is responsible for communication with your SOCKS server instance.
119-
Its constructor simply accepts an SOCKS proxy URI and a connector used to
120-
connect to the SOCKS proxy server address.
121118

122-
In its most simple form, you can simply pass ReactPHP's
123-
[`Connector`](https://github.com/reactphp/socket#connector)
124-
like this:
119+
Its constructor simply accepts a SOCKS proxy URI with the SOCKS proxy server address:
125120

126121
```php
127-
$connector = new React\Socket\Connector();
128-
$proxy = new Clue\React\Socks\Client('127.0.0.1:1080', $connector);
122+
$proxy = new Clue\React\Socks\Client('127.0.0.1:1080');
129123
```
130124

131125
You can omit the port if you're using the default SOCKS port 1080:
132126

133127
```php
134-
$proxy = new Clue\React\Socks\Client('127.0.0.1', $connector);
128+
$proxy = new Clue\React\Socks\Client('127.0.0.1');
135129
```
136130

137131
If you need custom connector settings (DNS resolution, TLS parameters, timeouts,
@@ -160,6 +154,7 @@ method can be used to establish a streaming connection.
160154
It returns a [Promise](https://github.com/reactphp/promise) which either
161155
fulfills with a [ConnectionInterface](https://github.com/reactphp/socket#connectioninterface)
162156
on success or rejects with an `Exception` on error.
157+
163158
This makes it fairly simple to add SOCKS proxy support to pretty much any
164159
higher-level component:
165160

@@ -178,10 +173,7 @@ As documented above, you can simply invoke its `connect()` method to establish
178173
a streaming plain TCP/IP connection and use any higher level protocol like so:
179174

180175
```php
181-
$proxy = new Clue\React\Socks\Client(
182-
'127.0.0.1:1080',
183-
new React\Socket\Connector()
184-
);
176+
$proxy = new Clue\React\Socks\Client('127.0.0.1:1080');
185177

186178
$proxy->connect('tcp://www.google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {
187179
echo 'connected to www.google.com:80';
@@ -197,10 +189,7 @@ You can either use the `Client` directly or you may want to wrap this connector
197189
in ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector):
198190

199191
```php
200-
$proxy = new Clue\React\Socks\Client(
201-
'127.0.0.1:1080',
202-
new React\Socket\Connector()
203-
);
192+
$proxy = new Clue\React\Socks\Client('127.0.0.1:1080');
204193

205194
$connector = new React\Socket\Connector(null, array(
206195
'tcp' => $proxy,
@@ -239,21 +228,16 @@ the resulting promise.
239228
This class can also be used if you want to establish a secure TLS connection
240229
(formerly known as SSL) between you and your destination, such as when using
241230
secure HTTPS to your destination site. You can simply wrap this connector in
242-
ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector) or the
243-
low-level [`SecureConnector`](https://github.com/reactphp/socket#secureconnector):
231+
ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector):
244232

245233
```php
246-
$proxy = new Clue\React\Socks\Client(
247-
'127.0.0.1:1080',
248-
new React\Socket\Connector()
249-
);
234+
$proxy = new Clue\React\Socks\Client('127.0.0.1:1080');
250235

251236
$connector = new React\Socket\Connector(null, array(
252237
'tcp' => $proxy,
253238
'dns' => false
254239
));
255240

256-
// now create an SSL encrypted connection (notice the $ssl instead of $tcp)
257241
$connector->connect('tls://www.google.com:443')->then(function (React\Socket\ConnectionInterface $connection) {
258242
// proceed with just the plain text data
259243
// everything is encrypted/decrypted automatically
@@ -268,10 +252,6 @@ $connector->connect('tls://www.google.com:443')->then(function (React\Socket\Con
268252

269253
See also the [second example](examples).
270254

271-
If you use the low-level `SecureConnector`, then the `tls://` scheme can also
272-
be omitted.
273-
Passing any other scheme will reject the promise.
274-
275255
Pending connection attempts can be cancelled by canceling its pending promise
276256
as usual.
277257

@@ -303,10 +283,7 @@ In order to send HTTP requests, you first have to add a dependency for
303283
This allows you to send both plain HTTP and TLS-encrypted HTTPS requests like this:
304284

305285
```php
306-
$proxy = new Clue\React\Socks\Client(
307-
'127.0.0.1:1080',
308-
new React\Socket\Connector()
309-
);
286+
$proxy = new Clue\React\Socks\Client('127.0.0.1:1080');
310287

311288
$connector = new React\Socket\Connector(null, array(
312289
'tcp' => $proxy,
@@ -401,16 +378,16 @@ URI scheme acts as an alias for the default `socks://` URI scheme.
401378

402379
```php
403380
// all three forms are equivalent
404-
$proxy = new Clue\React\Socks\Client('127.0.0.1', $connector);
405-
$proxy = new Clue\React\Socks\Client('socks://127.0.0.1', $connector);
406-
$proxy = new Clue\React\Socks\Client('socks5://127.0.0.1', $connector);
381+
$proxy = new Clue\React\Socks\Client('127.0.0.1');
382+
$proxy = new Clue\React\Socks\Client('socks://127.0.0.1');
383+
$proxy = new Clue\React\Socks\Client('socks5://127.0.0.1');
407384
```
408385

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

412389
```php
413-
$proxy = new Clue\React\Socks\Client('socks4://127.0.0.1', $connector);
390+
$proxy = new Clue\React\Socks\Client('socks4://127.0.0.1');
414391
```
415392

416393
#### DNS resolution
@@ -442,10 +419,7 @@ Given that remote DNS resolution is assumed to be the preferred mode, all
442419
other examples explicitly disable DNS resolution like this:
443420

444421
```php
445-
$proxy = new Clue\React\Socks\Client(
446-
'127.0.0.1:1080',
447-
new React\Socket\Connector()
448-
);
422+
$proxy = new Clue\React\Socks\Client('127.0.0.1:1080');
449423

450424
$connector = new React\Socket\Connector(null, array(
451425
'tcp' => $proxy,
@@ -457,10 +431,7 @@ If you want to explicitly use *local DNS resolution* (such as when explicitly
457431
using SOCKS4), you can use the following code:
458432

459433
```php
460-
$proxy = new Clue\React\Socks\Client(
461-
'127.0.0.1:1080',
462-
new React\Socket\Connector()
463-
);
434+
$proxy = new Clue\React\Socks\Client('127.0.0.1:1080');
464435

465436
// set up Connector which uses Google's public DNS (8.8.8.8)
466437
$connector = new React\Socket\Connector(null, array(
@@ -495,7 +466,7 @@ so this methods should not be used on a network where you have to worry about ea
495466
You can simply pass the authentication information as part of the SOCKS URI:
496467

497468
```php
498-
$proxy = new Clue\React\Socks\Client('username:password@127.0.0.1', $connector);
469+
$proxy = new Clue\React\Socks\Client('username:password@127.0.0.1');
499470
```
500471

501472
Note that both the username and password must be percent-encoded if they contain
@@ -504,11 +475,9 @@ special characters:
504475
```php
505476
$user = 'he:llo';
506477
$pass = 'p@ss';
478+
$url = rawurlencode($user) . ':' . rawurlencode($pass) . '@127.0.0.1';
507479

508-
$proxy = new Clue\React\Socks\Client(
509-
rawurlencode($user) . ':' . rawurlencode($pass) . '@127.0.0.1',
510-
$connector
511-
);
480+
$proxy = new Clue\React\Socks\Client($url);
512481
```
513482

514483
> The authentication details will be transmitted in cleartext to the SOCKS proxy
@@ -523,7 +492,7 @@ version 5 and complains if you have explicitly set anything else:
523492

524493
```php
525494
// throws InvalidArgumentException
526-
new Clue\React\Socks\Client('socks4://user:pass@127.0.0.1', $connector);
495+
new Clue\React\Socks\Client('socks4://user:pass@127.0.0.1');
527496
```
528497

529498
#### Proxy chaining
@@ -554,14 +523,8 @@ SOCKS connector from another SOCKS client like this:
554523
// which in turn then uses MiddlemanSocksServer.
555524
// this creates a TCP/IP connection to MiddlemanSocksServer, which then connects
556525
// to TargetSocksServer, which then connects to the TargetHost
557-
$middle = new Clue\React\Socks\Client(
558-
'127.0.0.1:1080',
559-
new React\Socket\Connector()
560-
);
561-
$target = new Clue\React\Socks\Client(
562-
'example.com:1080',
563-
$middle
564-
);
526+
$middle = new Clue\React\Socks\Client('127.0.0.1:1080');
527+
$target = new Clue\React\Socks\Client('example.com:1080', $middle);
565528

566529
$connector = new React\Socket\Connector(null, array(
567530
'tcp' => $target,
@@ -602,17 +565,12 @@ Many use cases require more control over the timeout and likely values much
602565
smaller, usually in the range of a few seconds only.
603566

604567
You can use ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector)
605-
or the low-level
606-
[`TimeoutConnector`](https://github.com/reactphp/socket#timeoutconnector)
607568
to decorate any given `ConnectorInterface` instance.
608569
It provides the same `connect()` method, but will automatically reject the
609570
underlying connection attempt if it takes too long:
610571

611572
```php
612-
$proxy = new Clue\React\Socks\Client(
613-
'127.0.0.1:1080',
614-
new React\Socket\Connector()
615-
);
573+
$proxy = new Clue\React\Socks\Client('127.0.0.1:1080');
616574

617575
$connector = new React\Socket\Connector(null, array(
618576
'tcp' => $proxy,
@@ -657,15 +615,9 @@ You can use the `sockss://` URI scheme or use an explicit
657615
[SOCKS protocol version](#protocol-version) like this:
658616

659617
```php
660-
$proxy = new Clue\React\Socks\Client(
661-
'sockss://127.0.0.1:1080',
662-
new React\Socket\Connector()
663-
);
664-
665-
$proxy = new Clue\React\Socks\Client(
666-
'socks4s://127.0.0.1:1080',
667-
new React\Socket\Connector()
668-
);
618+
$proxy = new Clue\React\Socks\Client('sockss://127.0.0.1:1080');
619+
620+
$proxy = new Clue\React\Socks\Client('socks4s://127.0.0.1:1080');
669621
```
670622

671623
See also [example 32](examples).
@@ -674,10 +626,7 @@ Similarly, you can also combine this with [authentication](#authentication)
674626
like this:
675627

676628
```php
677-
$proxy = new Clue\React\Socks\Client(
678-
'sockss://user:pass@127.0.0.1:1080',
679-
new React\Socket\Connector()
680-
);
629+
$proxy = new Clue\React\Socks\Client('sockss://user:pass@127.0.0.1:1080');
681630
```
682631

683632
> Note that for most use cases, [secure TLS connections](#secure-tls-connections)
@@ -709,25 +658,16 @@ You can use the `socks+unix://` URI scheme or use an explicit
709658
[SOCKS protocol version](#protocol-version) like this:
710659

711660
```php
712-
$proxy = new Clue\React\Socks\Client(
713-
'socks+unix:///tmp/proxy.sock'
714-
new React\Socket\Connector()
715-
);
716-
717-
$proxy = new Clue\React\Socks\Client(
718-
'socks4+unix:///tmp/proxy.sock',
719-
new React\Socket\Connector()
720-
);
661+
$proxy = new Clue\React\Socks\Client('socks+unix:///tmp/proxy.sock');
662+
663+
$proxy = new Clue\React\Socks\Client('socks4+unix:///tmp/proxy.sock');
721664
```
722665

723666
Similarly, you can also combine this with [authentication](#authentication)
724667
like this:
725668

726669
```php
727-
$proxy = new Clue\React\Socks\Client(
728-
'socks+unix://user:pass@/tmp/proxy.sock',
729-
new React\Socket\Connector()
730-
);
670+
$proxy = new Clue\React\Socks\Client('socks+unix://user:pass@/tmp/proxy.sock');
731671
```
732672

733673
> Note that Unix domain sockets (UDS) are considered advanced usage and PHP only
@@ -888,8 +828,7 @@ You can create a SOCKS `Client` instance like this:
888828

889829
```php
890830
// set next SOCKS server example.com:1080 as target
891-
$connector = new React\Socket\Connector();
892-
$proxy = new Clue\React\Socks\Client('user:pass@example.com:1080', $connector);
831+
$proxy = new Clue\React\Socks\Client('user:pass@example.com:1080');
893832

894833
// start a new server which forwards all connections to the other SOCKS server
895834
$server = new Clue\React\Socks\Server(null, $proxy);
@@ -1019,10 +958,7 @@ $ ssh -D 1080 example.com
1019958
Now you can simply use this SSH SOCKS server like this:
1020959

1021960
```PHP
1022-
$proxy = new Clue\React\Socks\Client(
1023-
'127.0.0.1:1080',
1024-
new React\Socket\Connector()
1025-
);
961+
$proxy = new Clue\React\Socks\Client('127.0.0.1:1080');
1026962

1027963
$proxy->connect('tcp://www.google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {
1028964
$connection->write("GET / HTTP/1.0\r\n\r\n");
@@ -1046,10 +982,7 @@ $ ssh -D/tmp/proxy.sock example.com
1046982
Now you can simply use this SSH SOCKS server like this:
1047983

1048984
```PHP
1049-
$proxy = new Clue\React\Socks\Client(
1050-
'socks+unix:///tmp/proxy.sock',
1051-
new React\Socket\Connector()
1052-
);
985+
$proxy = new Clue\React\Socks\Client('socks+unix:///tmp/proxy.sock');
1053986

1054987
$proxy->connect('tcp://www.google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {
1055988
$connection->write("GET / HTTP/1.0\r\n\r\n");
@@ -1074,10 +1007,7 @@ It presents a SOCKS5 and SOCKS4(a) interface on TCP port 9050 by default
10741007
which allows you to tunnel any traffic through the anonymity network:
10751008

10761009
```php
1077-
$proxy = new Clue\React\Socks\Client(
1078-
'127.0.0.1:9050',
1079-
new React\Socket\Connector()
1080-
);
1010+
$proxy = new Clue\React\Socks\Client('127.0.0.1:9050');
10811011

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

examples/01-https-request.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@
2121
$url = 'localhost:1080';
2222
}
2323

24-
$proxy = new Clue\React\Socks\Client(
25-
$url,
26-
new React\Socket\Connector()
27-
);
24+
$proxy = new Clue\React\Socks\Client($url);
2825

2926
$connector = new React\Socket\Connector(null, array(
3027
'tcp' => $proxy,

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818
$connector = null;
1919
$url = getenv('socks_proxy');
2020
if ($url !== false) {
21-
$proxy = new Clue\React\Socks\Client(
22-
$url,
23-
new React\Socket\Connector()
24-
);
21+
$proxy = new Clue\React\Socks\Client($url);
22+
2523
$connector = new React\Socket\Connector(null, array(
2624
'tcp' => $proxy,
2725
'timeout' => 3.0,

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@
2424
$url = 'localhost:1080';
2525
}
2626

27-
$proxy = new Clue\React\Socks\Client(
28-
$url,
29-
new React\Socket\Connector()
30-
);
27+
$proxy = new Clue\React\Socks\Client($url);
28+
3129
$connector = new React\Socket\Connector(null, array(
3230
'tcp' => $proxy,
3331
'timeout' => 3.0,

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,17 @@
2222

2323
require __DIR__ . '/../vendor/autoload.php';
2424

25-
$connector = new React\Socket\Connector();
26-
2725
$url = getenv('socks_proxy');
2826
if ($url !== false) {
29-
$proxy = new Clue\React\Socks\Client($url, $connector);
27+
$proxy = new Clue\React\Socks\Client($url);
28+
3029
$connector = new React\Socket\Connector(null, array(
3130
'tcp' => $proxy,
3231
'timeout' => 3.0,
3332
'dns' => false
3433
));
34+
} else {
35+
$connector = new React\Socket\Connector();
3536
}
3637

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

0 commit comments

Comments
 (0)