-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathproxy.cpp
More file actions
790 lines (718 loc) · 27 KB
/
proxy.cpp
File metadata and controls
790 lines (718 loc) · 27 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
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
#include "datapackage.h"
#include "proxy.h"
#include <chrono>
#include <iostream>
#include <mutex>
#include "rbslib/Streams.h"
#include <time.h>
class AsyncInputSocketStream : public RbsLib::Streams::IAsyncInputStream
{
private:
asio::ip::tcp::socket& socket;
public:
AsyncInputSocketStream(asio::ip::tcp::socket& socket) : socket(socket) {}
~AsyncInputSocketStream() noexcept {}
asio::awaitable<std::int64_t> ReadAsync(void* buffer, std::int64_t size) override
{
std::size_t n = co_await socket.async_read_some(asio::buffer(buffer, size), asio::use_awaitable);
co_return n;
}
asio::awaitable<const RbsLib::Buffer*> ReadAsync(RbsLib::Buffer& buffer, std::int64_t size = 0) override
{
buffer.Resize(size);
int64_t bytesRead = co_await socket.async_read_some(asio::buffer((void*)buffer.Data(), size), asio::use_awaitable);
buffer.SetLength(bytesRead);
buffer.Resize(bytesRead);
co_return &buffer;
}
};
std::size_t Proxy::ConnectionControl::UploadBytes(void) const noexcept
{
return this->upload_bytes;
}
std::time_t Proxy::ConnectionControl::ConnectTime(void) const noexcept
{
return this->connect_time;
}
auto Proxy::PingTest() const -> std::uint64_t
{
return asio::co_spawn(this->strand, [this]() -> asio::awaitable<std::uint64_t> {
try
{
// 解析主机名和端口
asio::ip::tcp::resolver resolver(co_await asio::this_coro::executor);
auto endpoints = co_await resolver.async_resolve(this->remote_server_addr, std::to_string(this->remote_server_port), asio::use_awaitable);
// 创建socket并连接
asio::ip::tcp::socket remote_server(co_await asio::this_coro::executor);
co_await asio::async_connect(remote_server, endpoints, asio::use_awaitable);
remote_server.set_option(asio::ip::tcp::no_delay(true));
// 创建握手数据包
HandshakeDataPack handshake_data_pack;
handshake_data_pack.id = 0;
handshake_data_pack.server_address = RbsLib::DataType::String(this->remote_server_addr);
handshake_data_pack.server_port = this->remote_server_port;
handshake_data_pack.next_state = 1;
handshake_data_pack.protocol_version = 754;
auto handshake_buffer = handshake_data_pack.ToBuffer();
co_await asio::async_write(remote_server,asio::buffer(handshake_buffer.Data(), handshake_buffer.GetLength()), asio::use_awaitable);
AsyncInputSocketStream stream(remote_server);
// 发送状态请求
StatusRequestDataPack status_request_data_pack;
auto status_buffer = status_request_data_pack.ToBuffer();
co_await asio::async_write(remote_server,asio::buffer(status_buffer.Data(), status_buffer.GetLength()), asio::use_awaitable);
// 接收状态并丢弃
co_await DataPack::ReadFullData(stream);
// ping
PingDataPack ping_data_pack, pong_data_pack;
ping_data_pack.payload = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
auto start = std::chrono::system_clock::now();
auto ping_buffer = ping_data_pack.ToBuffer();
co_await asio::async_write(remote_server,asio::buffer(ping_buffer.Data(), ping_buffer.GetLength()), asio::use_awaitable);
co_await pong_data_pack.ParseFromInputStream(stream);
auto end = std::chrono::system_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
if (pong_data_pack.payload != ping_data_pack.payload)
throw ProxyException("Ping test failed.");
co_return duration.count();
}
catch (const std::exception& e)
{
throw ProxyException(std::string("Ping test failed: ") + e.what());
}
}, asio::use_future).get();
}
static std::string convertDomainPattern(const std::string& pattern) {
std::string result = "^";
for (char c : pattern) {
if (c == '*') {
result += "(.+)";
}
else if (c == '.') {
result += "\\.";
}
else {
result += c;
}
}
result += "$";
return result;
}
void Proxy::EnableDomainNameProxy(const std::string& doman_name)
{
//将domjan_name中的*替换为.*,其他字符不变,构造正则表达式
std::string pattern = convertDomainPattern(doman_name);
std::cout << pattern << std::endl;
asio::co_spawn(this->strand, [this, &doman_name,&pattern]() -> asio::awaitable<void> {
co_await asio::dispatch(this->strand, asio::use_awaitable);
if (this->dn_proxy_map.has_value())
{
this->dn_proxy_map->first = std::regex{ pattern };
}
else
{
this->dn_proxy_map = std::make_optional<std::pair<std::regex, std::map<std::string, std::pair<std::string, std::uint16_t>>>>();
this->dn_proxy_map->first = std::regex{ pattern };
}
}, asio::use_future).get();
}
void Proxy::AddDomainNameProxyMapping(const std::string& domain_pattern, const std::string& target_address, std::uint16_t target_port)
{
asio::co_spawn(this->strand, [this, &domain_pattern, &target_address, target_port]()->asio::awaitable<void> {
co_await asio::dispatch(this->strand, asio::use_awaitable);
if (this->dn_proxy_map.has_value())
{
this->dn_proxy_map->second.insert({ domain_pattern, { target_address, target_port } });
}
else
{
throw ProxyException("Domain name proxy is not enabled.");
}
}, asio::use_future).get();
}
std::time_t Proxy::GetStartTime(void) const noexcept
{
return this->start_time;
}
Proxy::~Proxy() noexcept
{
//正常退出,关闭acceptor和所有连接,等待所有协程结束后其所属线程退出,然后关闭io_context
try
{
this->acceptor->close();
asio::co_spawn(this->strand,
[this]() -> asio::awaitable<void> {
for (auto& conn : this->connections)
{
try
{
conn->shutdown(asio::ip::tcp::socket::shutdown_both);
}
catch (...) {}
}
co_return;
}(),
asio::use_future).get();
}
catch (const std::exception* ex)
{
this->log_output((std::string("关闭监听器或连接时发生异常: ") + ex->what()).c_str());
}
for (auto& thread : this->io_threads)
{
if (thread.joinable())
thread.join();
}
try
{
this->io_context.stop();
}
catch (const std::exception* ex)
{
this->log_output((std::string("停止IO上下文时发生异常: ") + ex->what()).c_str());
}
}
void Proxy::RemoveDomainNameProxyMapping(const std::string& domain_pattern)
{
asio::co_spawn(this->strand, [this, &domain_pattern]() -> asio::awaitable<void> {
co_await asio::dispatch(this->strand, asio::use_awaitable);
if (this->dn_proxy_map.has_value())
{
this->dn_proxy_map->second.erase(domain_pattern);
}
else
{
throw ProxyException("Domain name proxy is not enabled.");
}
}, asio::use_future).get();
}
void Proxy::DisableDomainNameProxy()
{
asio::co_spawn(this->strand, [this]() -> asio::awaitable<void> {
co_await asio::dispatch(this->strand, asio::use_awaitable);
this->dn_proxy_map.reset();
}, asio::use_future).get();
}
asio::awaitable<void> Proxy::AcceptLoop(asio::ip::tcp::acceptor& acceptor)
{
try
{
while (true)
{
auto socket = co_await acceptor.async_accept(asio::use_awaitable);
asio::co_spawn(socket.get_executor(),
this->HandleConnection(std::move(socket)),
asio::detached);
}
}
catch (std::system_error const& ex)
{
if (ex.code() != asio::error::operation_aborted) {
throw;
}
}
}
asio::awaitable<void> Proxy::HandleConnection(asio::ip::tcp::socket socket)
{
//std::string remote_endpoint_addr = socket.remote_endpoint().address().to_string();
std::time_t connect_time = std::time(nullptr);
//立即将连接加入连接池
co_await asio::dispatch(strand, asio::use_awaitable);
this->connections.push_back(&socket);
try
{
this->log_output(("New connection from " + socket.remote_endpoint().address().to_string() + ":" + std::to_string(socket.remote_endpoint().port())).c_str());
ConnectionControl user_control(socket);
user_control.connect_time = connect_time;
int connection_status = 0;//未握手
//必须先握手
std::string remote_server_suffix;
HandshakeDataPack handshake_data_pack;
AsyncInputSocketStream stream(socket);
co_await handshake_data_pack.ParseFromInputStream(stream);
connection_status = handshake_data_pack.next_state.Value();
bool is_continue_loop = true;
while (is_continue_loop)
{
switch (connection_status)
{
case 1: {
RbsLib::Buffer buffer = co_await DataPack::ReadFullData(stream);
switch (NoCompressionDataPack::GetID(buffer))
{
case 0: {
//状态请求,直接响应
this->log_output(("Status request from " + socket.remote_endpoint().address().to_string() + ":" + std::to_string(socket.remote_endpoint().port())).c_str());
StatusResponseDataPack status_response_data_pack;
co_await asio::dispatch(strand, asio::use_awaitable); //确保在线程安全的环境中访问用户数据
auto motd = this->motd;
motd.SetOnlinePlayerNumber(this->users.size());
motd.SetSampleUsers(co_await this->GetUsersInfoAsync());
motd.SetVersion("", handshake_data_pack.protocol_version.Value());
this->max_player != -1 ? motd.SetPlayerMaxNumber(this->max_player) : motd.SetPlayerMaxNumber(9999999);
status_response_data_pack.json_response = RbsLib::DataType::String(motd.ToString());
co_await asio::async_write(socket,asio::buffer(status_response_data_pack.ToBuffer().Data(), status_response_data_pack.ToBuffer().GetLength()), asio::use_awaitable);
//debug motd
//std::cout << motd.motd_json.ToFormattedString() << std::endl;
break;
}
case 1: {
//ping,响应pong
PingDataPack ping_data_pack;
RbsLib::Streams::BufferInputStream bis(buffer);
ping_data_pack.ParseFromInputStream(bis);
PingDataPack pong_data_pack;
pong_data_pack.payload = ping_data_pack.payload;
co_await asio::async_write(socket,asio::buffer(pong_data_pack.ToBuffer().Data(), pong_data_pack.ToBuffer().GetLength()), asio::use_awaitable);
this->log_output(("Ping from " + socket.remote_endpoint().address().to_string() + ":" + std::to_string(socket.remote_endpoint().port()) + ", payload: " + std::to_string(ping_data_pack.payload)).c_str());
break;
}
default:
throw ProxyException("Invalid data pack id.");
}
break;
}
case 2: {
//登录请求,接收登录请求
//检查是否达到最大玩家数
this->log_output(("Login request from " + socket.remote_endpoint().address().to_string() + ":" + std::to_string(socket.remote_endpoint().port())).c_str());
co_await asio::dispatch(strand, asio::use_awaitable); //确保在线程安全的环境中访问用户数据
if (this->max_player != -1 && this->users.size() >= this->max_player) {
LoginFailureDataPack login_failed_data_pack("Server is full.");
co_await asio::async_write(socket,asio::buffer(login_failed_data_pack.ToBuffer().Data(), login_failed_data_pack.ToBuffer().GetLength()), asio::use_awaitable);
this->log_output(("Refused connection " + socket.remote_endpoint().address().to_string() + ":" + std::to_string(socket.remote_endpoint().port()) + ", reason: Server is full.").c_str());
throw ProxyException("Server is full.");
}
StartLoginDataPack start_login_data_pack;
auto login_start_row_packet = co_await DataPack::ReadFullData(stream);
RbsLib::Streams::BufferInputStream bis(login_start_row_packet);
start_login_data_pack.ParseFromInputStream(bis);
//检查是否是FML登录
if (handshake_data_pack.server_address.size() != std::strlen(handshake_data_pack.server_address.c_str()))
{
remote_server_suffix = handshake_data_pack.server_address.substr(std::strlen(handshake_data_pack.server_address.c_str()));
this->log_output(("FML login detected on " + socket.remote_endpoint().address().to_string() + ":" + std::to_string(socket.remote_endpoint().port()) + ", suffix: " + remote_server_suffix).c_str());
}
//调用登录回调
user_control.username = start_login_data_pack.user_name;
if (start_login_data_pack.have_uuid)
{
user_control.uuid = start_login_data_pack.GetUUID();
}
co_await asio::dispatch(strand, asio::use_awaitable);//串行化
this->on_login(user_control);
if (user_control.isEnableConnect == false)
{
LoginFailureDataPack login_failed_data_pack(user_control.reason);
auto buffer = login_failed_data_pack.ToBuffer();
co_await asio::async_write(socket, asio::buffer(buffer.Data(), buffer.GetLength()), asio::use_awaitable);
throw ProxyException(std::string("Callback disable user login: ") + user_control.reason);
}
//连接远程服务器
std::string remote_server_addr_real;
std::uint32_t remote_server_port_real;
remote_server_addr_real = this->remote_server_addr;
remote_server_port_real = this->remote_server_port;
co_await asio::dispatch(strand, asio::use_awaitable); //确保在线程安全的环境中访问用户数据
//先检查域名映射表
if (this->dn_proxy_map.has_value())
{
std::smatch match_result;
if (std::regex_search(handshake_data_pack.server_address, match_result, this->dn_proxy_map->first))
{
if (auto addr = this->dn_proxy_map->second.find(match_result[1]); addr != this->dn_proxy_map->second.end())
{
remote_server_addr_real = addr->second.first;
remote_server_port_real = addr->second.second;
}
}
}
//再检查用户代理映射表
if (auto usr = this->user_proxy_map.find(start_login_data_pack.user_name); usr != this->user_proxy_map.end())
{
remote_server_addr_real = usr->second.first;
remote_server_port_real = usr->second.second;
}
asio::ip::tcp::socket remote_server(socket.get_executor());
try
{
// 解析主机名和端口
asio::ip::tcp::resolver resolver(socket.get_executor());
auto endpoints = co_await resolver.async_resolve(remote_server_addr_real, std::to_string(remote_server_port_real), asio::use_awaitable);
// 尝试连接每个解析到的端点
co_await asio::async_connect(remote_server, endpoints, asio::use_awaitable);
this->log_output(("Connected to remote server " + remote_server_addr_real + ":" + std::to_string(remote_server_port_real)).c_str());
}
catch (const std::exception& e)
{
throw; // 重新抛出异常
}
remote_server.set_option(asio::ip::tcp::no_delay(true));
socket.set_option(asio::ip::tcp::no_delay(true));
auto user_ptr = std::make_shared<User>(&socket, &remote_server);
user_ptr->username = start_login_data_pack.user_name;
user_ptr->uuid = start_login_data_pack.GetUUID();
user_ptr->ip = socket.remote_endpoint().address().to_string();
user_ptr->connect_time = std::time(nullptr);
//加入用户池
co_await asio::dispatch(strand, asio::use_awaitable); //串行化
if (this->users.find(start_login_data_pack.user_name) != this->users.end())
{
LoginFailureDataPack login_failed_data_pack(start_login_data_pack.user_name + " already online");
RbsLib::Buffer buffer = login_failed_data_pack.ToBuffer();
co_await asio::async_write(socket,asio::buffer(buffer.Data(), buffer.GetLength()), asio::use_awaitable);
throw ProxyException("User already online: " + start_login_data_pack.user_name);
}
this->users[user_ptr->username] = user_ptr;
bool error_occurred = false;
//将远程服务器也加入连接池
co_await asio::dispatch(strand, asio::use_awaitable); //串行化
this->connections.push_back(&remote_server);
try
{
//此阶段若要断开连接需要从用户表中删除用户
//发送握手申请
handshake_data_pack.server_address = RbsLib::DataType::String(remote_server_addr_real + remote_server_suffix);
handshake_data_pack.server_port = remote_server_port_real;
auto buffer = handshake_data_pack.ToBuffer();
co_await asio::async_write(remote_server,asio::buffer(buffer.Data(), buffer.GetLength()), asio::use_awaitable);
//发送登录请求
co_await asio::async_write(remote_server,asio::buffer(login_start_row_packet.Data(), login_start_row_packet.GetLength()), asio::use_awaitable);
}
catch (...)
{
error_occurred = true;
}
if (!error_occurred)
{
//无错误进入转发,期间一般不会抛出异常,出现异常一般都是致命错误
//将连接加入连接池,用于退出服务器时关闭
co_await asio::dispatch(strand, asio::use_awaitable); //串行化
this->connections.push_back(&remote_server);
this->connections.push_back(&socket);
//开始转发数据
{
try
{
this->on_proxy_start(user_control); //调用代理开始回调
this->log_output(("Forwarding data between " + socket.remote_endpoint().address().to_string() + ":" + std::to_string(socket.remote_endpoint().port()) + " and " + remote_server.remote_endpoint().address().to_string() + ":" + std::to_string(remote_server.remote_endpoint().port())).c_str());
using namespace asio::experimental::awaitable_operators;
co_await(ForwardData(socket, remote_server, *user_ptr) && ForwardData(remote_server, socket, *user_ptr));
this->log_output(("Forwarding data between " + socket.remote_endpoint().address().to_string() + ":" + std::to_string(socket.remote_endpoint().port()) + " and " + remote_server.remote_endpoint().address().to_string() + ":" + std::to_string(remote_server.remote_endpoint().port()) + " ended.").c_str());
this->on_proxy_end(user_control); //调用代理结束回调
}
catch (const std::exception& ex)
{
//std::cout <<ex.what() << std::endl;
}
}
//转发结束,关闭连接
co_await asio::dispatch(strand, asio::use_awaitable); //串行化
this->connections.remove(&remote_server);
this->connections.remove(&socket);
}
//清理资源
co_await asio::dispatch(strand, asio::use_awaitable); //串行化
this->connections.remove(&remote_server);//将远程服务器连接从连接池删除
this->users.erase(start_login_data_pack.user_name);//从用户池中删除用户
user_control.username = user_ptr->username;
user_control.uuid = user_ptr->uuid;
user_control.upload_bytes = user_ptr->upload_bytes;
this->on_logout(user_control); //调用登出回调
is_continue_loop = false; //退出循环
}
break;
default:
this->log_output(("Invalid connection status: " + std::to_string(connection_status) + " from "+ socket.remote_endpoint().address().to_string()+":"+std::to_string(socket.remote_endpoint().port())).c_str());
throw ProxyException("Invalid connection status.");
}
}
}
catch (...) {}
//删除连接池中的连接
co_await asio::dispatch(strand, asio::use_awaitable); //串行化
this->connections.remove(&socket);
}
asio::awaitable<void> Proxy::ForwardData(
asio::ip::tcp::socket& from_socket,
asio::ip::tcp::socket& to_socket,
User& user_control) noexcept
{
std::unique_ptr<std::uint8_t[]> buffer = std::make_unique<std::uint8_t[]>(10240);
try
{
while (true)
{
std::size_t n = co_await from_socket.async_receive(asio::buffer(buffer.get(), 10240), asio::use_awaitable);
if (n == 0)
break;
co_await asio::async_write(to_socket, asio::buffer(buffer.get(), n), asio::use_awaitable);
user_control.upload_bytes += n;
}
}
catch (...)
{
//忽略转发时的异常
}
try
{
//禁用两个方向的连接
user_control.client->shutdown(asio::ip::tcp::socket::shutdown_both);
}
catch (const std::exception& e)
{
//忽略禁用连接时的异常
}
try
{
user_control.server->shutdown(asio::ip::tcp::socket::shutdown_both);
}
catch (const std::exception& e)
{
//忽略禁用连接时的异常
}
}
ProxyException::ProxyException(const std::string& message) noexcept
: message(message)
{
}
const char* ProxyException::what() const noexcept
{
return this->message.c_str();
}
void Motd::SetVersion(const std::string& version_name, int protocol)
{
this->motd_json.AddEmptySubObject("version");
this->motd_json["version"].Add("name", version_name);
this->motd_json["version"].Add("protocol", protocol);
}
void Motd::SetPlayerMaxNumber(int n)
{
this->motd_json.AddEmptySubObject("players");
this->motd_json["players"].Add("max", n);
}
void Motd::SetOnlinePlayerNumber(int n)
{
this->motd_json.AddEmptySubObject("players");
this->motd_json["players"].Add("online", n);
}
void Motd::SetSampleUsers(std::list<UserInfo> const& users)
{
this->motd_json.AddEmptySubObject("players");
if (this->motd_json["players"].KeyExist("sample") == true)
return;//如果已经存在sample则不添加
this->motd_json["players"].AddEmptySubArray("sample");
if (this->motd_json["players"]["sample"].GetArraySize() == 0)
{
for (auto& user : users)
{
if (user.uuid.empty()) continue;
neb::CJsonObject user_json;
user_json.Add("name", user.username);
user_json.Add("id", user.uuid);
this->motd_json["players"]["sample"].Add(user_json);
}
}
}
auto Motd::ToString() -> std::string
{
return this->motd_json.ToString();
}
auto Motd::LoadMotdFromFile(const std::string& path) -> std::string
{
if (path.empty())
return "{\"description\": {\"text\": \"Minecraft Speed Proxy\"}}";
try
{
RbsLib::Storage::FileIO::File file_io(path);
return file_io.Read(1024 * 1024).ToString();
}
catch (const std::exception& e)
{
throw ProxyException(std::string("Motd读取失败: ") + e.what());
}
}
Proxy::Proxy(const std::string& local_address, std::uint16_t local_port, const std::string& remote_server_addr, std::uint16_t remote_server_port)
:local_address(local_address), local_port(local_port), remote_server_addr(remote_server_addr), remote_server_port(remote_server_port), strand(asio::make_strand(io_context.get_executor()))
{
this->io_threads.resize(std::thread::hardware_concurrency());
}
void Proxy::Start() {
auto addr = asio::ip::make_address(this->local_address);
asio::ip::tcp::endpoint endpoint(addr, this->local_port);
// 1. 先创建 acceptor(未绑定)
this->acceptor = std::make_unique<asio::ip::tcp::acceptor>(this->io_context);
// 2. 设置选项(必须在 bind 前)
this->acceptor->open(endpoint.protocol()); // 显式打开协议
if (endpoint.protocol() == asio::ip::tcp::v6()) {
this->acceptor->set_option(asio::ip::v6_only(false)); // 关键:在 bind 前设置
}
this->acceptor->set_option(asio::socket_base::reuse_address(true));
// 3. 绑定到端点
this->acceptor->bind(endpoint);
// 4. 开始监听
this->acceptor->listen();
// 发布任务
asio::co_spawn(this->io_context, this->AcceptLoop(*this->acceptor), asio::detached);
// 启动线程池
for (auto& thread : this->io_threads) {
thread = std::thread([this]() {
this->io_context.run();
});
}
this->start_time = std::time(nullptr);
}
void Proxy::KickByUsername(const std::string& username)
{
asio::co_spawn(this->strand, [this, username]() -> asio::awaitable<void> {
co_await asio::dispatch(this->strand, asio::use_awaitable);
auto it = this->users.find(username);
if (it != this->users.end())
{
it->second->client->shutdown(asio::ip::tcp::socket::shutdown_both);
}
else
{
throw std::runtime_error(username + " not online");
}
}, asio::use_future).get();
}
void Proxy::KickByUUID(const std::string& uuid)
{
asio::co_spawn(this->strand, [this, &uuid]() -> asio::awaitable<void> {
co_await asio::dispatch(this->strand, asio::use_awaitable);
for (auto it = this->users.begin(); it != this->users.end(); ++it)
{
if (it->second->uuid == uuid)
{
it->second->client->shutdown(asio::ip::tcp::socket::shutdown_both);
break;
}
}
}, asio::use_future).get();
}
auto Proxy::GetUsersInfo() -> std::list<UserInfo>
{
return asio::co_spawn(this->strand, [this]() -> asio::awaitable<std::list<UserInfo>> {
std::list<UserInfo> users_info;
co_await asio::dispatch(this->strand, asio::use_awaitable);
for (auto& user : this->users)
{
UserInfo user_info;
user_info.username = user.second->username;
user_info.uuid = user.second->uuid;
user_info.ip = user.second->ip;
user_info.connect_time = user.second->connect_time;
user_info.upload_bytes = user.second->upload_bytes;
users_info.push_back(user_info);
}
co_return users_info;
}, asio::use_future).get();
}
auto Proxy::GetUsersInfoAsync() -> asio::awaitable<std::list<UserInfo>>
{
std::list<UserInfo> users_info;
co_await asio::dispatch(this->strand, asio::use_awaitable);
for (auto& user : this->users)
{
UserInfo user_info;
user_info.username = user.second->username;
user_info.uuid = user.second->uuid;
user_info.ip = user.second->ip;
user_info.connect_time = user.second->connect_time;
user_info.upload_bytes = user.second->upload_bytes;
users_info.push_back(user_info);
}
co_return users_info;
}
void Proxy::SetMotd(const std::string& motd)
{
asio::co_spawn(this->strand, [this, motd]() -> asio::awaitable<void> {
co_await asio::dispatch(this->strand, asio::use_awaitable);
this->motd.motd_json = neb::CJsonObject(motd);
}, asio::use_future).get();
}
neb::CJsonObject Proxy::GetMotd() const
{
return asio::co_spawn(this->strand, [this]() -> asio::awaitable<neb::CJsonObject> {
co_await asio::dispatch(this->strand, asio::use_awaitable);
co_return this->motd.motd_json;
}, asio::use_future).get();
}
void Proxy::SetMaxPlayer(int n)
{
this->max_player.store(n);//原子变量不需要发送到协程
}
int Proxy::GetMaxPlayer(void)
{
return this->max_player.load();//原子变量不需要发送到协程
}
void Proxy::SetUserProxy(const std::string& username, const std::string& proxy_address, std::uint16_t proxy_port)
{
asio::co_spawn(this->strand, [this, username, proxy_address, proxy_port]() -> asio::awaitable<void> {
co_await asio::dispatch(this->strand, asio::use_awaitable);
this->user_proxy_map[username] = std::make_pair(proxy_address, proxy_port);
}, asio::use_future).get();
}
auto Proxy::GetUserProxyMap() const -> std::map<std::string, std::pair<std::string, std::uint16_t>>
{
return asio::co_spawn(this->strand, [this]() -> asio::awaitable<std::map<std::string, std::pair<std::string, std::uint16_t>>> {
co_await asio::dispatch(this->strand, asio::use_awaitable);
co_return this->user_proxy_map;
}, asio::use_future).get();
}
void Proxy::DeleteUserProxy(const std::string& username)
{
asio::co_spawn(this->strand, [this, username]() -> asio::awaitable<void> {
co_await asio::dispatch(this->strand, asio::use_awaitable);
this->user_proxy_map.erase(username);
}, asio::use_future).get();
}
void Proxy::ClearUserProxy()
{
asio::co_spawn(this->strand, [this]() -> asio::awaitable<void> {
co_await asio::dispatch(this->strand, asio::use_awaitable);
this->user_proxy_map.clear();
}, asio::use_future).get();
}
auto Proxy::GetDefaultProxy() -> std::pair<std::string, std::uint16_t>
{
return std::make_pair(this->remote_server_addr, this->remote_server_port);
}
Proxy::ConnectionControl::ConnectionControl(asio::ip::tcp::socket& connection)
: socket(connection)
{
}
const std::string& Proxy::ConnectionControl::Username(void) const noexcept
{
return this->username;
}
const std::string& Proxy::ConnectionControl::UUID(void) const noexcept
{
return this->uuid;
}
User::User(asio::ip::tcp::socket* client, asio::ip::tcp::socket* server)
:client(client), server(server)
{
}
std::string Proxy::ConnectionControl::GetAddress(void) const noexcept
{
try
{
return this->socket.remote_endpoint().address().to_string();
}
catch (...)
{
return "";
}
}
std::string Proxy::ConnectionControl::GetPort(void) const noexcept
{
try
{
return this->socket.remote_endpoint().port() == 0 ? "0" : std::to_string(this->socket.remote_endpoint().port());
}
catch (...)
{
return 0;
}
}