Skip to content

Commit 6fee452

Browse files
committed
Client ctor now accepts a single SOCKS URI as first argument
1 parent 133eb1d commit 6fee452

7 files changed

Lines changed: 44 additions & 13 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ include_once __DIR__.'/vendor/autoload.php';
2020
$loop = React\EventLoop\Factory::create();
2121

2222
// create SOCKS client which communicates with SOCKS server 127.0.0.1:9050
23-
$client = new Clue\React\Socks\Client($loop, '127.0.0.1', 9050);
23+
$client = new Clue\React\Socks\Client('127.0.0.1:9050', $loop);
2424

2525
// now work with your $client, see below
2626

examples/client.php

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

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

12-
$client = new Client($loop, '127.0.0.1', $port);
12+
$client = new Client('127.0.0.1:' . $port, $loop);
1313
$client->setTimeout(3.0);
1414
$client->setResolveLocal(false);
1515
// $client->setProtocolVersion(5);

examples/server-advanced-outgoing/server-forward-random-pool.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
$connector = new ConnectionManagerRandom();
1717

1818
// forward to socks server listening on 127.0.0.1:9051
19-
$client = new Client($loop, '127.0.0.1', 9051);
19+
$client = new Client('127.0.0.1:9051', $loop);
2020
$connector->addConnectionManager($client->createConnector());
2121

2222
// forward to socks server listening on 127.0.0.1:9052
23-
$client = new Client($loop, '127.0.0.1', 9052);
23+
$client = new Client('127.0.0.1:9052', $loop);
2424
$connector->addConnectionManager($client->createConnector());
2525

2626
// forward to socks server listening on 127.0.0.1:9053
27-
$client = new Client($loop, '127.0.0.1', 9053);
27+
$client = new Client('127.0.0.1:9053', $loop);
2828
$connector->addConnectionManager($client->createConnector());
2929

3030
// start the server socket listening on localhost:$port for incoming socks connections

examples/server-middleman.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
$loop = React\EventLoop\Factory::create();
1313

1414
// set next SOCKS server localhost:$otherPort as target
15-
$target = new Client($loop, '127.0.0.1', $otherPort);
16-
$target->setAuth('user','p@ssw0rd');
15+
$target = new Client('127.0.0.1:' . $otherPort, $loop);
16+
$target->setAuth('user', 'p@ssw0rd');
1717

1818
// listen on localhost:$myPort
1919
$socket = new Socket($loop);

src/Client.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,19 @@ class Client
4646

4747
protected $auth = null;
4848

49-
public function __construct(LoopInterface $loop, $socksHost, $socksPort, ConnectorInterface $connector = null, Resolver $resolver = null)
49+
public function __construct($socksUrl, LoopInterface $loop, ConnectorInterface $connector = null, Resolver $resolver = null)
5050
{
51+
// assume default scheme if none is given
52+
if (strpos($socksUrl, '://') === false) {
53+
$socksUrl = 'socks://' . $socksUrl;
54+
}
55+
56+
// parse URL into individual parts
57+
$parts = parse_url($socksUrl);
58+
if (!$parts || !isset($parts['scheme'], $parts['host'], $parts['port'])) {
59+
throw new \InvalidArgumentException('Invalid SOCKS URL');
60+
}
61+
5162
if ($resolver === null) {
5263
// default to using Google's public DNS server
5364
$dnsResolverFactory = new DnsFactory();
@@ -58,8 +69,8 @@ public function __construct(LoopInterface $loop, $socksHost, $socksPort, Connect
5869
}
5970

6071
$this->loop = $loop;
61-
$this->socksHost = $socksHost;
62-
$this->socksPort = $socksPort;
72+
$this->socksHost = $parts['host'];
73+
$this->socksPort = $parts['port'];
6374
$this->connector = $connector;
6475
$this->resolver = $resolver;
6576

tests/ClientTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,33 @@
44

55
class ClientTest extends TestCase
66
{
7+
private $loop;
8+
79
/** @var Client */
810
private $client;
911

1012
public function setUp()
1113
{
12-
$loop = React\EventLoop\Factory::create();
13-
$this->client = new Client($loop, '127.0.0.1', 9050);
14+
$this->loop = React\EventLoop\Factory::create();
15+
$this->client = new Client('127.0.0.1:9050', $this->loop);
16+
}
17+
18+
public function testCtorAcceptsUriWithHostAndPort()
19+
{
20+
$client = new Client('127.0.0.1:9050', $this->loop);
21+
}
22+
23+
public function testCtorAcceptsUriWithScheme()
24+
{
25+
$client = new Client('socks://127.0.0.1:9050', $this->loop);
26+
}
27+
28+
/**
29+
* @expectedException InvalidArgumentException
30+
*/
31+
public function testCtorThrowsForInvalidUri()
32+
{
33+
new Client('12345', $this->loop);
1434
}
1535

1636
/**

tests/FunctionalTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function setUp()
2020
$this->assertNotEquals(0, $port);
2121

2222
$this->server = new Server($this->loop, $socket);
23-
$this->client = new Client($this->loop, '127.0.0.1', $port);
23+
$this->client = new Client('127.0.0.1:' . $port, $this->loop);
2424
}
2525

2626
public function testConnection()

0 commit comments

Comments
 (0)