Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 53 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ to google.com via a local SOCKS proxy server:

```php
$loop = React\EventLoop\Factory::create();
$client = new Client('127.0.0.1:1080', new Connector($loop));
$connector = new React\Socket\Connector($loop);
$client = new Clue\React\Socks\Client('127.0.0.1:1080', $connector);

$client->connect('tcp://www.google.com:80')->then(function (ConnectionInterface $stream) {
$stream->write("GET / HTTP/1.0\r\n\r\n");
Expand All @@ -59,11 +60,12 @@ proxy server listening for connections on `localhost:1080`:
```php
$loop = React\EventLoop\Factory::create();

// listen on localhost:1080
$socket = new Socket('127.0.0.1:1080', $loop);
// start a new SOCKS proxy server
$server = new Clue\React\Socks\Server($loop);

// start a new server listening for incoming connection on the given socket
$server = new Server($loop, $socket);
// listen on localhost:1080
$socket = new React\Socket\Server('127.0.0.1:1080', $loop);
$server->listen($socket);

$loop->run();
```
Expand Down Expand Up @@ -93,20 +95,17 @@ You can omit the port if you're using the default SOCKS port 1080:
$client = new Client('127.0.0.1', $connector);
```

If you need custom connector settings (DNS resolution, timeouts etc.), you can explicitly pass a
custom instance of the [`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):
If you need custom connector settings (DNS resolution, TLS parameters, timeouts,
proxy servers etc.), you can explicitly pass a custom instance of the
[`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):

```php
// use local DNS server
$dnsResolverFactory = new DnsFactory();
$resolver = $dnsResolverFactory->createCached('127.0.0.1', $loop);

// outgoing connections to SOCKS server via interface 192.168.10.1
// this is not to be confused with local DNS resolution (see further below)
$connector = new DnsConnector(
new TcpConnector($loop, array('bindto' => '192.168.10.1:0')),
$resolver
);
$connector = new React\Socket\Connector($loop, array(
'dns' => '127.0.0.1',
'tcp' => array(
'bindto' => '192.168.10.1:0'
)
));

$client = new Client('my-socks-server.local:1080', $connector);
```
Expand Down Expand Up @@ -622,12 +621,15 @@ It also registers everything with the main [`EventLoop`](https://github.com/reac
and an underlying TCP/IP socket server like this:

```php
$loop = \React\EventLoop\Factory::create();
$loop = React\EventLoop\Factory::create();

$server = new Clue\React\Socks\Server($loop);

// listen on localhost:$port
$socket = new Socket($port, $loop);
$socket = new React\Socket\Server($port, $loop);
$server->listen($socket);

$server = new Server($loop, $socket);
$loop->run();
```

#### Server connector
Expand All @@ -636,21 +638,19 @@ The `Server` uses an instance of ReactPHP's
[`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface)
to establish outgoing connections for each incoming connection request.

If you need custom connector settings (DNS resolution, timeouts etc.), you can explicitly pass a
custom instance of the [`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):
If you need custom connector settings (DNS resolution, TLS parameters, timeouts,
proxy servers etc.), you can explicitly pass a custom instance of the
[`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):

```php
// use local DNS server
$dnsResolverFactory = new DnsFactory();
$resolver = $dnsResolverFactory->createCached('127.0.0.1', $loop);

// outgoing connections to target host via interface 192.168.10.1
$connector = new DnsConnector(
new TcpConnector($loop, array('bindto' => '192.168.10.1:0')),
$resolver
);
$connector = new React\Socket\Connector($loop, array(
'dns' => '127.0.0.1',
'tcp' => array(
'bindto' => '192.168.10.1:0'
)
));

$server = new Server($loop, $socket, $connector);
$server = new Clue\React\Socks\Server($loop, $connector);
```

If you want to forward the outgoing connection through another SOCKS proxy, you
Expand Down Expand Up @@ -758,15 +758,20 @@ In order to connect through another SOCKS server, you can simply use the
You can create a SOCKS `Client` instance like this:

```php
$loop = React\EventLoop\Factory::create();

// set next SOCKS server example.com:1080 as target
$connector = new React\Socket\Connector($loop);
$client = new Client('user:pass@example.com:1080', $connector);
$client = new Clue\React\Socks\Client('user:pass@example.com:1080', $connector);

// start a new server which forwards all connections to the other SOCKS server
$server = new Clue\React\Socks\Server($loop, $client);

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

// start a new server which forwards all connections to the other SOCKS server
$server = new Server($loop, $socket, $client);
$loop->run();
```

See also [example #21](examples).
Expand Down Expand Up @@ -807,15 +812,19 @@ details.
You can simply start your listening socket on the `tls://` URI scheme like this:

```php
$loop = \React\EventLoop\Factory::create();
$loop = React\EventLoop\Factory::create();

$server = new Clue\React\Socks\Server($loop);

// listen on tls://127.0.0.1:1080 with the given server certificate
$socket = new React\Socket\Server('tls://127.0.0.1:1080', $loop, array(
'tls' => array(
'local_cert' => __DIR__ . '/localhost.pem',
)
));
$server = new Server($loop, $socket);
$server->listen($socket);

$loop->run();
```

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

```php
$loop = \React\EventLoop\Factory::create();
$loop = React\EventLoop\Factory::create();

$server = new Clue\React\Socks\Server($loop);

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

$loop->run();
```

> Note that Unix domain sockets (UDS) are considered advanced usage and that
Expand Down
10 changes: 5 additions & 5 deletions examples/11-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

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

// listen on 127.0.0.1:1080 or first argument
$listen = isset($argv[1]) ? $argv[1] : '127.0.0.1:1080';
$socket = new Socket($listen, $loop);
// start a new SOCKS proxy server
$server = new Server($loop);

// start a new server listening for incoming connection on the given socket
$server = new Server($loop, $socket);
// listen on 127.0.0.1:1080 or first argument
$socket = new Socket(isset($argv[1]) ? $argv[1] : '127.0.0.1:1080', $loop);
$server->listen($socket);

echo 'SOCKS server listening on ' . $socket->getAddress() . PHP_EOL;

Expand Down
12 changes: 6 additions & 6 deletions examples/12-server-with-password.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@

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

// listen on 127.0.0.1:1080 or first argument
$listen = isset($argv[1]) ? $argv[1] : '127.0.0.1:1080';
$socket = new Socket($listen, $loop);

// start a new server listening for incoming connection on the given socket
// start a new SOCKS proxy server
// require authentication and hence make this a SOCKS5-only server
$server = new Server($loop, $socket);
$server = new Server($loop);
$server->setAuthArray(array(
'tom' => 'god',
'user' => 'p@ssw0rd'
));

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

echo 'SOCKS5 server requiring authentication listening on ' . $socket->getAddress() . PHP_EOL;

$loop->run();
10 changes: 5 additions & 5 deletions examples/13-server-blacklist.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
'*' => $permit
));

