Skip to content

Commit de3dc62

Browse files
committed
Merge pull request #1 from clue/socket-client
Update react to v0.3 and implement its SocketClient
2 parents db42cb9 + eb1f9f3 commit de3dc62

15 files changed

Lines changed: 323 additions & 44 deletions

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ This file is a manually maintained list of changes for each release. Feel free
44
to add your changes here when sending pull requests. Also send corrections if
55
you spot any mistakes.
66

7+
## 0.4.0 (2013-XX-XX)
8+
9+
* BC break: Update react to current v0.3 and thus also replace `ConnectionManager` with `Connector`
10+
* BC break: New `Client::createConnector()` replaces inheriting `ConnectionManagerInterface`
11+
* BC break: New `Client::createSecureConnector()` replaces `Client::createSecureConnectionManager()`
12+
713
## 0.3.1 (2012-12-29)
814

915
* Fix: Server event logging
@@ -25,7 +31,6 @@ you spot any mistakes.
2531
* Feature: Simple interface for SSL/TLS over SOCKS: `Client` now implements `ConnectionManagerInterface`
2632
* Feature: Simple interface for TCP over SOCKS: `SecureConnectionManager Client::createSecureConnectionManager()`
2733

28-
2934
## 0.1.0 (2011-05-16)
3035

