-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsocks.cpp
214 lines (175 loc) · 4.44 KB
/
socks.cpp
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
/*
SOCKS5 Client Library
Copyright (c) 2016 NuclearC
*/
#include "socks.h"
namespace socks5cpp {
SocksClient::SocksClient(std::string _Ip, std::string _TargetIp) :
ip(_Ip) ,targetIP(_TargetIp)
{
url = scanURL(ip);
targetUrl = scanURL(targetIP);
}
SocksClient::~SocksClient()
{
destroy();
}
SocksClient::SocksUrl SocksClient::scanURL(const std::string _url)
{
SocksClient::SocksUrl res;
const char * url = _url.c_str();
char ip[256];
unsigned int port;
// scan string
if (sscanf(url, "ws://%19[^:]:%d", ip, &port)) {
res.ip = ip;
res.port = port;
res.type = SocksUrl::Type::WS;
}
else if (sscanf(url, "wss://%19[^:]:%d", ip, &port)) {
res.ip = ip;
res.port = port;
res.type = SocksUrl::Type::WSS;
}
else if (sscanf(url, "http://%19[^:]:%d", ip, &port)) {
res.ip = ip;
res.port = port;
res.type = SocksUrl::Type::HTTP;
}
else if (sscanf(url, "%19[^:]:%d", ip, &port)) {
res.ip = ip;
res.port = port;
res.type = SocksUrl::Type::HTTP;
}
sscanf(res.ip.c_str(), "%hu.%hu.%hu.%hu", &res.ipv4[0], &res.ipv4[1],
&res.ipv4[2], &res.ipv4[3]);
return res;
}
int SocksClient::connect(SOCKET& sfd)
{
state = SocksState::Connecting;
struct addrinfo
*result = NULL,
*ptr = NULL,
hints;
int iResult;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
iResult = getaddrinfo(url.ip.c_str(), std::to_string(url.port).c_str(),
&hints, &result);
if (iResult != 0) {
return -1;
}
// Attempt to connect to an address until one succeeds
for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
// Create a SOCKET for connecting to server
sfd = ::socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol);
if (sfd == INVALID_SOCKET) {
return -1;
}
// Connect to server.
iResult = ::connect(sfd, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(sfd);
sfd = INVALID_SOCKET;
continue;
}
// put non-blocking mode
u_long iMode = 1;
iResult = ioctlsocket(sfd, FIONBIO, &iMode);
break;
}
freeaddrinfo(result);
if (sfd == INVALID_SOCKET) {
return -1;
}
// Handhake with SOCKS5 proxy
std::vector<uint8_t> buffer;
buffer.clear();
buffer.push_back(0x05); // version
buffer.push_back(0x01); // method count
buffer.push_back(0x00); // first method
::send(sfd, reinterpret_cast<const char*>(buffer.data()), buffer.size(), 0);
char handshakeBuffer[512];
do
{
iResult = recv(sfd, handshakeBuffer, 512, 0);
if (iResult > 0)
{
switch (handshakeBuffer[1])
{
case 0x00:
break;
default:
return -1;
break;
}
break;
}
} while (true);
buffer.clear();
buffer.push_back(0x05); // version
buffer.push_back(0x01); // TCP/IP
buffer.push_back(0x00); // must be 0x00 always
buffer.push_back(0x01); // IPv4
buffer.push_back(targetUrl.ipv4[0]);
buffer.push_back(targetUrl.ipv4[1]);
buffer.push_back(targetUrl.ipv4[2]);
buffer.push_back(targetUrl.ipv4[3]);
buffer.push_back(targetUrl.port >> 8);
buffer.push_back(targetUrl.port & 0xff);
::send(sfd, reinterpret_cast<const char*>(buffer.data()), buffer.size(), 0);
std::chrono::time_point<std::chrono::steady_clock> begin =
std::chrono::high_resolution_clock::now();
std::chrono::time_point<std::chrono::steady_clock> end;
std::chrono::duration<float> duration;
std::chrono::milliseconds time;
do
{
end = std::chrono::high_resolution_clock::now();
duration = end - begin;
time = std::chrono::duration_cast<std::chrono::milliseconds>(duration);
if (time.count() > timeoutDuration)
{
iResult = -2;
break;
}
iResult = recv(sfd, handshakeBuffer, 512, 0);
if (iResult > 0)
{
iResult = handshakeBuffer[1];
switch (iResult)
{
case 0x00:
state = SocksState::Open;
break;
default:
state = SocksState::Closed;
break;
}
break;
}
} while (true);
return iResult;
}
int SocksClient::sendPacket(SOCKET& sfd, const char * _Buffer, const size_t _Size)
{
return ::send(sfd, _Buffer, _Size, 0);
}
int SocksClient::recvPacket(SOCKET& sfd, char * _Buffer, const size_t _Size)
{
return ::recv(sfd, _Buffer, _Size, 0);
}
void SocksClient::destroy()
{
state = SocksState::Closed;
}
const SocksClient::SocksState & SocksClient::getState()
{
return state;
}
} // socks5cpp