Skip to content

Commit b463286

Browse files
committed
Merge pull request #26 from clue-labs/port
Automatically assume default SOCKS port (1080) if not given explicitly
2 parents 898706f + 75110fc commit b463286

3 files changed

Lines changed: 18 additions & 2 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ $loop = \React\EventLoop\Factory::create();
131131
$client = new Client('127.0.0.1:1080', $loop);
132132
```
133133

134+
You can omit the port if you're using the default SOCKS port 1080:
135+
136+
```php
137+
$client = new Client('127.0.0.1', $loop);
138+
```
139+
134140
If you need custom connector settings (DNS resolution, timeouts etc.), you can explicitly pass a
135141
custom instance of the [`ConnectorInterface`](https://github.com/reactphp/socket-client#connectorinterface):
136142

src/Client.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,15 @@ public function __construct($socksUri, LoopInterface $loop, ConnectorInterface $
5555

5656
// parse URI into individual parts
5757
$parts = parse_url($socksUri);
58-
if (!$parts || !isset($parts['scheme'], $parts['host'], $parts['port'])) {
58+
if (!$parts || !isset($parts['scheme'], $parts['host'])) {
5959
throw new \InvalidArgumentException('Invalid SOCKS server URI "' . $socksUri . '"');
6060
}
6161

62+
// assume default port
63+
if (!isset($parts['port'])) {
64+
$parts['port'] = 1080;
65+
}
66+
6267
if ($resolver === null) {
6368
// default to using Google's public DNS server
6469
$dnsResolverFactory = new DnsFactory();

tests/ClientTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,17 @@ public function testCtorAcceptsUriWithScheme()
2525
$client = new Client('socks://127.0.0.1:9050', $this->loop);
2626
}
2727

28+
public function testCtorAcceptsUriWithHostOnlyAssumesDefaultPort()
29+
{
30+
$client = new Client('127.0.0.1', $this->loop);
31+
}
32+
2833
/**
2934
* @expectedException InvalidArgumentException
3035
*/
3136
public function testCtorThrowsForInvalidUri()
3237
{
33-
new Client('12345', $this->loop);
38+
new Client('////', $this->loop);
3439
}
3540

3641
/**

0 commit comments

Comments
 (0)