-
Notifications
You must be signed in to change notification settings - Fork 125
/
socks_proxy.hpp
489 lines (406 loc) · 13.3 KB
/
socks_proxy.hpp
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//
// Copyright (C) 2019 Jack.
//
// Author: jack
// Email: jack.wgm at gmail dot com
//
#pragma once
#include <boost/asio/read.hpp>
#include <boost/asio/streambuf.hpp>
#include <boost/asio/spawn.hpp>
#include "url_parser.hpp"
#include "io.hpp"
#include "logging.hpp"
#include "handler_type_check.hpp"
namespace socks
{
//////////////////////////////////////////////////////////////////////////
class error_category_impl;
template<class error_category>
const boost::system::error_category& error_category_single()
{
static error_category error_category_instance;
return reinterpret_cast<const boost::system::error_category&>(error_category_instance);
}
inline const boost::system::error_category& error_category()
{
return error_category_single<socks::error_category_impl>();
}
namespace errc {
enum errc_t
{
/// SOCKS unsupported version.
socks_unsupported_version = 1000,
/// SOCKS username required.
socks_username_required,
/// SOCKS unsupported authentication version.
socks_unsupported_authentication_version,
/// SOCKS authentication error.
socks_authentication_error,
/// SOCKS general failure.
socks_general_failure,
/// SOCKS command not supported.
socks_command_not_supported,
/// SOCKS no identd running.
socks_no_identd,
/// SOCKS no identd running.
socks_identd_error,
/// request rejected or failed.
socks_request_rejected_or_failed,
/// request rejected becasue SOCKS server cannot connect to identd on the client.
socks_request_rejected_cannot_connect,
/// request rejected because the client program and identd report different user - ids
socks_request_rejected_incorrect_userid,
/// unknown error.
socks_unknown_error,
};
inline boost::system::error_code make_error_code(errc_t e)
{
return boost::system::error_code(static_cast<int>(e), socks::error_category());
}
}
class error_category_impl
: public boost::system::error_category
{
virtual const char* name() const BOOST_SYSTEM_NOEXCEPT
{
return "SOCKS";
}
virtual std::string message(int e) const
{
switch (e)
{
case errc::socks_unsupported_version:
return "SOCKS unsupported version";
case errc::socks_username_required:
return "SOCKS username required";
case errc::socks_unsupported_authentication_version:
return "SOCKS unsupported authentication version";
case errc::socks_authentication_error:
return "SOCKS authentication error";
case errc::socks_general_failure:
return "SOCKS general failure";
case errc::socks_command_not_supported:
return "SOCKS command not supported";
case errc::socks_no_identd:
return "SOCKS no identd running";
case errc::socks_identd_error:
return "SOCKS no identd running";
case errc::socks_request_rejected_or_failed:
return "SOCKS request rejected or failed";
case errc::socks_request_rejected_cannot_connect:
return "SOCKS request rejected becasue SOCKS server cannot connect to identd on the client";
case errc::socks_request_rejected_incorrect_userid:
return "SOCKS request rejected because the client program and identd report different user";
case errc::socks_unknown_error:
return "SOCKS unknown error";
default:
return "Unknown PROXY error";
}
}
};
}
namespace boost {
namespace system {
template <>
struct is_error_code_enum<socks::errc::errc_t>
{
static const bool value = true;
};
} // namespace system
} // namespace boost
namespace socks {
using namespace endian;
using namespace util;
using boost::asio::ip::tcp;
inline boost::system::error_code do_socks5(uri& parser,
const std::string& target, const std::string& target_port,
tcp::socket& socket, boost::asio::yield_context& yield)
{
std::size_t bytes_to_write = parser.username().empty() ? 3 : 4;
boost::asio::streambuf request;
boost::asio::mutable_buffer b = request.prepare(bytes_to_write);
char* p = boost::asio::buffer_cast<char*>(b);
write_uint8(5, p); // SOCKS VERSION 5.
if (parser.username().empty())
{
write_uint8(1, p); // 1 authentication method (no auth)
write_uint8(0, p); // no authentication
}
else
{
write_uint8(2, p); // 2 authentication methods
write_uint8(0, p); // no authentication
write_uint8(2, p); // username/password
}
request.commit(bytes_to_write);
boost::system::error_code ec;
boost::asio::async_write(socket, request, boost::asio::transfer_exactly(bytes_to_write), yield[ec]);
if (ec)
return ec;
boost::asio::streambuf response;
boost::asio::async_read(socket, response, boost::asio::transfer_exactly(2), yield[ec]);
if (ec)
return ec;
int method;
bool authed = false;
{
int version;
boost::asio::const_buffer b1 = response.data();
const char* p1 = boost::asio::buffer_cast<const char*>(b1);
version = read_uint8(p1);
method = read_uint8(p1);
if (version != 5) // 版本不等于5, 不支持socks5.
{
ec = socks::errc::socks_unsupported_version;
return ec;
}
}
if (method == 2)
{
if (parser.username().empty())
{
ec = socks::errc::socks_username_required;
return ec;
}
// start sub-negotiation.
request.consume(request.size());
std::size_t bytes_to_write1 = parser.username().size() + parser.password().size() + 3;
boost::asio::mutable_buffer mb = request.prepare(bytes_to_write1);
char* mp = boost::asio::buffer_cast<char*>(mb);
write_uint8(1, mp);
write_uint8(static_cast<int8_t>(parser.username().size()), mp);
write_string(std::string(parser.username()), mp);
write_uint8(static_cast<int8_t>(parser.password().size()), mp);
write_string(std::string(parser.password()), mp);
request.commit(bytes_to_write1);
// 发送用户密码信息.
boost::asio::async_write(socket, request, boost::asio::transfer_exactly(bytes_to_write1), yield[ec]);
if (ec)
return ec;
// 读取状态.
response.consume(response.size());
boost::asio::async_read(socket, response, boost::asio::transfer_exactly(2), yield[ec]);
if (ec)
return ec;
// 读取版本状态.
boost::asio::const_buffer cb = response.data();
const char* cp = boost::asio::buffer_cast<const char*>(cb);
int version = read_uint8(cp);
int status = read_uint8(cp);
// 不支持的认证版本.
if (version != 1)
{
ec = errc::socks_unsupported_authentication_version;
return ec;
}
// 认证错误.
if (status != 0)
{
ec = errc::socks_authentication_error;
return ec;
}
authed = true;
}
if (method == 0 || authed)
{
request.consume(request.size());
std::size_t bytes_to_write1 = 7 + target.size();
boost::asio::mutable_buffer mb = request.prepare(bytes_to_write1);
char* wp = boost::asio::buffer_cast<char*>(mb);
// 发送socks5连接命令.
write_uint8(5, wp); // SOCKS VERSION 5.
write_uint8(1, wp); // CONNECT command.
write_uint8(0, wp); // reserved.
write_uint8(3, wp); // address type. TODO: 这里根据使用是IP还是域名区别使用1或3
BOOST_ASSERT(target.size() <= 255);
write_uint8(static_cast<int8_t>(target.size()), wp); // domainname size.
std::copy(target.begin(), target.end(), wp); // domainname.
wp += target.size();
write_uint16(static_cast<uint16_t>(atoi(target_port.c_str())), wp); // port.
request.commit(bytes_to_write1);
boost::asio::async_write(socket, request, boost::asio::transfer_exactly(bytes_to_write1), yield[ec]);
if (ec)
return ec;
std::size_t bytes_to_read = 10;
response.consume(response.size());
boost::asio::async_read(socket, response,
boost::asio::transfer_exactly(bytes_to_read), yield[ec]);
if (ec)
return ec;
boost::asio::const_buffer cb = response.data();
const char* rp = boost::asio::buffer_cast<const char*>(cb);
int version = read_uint8(rp);
int resp = read_uint8(rp);
read_uint8(rp); // skip RSV.
int atyp = read_uint8(rp);
if (atyp == 1) // ADDR.PORT
{
tcp::endpoint remote_endp;
remote_endp.address(boost::asio::ip::address_v4(read_uint32(rp)));
remote_endp.port(read_uint16(rp));
// LOG_DBG << "* SOCKS remote host: " << remote_endp.address().to_string() << ":" << remote_endp.port();
}
else if (atyp == 3) // DOMAIN
{
auto domain_length = read_uint8(rp);
boost::asio::async_read(socket, response,
boost::asio::transfer_exactly(domain_length - 3), yield[ec]);
if (ec)
return ec;
rp = boost::asio::buffer_cast<const char*>(response.data()) + 5;
std::string domain;
for (int i = 0; i < domain_length; i++)
domain.push_back(read_uint8(rp));
auto port = read_uint16(rp);
(void)port;
// LOG_DBG << "* SOCKS remote host: " << domain << ":" << port;
}
else
{
ec = errc::socks_general_failure;
return ec;
}
if (version != 5)
{
ec = errc::socks_unsupported_version;
return ec;
}
if (resp != 0)
{
ec = errc::socks_general_failure;
// 得到更详细的错误信息.
switch (resp)
{
case 2: ec = boost::asio::error::no_permission; break;
case 3: ec = boost::asio::error::network_unreachable; break;
case 4: ec = boost::asio::error::host_unreachable; break;
case 5: ec = boost::asio::error::connection_refused; break;
case 6: ec = boost::asio::error::timed_out; break;
case 7: ec = errc::socks_command_not_supported; break;
case 8: ec = boost::asio::error::address_family_not_supported; break;
}
return ec;
}
ec = boost::system::error_code(); // 没有发生错误, 返回.
return ec;
}
ec = boost::asio::error::address_family_not_supported;
return ec;
}
enum
{
SOCKS_CMD_CONNECT = 1,
SOCKS_VERSION_4 = 4,
SOCKS4_REQUEST_GRANTED = 90,
SOCKS4_REQUEST_REJECTED_OR_FAILED = 91,
SOCKS4_CANNOT_CONNECT_TARGET_SERVER = 92,
SOCKS4_REQUEST_REJECTED_USER_NO_ALLOW = 93,
};
inline boost::system::error_code do_socks4(uri& parser,
const std::string& target, const std::string& target_port,
tcp::socket& socket, boost::asio::yield_context& yield)
{
std::size_t bytes_to_write = parser.username().empty() ? 3 : 4;
boost::asio::streambuf request;
boost::asio::mutable_buffer b = request.prepare(bytes_to_write);
char* p = boost::asio::buffer_cast<char*>(b);
write_uint8(SOCKS_VERSION_4, p); // SOCKS VERSION 4.
write_uint8(SOCKS_CMD_CONNECT, p); // CONNECT.
write_uint16(static_cast<uint16_t>(atoi(target_port.c_str())), p); // DST PORT.
boost::system::error_code ec;
auto address = boost::asio::ip::address_v4::from_string(target, ec);
if (ec)
return ec;
write_uint32(address.to_uint(), p); // DST IP.
if (!parser.username().empty())
{
std::copy(parser.username().begin(), parser.username().end(), p); // USERID
p += parser.username().size();
}
write_uint8(0, p); // NULL.
request.commit(bytes_to_write);
boost::asio::async_write(socket, request, yield[ec]);
if (ec)
return ec;
boost::asio::streambuf response;
boost::asio::async_read(socket, response,
boost::asio::transfer_exactly(8), yield[ec]);
if (ec)
return ec;
auto resp = boost::asio::buffer_cast<const char*>(response.data());
read_uint8(resp); // VN is the version of the reply code and should be 0.
auto cd = read_uint8(resp);
if (cd != SOCKS4_REQUEST_GRANTED)
{
switch (cd)
{
case SOCKS4_REQUEST_REJECTED_OR_FAILED:
ec = socks::errc::socks_request_rejected_or_failed;
break;
case SOCKS4_CANNOT_CONNECT_TARGET_SERVER:
ec = errc::socks_request_rejected_cannot_connect;
break;
case SOCKS4_REQUEST_REJECTED_USER_NO_ALLOW:
ec = errc::socks_request_rejected_incorrect_userid;
break;
default:
ec = errc::socks_unknown_error;
break;
}
}
return ec;
}
inline std::tuple<bool, boost::system::error_code> do_proxy(
const std::string& url, tcp::socket& socket,
const std::string& target, const std::string& target_port, boost::asio::yield_context& yield)
{
boost::system::error_code ec;
uri parser;
// Parser socks url.
if (!parser.parse(url))
{
ec = boost::asio::error::make_error_code(boost::asio::error::invalid_argument);
return {false, ec};
}
std::string port_string(parser.port());
if (port_string.empty())
port_string = "1080";
// do socks5.
if (parser.scheme() == "socks5")
{
ec = do_socks5(parser, target, target_port, socket, yield);
return { !ec, ec };
}
// do socks4.
if (parser.scheme() == "socks4")
{
ec = do_socks4(parser, target, target_port, socket, yield);
return { !ec, ec };
}
ec = boost::asio::error::make_error_code(boost::asio::error::invalid_argument);
return {false, ec};
}
template<class Handler>
BOOST_ASIO_INITFN_RESULT_TYPE(Handler, void(boost::system::error_code, bool))
async_do_proxy(const std::string& socks_url,
const std::string& address, const std::string& port, tcp::socket& socket, Handler&& handler)
{
AVHTTP_HANDLER_TYPE_CHECK(Handler, void(boost::system::error_code, bool));
boost::asio::async_completion<Handler, void(boost::system::error_code, bool)> init(handler);
boost::asio::spawn(socket.get_executor(),
[url = socks_url, &socket, target = address, port = port, handler = init.completion_handler]
(boost::asio::yield_context yield) mutable
{
boost::system::error_code ec;
bool ret;
std::tie(ret, ec) = do_proxy(url, socket, target, port, yield);
auto executor = boost::asio::get_associated_executor(handler);
boost::asio::post(executor, [ec, ret, handler]() mutable
{
handler(ec, ret);
});
});
return init.result.get();
}
}