Skip to content

Commit

Permalink
Simplify usage by supporting new Socket API without nullable loop
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Aug 5, 2021
1 parent cc0d4c1 commit 32da2c8
Show file tree
Hide file tree
Showing 21 changed files with 125 additions and 118 deletions.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ proxy server listening for connections on `localhost:1080`:
$server = new Clue\React\Socks\Server();

// listen on localhost:1080
$socket = new React\Socket\Server('127.0.0.1:1080');
$socket = new React\Socket\SocketServer('127.0.0.1:1080');
$server->listen($socket);
```

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

```php
$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'dns' => '127.0.0.1',
'tcp' => array(
'bindto' => '192.168.10.1:0'
Expand Down Expand Up @@ -192,7 +192,7 @@ in ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector):
```php
$proxy = new Clue\React\Socks\Client('127.0.0.1:1080');

$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'dns' => false
));
Expand Down Expand Up @@ -234,7 +234,7 @@ ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector):
```php
$proxy = new Clue\React\Socks\Client('127.0.0.1:1080');

$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'dns' => false
));
Expand Down Expand Up @@ -264,7 +264,7 @@ You can optionally pass additional
to the constructor like this:

```php
$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'tls' => array(
'verify_peer' => false,
Expand All @@ -286,12 +286,12 @@ This allows you to send both plain HTTP and TLS-encrypted HTTPS requests like th
```php
$proxy = new Clue\React\Socks\Client('127.0.0.1:1080');

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

$browser = new React\Http\Browser(null, $connector);
$browser = new React\Http\Browser($connector);

$browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) {
var_dump($response->getHeaders(), (string) $response->getBody());
Expand Down Expand Up @@ -422,7 +422,7 @@ other examples explicitly disable DNS resolution like this:
```php
$proxy = new Clue\React\Socks\Client('127.0.0.1:1080');

$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'dns' => false
));
Expand All @@ -435,7 +435,7 @@ using SOCKS4), you can use the following code:
$proxy = new Clue\React\Socks\Client('127.0.0.1:1080');

// set up Connector which uses Google's public DNS (8.8.8.8)
$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'dns' => '8.8.8.8'
));
Expand Down Expand Up @@ -527,7 +527,7 @@ SOCKS connector from another SOCKS client like this:
$middle = new Clue\React\Socks\Client('127.0.0.1:1080');
$target = new Clue\React\Socks\Client('example.com:1080', $middle);

$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $target,
'dns' => false
));
Expand Down Expand Up @@ -573,7 +573,7 @@ underlying connection attempt if it takes too long:
```php
$proxy = new Clue\React\Socks\Client('127.0.0.1:1080');

$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'dns' => false,
'timeout' => 3.0
Expand Down Expand Up @@ -692,8 +692,8 @@ You can start listening on an underlying TCP/IP socket server like this:
```php
$server = new Clue\React\Socks\Server();

// listen on localhost:$port
$socket = new React\Socket\Server($port);
// listen on localhost:1080
$socket = new React\Socket\SocketServer('127.0.0.1:1080');
$server->listen($socket);
```

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

```php
$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'dns' => '127.0.0.1',
'tcp' => array(
'bindto' => '192.168.10.1:0'
Expand Down Expand Up @@ -835,7 +835,7 @@ $proxy = new Clue\React\Socks\Client('user:pass@example.com:1080');
$server = new Clue\React\Socks\Server(null, $proxy);

// listen on localhost:1080
$socket = new React\Socket\Server('127.0.0.1:1080');
$socket = new React\Socket\SocketServer('127.0.0.1:1080');
$server->listen($socket);
```

Expand Down Expand Up @@ -880,7 +880,7 @@ You can simply start your listening socket on the `tls://` URI scheme like this:
$server = new Clue\React\Socks\Server();

// listen on tls://127.0.0.1:1080 with the given server certificate
$socket = new React\Socket\Server('tls://127.0.0.1:1080', null, array(
$socket = new React\Socket\SocketServer('tls://127.0.0.1:1080', array(
'tls' => array(
'local_cert' => __DIR__ . '/localhost.pem',
)
Expand Down Expand Up @@ -915,7 +915,7 @@ You can simply start your listening socket on the `unix://` URI scheme like this
$server = new Clue\React\Socks\Server();

// listen on /tmp/proxy.sock
$socket = new React\Socket\Server('unix:///tmp/proxy.sock');
$socket = new React\Socket\SocketServer('unix:///tmp/proxy.sock');
$server->listen($socket);
```

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
"require": {
"php": ">=5.3",
"react/promise": "^2.1 || ^1.2",
"react/socket": "^1.8"
"react/socket": "^1.9"
},
"require-dev": {
"clue/block-react": "^1.1",
"clue/connection-manager-extra": "^1.0 || ^0.7",
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35",
"react/event-loop": "^1.2",
"react/http": "^1.4"
"react/http": "^1.5"
},
"autoload": {
"psr-4": { "Clue\\React\\Socks\\": "src/" }
Expand Down
4 changes: 2 additions & 2 deletions examples/01-https-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@

$proxy = new Clue\React\Socks\Client($url);

$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'timeout' => 3.0,
'dns' => false
));

$browser = new React\Http\Browser(null, $connector);
$browser = new React\Http\Browser($connector);

$browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) {
var_dump($response->getHeaders(), (string) $response->getBody());
Expand Down
4 changes: 2 additions & 2 deletions examples/02-optional-proxy-https-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
if ($url !== false) {
$proxy = new Clue\React\Socks\Client($url);

$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'timeout' => 3.0,
'dns' => false
));
}

$browser = new React\Http\Browser(null, $connector);
$browser = new React\Http\Browser($connector);

$browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) {
var_dump($response->getHeaders(), (string) $response->getBody());
Expand Down
2 changes: 1 addition & 1 deletion examples/11-proxy-raw-http-protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

$proxy = new Clue\React\Socks\Client($url);

$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'timeout' => 3.0,
'dns' => false
Expand Down
2 changes: 1 addition & 1 deletion examples/12-optional-proxy-raw-http-protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
if ($url !== false) {
$proxy = new Clue\React\Socks\Client($url);

$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'timeout' => 3.0,
'dns' => false
Expand Down
2 changes: 1 addition & 1 deletion examples/13-proxy-raw-https-protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

$proxy = new Clue\React\Socks\Client($url);

$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'timeout' => 3.0,
'dns' => false
Expand Down
2 changes: 1 addition & 1 deletion examples/14-optional-proxy-raw-https-protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
if ($url !== false) {
$proxy = new Clue\React\Socks\Client($url);

$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'timeout' => 3.0,
'dns' => false
Expand Down
2 changes: 1 addition & 1 deletion examples/15-proxy-chaining.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

// please note how the client uses p3 (not p1!), which in turn then uses the complete chain
// this creates a TCP/IP connection to p1, which then connects to p2, then to p3, which then connects to the target
$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $connector,
'timeout' => 3.0,
'dns' => false
Expand Down
2 changes: 1 addition & 1 deletion examples/16-local-dns.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
$proxy = new Clue\React\Socks\Client($url);

// set up DNS server to use (Google's public DNS)
$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'timeout' => 3.0,
'dns' => '8.8.8.8'
Expand Down
2 changes: 1 addition & 1 deletion examples/21-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
$server = new Clue\React\Socks\Server();

// listen on 127.0.0.1:1080 or first argument
$socket = new React\Socket\Server(isset($argv[1]) ? $argv[1] : '127.0.0.1:1080');
$socket = new React\Socket\SocketServer(isset($argv[1]) ? $argv[1] : '127.0.0.1:1080');
$server->listen($socket);

echo 'SOCKS server listening on ' . $socket->getAddress() . PHP_EOL;
2 changes: 1 addition & 1 deletion examples/22-server-with-password.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
));

// listen on 127.0.0.1:1080 or first argument
$socket = new React\Socket\Server(isset($argv[1]) ? $argv[1] : '127.0.0.1:1080');
$socket = new React\Socket\SocketServer(isset($argv[1]) ? $argv[1] : '127.0.0.1:1080');
$server->listen($socket);

echo 'SOCKS5 server requiring authentication listening on ' . $socket->getAddress() . PHP_EOL;
2 changes: 1 addition & 1 deletion examples/23-server-blacklist.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
$server = new Clue\React\Socks\Server(null, $connector);

// listen on 127.0.0.1:1080 or first argument
$socket = new React\Socket\Server(isset($argv[1]) ? $argv[1] : '127.0.0.1:1080');
$socket = new React\Socket\SocketServer(isset($argv[1]) ? $argv[1] : '127.0.0.1:1080');
$server->listen($socket);

echo 'SOCKS server listening on ' . $socket->getAddress() . PHP_EOL;
2 changes: 1 addition & 1 deletion examples/31-server-proxy-chaining.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
$server = new Clue\React\Socks\Server(null, $connector);

// listen on 127.0.0.1:1080 or first argument
$socket = new React\Socket\Server($listen);
$socket = new React\Socket\SocketServer($listen);
$server->listen($socket);

echo 'SOCKS server listening on ' . $socket->getAddress() . PHP_EOL;
Expand Down
2 changes: 1 addition & 1 deletion examples/32-server-proxy-chaining-from-random-pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
$server = new Clue\React\Socks\Server(null, $connector);

// listen on 127.0.0.1:1080 or first argument
$socket = new React\Socket\Server($listen);
$socket = new React\Socket\SocketServer($listen);
$server->listen($socket);

echo 'SOCKS server listening on ' . $socket->getAddress() . PHP_EOL;
Expand Down
10 changes: 6 additions & 4 deletions examples/41-server-secure.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
$server = new Clue\React\Socks\Server();

// listen on tls://127.0.0.1:1080 or first argument
$listen = isset($argv[1]) ? $argv[1] : '127.0.0.1:1080';
$socket = new React\Socket\Server('tls://' . $listen, null, array('tls' => array(
'local_cert' => __DIR__ . '/localhost.pem',
)));
$uri = 'tls://' . (isset($argv[1]) ? $argv[1] : '127.0.0.1:1080');
$socket = new React\Socket\SocketServer($uri, array(
'tls' => array(
'local_cert' => __DIR__ . '/localhost.pem',
)
));
$server->listen($socket);

echo 'SOCKS over TLS server listening on ' . str_replace('tls:', 'sockss:', $socket->getAddress()) . PHP_EOL;
16 changes: 9 additions & 7 deletions examples/42-http-secure.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,24 @@

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

$url = isset($argv[1]) ? $argv[1] : '127.0.0.1:1080';
$url = isset($argv[1]) ? $argv[1] : 'sockss://127.0.0.1:1080';

$connector = new React\Socket\Connector(null, array('tls' => array(
'verify_peer' => false,
'verify_peer_name' => false
)));
$connector = new React\Socket\Connector(array(
'tls' => array(
'verify_peer' => false,
'verify_peer_name' => false
)
));

$proxy = new Clue\React\Socks\Client($url, $connector);

$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'timeout' => 3.0,
'dns' => false
));

echo 'Demo SOCKS over TLS client connecting to secure SOCKS server ' . $proxy . PHP_EOL;
echo 'Demo SOCKS over TLS client connecting to secure SOCKS server ' . $url . PHP_EOL;

$connector->connect('tcp://www.google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {
echo 'connected' . PHP_EOL;
Expand Down
2 changes: 1 addition & 1 deletion src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function __construct(LoopInterface $loop = null, ConnectorInterface $conn
}

$this->loop = $loop ?: Loop::get();
$this->connector = $connector ?: new Connector($this->loop);
$this->connector = $connector ?: new Connector(array(), $this->loop);
}

/**
Expand Down
5 changes: 1 addition & 4 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
namespace Clue\Tests\React\Socks;

use Clue\React\Socks\Client;
use React\Promise\Promise;
use Clue\React\Socks\Server;
use React\Promise\Deferred;
use React\Promise\Promise;

class ClientTest extends TestCase
{
private $loop;

private $connector;

/** @var Client */
Expand All @@ -21,7 +19,6 @@ class ClientTest extends TestCase
*/
public function setUpMocks()
{
$this->loop = \React\EventLoop\Factory::create();
$this->connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
$this->client = new Client('127.0.0.1:1080', $this->connector);
}
Expand Down
Loading

0 comments on commit 32da2c8

Please sign in to comment.