-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSocksTcpClient.cs
142 lines (114 loc) · 4.68 KB
/
SocksTcpClient.cs
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
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
namespace System.Net.Socks
{
public delegate void ClientStatusChangedEvent(object sender, string status);
public class SocksTcpClient
{
public static event ClientStatusChangedEvent StatusChanged;
private enum SocksType
{
Socks4 = 0x04,
Socks5 = 0x05
}
private enum Authentication
{
None = 0x00,
GSSApi = 0x01,
Credentials = 0x02,
NoAcceptableMethods = 0xFF
}
private enum Method
{
Connect = 0x01,
Bind = 0x02,
UDP = 0x03
}
private enum AddressType
{
IPv4 = 0x01,
IPv6 = 0x04
}
private static string[] errorMsgs = new string[] {
"Successfully connected to remote host!",
"General SOCKS server failure!",
"Connection not allowed by ruleset!",
"Network unreachable!",
"Host unreachable!",
"Connection refused!",
"TTL expired!",
"Command not supported!",
"Address type not supported!"
};
public static TcpClient Connect(string proxyHost, int proxyPort, string targetHost, int targetPort)
{
TcpClient SocksClient = new TcpClient();
IPAddress proxyHostAddress;
if (!IPAddress.TryParse(proxyHost, out proxyHostAddress))
proxyHostAddress = Dns.GetHostAddresses(proxyHost)[0];
IPEndPoint proxyEndPoint = new IPEndPoint(proxyHostAddress, proxyPort);
IPAddress targetHostAddress;
if (!IPAddress.TryParse(targetHost, out targetHostAddress))
targetHostAddress = Dns.GetHostAddresses(targetHost).Where(ip => ip.AddressFamily == AddressFamily.InterNetwork).FirstOrDefault();
Socket socket = SocksClient.Client;
socket.Connect(proxyEndPoint);
socket = Negotiate(socket, targetHostAddress, targetPort);
if (socket == null)
return null;
return SocksClient;
}
private static Socket Negotiate(Socket socket, IPAddress targetHostAddress, int targetPort)
{
byte[] requestBytes = new byte[] { (byte)SocksType.Socks5, 0x03, (byte)Authentication.None, (byte)Authentication.GSSApi, (byte)Authentication.NoAcceptableMethods };
byte[] responseBytes = new byte[2];
socket.Send(requestBytes, requestBytes.Length, SocketFlags.None);
int receivedBytes = socket.Receive(responseBytes, 2, SocketFlags.None);
if (receivedBytes < 2)
{
OnStatusChanged(socket, "Bad proxy!");
return null;
}
if (responseBytes[0] == (byte)SocksType.Socks5 && responseBytes[1] == (byte)Authentication.None)
{
OnStatusChanged(socket, "Successfully negotiated with the proxy!");
socket = Handshake(socket, targetHostAddress, targetPort);
return socket;
}
else
{
OnStatusChanged(socket, "Failed to negotiate with the proxy!");
return null;
}
}
private static Socket Handshake(Socket socket, IPAddress targetHostAddress, int targetPort)
{
byte[] addressBytes = targetHostAddress.GetAddressBytes();
byte[] portBytes = new byte[2] { (byte)(targetPort / 256), (byte)(targetPort % 256) };
byte[] requestBytes = new byte[10];
byte[] responseBytes = new byte[2];
requestBytes[0] = (byte)SocksType.Socks5;
requestBytes[1] = (byte)Method.Connect;
requestBytes[2] = 0x00;
if (targetHostAddress.AddressFamily == AddressFamily.InterNetwork)
requestBytes[3] = (byte)AddressType.IPv4;
else if (targetHostAddress.AddressFamily == AddressFamily.InterNetworkV6)
requestBytes[3] = (byte)AddressType.IPv6;
addressBytes.CopyTo(requestBytes, 4);
portBytes.CopyTo(requestBytes, 8);
socket.Send(requestBytes, 10, SocketFlags.None);
socket.Receive(responseBytes, SocketFlags.None);
OnStatusChanged(socket, errorMsgs[responseBytes[1]]);
if (responseBytes[1] != 0x00)
return null;
return socket;
}
private static void OnStatusChanged(object sender, string status)
{
ClientStatusChangedEvent handler = StatusChanged;
if (handler != null)
handler(sender, status);
}
}
}