// listen on 127.0.0.1:1080 or first argument
$listen = isset($argv[1]) ? $argv[1] : '127.0.0.1:1080';
$socket = new Socket($listen, $loop);
// start a new SOCKS proxy server using our connection manager for outgoing connections
$server = new Server($loop, $connector);

// start the actual socks server on the given server socket and using our connection manager for outgoing connections
$server = new Server($loop, $socket, $connector);
// listen on 127.0.0.1:1080 or first argument
$socket = new Socket(isset($argv[1]) ? $argv[1] : '127.0.0.1:1080', $loop);
$server->listen($socket);

echo 'SOCKS server listening on ' . $socket->getAddress() . PHP_EOL;

Expand Down
7 changes: 4 additions & 3 deletions examples/21-server-proxy-chaining.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@
$connector = new Client($proxy, $connector);
}

// start a new SOCKS proxy server which forwards all connections to the other SOCKS server
$server = new Server($loop, $connector);

// listen on 127.0.0.1:1080 or first argument
$socket = new Socket($listen, $loop);

// start a new server which forwards all connections to the other SOCKS server
$server = new Server($loop, $socket, $connector);
$server->listen($socket);

echo 'SOCKS server listening on ' . $socket->getAddress() . PHP_EOL;
echo 'Forwarding via: ' . implode(' -> ', $path) . PHP_EOL;
Expand Down
8 changes: 5 additions & 3 deletions examples/22-server-proxy-chaining-from-random-pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@
}
$connector = new ConnectionManagerRandom($clients);

$socket = new Socket($listen, $loop);

// start the actual socks server on the given server socket and using our connection manager for outgoing connections
// start the SOCKS proxy server using our connection manager for outgoing connections
$server = new Server($loop, $socket, $connector);

// listen on 127.0.0.1:1080 or first argument
$socket = new Socket($listen, $loop);
$server->listen($socket);

