-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocksConnection.cs
More file actions
203 lines (175 loc) · 7.3 KB
/
SocksConnection.cs
File metadata and controls
203 lines (175 loc) · 7.3 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
//-----------------------------------------------------------------------------
// Copyright (c) 2016 Silv3r & g0ld
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
// SocksConnection is a simple static class to connect a socket to an address
// through one or more servers SOCKS.
// Its code is distributed under the MIT license.
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
// TODO: GSSAPI
// cf. http://tools.ietf.org/rfc/rfc1928.txt
// https://msdn.microsoft.com/en-us/library/ms995352.aspx
// https://msdn.microsoft.com/en-us/library/system.servicemodel.security.sspisecuritytokenprovider.aspx
// May god have mercy upon you
public static class SocksConnection
{
public static async Task<Socket> OpenConnection(int version, string serverAddress, int serverPort,
string socksAddress, int socksPort, string username = null, string password = null)
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
var endPoint = new DnsEndPoint(socksAddress, socksPort);
Func<AsyncCallback, object, IAsyncResult> begin =
(cb, s) => socket.BeginConnect(endPoint, cb, s);
await Task.Factory.FromAsync(begin, socket.EndConnect, null);
await OpenConnectionFromSocks(socket, version, serverAddress, serverPort, username, password);
return socket;
}
public static async Task OpenConnectionFromSocks(Socket socket, int version, string serverAddress, int serverPort,
string username = null, string password = null)
{
if (socket == null)
{
throw new ArgumentNullException("Socket cannot be null");
}
else if (!socket.Connected)
{
throw new ArgumentException("Socket has to be connected");
}
if (version == 5)
{
await HandleSocks5(socket, serverAddress, serverPort, username, password);
}
else if (version == 4)
{
await HandleSocks4(socket, serverAddress, serverPort);
}
}
private static async Task SendAsync(Socket socket, byte[] buffer, int offset, int count)
{
Func<AsyncCallback, object, IAsyncResult> begin =
(cb, s) => socket.BeginSend(buffer, offset, count, SocketFlags.None, cb, s);
await Task.Factory.FromAsync<int>(begin, socket.EndSend, null);
}
private static async Task ReceiveAsync(Socket socket, byte[] buffer, int offset, int count)
{
Func<AsyncCallback, object, IAsyncResult> begin =
(cb, s) => socket.BeginReceive(buffer, offset, count, SocketFlags.None, cb, s);
await Task.Factory.FromAsync<int>(begin, socket.EndReceive, null);
}
private static async Task HandleSocks5(Socket socket, string serverAddress, int serverPort, string username, string password)
{
byte[] buffer = new byte[1024];
buffer[0] = 0x05;
if (username != null)
{
buffer[1] = 0x02;
buffer[2] = 0x00;
buffer[3] = 0x02;
await SendAsync(socket, buffer, 0, 4);
}
else
{
buffer[1] = 0x01;
buffer[2] = 0x00;
await SendAsync(socket, buffer, 0, 3);
}
await ReceiveAsync(socket, buffer, 0, 2);
if (buffer[0] != 0x05)
{
throw new Exception("Received invalid version from the proxy server");
}
if (buffer[1] == 0x02)
{
byte[] usernameArray = Encoding.ASCII.GetBytes(username);
byte[] passwordArray = Encoding.ASCII.GetBytes(password);
int i = 0;
buffer[i++] = 0x01;
buffer[i++] = (byte)username.Length;
Array.Copy(usernameArray, 0, buffer, i, username.Length);
i += username.Length;
buffer[i++] = (byte)password.Length;
Array.Copy(passwordArray, 0, buffer, i, password.Length);
i += password.Length;
await SendAsync(socket, buffer, 0, i);
await ReceiveAsync(socket, buffer, 0, 2);
if (buffer[0] != 1)
{
throw new Exception("Received invalid authentication version from the proxy server");
}
if (buffer[1] != 0)
{
throw new Exception("The proxy server has refused the username/password authentication");
}
}
else if (buffer[1] != 0x00)
{
throw new Exception("Received invalid authentication method from the proxy server");
}
byte[] address = IPAddress.Parse(serverAddress).GetAddressBytes();
byte[] port = BitConverter.GetBytes((ushort)serverPort);
Array.Reverse(port);
buffer[0] = 0x05;
buffer[1] = 0x01;
buffer[2] = 0x00;
buffer[3] = 0x01;
Array.Copy(address, 0, buffer, 4, 4);
Array.Copy(port, 0, buffer, 8, 2);
await SendAsync(socket, buffer, 0, 10);
await ReceiveAsync(socket, buffer, 0, 10);
if (buffer[0] != 5)
{
throw new Exception("Received invalid version from the proxy server");
}
if (buffer[1] != 0)
{
throw new Exception("Received connection failure from the proxy server");
}
if (buffer[3] != 0x01)
{
throw new Exception("Received invalid address type from the proxy server");
}
}
private static async Task HandleSocks4(Socket socket, string serverAddress, int serverPort)
{
byte[] buffer = new byte[1024];
byte[] address = IPAddress.Parse(serverAddress).GetAddressBytes();
byte[] port = BitConverter.GetBytes((ushort)serverPort);
Array.Reverse(port);
buffer[0] = 0x04;
buffer[1] = 0x01;
Array.Copy(port, 0, buffer, 2, 2);
Array.Copy(address, 0, buffer, 4, 4);
buffer[8] = 0x00;
await SendAsync(socket, buffer, 0, 9);
await ReceiveAsync(socket, buffer, 0, 8);
if (buffer[0] != 0)
{
throw new Exception("Received invalid header from the proxy server");
}
if (buffer[1] != 0x5a)
{
throw new Exception("The proxy server rejected the connection");
}
}
}