-
-
Notifications
You must be signed in to change notification settings - Fork 980
Expand file tree
/
Copy pathSocks5Connector.cs
More file actions
279 lines (220 loc) · 10.2 KB
/
Socks5Connector.cs
File metadata and controls
279 lines (220 loc) · 10.2 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using System;
using System.Buffers.Binary;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Text;
using Microsoft.Extensions.Logging;
using Renci.SshNet.Abstractions;
using Renci.SshNet.Common;
namespace Renci.SshNet.Connection
{
/// <summary>
/// Establishes a tunnel via a SOCKS5 proxy server.
/// </summary>
/// <remarks>
/// https://en.wikipedia.org/wiki/SOCKS#SOCKS5.
/// </remarks>
internal sealed class Socks5Connector : ProxyConnector
{
public Socks5Connector(ISocketFactory socketFactory, ILoggerFactory loggerFactory)
: base(socketFactory, loggerFactory)
{
}
/// <summary>
/// Establishes a connection to the server via a SOCKS5 proxy.
/// </summary>
/// <param name="connectionInfo">The connection information.</param>
/// <param name="socket">The <see cref="Socket"/>.</param>
protected override void HandleProxyConnect(IConnectionInfo connectionInfo, Socket socket)
{
var greeting = new byte[]
{
// SOCKS version number
0x05,
// Number of supported authentication methods
0x02,
// No authentication
0x00,
// Username/Password authentication
0x02
};
SocketAbstraction.Send(socket, greeting);
var socksVersion = SocketReadByte(socket, connectionInfo.Timeout);
if (socksVersion != 0x05)
{
throw new ProxyException(string.Format("SOCKS Version '{0}' is not supported.", socksVersion));
}
var authenticationMethod = SocketReadByte(socket, connectionInfo.Timeout);
switch (authenticationMethod)
{
case 0x00:
// No authentication
break;
case 0x02:
// Create username/password authentication request
var authenticationRequest = CreateSocks5UserNameAndPasswordAuthenticationRequest(connectionInfo.ProxyUsername, connectionInfo.ProxyPassword);
// Send authentication request
SocketAbstraction.Send(socket, authenticationRequest);
// Read authentication result
var authenticationResult = SocketAbstraction.Read(socket, 2, connectionInfo.Timeout);
if (authenticationResult[0] != 0x01)
{
throw new ProxyException("SOCKS5: Server authentication version is not valid.");
}
if (authenticationResult[1] != 0x00)
{
throw new ProxyException("SOCKS5: Username/Password authentication failed.");
}
break;
case 0xFF:
throw new ProxyException("SOCKS5: No acceptable authentication methods were offered.");
default:
throw new ProxyException($"SOCKS5: Chosen authentication method '0x{authenticationMethod:x2}' is not supported.");
}
var connectionRequest = CreateSocks5ConnectionRequest(connectionInfo.Host, (ushort)connectionInfo.Port);
SocketAbstraction.Send(socket, connectionRequest);
// Read Server SOCKS5 version
if (SocketReadByte(socket, connectionInfo.Timeout) != 5)
{
throw new ProxyException("SOCKS5: Version 5 is expected.");
}
// Read response code
var status = SocketReadByte(socket, connectionInfo.Timeout);
switch (status)
{
case 0x00:
break;
case 0x01:
throw new ProxyException("SOCKS5: General failure.");
case 0x02:
throw new ProxyException("SOCKS5: Connection not allowed by ruleset.");
case 0x03:
throw new ProxyException("SOCKS5: Network unreachable.");
case 0x04:
throw new ProxyException("SOCKS5: Host unreachable.");
case 0x05:
throw new ProxyException("SOCKS5: Connection refused by destination host.");
case 0x06:
throw new ProxyException("SOCKS5: TTL expired.");
case 0x07:
throw new ProxyException("SOCKS5: Command not supported or protocol error.");
case 0x08:
throw new ProxyException("SOCKS5: Address type not supported.");
default:
throw new ProxyException("SOCKS5: Not valid response.");
}
// Read reserved byte
if (SocketReadByte(socket, connectionInfo.Timeout) != 0)
{
throw new ProxyException("SOCKS5: 0 byte is expected.");
}
var addressType = SocketReadByte(socket, connectionInfo.Timeout);
switch (addressType)
{
case 0x01:
var ipv4 = new byte[4];
_ = SocketRead(socket, ipv4, 0, 4, connectionInfo.Timeout);
break;
case 0x04:
var ipv6 = new byte[16];
_ = SocketRead(socket, ipv6, 0, 16, connectionInfo.Timeout);
break;
default:
throw new ProxyException(string.Format("Address type '{0}' is not supported.", addressType));
}
var port = new byte[2];
// Read 2 bytes to be ignored
_ = SocketRead(socket, port, 0, 2, connectionInfo.Timeout);
}
/// <summary>
/// https://tools.ietf.org/html/rfc1929.
/// </summary>
private static byte[] CreateSocks5UserNameAndPasswordAuthenticationRequest(string username, string password)
{
if (username.Length > byte.MaxValue)
{
throw new ProxyException("Proxy username is too long.");
}
if (password.Length > byte.MaxValue)
{
throw new ProxyException("Proxy password is too long.");
}
var authenticationRequest = new byte[// Version of the negotiation
1 +
// Length of the username
1 +
// Username
username.Length +
// Length of the password
1 +
// Password
password.Length];
var index = 0;
// Version of the negotiation
authenticationRequest[index++] = 0x01;
// Length of the username
authenticationRequest[index++] = (byte)username.Length;
// Username
_ = SshData.Ascii.GetBytes(username, 0, username.Length, authenticationRequest, index);
index += username.Length;
// Length of the password
authenticationRequest[index++] = (byte)password.Length;
// Password
_ = SshData.Ascii.GetBytes(password, 0, password.Length, authenticationRequest, index);
return authenticationRequest;
}
private static byte[] CreateSocks5ConnectionRequest(string hostname, ushort port)
{
var addressBytes = GetSocks5DestinationAddress(hostname, out var addressType);
var connectionRequest = new byte[// SOCKS version number
1 +
// Command code
1 +
// Reserved
1 +
// Address type
1 +
// Address
addressBytes.Length +
// Port number
2];
var index = 0;
// SOCKS version number
connectionRequest[index++] = 0x05;
// Command code
connectionRequest[index++] = 0x01; // establish a TCP/IP stream connection
// Reserved
connectionRequest[index++] = 0x00;
// Address type
connectionRequest[index++] = addressType;
// Address
Buffer.BlockCopy(addressBytes, 0, connectionRequest, index, addressBytes.Length);
index += addressBytes.Length;
// Port number
BinaryPrimitives.WriteUInt16BigEndian(connectionRequest.AsSpan(index), port);
return connectionRequest;
}
private static byte[] GetSocks5DestinationAddress(string hostname, out byte addressType)
{
if (IPAddress.TryParse(hostname, out var ipAddress))
{
Debug.Assert(ipAddress.AddressFamily is AddressFamily.InterNetwork or AddressFamily.InterNetworkV6);
addressType = ipAddress.AddressFamily == AddressFamily.InterNetwork
? (byte)0x01 // IPv4
: (byte)0x04; // IPv6
return ipAddress.GetAddressBytes();
}
addressType = 0x03; // Domain name
var byteCount = Encoding.UTF8.GetByteCount(hostname);
if (byteCount > byte.MaxValue)
{
throw new ProxyException(string.Format("SOCKS5: SOCKS 5 cannot support host names longer than 255 chars ('{0}').", hostname));
}
var address = new byte[1 + byteCount];
address[0] = (byte)byteCount;
_ = Encoding.UTF8.GetBytes(hostname, 0, hostname.Length, address, 1);
return address;
}
}
}