Skip to content

Commit fd0691e

Browse files
committed
Simplify usage by supporting new default loop
1 parent c64bc0d commit fd0691e

19 files changed

Lines changed: 127 additions & 186 deletions

README.md

Lines changed: 52 additions & 70 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-
$loop = React\EventLoop\Factory::create();
88-
$connector = new React\Socket\Connector($loop);
87+
$connector = new React\Socket\Connector();
8988
$proxy = new Clue\React\Socks\Client('127.0.0.1:1080', $connector);
9089

9190
$proxy->connect('tcp://www.google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {
@@ -95,25 +94,19 @@ $proxy->connect('tcp://www.google.com:80')->then(function (React\Socket\Connecti
9594
echo $chunk;
9695
});
9796
});
98-
99-
$loop->run();
10097
```
10198

10299
If you're not already running any other [SOCKS proxy server](#servers),
103100
you can use the following code to create a SOCKS
104101
proxy server listening for connections on `localhost:1080`:
105102

106103
```php
107-
$loop = React\EventLoop\Factory::create();
108-
109104
// start a new SOCKS proxy server
110-
$server = new Clue\React\Socks\Server($loop);
105+
$server = new Clue\React\Socks\Server();
111106

112107
// listen on localhost:1080
113-
$socket = new React\Socket\Server('127.0.0.1:1080', $loop);
108+
$socket = new React\Socket\Server('127.0.0.1:1080');
114109
$server->listen($socket);
115-
116-
$loop->run();
117110
```
118111

119112
See also the [examples](examples).
@@ -131,7 +124,7 @@ In its most simple form, you can simply pass ReactPHP's
131124
like this:
132125

133126
```php
134-
$connector = new React\Socket\Connector($loop);
127+
$connector = new React\Socket\Connector();
135128
$proxy = new Clue\React\Socks\Client('127.0.0.1:1080', $connector);
136129
```
137130

@@ -146,7 +139,7 @@ proxy servers etc.), you can explicitly pass a custom instance of the
146139
[`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):
147140

148141
```php
149-
$connector = new React\Socket\Connector($loop, array(
142+
$connector = new React\Socket\Connector(null, array(
150143
'dns' => '127.0.0.1',
151144
'tcp' => array(
152145
'bindto' => '192.168.10.1:0'
@@ -187,7 +180,7 @@ a streaming plain TCP/IP connection and use any higher level protocol like so:
187180
```php
188181
$proxy = new Clue\React\Socks\Client(
189182
'127.0.0.1:1080',
190-
new React\Socket\Connector($loop)
183+
new React\Socket\Connector()
191184
);
192185

193186
$proxy->connect('tcp://www.google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {
@@ -206,10 +199,10 @@ in ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector):
206199
```php
207200
$proxy = new Clue\React\Socks\Client(
208201
'127.0.0.1:1080',
209-
new React\Socket\Connector($loop)
202+
new React\Socket\Connector()
210203
);
211204

212-
$connector = new React\Socket\Connector($loop, array(
205+
$connector = new React\Socket\Connector(null, array(
213206
'tcp' => $proxy,
214207
'dns' => false
215208
));
@@ -252,10 +245,10 @@ low-level [`SecureConnector`](https://github.com/reactphp/socket#secureconnector
252245
```php
253246
$proxy = new Clue\React\Socks\Client(
254247
'127.0.0.1:1080',
255-
new React\Socket\Connector($loop)
248+
new React\Socket\Connector()
256249
);
257250