echo 'SOCKS server listening on ' . $socket->getAddress() . PHP_EOL;
echo 'Randomly picking from: ' . implode(', ', $pool) . PHP_EOL;

Expand Down
6 changes: 3 additions & 3 deletions examples/31-server-secure.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

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

// start a new SOCKS proxy server
$server = new Server($loop);

// listen on tls://127.0.0.1:1080 or first argument
$listen = isset($argv[1]) ? $argv[1] : '127.0.0.1:1080';
$socket = new Socket('tls://' . $listen, $loop, array('tls' => array(
'local_cert' => __DIR__ . '/localhost.pem',
)));

// start a new server listening for incoming connection on the given socket
$server = new Server($loop, $socket);

echo 'SOCKS over TLS server listening on ' . str_replace('tls:', 'sockss:', $socket->getAddress()) . PHP_EOL;

$loop->run();
11 changes: 9 additions & 2 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,24 @@ final class Server

private $protocolVersion = null;

public function __construct(LoopInterface $loop, ServerInterface $serverInterface, ConnectorInterface $connector = null)
public function __construct(LoopInterface $loop, ConnectorInterface $connector = null)
{
if ($connector === null) {
$connector = new Connector($loop);
}

$this->loop = $loop;
$this->connector = $connector;
}

/**
* @param ServerInterface $socket
* @return void
*/
public function listen(ServerInterface $socket)
{
$that = $this;
$serverInterface->on('connection', function ($connection) use ($that) {
$socket->on('connection', function ($connection) use ($that) {
$that->onConnection($connection);
});
}
Expand Down
18 changes: 12 additions & 6 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public function setUp()
$this->port = parse_url($address, PHP_URL_PORT);
$this->assertNotEquals(0, $this->port);

$this->server = new Server($this->loop, $socket);
$this->server = new Server($this->loop);
$this->server->listen($socket);
$this->connector = new TcpConnector($this->loop);
$this->client = new Client('127.0.0.1:' . $this->port, $this->connector);
}
Expand Down Expand Up @@ -113,7 +114,8 @@ public function testConnectionSocksOverTls()
$socket = new \React\Socket\Server('tls://127.0.0.1:0', $this->loop, array('tls' => array(
'local_cert' => __DIR__ . '/../examples/localhost.pem',
)));
$this->server = new Server($this->loop, $socket);
$this->server = new Server($this->loop);
$this->server->listen($socket);

$this->connector = new Connector($this->loop, array('tls' => array(
'verify_peer' => false,
Expand All @@ -137,7 +139,8 @@ public function testConnectionSocksOverTlsUsesPeerNameFromSocksUri()
$socket = new \React\Socket\Server('tls://127.0.0.1:0', $this->loop, array('tls' => array(
'local_cert' => __DIR__ . '/../examples/localhost.pem',
)));
$this->server = new Server($this->loop, $socket);
$this->server = new Server($this->loop);
$this->server->listen($socket);

$this->connector = new Connector($this->loop, array('tls' => array(
'verify_peer' => false,
Expand All @@ -157,7 +160,8 @@ public function testConnectionSocksOverUnix()

$path = sys_get_temp_dir() . '/test' . mt_rand(1000, 9999) . '.sock';
$socket = new UnixServer($path, $this->loop);
$this->server = new Server($this->loop, $socket);
$this->server = new Server($this->loop);
$this->server->listen($socket);

$this->connector = new Connector($this->loop);
$this->client = new Client('socks+unix://' . $path, $this->connector);
Expand All @@ -176,7 +180,8 @@ public function testConnectionSocks5OverUnix()

$path = sys_get_temp_dir() . '/test' . mt_rand(1000, 9999) . '.sock';
$socket = new UnixServer($path, $this->loop);
$this->server = new Server($this->loop, $socket);
$this->server = new Server($this->loop);
$this->server->listen($socket);
$this->server->setProtocolVersion(5);

$this->connector = new Connector($this->loop);
Expand All @@ -196,7 +201,8 @@ public function testConnectionSocksWithAuthenticationOverUnix()

$path = sys_get_temp_dir() . '/test' . mt_rand(1000, 9999) . '.sock';
$socket = new UnixServer($path, $this->loop);
$this->server = new Server($this->loop, $socket);
$this->server = new Server($this->loop);
$this->server->listen($socket);
$this->server->setAuthArray(array('name' => 'pass'));

$this->connector = new Connector($this->loop);
Expand Down
Loading