Skip to content

Commit 58ff8c4

Browse files
committed
Remove unneeded Server::setProtocolVersion() method
1 parent 3e2e295 commit 58ff8c4

4 files changed

Lines changed: 13 additions & 118 deletions

File tree

README.md

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ of the actual application level protocol, such as HTTP, SMTP, IMAP, Telnet etc.
2424
* [Unix domain sockets](#unix-domain-sockets)
2525
* [Server](#server)
2626
* [Server connector](#server-connector)
27-
* [Protocol version](#server-protocol-version)
2827
* [Authentication](#server-authentication)
2928
* [Proxy chaining](#server-proxy-chaining)
3029
* [SOCKS over TLS](#server-socks-over-tls)
@@ -613,6 +612,7 @@ $client = new Client('socks+unix://user:pass@/tmp/proxy.sock', new Connector($lo
613612

614613
The `Server` is responsible for accepting incoming communication from SOCKS clients
615614
and forwarding the requested connection to the target host.
615+
It supports the SOCKS5 and SOCKS4(a) protocol versions by default.
616616
It also registers everything with the main [`EventLoop`](https://github.com/reactphp/event-loop#usage)
617617
and an underlying TCP/IP socket server like this:
618618

@@ -662,23 +662,6 @@ You can use this parameter for logging purposes or to restrict connection
662662
requests for certain clients by providing a custom implementation of the
663663
[`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface).
664664

665-
#### Server protocol version
666-
667-
The `Server` supports the SOCKS5 and SOCKS4(a) protocol versions by default.
668-
669-
If want to explicitly set the protocol version, use the supported values `4`, or `5`:
670-
671-
```PHP
672-
$server->setProtocolVersion(5);
673-
```
674-
675-
In order to reset the protocol version to its default (i.e. automatic detection),
676-
use `null` as protocol version.
677-
678-
```PHP
679-
$server->setProtocolVersion(null);
680-
```
681-
682665
#### Server authentication
683666

684667
By default, the `Server` does not require any authentication from the clients.
@@ -785,8 +768,8 @@ Proxy chaining can happen on the server side and/or the client side:
785768

786769
#### Server SOCKS over TLS
787770

788-
All [SOCKS protocol versions](#server-protocol-version) support forwarding TCP/IP
789-
based connections and higher level protocols.
771+
Both SOCKS5 and SOCKS4(a) protocol versions support forwarding TCP/IP based
772+
connections and higher level protocols.
790773
This implies that you can also use [secure TLS connections](#secure-tls-connections)
791774
to transfer sensitive data across SOCKS proxy servers.
792775
This means that no eavesdropper nor the proxy server will be able to decrypt
@@ -837,8 +820,8 @@ See also [example 31](examples).
837820

838821
#### Server Unix domain sockets
839822

840-
All [SOCKS protocol versions](#server-protocol-version) support forwarding TCP/IP
841-
based connections and higher level protocols.
823+
Both SOCKS5 and SOCKS4(a) protocol versions support forwarding TCP/IP based
824+
connections and higher level protocols.
842825
In some advanced cases, it may be useful to let your SOCKS server listen on a
843826
Unix domain socket (UDS) path instead of a IP:port combination.
844827
For example, this allows you to rely on file system permissions instead of

src/Server.php

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ final class Server
4141

4242
private $auth = null;
4343

44-
private $protocolVersion = null;
45-
4644
public function __construct(LoopInterface $loop, ConnectorInterface $connector = null)
4745
{
4846
if ($connector === null) {
@@ -65,28 +63,12 @@ public function listen(ServerInterface $socket)
6563
});
6664
}
6765

68-
public function setProtocolVersion($version)
69-
{
70-
if ($version !== null) {
71-
$version = (string)$version;
72-
if (!in_array($version, array('4', '5'), true)) {
73-
throw new InvalidArgumentException('Invalid protocol version given');
74-
}
75-
if ($version !== '5' && $this->auth !== null){
76-
throw new UnexpectedValueException('Unable to change protocol version to anything but SOCKS5 while authentication is used. Consider removing authentication info or sticking to SOCKS5');
77-
}
78-
}
79-
$this->protocolVersion = $version;
80-
}
81-
8266
public function setAuth($auth)
8367
{
8468
if (!is_callable($auth)) {
8569
throw new InvalidArgumentException('Given authenticator is not a valid callable');
8670
}
87-
if ($this->protocolVersion !== null && $this->protocolVersion !== '5') {
88-
throw new UnexpectedValueException('Authentication requires SOCKS5. Consider using protocol version 5 or waive authentication');
89-
}
71+
9072
// wrap authentication callback in order to cast its return value to a promise
9173
$this->auth = function($username, $password, $remote) use ($auth) {
9274
$ret = call_user_func($auth, $username, $password, $remote);
@@ -163,26 +145,15 @@ private function handleSocks(ConnectionInterface $stream)
163145
$stream->on('data', array($reader, 'write'));
164146

165147
$that = $this;
166-
$that = $this;
167-
168148
$auth = $this->auth;
169-
$protocolVersion = $this->protocolVersion;
170149

171-
// authentication requires SOCKS5
172-
if ($auth !== null) {
173-
$protocolVersion = '5';
174-
}
175-
176-
return $reader->readByte()->then(function ($version) use ($stream, $that, $protocolVersion, $auth, $reader){
150+
return $reader->readByte()->then(function ($version) use ($stream, $that, $auth, $reader){
177151
if ($version === 0x04) {
178-
if ($protocolVersion === '5') {
179-
throw new UnexpectedValueException('SOCKS4 not allowed due to configuration');
152+
if ($auth !== null) {
153+
throw new UnexpectedValueException('SOCKS4 not allowed because authentication is required');
180154
}
181155
return $that->handleSocks4($stream, $reader);
182156
} else if ($version === 0x05) {
183-
if ($protocolVersion !== null && $protocolVersion !== '5') {
184-
throw new UnexpectedValueException('SOCKS5 not allowed due to configuration');
185-
}
186157
return $that->handleSocks5($stream, $auth, $reader);
187158
}
188159
throw new UnexpectedValueException('Unexpected/unknown version number');

tests/FunctionalTest.php

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ public function testConnectionInvalid()
5050

5151
public function testConnectionWithIpViaSocks4()
5252
{
53-
$this->server->setProtocolVersion('4');
54-
5553
$this->client = new Client('socks4://127.0.0.1:' . $this->port, $this->connector);
5654

5755
$this->assertResolveStream($this->client->connect('127.0.0.1:' . $this->port));
@@ -81,20 +79,11 @@ public function testConnectionWithIpv6ViaSocks4Fails()
8179
/** @group internet */
8280
public function testConnectionSocks5()
8381
{
84-
$this->server->setProtocolVersion(5);
8582
$this->client = new Client('socks5://127.0.0.1:' . $this->port, $this->connector);
8683

8784
$this->assertResolveStream($this->client->connect('www.google.com:80'));
8885
}
8986

90-
/** @group internet */
91-
public function testConnectionDefaultsToSocks5()
92-
{
93-
$this->server->setProtocolVersion(5);
94-
95-
$this->assertResolveStream($this->client->connect('www.google.com:80'));
96-
}
97-
9887
/** @group internet */
9988
public function testConnectionSocksOverTls()
10089
{
@@ -173,7 +162,6 @@ public function testConnectionSocks5OverUnix()
173162
$socket = new UnixServer($path, $this->loop);
174163
$this->server = new Server($this->loop);
175164
$this->server->listen($socket);
176-
$this->server->setProtocolVersion(5);
177165

178166
$this->connector = new Connector($this->loop);
179167
$this->client = new Client('socks5+unix://' . $path, $this->connector);
@@ -287,20 +275,13 @@ public function testConnectionAuthenticationUnused()
287275
$this->assertResolveStream($this->client->connect('www.google.com:80'));
288276
}
289277

290-
public function testConnectionInvalidProtocolDoesNotMatchSocks5()
278+
public function testConnectionInvalidNoAuthenticationOverLegacySocks4()
291279
{
292-
$this->server->setProtocolVersion(5);
293-
$this->client = new Client('socks4://127.0.0.1:' . $this->port, $this->connector);
294-
295-
$this->assertRejectPromise($this->client->connect('www.google.com:80'), null, SOCKET_ECONNRESET);
296-
}
280+
$this->server->setAuthArray(array('name' => 'pass'));
297281

298-
public function testConnectionInvalidProtocolDoesNotMatchSocks4()
299-
{
300-
$this->server->setProtocolVersion(4);
301-
$this->client = new Client('socks5://127.0.0.1:' . $this->port, $this->connector);
282+
$this->client = new Client('socks4://127.0.0.1:' . $this->port, $this->connector);
302283

303-
$this->assertRejectPromise($this->client->connect('www.google.com:80'), null, SOCKET_ECONNRESET);
284+
$this->assertRejectPromise($this->client->connect('www.google.com:80'));
304285
}
305286

306287
public function testConnectionInvalidNoAuthentication()

tests/ServerTest.php

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,6 @@ public function testListen()
3131
$this->server->listen($socket);
3232
}
3333

34-
public function testSetProtocolVersion()
35-
{
36-
$this->server->setProtocolVersion(4);
37-
$this->server->setProtocolVersion(5);
38-
$this->server->setProtocolVersion(null);
39-
40-
$this->assertTrue(true);
41-
}
42-
43-
/**
44-
* @expectedException InvalidArgumentException
45-
*/
46-
public function testSetInvalidProtocolVersion()
47-
{
48-
$this->server->setProtocolVersion(6);
49-
}
50-
5134
public function testSetAuthArray()
5235
{
5336
$this->server->setAuthArray(array());
@@ -68,29 +51,6 @@ public function testSetAuthInvalid()
6851
$this->server->setAuth(true);
6952
}
7053

71-
/**
72-
* @expectedException UnexpectedValueException
73-
*/
74-
public function testUnableToSetAuthIfProtocolDoesNotSupportAuth()
75-
{
76-
$this->server->setProtocolVersion(4);
77-
78-
$this->server->setAuthArray(array());
79-
}
80-
81-
/**
82-
* @expectedException UnexpectedValueException
83-
*/
84-
public function testUnableToSetProtocolWhichDoesNotSupportAuth()
85-
{
86-
$this->server->setAuthArray(array());
87-
88-
// this is okay
89-
$this->server->setProtocolVersion(5);
90-
91-
$this->server->setProtocolVersion(4);
92-
}
93-
9454
public function testConnectWillCreateConnection()
9555
{
9656
$stream = $this->getMockBuilder('React\Socket\ConnectionInterface')->getMock();

0 commit comments

Comments
 (0)