3136
* First tagged release

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ $loop->start();
3737
The `Socks/Client` uses a [Promise](https://github.com/reactphp/promise)-based interface which makes working with asynchronous functions a breeze.
3838
Let's open up a TCP [Stream](https://github.com/reactphp/stream) connection and write some data:
3939
```PHP
40-
$client->getConnection('www.google.com',80)->then(function (React\Stream\Stream $stream) {
40+
$tcp = $client->createConnector();
41+
42+
$tcp->create('www.google.com',80)->then(function (React\Stream\Stream $stream) {
4143
echo 'connected to www.google.com:80';
4244
$stream->write("GET / HTTP/1.0\r\n\r\n");
4345
// ...
@@ -67,10 +69,10 @@ Yes, this works for both plain HTTP and SSL encrypted HTTPS requests.
6769

6870
If you want to connect to arbitrary SSL/TLS servers, there sure too is an easy to use API available:
6971
```PHP
70-
$ssl = $client->createSecureConnectionManager();
72+
$ssl = $client->createSecureConnector();
7173

72-
// now create an SSL encrypted connection (notice the $ssl instead of $client)
73-
$ssl->getConnection('www.google.com',443)->then(function (React\Stream\Stream $stream) {
74+
// now create an SSL encrypted connection (notice the $ssl instead of $tcp)
75+
$ssl->create('www.google.com',443)->then(function (React\Stream\Stream $stream) {
7476
// proceed with just the plain text data and everything is encrypted/decrypted automatically
7577
echo 'connected to SSL encrypted www.google.com';
7678
$stream->write("GET / HTTP/1.0\r\n\r\n");

Socks/Client.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,20 @@
88
use React\Dns\Resolver\Resolver;
99
use React\Stream\Stream;
1010
use React\EventLoop\LoopInterface;
11-
use ConnectionManager\ConnectionManagerInterface;
12-
use ConnectionManager\SecureConnectionManager;
11+
use React\SocketClient\ConnectorInterface;
12+
use React\SocketClient\SecureConnector;
13+
use Socks\Connector;
1314
use \Exception;
1415
use \InvalidArgumentException;
1516
use \UnexpectedValueException;
1617

17-
class Client implements ConnectionManagerInterface
18+
class Client
1819
{
1920
/**
2021
*
21-
* @var ConnectionManagerInterface
22+
* @var ConnectorInterface
2223
*/
23-
private $connectionManager;
24+
private $connector;
2425

2526
/**
2627
*
@@ -45,10 +46,10 @@ class Client implements ConnectionManagerInterface
4546

4647
protected $auth = null;
4748

48-
public function __construct(LoopInterface $loop, ConnectionManagerInterface $connectionManager, Resolver $resolver, $socksHost, $socksPort)
49+
public function __construct(LoopInterface $loop, ConnectorInterface $connector, Resolver $resolver, $socksHost, $socksPort)
4950
{
5051
$this->loop = $loop;
51-
$this->connectionManager = $connectionManager;
52+
$this->connector = $connector;
5253
$this->socksHost = $socksHost;
5354
$this->socksPort = $socksPort;
5455
$this->resolver = $resolver;
@@ -110,12 +111,17 @@ public function unsetAuth()
110111

111112
public function createHttpClient()
112113
{
113-
return new HttpClient($this->loop, $this, $this->createSecureConnectionManager());
114+
return new HttpClient($this->loop, $this->createConnector(), $this->createSecureConnector());
114115
}
115116

116-
public function createSecureConnectionManager()
117+
public function createSecureConnector()
117118
{
118-
return new SecureConnectionManager($this, $this->loop);
119+
return new SecureConnector($this->createConnector(), $this->loop);
120+
}
121+
122+
public function createConnector()
123+
{
124+
return new Connector($this);
119125
}
120126

121127
public function getConnection($host, $port)
@@ -145,7 +151,7 @@ public function getConnection($host, $port)
145151
$that = $this;
146152
When::all(
147153
array(
148-
$this->connectionManager->getConnection($this->socksHost, $this->socksPort)->then(
154+
$this->connector->create($this->socksHost, $this->socksPort)->then(
149155
null,
150156
function ($error) {
151157
throw new Exception('Unable to connect to socks server', 0, $error);

Socks/Connector.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Socks;
4+
5+
use React\SocketClient\ConnectorInterface;
6+
use Socks\Client;
7+
8+
class Connector implements ConnectorInterface
9+
{
10+
private $client;
11+
12+
public function __construct(Client $socksClient)
13+
{
14+
$this->client = $socksClient;
15+
}
16+
17+
public function create($host, $port)
18+
{
19+
return $this->client->getConnection($host, $port);
20+
}
21+
}

Socks/Factory.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Socks;
44

5-
use ConnectionManager\ConnectionManager;
65
use React\Dns\Resolver\Resolver;
76
use React\EventLoop\LoopInterface;
7+
use React\SocketClient\Connector;
88

99
class Factory
1010
{
@@ -16,18 +16,18 @@ public function __construct(LoopInterface $loop, Resolver $resolver)
1616

1717
public function createClient($socksHost, $socksPort)
1818
{
19-
$connector = $this->createConnectionManager();
19+
$connector = $this->createConnector();
2020
return new Client($this->loop, $connector, $this->resolver, $socksHost, $socksPort);
2121
}
2222

2323
public function createServer($socket)
2424
{
25-
$connector = $this->createConnectionManager();
25+
$connector = $this->createConnector();
2626
return new Server($socket, $this->loop, $connector);
2727
}
2828

29-
protected function createConnectionManager()
29+
protected function createConnector()
3030
{
31-
return new ConnectionManager($this->loop, $this->resolver);
31+
return new Connector($this->loop, $this->resolver);
3232
}
3333
}

Socks/Server.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use React\Promise\When;
88
use React\Promise\PromiseInterface;
99
use React\Stream\Stream;
10-
use ConnectionManager\ConnectionManagerInterface;
10+
use React\SocketClient\ConnectorInterface;
1111
use React\Socket\Connection;
1212
use React\EventLoop\LoopInterface;
1313
use \UnexpectedValueException;
@@ -18,16 +18,16 @@ class Server extends EventEmitter
1818
{
1919
protected $loop;
2020

21-
private $connectionManager;
21+
private $connector;
2222

2323
private $auth = null;
2424

2525
private $protocolVersion = null;
2626

27-
public function __construct(ServerInterface $serverInterface, LoopInterface $loop, ConnectionManagerInterface $connectionManager)
27+
public function __construct(ServerInterface $serverInterface, LoopInterface $loop, ConnectorInterface $connector)
2828
{
2929
$this->loop = $loop;
30-
$this->connectionManager = $connectionManager;
30+
$this->connector = $connector;
3131

3232
$that = $this;
3333
$serverInterface->on('connection', function ($connection) use ($that) {
@@ -302,7 +302,7 @@ public function connectTarget(Stream $stream, array $target)
302302
{
303303
$stream->emit('target', $target);
304304
$that = $this;
305-
return $this->connectionManager->getConnection($target[0], $target[1])->then(function (Stream $remote) use ($stream, $that) {
305+
return $this->connector->create($target[0], $target[1])->then(function (Stream $remote) use ($stream, $that) {
306306
if (!$stream->isWritable()) {
307307
$remote->close();
308308
throw new UnexpectedValueException('Remote connection successfully established after client connection closed');

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
"psr-0": {"Socks": ""}
1010
},
1111
"require": {
12-
"react/http-client": "0.2.*",
13-
"react/event-loop": "0.2.*",
14-
"clue/connection-manager": "0.1.*"
12+
"react/http-client": "0.3.*",
13+
"react/event-loop": "0.3.*",
14+
"react/socket-client": "0.3.*"
1515
}
1616
}

examples/client.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,21 @@ function (Exception $error) use ($name) {
6262
);
6363
}
6464

65-
assertOkay($client->getConnection('www.google.com', 80), 'www.google.com:80');
65+
$tcp = $client->createConnector();
6666

67-
assertFail($client->getConnection('www.google.commm', 80), 'www.google.commm:80');
67+
assertOkay($tcp->create('www.google.com', 80), 'www.google.com:80');
6868

69-
assertFail($client->getConnection('www.google.com', 8080), 'www.google.com:8080');
69+
assertFail($tcp->create('www.google.commm', 80), 'www.google.commm:80');
7070

71-
$ssl = $client->createSecureConnectionManager();
71+
assertFail($tcp->create('www.google.com', 8080), 'www.google.com:8080');
7272

73-
assertOkay($ssl->getConnection('www.google.com', 443), 'ssl://www.google.com:443');
73+
$ssl = $client->createSecureConnector();
7474

75-
assertFail($ssl->getConnection('www.google.com', 80), 'ssl://www.google.com:80');
75+
assertOkay($ssl->create('www.google.com', 443), 'ssl://www.google.com:443');
7676

77-
assertFail($ssl->getConnection('www.google.com', 8080), 'ssl://www.google.com:8080');
77+
assertFail($ssl->create('www.google.com', 80), 'ssl://www.google.com:80');
78+
79+
assertFail($ssl->create('www.google.com', 8080), 'ssl://www.google.com:8080');
7880

7981
// $ssl->getConnection('127.0.0.1','443')->then(function (React\Stream $stream) {
8082
// echo 'connected';

examples/server-middleman.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
// start a new server which forwards all connections to another SOCKS server
1717
$socket = new React\Socket\Server($loop);
18-
$server = new Socks\Server($socket, $loop, $target);
18+
$server = new Socks\Server($socket, $loop, $target->createConnector());
1919

2020
$socket->listen('9051','localhost');
2121

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

3-
<phpunit colors="true">
3+
<phpunit colors="true" bootstrap="./tests/bootstrap.php">
44
<testsuites>
55
<testsuite name="Socks Test Suite">
66
<directory>./tests/</directory>

0 commit comments

Comments
 (0)