258-
$connector = new React\Socket\Connector($loop, array(
251+
$connector = new React\Socket\Connector(null, array(
259252
'tcp' => $proxy,
260253
'dns' => false
261254
));
@@ -290,7 +283,7 @@ You can optionally pass additional
290283
to the constructor like this:
291284

292285
```php
293-
$connector = new React\Socket\Connector($loop, array(
286+
$connector = new React\Socket\Connector(null, array(
294287
'tcp' => $proxy,
295288
'tls' => array(
296289
'verify_peer' => false,
@@ -312,15 +305,15 @@ This allows you to send both plain HTTP and TLS-encrypted HTTPS requests like th
312305
```php
313306
$proxy = new Clue\React\Socks\Client(
314307
'127.0.0.1:1080',
315-
new React\Socket\Connector($loop)
308+
new React\Socket\Connector()
316309
);
317310

318-
$connector = new React\Socket\Connector($loop, array(
311+
$connector = new React\Socket\Connector(null, array(
319312
'tcp' => $proxy,
320313
'dns' => false
321314
));
322315

323-
$browser = new React\Http\Browser($loop, $connector);
316+
$browser = new React\Http\Browser(null, $connector);
324317

325318
$browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) {
326319
var_dump($response->getHeaders(), (string) $response->getBody());
@@ -451,10 +444,10 @@ other examples explicitly disable DNS resolution like this:
451444
```php
452445
$proxy = new Clue\React\Socks\Client(
453446
'127.0.0.1:1080',
454-
new React\Socket\Connector($loop)
447+
new React\Socket\Connector()
455448
);
456449

457-
$connector = new React\Socket\Connector($loop, array(
450+
$connector = new React\Socket\Connector(null, array(
458451
'tcp' => $proxy,
459452
'dns' => false
460453
));
@@ -466,11 +459,11 @@ using SOCKS4), you can use the following code:
466459
```php
467460
$proxy = new Clue\React\Socks\Client(
468461
'127.0.0.1:1080',
469-
new React\Socket\Connector($loop)
462+
new React\Socket\Connector()
470463
);
471464

472465
// set up Connector which uses Google's public DNS (8.8.8.8)
473-
$connector = new React\Socket\Connector($loop, array(
466+
$connector = new React\Socket\Connector(null, array(
474467
'tcp' => $proxy,
475468
'dns' => '8.8.8.8'
476469
));
@@ -563,14 +556,14 @@ SOCKS connector from another SOCKS client like this:
563556
// to TargetSocksServer, which then connects to the TargetHost
564557
$middle = new Clue\React\Socks\Client(
565558
'127.0.0.1:1080',
566-
new React\Socket\Connector($loop)
559+
new React\Socket\Connector()
567560
);
568561
$target = new Clue\React\Socks\Client(
569562
'example.com:1080',
570563
$middle
571564
);
572565

573-
$connector = new React\Socket\Connector($loop, array(
566+
$connector = new React\Socket\Connector(null, array(
574567
'tcp' => $target,
575568
'dns' => false
576569
));
@@ -618,10 +611,10 @@ underlying connection attempt if it takes too long:
618611
```php
619612
$proxy = new Clue\React\Socks\Client(
620613
'127.0.0.1:1080',
621-
new React\Socket\Connector($loop)
614+
new React\Socket\Connector()
622615
);
623616

624-
$connector = new React\Socket\Connector($loop, array(
617+
$connector = new React\Socket\Connector(null, array(
625618
'tcp' => $proxy,
626619
'dns' => false,
627620
'timeout' => 3.0
@@ -666,12 +659,12 @@ You can use the `sockss://` URI scheme or use an explicit
666659
```php
667660
$proxy = new Clue\React\Socks\Client(
668661
'sockss://127.0.0.1:1080',
669-
new React\Socket\Connector($loop)
662+
new React\Socket\Connector()
670663
);
671664

672665
$proxy = new Clue\React\Socks\Client(
673666
'socks4s://127.0.0.1:1080',
674-
new React\Socket\Connector($loop)
667+
new React\Socket\Connector()
675668
);
676669
```
677670

@@ -683,7 +676,7 @@ like this:
683676
```php
684677
$proxy = new Clue\React\Socks\Client(
685678
'sockss://user:pass@127.0.0.1:1080',
686-
new React\Socket\Connector($loop)
679+
new React\Socket\Connector()
687680
);
688681
```
689682

@@ -718,12 +711,12 @@ You can use the `socks+unix://` URI scheme or use an explicit
718711
```php
719712
$proxy = new Clue\React\Socks\Client(
720713
'socks+unix:///tmp/proxy.sock'
721-
new React\Socket\Connector($loop)
714+
new React\Socket\Connector()
722715
);
723716

724717
$proxy = new Clue\React\Socks\Client(
725718
'socks4+unix:///tmp/proxy.sock',
726-
new React\Socket\Connector($loop)
719+
new React\Socket\Connector()
727720
);
728721
```
729722

@@ -733,7 +726,7 @@ like this:
733726
```php
734727
$proxy = new Clue\React\Socks\Client(
735728
'socks+unix://user:pass@/tmp/proxy.sock',
736-
new React\Socket\Connector($loop)
729+
new React\Socket\Connector()
737730
);
738731
```
739732

@@ -753,21 +746,22 @@ $proxy = new Clue\React\Socks\Client(
753746
The `Server` is responsible for accepting incoming communication from SOCKS clients
754747
and forwarding the requested connection to the target host.
755748
It supports the SOCKS5 and SOCKS4(a) protocol versions by default.
756-
It also registers everything with the main [`EventLoop`](https://github.com/reactphp/event-loop#usage)
757-
and an underlying TCP/IP socket server like this:
749+
You can start listening on an underlying TCP/IP socket server like this:
758750

759751
```php
760-
$loop = React\EventLoop\Factory::create();
761-
762-
$server = new Clue\React\Socks\Server($loop);
752+
$server = new Clue\React\Socks\Server();
763753

764754
// listen on localhost:$port
765-
$socket = new React\Socket\Server($port, $loop);
755+
$socket = new React\Socket\Server($port);
766756
$server->listen($socket);
767-
768-
$loop->run();
769757
```
770758

759+
This class takes an optional `LoopInterface|null $loop` parameter that can be used to
760+
pass the event loop instance to use for this object. You can use a `null` value
761+
here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
762+
This value SHOULD NOT be given unless you're sure you want to explicitly use a
763+
given event loop instance.
764+
771765
Additionally, the `Server` constructor accepts optional parameters to explicitly
772766
configure the [connector](#server-connector) to use and to require
773767
[authentication](#server-authentication). For more details, read on...
@@ -783,14 +777,14 @@ proxy servers etc.), you can explicitly pass a custom instance of the
783777
[`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):
784778

785779
```php
786-
$connector = new React\Socket\Connector($loop, array(
780+
$connector = new React\Socket\Connector(null, array(
787781
'dns' => '127.0.0.1',
788782
'tcp' => array(
789783
'bindto' => '192.168.10.1:0'
790784
)
791785
));
792786

793-
$server = new Clue\React\Socks\Server($loop, $connector);
787+
$server = new Clue\React\Socks\Server(null, $connector);
794788
```
795789

796790
If you want to forward the outgoing connection through another SOCKS proxy, you
@@ -823,7 +817,7 @@ If you only want to accept static authentication details, you can simply pass an
823817
additional assoc array with your authentication details to the `Server` like this:
824818

825819
```php
826-
$server = new Clue\React\Socks\Server($loop, null, array(
820+
$server = new Clue\React\Socks\Server(null, null, array(
827821
'tom' => 'password',
828822
'admin' => 'root'
829823
));
@@ -835,7 +829,7 @@ If you want more control over authentication, you can pass an authenticator
835829
function that should return a `bool` value like this synchronous example:
836830

837831
```php
838-
$server = new Clue\React\Socks\Server($loop, null, function ($user, $pass, $remote) {
832+
$server = new Clue\React\Socks\Server(null, null, function ($user, $pass, $remote) {
839833
// $remote is a full URI à la socks://user:pass@192.168.1.1:1234
840834
// or sockss://user:pass@192.168.1.1:1234 for SOCKS over TLS
841835
// or may be null when remote is unknown (SOCKS over Unix Domain Sockets)
@@ -856,7 +850,7 @@ from the authenticator function that will fulfill with a `bool` value like this
856850
async example:
857851

858852
```php
859-
$server = new Clue\React\Socks\Server($loop, null, function ($user, $pass) use ($db) {
853+
$server = new Clue\React\Socks\Server(null, null, function ($user, $pass) use ($db) {
860854
// pseudo-code: query database for given authentication details
861855
return $db->query(
862856
'SELECT 1 FROM users WHERE name = ? AND password = ?',
@@ -893,20 +887,16 @@ In order to connect through another SOCKS server, you can simply use the
893887
You can create a SOCKS `Client` instance like this:
894888

895889
```php
896-
$loop = React\EventLoop\Factory::create();
897-
898890
// set next SOCKS server example.com:1080 as target
899-
$connector = new React\Socket\Connector($loop);
891+
$connector = new React\Socket\Connector();
900892
$proxy = new Clue\React\Socks\Client('user:pass@example.com:1080', $connector);
901893

902894
// start a new server which forwards all connections to the other SOCKS server
903-
$server = new Clue\React\Socks\Server($loop, $proxy);
895+
$server = new Clue\React\Socks\Server(null, $proxy);
904896

905897
// listen on localhost:1080
906-
$socket = new React\Socket\Server('127.0.0.1:1080', $loop);
898+
$socket = new React\Socket\Server('127.0.0.1:1080');
907899
$server->listen($socket);
908-
909-
$loop->run();
910900
```
911901

912902
See also [example #21](examples).
@@ -947,19 +937,15 @@ details.
947937
You can simply start your listening socket on the `tls://` URI scheme like this:
948938

949939
```php
950-
$loop = React\EventLoop\Factory::create();
951-
952-
$server = new Clue\React\Socks\Server($loop);
940+
$server = new Clue\React\Socks\Server();
953941

954942
// listen on tls://127.0.0.1:1080 with the given server certificate
955-
$socket = new React\Socket\Server('tls://127.0.0.1:1080', $loop, array(
943+
$socket = new React\Socket\Server('tls://127.0.0.1:1080', null, array(
956944
'tls' => array(
957945
'local_cert' => __DIR__ . '/localhost.pem',
958946
)
959947
));
960948
$server->listen($socket);
961-
962-
$loop->run();
963949
```
964950

965951
See also [example 31](examples).
@@ -986,15 +972,11 @@ having to rely on explicit [authentication](#server-authentication).
986972
You can simply start your listening socket on the `unix://` URI scheme like this:
987973

988974
```php
989-
$loop = React\EventLoop\Factory::create();
990-
991-
$server = new Clue\React\Socks\Server($loop);
975+
$server = new Clue\React\Socks\Server();
992976

993977
// listen on /tmp/proxy.sock
994-
$socket = new React\Socket\Server('unix:///tmp/proxy.sock', $loop);
978+
$socket = new React\Socket\Server('unix:///tmp/proxy.sock');
995979
$server->listen($socket);
996-
997-
$loop->run();
998980
```
999981

1000982
> Note that Unix domain sockets (UDS) are considered advanced usage and that
@@ -1039,7 +1021,7 @@ Now you can simply use this SSH SOCKS server like this:
10391021
```PHP
10401022
$proxy = new Clue\React\Socks\Client(
10411023
'127.0.0.1:1080',
1042-
new React\Socket\Connector($loop)
1024+
new React\Socket\Connector()
10431025
);
10441026

10451027
$proxy->connect('tcp://www.google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {
@@ -1066,7 +1048,7 @@ Now you can simply use this SSH SOCKS server like this:
10661048
```PHP
10671049
$proxy = new Clue\React\Socks\Client(
10681050
'socks+unix:///tmp/proxy.sock',
1069-
new React\Socket\Connector($loop)
1051+
new React\Socket\Connector()
10701052
);
10711053

10721054
$proxy->connect('tcp://www.google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {
@@ -1094,7 +1076,7 @@ which allows you to tunnel any traffic through the anonymity network:
10941076
```php
10951077
$proxy = new Clue\React\Socks\Client(
10961078
'127.0.0.1:9050',
1097-
new React\Socket\Connector($loop)
1079+
new React\Socket\Connector()
10981080
);
10991081

11001082
$proxy->connect('tcp://www.google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {

0 commit comments

Comments
 (0)