forked from philave/boost_socks5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boost_socks5.cpp
465 lines (401 loc) · 13.1 KB
/
boost_socks5.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
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
/**
* @file boost_socks5.cpp
* @brief Simple SOCKS5 proxy server realization using boost::asio library
* @author philave (philave7@gmail.com)
*/
#include <cstdlib>
#include <iostream>
#include <string>
#include <memory>
#include <utility>
#include <boost/asio.hpp>
#include <fstream>
#include "config_reader.hpp"
using boost::asio::ip::tcp;
// Common log function
inline void write_log(int prefix, short verbose, short verbose_level, int session_id, const std::string& what, const std::string& error_message = "")
{
if (verbose > verbose_level) return;
std::string session = "";
if (session_id >= 0) { session += "session("; session += std::to_string(session_id); session += "): "; }
if (prefix > 0)
{
std::cerr << (prefix == 1 ? "Error: " : "Warning: ") << session << what;
if (error_message.size() > 0)
std::cerr << ": " << error_message;
std::cerr << std::endl;
}
else
{
std::cout << session << what;
if (error_message.size() > 0)
std::cout << ": " << error_message;
std::cout << std::endl;
}
}
class Session : public std::enable_shared_from_this<Session>
{
public:
Session(tcp::socket in_socket, unsigned session_id, size_t buffer_size, short verbose)
: in_socket_(std::move(in_socket)),
out_socket_(in_socket.get_io_service()),
resolver(in_socket.get_io_service()),
in_buf_(buffer_size),
out_buf_(buffer_size),
session_id_(session_id),
verbose_(verbose)
{
}
void start()
{
read_socks5_handshake();
}
private:
void read_socks5_handshake()
{
auto self(shared_from_this());
in_socket_.async_receive(boost::asio::buffer(in_buf_),
[this, self](boost::system::error_code ec, std::size_t length)
{
if (!ec)
{
/*
The client connects to the server, and sends a version
identifier/method selection message:
+----+----------+----------+
|VER | NMETHODS | METHODS |
+----+----------+----------+
| 1 | 1 | 1 to 255 |
+----+----------+----------+
The values currently defined for METHOD are:
o X'00' NO AUTHENTICATION REQUIRED
o X'01' GSSAPI
o X'02' USERNAME/PASSWORD
o X'03' to X'7F' IANA ASSIGNED
o X'80' to X'FE' RESERVED FOR PRIVATE METHODS
o X'FF' NO ACCEPTABLE METHODS
*/
if (length < 3 || in_buf_[0] != 0x05)
{
write_log(1, 0, verbose_, session_id_, "SOCKS5 handshake request is invalid. Closing session.");
return;
}
uint8_t num_methods = in_buf_[1];
// Prepare request
in_buf_[1] = 0xFF;
// Only 0x00 - 'NO AUTHENTICATION REQUIRED' is now support_ed
for (uint8_t method = 0; method < num_methods; ++method)
if (in_buf_[2 + method] == 0x00) { in_buf_[1] = 0x00; break; }
write_socks5_handshake();
}
else
write_log(1, 0, verbose_, session_id_, "SOCKS5 handshake request", ec.message());
});
}
void write_socks5_handshake()
{
auto self(shared_from_this());
boost::asio::async_write(in_socket_, boost::asio::buffer(in_buf_, 2), // Always 2-byte according to RFC1928
[this, self](boost::system::error_code ec, std::size_t length)
{
if (!ec)
{
if (in_buf_[1] == 0xFF) return; // No appropriate auth method found. Close session.
read_socks5_request();
}
else
write_log(1, 0, verbose_, session_id_, "SOCKS5 handshake response write", ec.message());
});
}
void read_socks5_request()
{
auto self(shared_from_this());
in_socket_.async_receive(boost::asio::buffer(in_buf_),
[this, self](boost::system::error_code ec, std::size_t length)
{
if (!ec)
{
/*
The SOCKS request is formed as follows:
+----+-----+-------+------+----------+----------+
|VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT |
+----+-----+-------+------+----------+----------+
| 1 | 1 | X'00' | 1 | Variable | 2 |
+----+-----+-------+------+----------+----------+
Where:
o VER protocol version: X'05'
o CMD
o CONNECT X'01'
o BIND X'02'
o UDP ASSOCIATE X'03'
o RSV RESERVED
o ATYP address type of following address
o IP V4 address: X'01'
o DOMAINNAME: X'03'
o IP V6 address: X'04'
o DST.ADDR desired destination address
o DST.PORT desired destination port_ in network octet
order
The SOCKS server will typically evaluate the request based on source
and destination addresses, and return one or more reply messages, as
appropriate for the request type.
*/
if (length < 5 || in_buf_[0] != 0x05 || in_buf_[1] != 0x01)
{
write_log(1, 0, verbose_, session_id_, "SOCKS5 request is invalid. Closing session.");
return;
}
uint8_t addr_type = in_buf_[3], host_length;
switch (addr_type)
{
case 0x01: // IP V4 addres
if (length != 10) { write_log(1, 0, verbose_, session_id_, "SOCKS5 request length is invalid. Closing session."); return; }
remote_host_ = boost::asio::ip::address_v4(ntohl(*((uint32_t*)&in_buf_[4]))).to_string();
remote_port_ = std::to_string(ntohs(*((uint16_t*)&in_buf_[8])));
break;
case 0x03: // DOMAINNAME
host_length = in_buf_[4];
if (length != (size_t)(5 + host_length + 2)) { write_log(1, 0, verbose_, session_id_, "SOCKS5 request length is invalid. Closing session."); return; }
remote_host_ = std::string(&in_buf_[5], host_length);
remote_port_ = std::to_string(ntohs(*((uint16_t*)&in_buf_[5 + host_length])));
break;
default:
write_log(1, 0, verbose_, session_id_, "unsupport_ed address type in SOCKS5 request. Closing session.");
break;
}
do_resolve();
}
else
write_log(1, 0, verbose_, session_id_, "SOCKS5 request read", ec.message());
});
}
void do_resolve()
{
auto self(shared_from_this());
resolver.async_resolve(tcp::resolver::query({ remote_host_, remote_port_ }),
[this, self](const boost::system::error_code& ec, tcp::resolver::iterator it)
{
if (!ec)
{
do_connect(it);
}
else
{
std::ostringstream what; what << "failed to resolve " << remote_host_ << ":" << remote_port_;
write_log(1, 0, verbose_, session_id_, what.str(), ec.message());
}
});
}
void do_connect(tcp::resolver::iterator& it)
{
auto self(shared_from_this());
out_socket_.async_connect(*it,
[this, self](const boost::system::error_code& ec)
{
if (!ec)
{
std::ostringstream what; what << "connected to " << remote_host_ << ":" << remote_port_;
write_log(0, 1, verbose_, session_id_, what.str());
write_socks5_response();
}
else
{
std::ostringstream what; what << "failed to connect " << remote_host_ << ":" << remote_port_;
write_log(1, 0, verbose_, session_id_, what.str(), ec.message());
}
});
}
void write_socks5_response()
{
auto self(shared_from_this());
/*
The SOCKS request information is sent by the client as soon as it has
established a connection to the SOCKS server, and completed the
authentication negotiations. The server evaluates the request, and
returns a reply formed as follows:
+----+-----+-------+------+----------+----------+
|VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
+----+-----+-------+------+----------+----------+
| 1 | 1 | X'00' | 1 | Variable | 2 |
+----+-----+-------+------+----------+----------+
Where:
o VER protocol version: X'05'
o REP Reply field:
o X'00' succeeded
o X'01' general SOCKS server failure
o X'02' connection not allowed by ruleset
o X'03' Network unreachable
o X'04' Host unreachable
o X'05' Connection refused
o X'06' TTL expired
o X'07' Command not support_ed
o X'08' Address type not support_ed
o X'09' to X'FF' unassigned
o RSV RESERVED
o ATYP address type of following address
o IP V4 address: X'01'
o DOMAINNAME: X'03'
o IP V6 address: X'04'
o BND.ADDR server bound address
o BND.PORT server bound port_ in network octet order
Fields marked RESERVED (RSV) must be set to X'00'.
*/
in_buf_[0] = 0x05; in_buf_[1] = 0x00; in_buf_[2] = 0x00; in_buf_[3] = 0x01;
uint32_t realRemoteIP = out_socket_.remote_endpoint().address().to_v4().to_ulong();
uint16_t realRemoteport = htons(out_socket_.remote_endpoint().port());
std::memcpy(&in_buf_[4], &realRemoteIP, 4);
std::memcpy(&in_buf_[8], &realRemoteport, 2);
boost::asio::async_write(in_socket_, boost::asio::buffer(in_buf_, 10), // Always 10-byte according to RFC1928
[this, self](boost::system::error_code ec, std::size_t length)
{
if (!ec)
{
do_read(3); // Read both sockets
}
else
write_log(1, 0, verbose_, session_id_, "SOCKS5 response write", ec.message());
});
}
void do_read(int direction)
{
auto self(shared_from_this());
// We must divide reads by direction to not permit second read call on the same socket.
if (direction & 0x1)
in_socket_.async_receive(boost::asio::buffer(in_buf_),
[this, self](boost::system::error_code ec, std::size_t length)
{
if (!ec)
{
std::ostringstream what; what << "--> " << std::to_string(length) << " bytes";
write_log(0, 2, verbose_, session_id_, what.str());
do_write(1, length);
}
else //if (ec != boost::asio::error::eof)
{
write_log(2, 1, verbose_, session_id_, "closing session. Client socket read error", ec.message());
// Most probably client closed socket. Let's close both sockets and exit session.
in_socket_.close(); out_socket_.close();
}
});
if (direction & 0x2)
out_socket_.async_receive(boost::asio::buffer(out_buf_),
[this, self](boost::system::error_code ec, std::size_t length)
{
if (!ec)
{
std::ostringstream what; what << "<-- " << std::to_string(length) << " bytes";
write_log(0, 2, verbose_, session_id_, what.str());
do_write(2, length);
}
else //if (ec != boost::asio::error::eof)
{
write_log(2, 1, verbose_, session_id_, "closing session. Remote socket read error", ec.message());
// Most probably remote server closed socket. Let's close both sockets and exit session.
in_socket_.close(); out_socket_.close();
}
});
}
void do_write(int direction, std::size_t Length)
{
auto self(shared_from_this());
switch (direction)
{
case 1:
boost::asio::async_write(out_socket_, boost::asio::buffer(in_buf_, Length),
[this, self, direction](boost::system::error_code ec, std::size_t length)
{
if (!ec)
do_read(direction);
else
{
write_log(2, 1, verbose_, session_id_, "closing session. Client socket write error", ec.message());
// Most probably client closed socket. Let's close both sockets and exit session.
in_socket_.close(); out_socket_.close();
}
});
break;
case 2:
boost::asio::async_write(in_socket_, boost::asio::buffer(out_buf_, Length),
[this, self, direction](boost::system::error_code ec, std::size_t length)
{
if (!ec)
do_read(direction);
else
{
write_log(2, 1, verbose_, session_id_, "closing session. Remote socket write error", ec.message());
// Most probably remote server closed socket. Let's close both sockets and exit session.
in_socket_.close(); out_socket_.close();
}
});
break;
}
}
tcp::socket in_socket_;
tcp::socket out_socket_;
tcp::resolver resolver;
std::string remote_host_;
std::string remote_port_;
std::vector<char> in_buf_;
std::vector<char> out_buf_;
int session_id_;
short verbose_;
};
class Server
{
public:
Server(boost::asio::io_service& io_service, short port, unsigned buffer_size, short verbose)
: acceptor_(io_service, tcp::endpoint(tcp::v4(), port)),
in_socket_(io_service), buffer_size_(buffer_size), verbose_(verbose), session_id_(0)
{
do_accept();
}
private:
void do_accept()
{
acceptor_.async_accept(in_socket_,
[this](boost::system::error_code ec)
{
if (!ec)
{
std::make_shared<Session>(std::move(in_socket_), session_id_++, buffer_size_, verbose_)->start();
}
else
write_log(1, 0, verbose_, session_id_, "socket accept error", ec.message());
do_accept();
});
}
tcp::acceptor acceptor_;
tcp::socket in_socket_;
size_t buffer_size_;
short verbose_;
unsigned session_id_;
};
int main(int argc, char* argv[])
{
short verbose = 0;
try
{
if (argc != 2)
{
std::cout << "Usage: boost_socks5 <config_file>" << std::endl;
return 1;
}
ConfigReader conf;
conf.parse(argv[1]);
short port = conf.check_key("port") ? std::atoi(conf.get_key_value("port")) : 1080; // Default port_
size_t buffer_size = conf.check_key("buffer_size") ? std::atoi(conf.get_key_value("buffer_size")) : 8192; // Default buffer_size
verbose = conf.check_key("verbose") ? std::atoi(conf.get_key_value("verbose")) : 0; // Default verbose_
boost::asio::io_service io_service;
Server server(io_service, port, buffer_size, verbose);
io_service.run();
}
catch (std::exception& e)
{
write_log(1, 0, verbose, -1, "", e.what());
}
catch (...)
{
write_log(1, 0, verbose, -1, "", "exception...");
}
return 0;
}