-
-
Notifications
You must be signed in to change notification settings - Fork 714
/
Copy pathSocksProxy.php
218 lines (213 loc) · 7.85 KB
/
SocksProxy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
declare(strict_types=1);
/**
* Socks5 stream wrapper.
*
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2025 Daniil Gentili <daniil@daniil.it>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto\Stream\Proxy;
use Amp\Cancellation;
use Amp\Socket\ClientTlsContext;
use Amp\Socket\Socket;
use danog\MadelineProto\Exception;
use danog\MadelineProto\Logger;
use danog\MadelineProto\Stream\BufferedProxyStreamInterface;
use danog\MadelineProto\Stream\BufferedStreamInterface;
use danog\MadelineProto\Stream\ConnectionContext;
use danog\MadelineProto\Stream\RawProxyStreamInterface;
use danog\MadelineProto\Stream\RawStreamInterface;
use Webmozart\Assert\Assert;
/**
* Socks5 stream wrapper.
*
* @author Daniil Gentili <daniil@daniil.it>
*
* @implements RawProxyStreamInterface<array{address: string, port: int, username?: string, password?: string}>
* @implements BufferedProxyStreamInterface<array{address: string, port: int, username?: string, password?: string}>
*/
final class SocksProxy implements RawProxyStreamInterface, BufferedProxyStreamInterface
{
private const REPS = [0 => 'succeeded', 1 => 'general SOCKS server failure', 2 => 'connection not allowed by ruleset', 3 => 'Network unreachable', 4 => 'Host unreachable', 5 => 'Connection refused', 6 => 'TTL expired', 7 => 'Command not supported', 8 => 'Address type not supported'];
/**
* Stream.
*
*/
protected RawStreamInterface&BufferedStreamInterface $stream;
private $extra;
/**
* Connect to stream.
*
* @param ConnectionContext $ctx The connection context
*/
#[\Override]
public function connect(ConnectionContext $ctx, string $header = ''): void
{
$ctx = $ctx->clone();
$uri = $ctx->getUri();
$secure = $ctx->isSecure();
if ($secure) {
$ctx->setSocketContext($ctx->getSocketContext()->withTlsContext(new ClientTlsContext($uri->getHost())));
}
$ctx->setUri('tcp://'.$this->extra['address'].':'.$this->extra['port'])->secure(false);
$methods = \chr(0);
if (isset($this->extra['username']) && isset($this->extra['password'])) {
$methods .= \chr(2);
}
$this->stream = $ctx->getStream(\chr(5).\chr(\strlen($methods)).$methods);
Assert::true($this->stream instanceof BufferedStreamInterface);
Assert::true($this->stream instanceof RawStreamInterface);
$l = 2;
$buffer = $this->stream->getReadBuffer($l);
$version = \ord($buffer->bufferRead(1));
$method = \ord($buffer->bufferRead(1));
if ($version !== 5) {
throw new Exception("Wrong SOCKS5 version: {$version}");
}
if ($method === 2) {
$auth = \chr(1).\chr(\strlen($this->extra['username'])).$this->extra['username'].\chr(\strlen($this->extra['password'])).$this->extra['password'];
$this->stream->write($auth);
$buffer = $this->stream->getReadBuffer($l);
$version = \ord($buffer->bufferRead(1));
$result = \ord($buffer->bufferRead(1));
if ($version !== 1) {
throw new Exception("Wrong authorized SOCKS version: {$version}");
}
if ($result !== 0) {
throw new Exception("Wrong authorization status: {$version}");
}
} elseif ($method !== 0) {
throw new Exception("Wrong method: {$method}");
}
$payload = pack('C3', 0x5, 0x1, 0x0);
try {
$ip = inet_pton($uri->getHost());
$payload .= $ip ? pack('C1', \strlen($ip) === 4 ? 0x1 : 0x4).$ip : pack('C2', 0x3, \strlen($uri->getHost())).$uri->getHost();
} catch (Exception $e) {
$payload .= pack('C2', 0x3, \strlen($uri->getHost())).$uri->getHost();
}
$payload .= pack('n', $uri->getPort());
$this->stream->write($payload);
$l = 4;
$buffer = $this->stream->getReadBuffer($l);
$version = \ord($buffer->bufferRead(1));
if ($version !== 5) {
throw new Exception("Wrong SOCKS5 version: {$version}");
}
$rep = \ord($buffer->bufferRead(1));
if ($rep !== 0) {
$rep = self::REPS[$rep] ?? $rep;
throw new Exception("Wrong SOCKS5 rep: {$rep}");
}
$rsv = \ord($buffer->bufferRead(1));
if ($rsv !== 0) {
throw new Exception("Wrong socks5 final RSV: {$rsv}");
}
switch (\ord($buffer->bufferRead(1))) {
case 1:
$buffer = $this->stream->getReadBuffer($l);
$ip = inet_ntop($buffer->bufferRead(4));
break;
case 4:
$l = 16;
$buffer = $this->stream->getReadBuffer($l);
$ip = inet_ntop($buffer->bufferRead(16));
break;
case 3:
$l = 1;
$buffer = $this->stream->getReadBuffer($l);
$length = \ord($buffer->bufferRead(1));
$buffer = $this->stream->getReadBuffer($length);
$ip = $buffer->bufferRead($length);
break;
}
$l = 2;
$buffer = $this->stream->getReadBuffer($l);
$port = unpack('n', $buffer->bufferRead(2))[1];
Logger::log(['Connected to '.$ip.':'.$port.' via socks5']);
if ($secure) {
$this->getSocket()->setupTls();
}
if (\strlen($header)) {
($this->stream->getWriteBuffer(\strlen($header)))->bufferWrite($header);
}
}
/**
* Async close.
*/
#[\Override]
public function disconnect(): void
{
$this->stream->disconnect();
}
/**
* Get write buffer asynchronously.
*
* @param int $length Length of data that is going to be written to the write buffer
*/
#[\Override]
public function getWriteBuffer(int $length, string $append = ''): \danog\MadelineProto\Stream\WriteBufferInterface
{
return $this->stream->getWriteBuffer($length, $append);
}
/**
* Get read buffer asynchronously.
*
* @param int $length Length of payload, as detected by this layer
*/
#[\Override]
public function getReadBuffer(?int &$length): \danog\MadelineProto\Stream\ReadBufferInterface
{
return $this->stream->getReadBuffer($length);
}
#[\Override]
public function read(?Cancellation $token = null): ?string
{
return $this->stream->read($token);
}
#[\Override]
public function write(string $data): void
{
$this->stream->write($data);
}
/**
* Sets proxy data.
*
* @param array $extra Proxy data
*/
#[\Override]
public function setExtra($extra): void
{
$this->extra = $extra;
}
/**
* {@inheritDoc}
*/
#[\Override]
public function getStream(): RawStreamInterface
{
return $this->stream;
}
/**
* {@inheritdoc}
*/
#[\Override]
public function getSocket(): Socket
{
return $this->stream->getSocket();
}
#[\Override]
public static function getName(): string
{
return self::class;
}
}