-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserverForTelnetUbuntuV.cpp
1268 lines (1243 loc) · 43.3 KB
/
serverForTelnetUbuntuV.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
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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "global.h"
#include "saveData.h"
#include "filter.h"
#include "log.h"
logNameSpace::Log prlog;
int showForSend(string, filter, bool, ClientSocketFlagStruct::states);
map<string, int> StringToInt =
{
{"connect", 1},
{"del", 2},
{"show", 3},
{"cmd", 4}
};
bool send_message(SOCKET sock, const std::string &message)
{
std::ostringstream oss;
oss << message.size() << "\r\n\r\n\r\n\r\n\r\n"
<< message; // 构建消息,包含长度和实际数据
std::string formatted_message = oss.str();
int total_sent = 0;
int message_length = formatted_message.size();
const char *data = formatted_message.c_str();
while (total_sent < message_length)
{
int bytes_sent = send(sock, data + total_sent, message_length - total_sent, 0);
if (bytes_sent == SOCKET_ERROR)
{
return false; // 发送失败
}
total_sent += bytes_sent;
}
return true; // 发送成功
}
bool receive_message(SOCKET sock, std::string &message)
{
std::string length_str;
char buffer[16384] = {0};
int received;
// 首先读取长度部分,直到接收到 \r\n
while (true)
{
received = recv(sock, buffer, 1, 0); // 每次读取一个字节
if (received <= 0)
{
return false; // 连接断开或读取出错
}
if (buffer[0] == '\r')
{
// 继续读取\n
received = recv(sock, buffer, 1, 0);
if (received <= 0 || buffer[0] != '\n')
{
return false; // 格式错误
}
for (int i = 1; i <= 4; i++)
{
received = recv(sock, buffer, 1, 0);
if (received <= 0 || buffer[0] != '\r')
{
return false; // 格式错误
}
received = recv(sock, buffer, 1, 0);
if (received <= 0 || buffer[0] != '\n')
{
return false; // 格式错误
}
}
break; // 读取到 \r\n,退出循环
}
length_str += buffer[0];
}
int data_length = std::stoi(length_str); // 转换长度字符串为整数
message.resize(data_length);
int total_received = 0;
while (total_received < data_length)
{
received = recv(sock, &message[total_received], data_length - total_received, 0);
if (received <= 0)
{
return false; // 连接断开或读取出错
}
total_received += received;
}
return true; // 接收成功
}
int initServer(SOCKET &ListenSocket, sockaddr_in &sockAddr, int port)
{
auto funlog = prlog.getFunLog("initServer");
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0)
{
*funlog << "WSAStartup() failed with error: " << iResult << "\n";
*funlog << "initServer End\n";
return iResult;
}
#endif
// 创建套接字
ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ListenSocket == INVALID_SOCKET)
{
int errorCode = WSAGetLastError();
*funlog << "socket() failed with error: " << errorCode << "\n";
*funlog << "initServer End\n";
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
WSACleanup();
#endif
return errorCode;
}
// 绑定套接字
sockAddr.sin_family = AF_INET;
sockAddr.sin_addr.s_addr = INADDR_ANY;
sockAddr.sin_port = htons(port);
if (bind(ListenSocket, (SOCKADDR *)&sockAddr, sizeof(sockAddr)) == SOCKET_ERROR)
{
closesocket(ListenSocket);
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
WSACleanup();
#endif
int errorCode = WSAGetLastError();
*funlog << "bind() failed with error: " << errorCode << "\n";
*funlog << "initServer End\n";
return errorCode;
}
// 监听套接字
if (listen(ListenSocket, 5) == SOCKET_ERROR)
{
closesocket(ListenSocket);
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
WSACleanup();
#endif
int errorCode = WSAGetLastError();
*funlog << "listen() failed with error: " << errorCode << "\n";
*funlog << "initServer End\n";
return errorCode;
}
*funlog << "initServer End\n";
return 0;
}
string StringTime(time_t t1)
{
auto funlog = prlog.getFunLog("StringTime");
time_t t = t1;
char tmp[64];
struct tm *timinfo;
timinfo = localtime(&t);
strftime(tmp, sizeof(tmp), "%Y%m%d%H%M", timinfo);
*funlog << "String Time End\n";
return tmp;
}
void setServerOrClientExit(SEIDForSocketStruct &s)
{
auto funlog = prlog.getFunLog("setServerOrClientExit SEID:" + s.SEID);
// 设置退出标志
{
std::lock_guard<std::mutex> lock(s.OtherValueLock);
s.isBack = true;
s.isSocketExit = false;
*funlog << "set status isBack\n";
}
// 关闭测试套接字
{
std::lock_guard<std::mutex> lock(s.ServerHealthySocketLock);
closesocket(s.socketH);
*funlog << "close healthy cheacksocket\n";
}
// 关闭连接套接字
{
std::lock_guard<std::mutex> lock(s.ServerSocketLock);
closesocket(s.ServerSocket);
*funlog << "close ServerSocket\n";
}
*funlog << "setServerOrClientExit End\n";
}
void HealthyCheack()
{
auto funlog = prlog.getFunLog("HealthyCheack");
while (1)
{
if (!HealthyQueue.empty())
{
healthyLock();
for (int i = 0; i < HealthyQueue.size(); i++)
{
SOCKET s;
SEIDForSocketStruct *temp;
string SEID = HealthyQueue.front().SEID;
if (HealthyQueue.front().isServer)
{
*funlog << "start Server HealthyCheck,SEID:" << SEID << "\n";
temp = &ServerSEIDMap[SEID];
}
else
{
*funlog << "start Client HealthyCheck,SEID:" << SEID << "\n";
temp = &ClientSEIDMap[SEID];
}
std::lock_guard<std::mutex> lock(temp->ServerHealthySocketLock);
srand(time(NULL) + rand());
string sendMsg = to_string(rand() % 1000000000);
string buf;
int timeout = 3000;
int state = send_message(temp->socketH, sendMsg);
int state1 = receive_message(temp->socketH, buf);
setsockopt(temp->socketH, SOL_SOCKET, SO_SNDTIMEO, (const char *)&timeout, sizeof(timeout));
setsockopt(temp->socketH, SOL_SOCKET, SO_RCVTIMEO, (const char *)&timeout, sizeof(timeout));
if (strcmp(buf.c_str(), "\r\nClose\r\n") == 0)
{
*funlog << (HealthyQueue.front().isServer ? "Server" : "Client")
<< " HealthyCheck Status:Close SEID:" << SEID << "\n";
HealthyQueue.pop();
setServerOrClientExit(*temp);
continue;
}
if (strcmp(buf.c_str(), sendMsg.c_str()) != 0 || temp->isBack || !state || !state1)
{
*funlog << (HealthyQueue.front().isServer ? "Server" : "Client")
<< " HealthyCheck Status:Error,error code:"
<< WSAGetLastError()
<< " SEID:" << SEID << "\n";
HealthyQueue.pop();
setServerOrClientExit(*temp);
continue;
}
HealthyQueue.push({temp->SEID, HealthyQueue.front().isServer});
HealthyQueue.pop();
temp = nullptr;
Sleep(300);
}
healthyUnlock();
}
}
}
int showForSend(string seid, filter f, bool startIf = false, ClientSocketFlagStruct::states state = ClientSocketFlagStruct::NULLs)
{
SOCKET &s = ServerSEIDMap[seid].ServerSocket;
int canShowClient = 0;
std::lock_guard<std::mutex> lock(ClientMapLock);
for (int i = 1; i <= ClientMap.size(); i++)
{
if (ServerSEIDMap[seid].isBack)
{
return -1;
}
if (!startIf || ClientMap[i - 1].state == state || f.matching(ClientMap[i - 1].ClientWanIp, ClientMap[i - 1].ClientLanIp, to_string(ClientMap[i - 1].ClientConnectPort)))
{
canShowClient++;
string sendBuf = ClientMap[i - 1].ClientWanIp + " " + ClientMap[i - 1].ClientLanIp + " " + to_string(ClientMap[i - 1].ClientConnectPort) + " " + to_string(ClientMap[i - 1].state);
std::lock_guard<std::mutex> lockSEID(ServerSEIDMap[seid].ServerSocketLock);
if (!send_message(s, sendBuf))
{
return -2;
}
}
}
std::lock_guard<std::mutex> lockSEID(ServerSEIDMap[seid].ServerSocketLock);
if (!send_message(s, "\r\n\r\nend\r\n\r\n"))
{
return -3;
}
return canShowClient;
}
void Connect(string seid, vector<string> cmods, int cmodsNum)
{
auto funlog = prlog.getFunLog("Connect SEID:" + seid);
SOCKET &s = ServerSEIDMap[seid].ServerSocket;
string recvBuf;
while (1)
{
recvBuf.clear();
{
std::lock_guard<std::mutex> lockSEID(ServerSEIDMap[seid].OtherValueLock);
if (ServerSEIDMap[seid].isBack)
{
*funlog << "Server is exit\n";
break;
}
}
filter temp;
if (showForSend(seid, temp, true, ClientSocketFlagStruct::Online) < 0)
{
break;
}
bool state = false;
{
std::lock_guard<std::mutex> lockSEID(ServerSEIDMap[seid].ServerSocketLock);
state = receive_message(s, recvBuf);
}
if (!state)
{
*funlog << "Server recv error "
<< "error code:" << WSAGetLastError() << "\n";
break;
}
else if (state > 0)
{
if (strcmp(recvBuf.c_str(), "\r\nexit\r\n") == 0)
{
*funlog << "Server exit Connect "
<< "error code:" << WSAGetLastError() << "\n";
break;
}
else if (strcmp(recvBuf.c_str(), "\r\nnext\r\n") == 0)
{
continue;
}
int setClientId = atoi(recvBuf.c_str());
int ClientIndex = -1;
for (int i = 0; i < ClientMap.size(); i++)
{
if (ClientMap[i].state == ClientSocketFlagStruct::Online)
{
setClientId--;
if (setClientId == 0)
{
ClientMap[i].state = ClientSocketFlagStruct::Use;
ClientIndex = i;
break;
}
}
}
if (ClientIndex != -1)
{
string buf, buf2, temp;
{
*funlog << "ClientIndex:" << ClientIndex << "\n";
*funlog << "ClientMap[ClientIndex].SEID:" << ClientMap[ClientIndex].SEID << "\n";
*funlog << "Server start connect Client\n";
std::lock_guard<std::mutex> lockSEIDForServer(ServerSEIDMap[seid].ServerSocketLock);
std::lock_guard<std::mutex> lockSEIDForClient(ClientSEIDMap[ClientMap[ClientIndex].SEID].ServerSocketLock);
if (!send_message(s, "\r\n\r\nsec\r\n\r\n"))
{
*funlog << "send to Server error "
<< "error code:" << WSAGetLastError() << "\n";
break;
}
if (!send_message(ClientSEIDMap[ClientMap[ClientIndex].SEID].ServerSocket, "\r\nstart\r\n") ||
!receive_message(ClientSEIDMap[ClientMap[ClientIndex].SEID].ServerSocket, buf2) ||
!send_message(ClientSEIDMap[ClientMap[ClientIndex].SEID].ServerSocket, "\r\n") ||
!receive_message(ClientSEIDMap[ClientMap[ClientIndex].SEID].ServerSocket, temp))
{
*funlog << "send/recv error "
<< "error code:" << WSAGetLastError() << "\n";
break;
}
buf2 += temp;
if (!send_message(s, buf2))
{
*funlog << "send to Server error "
<< "error code:" << WSAGetLastError() << "\n";
break;
}
*funlog << "Server connect init ok\n";
}
while (1)
{
if (ServerSEIDMap[seid].isBack)
{
*funlog << "Server is exit\n";
break;
}
buf.clear();
buf2.clear();
bool state = false;
{
std::lock_guard<std::mutex> lockSEIDForServer(ServerSEIDMap[seid].ServerSocketLock);
state = receive_message(s, buf);
}
if (!state)
{
*funlog << "Server recv error "
<< "error code:" << WSAGetLastError() << "\n";
break;
}
else if (state > 0)
{
#ifdef _DEBUG
*funlog << "recv:" << buf << "\n";
#endif
std::lock_guard<std::mutex> lockSEIDForClient(ClientSEIDMap[ClientMap[ClientIndex].SEID].ServerSocketLock);
if (strcmp(buf.c_str(), "\r\nfexit\r\n") == 0)
{
if (!send_message(ClientSEIDMap[ClientMap[ClientIndex].SEID].ServerSocket, "exit\r\n") ||
receive_message(ClientSEIDMap[ClientMap[ClientIndex].SEID].ServerSocket, buf2))
{
*funlog << "send/recv error "
<< "error code:" << WSAGetLastError() << "\n";
break;
}
ClientMap[ClientIndex].state = ClientSocketFlagStruct::Online;
*funlog << "Server exit connect\n";
break;
}
else
{
if (!send_message(ClientSEIDMap[ClientMap[ClientIndex].SEID].ServerSocket, buf.c_str()))
{
*funlog << "send/recv error "
<< "error code:" << WSAGetLastError() << "\n";
break;
}
}
if (!receive_message(ClientSEIDMap[ClientMap[ClientIndex].SEID].ServerSocket, buf2) || !send_message(s, buf2))
{
*funlog << "send/recv error "
<< "error code:" << WSAGetLastError() << "\n";
break;
}
}
else
{
*funlog << "Server unkonwn error\n";
}
}
}
}
else
{
std::lock_guard<std::mutex> lockSEID(ServerSEIDMap[seid].ServerSocketLock);
if (!send_message(s, "\r\n\r\nfail\r\n\r\n"))
{
*funlog << "send to Server error "
<< "error code:" << WSAGetLastError() << "\n";
break;
}
*funlog << "Can`t find client\n";
}
}
// break;
*funlog << "Connect End\n";
}
int delForId(int ClientId)
{
std::lock_guard<std::mutex> lock(ClientMapLock);
SOCKET &ClientSocket = ClientSEIDMap[ClientMap[ClientId - 1].SEID].ServerSocket;
auto funlog = prlog.getFunLog("delForId SEID:" + ClientMap[ClientId - 1].SEID);
if (ClientSocket != INVALID_SOCKET && ClientMap[ClientId - 1].state != ClientSocketFlagStruct::Use)
{
if (send_message(ClientSocket, "del"))
{
*funlog << "del ok\n";
*funlog << "delForId End\n";
return 0;
}
else
{
*funlog << "del error\n";
*funlog << "delForId End\n";
return 1;
}
}
else if (ClientSocket != INVALID_SOCKET && ClientMap[ClientId - 1].state == ClientSocketFlagStruct::Use)
{
*funlog << "Is Use,can`t del\n";
*funlog << "delForId End\n";
return 2;
}
*funlog << "unknown error\n";
*funlog << "delForId End\n";
return -1;
}
void del(string seid, vector<string> cmods, int cmodsNum)
{
auto funlog = prlog.getFunLog("del SEID:" + seid);
SOCKET &s = ServerSEIDMap[seid].ServerSocket;
string recvBuf;
while (1)
{
{
std::lock_guard<std::mutex> lock(ServerSEIDMap[seid].OtherValueLock);
if (ServerSEIDMap[seid].isBack)
{
*funlog << "Server is exit\n";
break;
}
}
filter temp;
if (showForSend(seid, temp) < 0)
{
*funlog << "showForSend error\n";
break;
}
bool state = false;
{
std::lock_guard<std::mutex> lock(ServerSEIDMap[seid].ServerSocketLock);
state = receive_message(s, recvBuf);
}
if (!state)
{
*funlog << "Server Socket error "
<< "error code:" << WSAGetLastError() << "\n";
break;
}
else if (state > 0)
{
if (strcmp(recvBuf.c_str(), "\r\nexit\r\n") == 0)
{
*funlog << "Server exit del\n";
break;
}
else if (strcmp(recvBuf.c_str(), "\r\nnext\r\n") == 0)
{
continue;
}
int setClientId = atoi(recvBuf.c_str());
int res = delForId(setClientId);
std::lock_guard<std::mutex> lock(ServerSEIDMap[seid].ServerSocketLock);
string sendMsg;
if (res == 0)
{
sendMsg = "\r\n\r\nok\r\n\r\n";
}
else if (res == 2)
{
sendMsg = "\r\n\r\nUse\r\n\r\n";
}
else
{
sendMsg = "\r\n\r\nUnError\r\n\r\n";
}
if (!send_message(s, sendMsg))
{
*funlog << "send to Server error "
<< "error code:" << WSAGetLastError() << "\n";
break;
}
}
}
*funlog << "del End\n";
}
void show(string seid, vector<string> cmods, int cmodsNum)
{
auto funlog = prlog.getFunLog("show SEID:" + seid);
SOCKET &s = ServerSEIDMap[seid].ServerSocket;
string recvBuf;
while (1)
{
filter temp;
{
std::lock_guard<std::mutex> lock(ServerSEIDMap[seid].OtherValueLock);
if (ServerSEIDMap[seid].isBack)
{
*funlog << "Server is exit\n";
break;
}
}
if (showForSend(seid, temp) < 0)
{
*funlog << "showForSend error\n";
break;
}
bool state = false;
{
std::lock_guard<std::mutex> lock(ServerSEIDMap[seid].ServerSocketLock);
state = receive_message(s, recvBuf);
}
if (!state)
{
*funlog << "Server Socket error "
<< "error code:" << WSAGetLastError() << "\n";
*funlog << "show End\n";
return;
}
else if (strcmp(recvBuf.c_str(), "\r\nclose\r\n") == 0)
{
*funlog << "Server exit show "
<< "error code:" << WSAGetLastError() << "\n";
*funlog << "show End\n";
return;
}
}
*funlog << "show End\n";
return;
}
void cmod(string seid, vector<string> cmods, int cmodsNum)
{
//[del,cmd,show][all,]
auto funlog = prlog.getFunLog("cmod SEID:" + seid);
SOCKET &s = ServerSEIDMap[seid].ServerSocket;
filter f;
{
addRule_error:
if (!send_message(s, "\r\ncmd error\r\n"))
{
*funlog << "send to Server error "
<< "error code:" << WSAGetLastError() << "\n";
}
*funlog << "Server cmd error\n";
*funlog << "cmod End\n";
return;
}
if (strcmp(cmods[1].c_str(), "del") == 0 && cmodsNum >= 3)
{
{
std::lock_guard<std::mutex> lock(ServerSEIDMap[seid].OtherValueLock);
if (ServerSEIDMap[seid].isBack)
{
*funlog << "Server is exit\n";
*funlog << "cmod End\n";
return;
}
}
{
std::lock_guard<std::mutex> lock(ServerSEIDMap[seid].ServerSocketLock);
if (!send_message(s, "\r\ndel\r\n"))
{
*funlog << "send to Server error "
<< "error code:" << WSAGetLastError() << "\n";
*funlog << "cmod End\n";
return;
}
}
for (int i = 2; i <= cmodsNum; i += 3)
{
{
std::lock_guard<std::mutex> lock(ServerSEIDMap[seid].OtherValueLock);
if (ServerSEIDMap[seid].isBack)
{
*funlog << "Server is exit\n";
*funlog << "cmod End\n";
return;
}
}
std::lock_guard<std::mutex> lock(ServerSEIDMap[seid].ServerSocketLock);
switch (StringToIntInComd[cmods[i]])
{
case 5:
if (!f.addRule(filter::ruleDataType::port, cmods[i + 1], cmods[i + 2]))
{
goto addRule_error;
}
break;
case 6:
if (!f.addRule(filter::ruleDataType::wanip, cmods[i + 1], cmods[i + 2]))
{
goto addRule_error;
}
break;
case 7:
if (!f.addRule(filter::ruleDataType::lanip, cmods[i + 1], cmods[i + 2]))
{
goto addRule_error;
}
break;
case 8:
if (!f.addRule(filter::ruleDataType::all, cmods[i + 1], cmods[i + 2]))
{
goto addRule_error;
}
break;
}
}
int sectClient = 0;
for (int i = 1; i <= ClientMap.size(); i++)
{
if (f.matching(ClientMap[i - 1].ClientWanIp,
ClientMap[i - 1].ClientLanIp,
to_string(ClientMap[i - 1].ClientConnectPort)) &&
ClientMap[i - 1].state != ClientSocketFlagStruct::Use)
{
sectClient++;
delForId(i);
}
}
if (ServerSEIDMap[seid].isBack)
{
*funlog << "Server exit\n";
*funlog << "cmod End\n";
return;
}
std::lock_guard<std::mutex> lock(ServerSEIDMap[seid].ServerSocketLock);
if (!send_message(s, "\r\nok\r\n") ||
!send_message(s, to_string(sectClient).c_str()) ||
!send_message(s, to_string(ClientMap.size()).c_str()))
{
*funlog << "send to Server error "
<< "error code:" << WSAGetLastError() << "\n";
}
}
else if (strcmp(cmods[1].c_str(), "cmd") == 0 && cmodsNum >= 4)
{
{
std::lock_guard<std::mutex> lock(ServerSEIDMap[seid].OtherValueLock);
if (ServerSEIDMap[seid].isBack)
{
*funlog << "Server is exit\n";
*funlog << "cmod End\n";
return;
}
}
std::lock_guard<std::mutex> lock(ServerSEIDMap[seid].ServerSocketLock);
if (!send_message(s, "\r\ncmd\r\n"))
{
*funlog << "send to Server error "
<< "error code:" << WSAGetLastError() << "\n";
*funlog << "cmod End\n";
return;
}
int cmdstart = 0;
// 一条指令
// cmd [port [!=,==,>,<,>=,<=] [port]] [wanip [!=,==] [ip]] [lanip [!=,==] [ip]] [all] "指令"
for (int i = 2; i <= cmodsNum && cmods[i][0] != '"'; i += 3, cmdstart = i)
{
{
std::lock_guard<std::mutex> lock(ServerSEIDMap[seid].OtherValueLock);
if (ServerSEIDMap[seid].isBack)
{
*funlog << "Server is exit\n";
*funlog << "cmod End\n";
return;
}
}
if (cmods[i][0] == '"')
{
break;
}
switch (StringToIntInComd[cmods[i]])
{
case 5:
if (!f.addRule(filter::ruleDataType::port, cmods[i + 1], cmods[i + 2]))
{
goto addRule_error;
}
break;
case 6:
if (!f.addRule(filter::ruleDataType::wanip, cmods[i + 1], cmods[i + 2]))
{
goto addRule_error;
}
break;
case 7:
if (!f.addRule(filter::ruleDataType::lanip, cmods[i + 1], cmods[i + 2]))
{
goto addRule_error;
}
case 8:
if (!f.addRule(filter::ruleDataType::all, cmods[i + 1], cmods[i + 2]))
{
goto addRule_error;
}
break;
}
}
if (cmdstart > cmodsNum) // 判断指令格式是否标准
{
{
std::lock_guard<std::mutex> lock(ServerSEIDMap[seid].OtherValueLock);
if (ServerSEIDMap[seid].isBack)
{
*funlog << "Server is exit\n";
*funlog << "cmod End\n";
return;
}
}
if (!send_message(s, "\r\ncmd error\r\n"))
{
*funlog << "send to Server error "
<< "error code:" << WSAGetLastError() << "\n";
*funlog << "cmod End\n";
return;
}
*funlog << "Server cmd error\n";
*funlog << "cmod End\n";
return;
}
string sendBuf;
int sectClient = 0;
// 合并指令
/*
之前的存储模式:按空格分割
*/
for (int i = cmdstart; i <= cmodsNum; i++)
{
sendBuf += cmods[i] + " ";
}
sendBuf = sendBuf.substr(1, sendBuf.length() - 2); // 去掉头尾的 ' " '
sendBuf += "\r\nexit\r\n";
std::lock_guard<std::mutex> lockForClientMapLock(ClientMapLock);
for (int i = 1; i <= ClientMap.size(); i++)
{
if (f.matching(ClientMap[i - 1].ClientWanIp, ClientMap[i - 1].ClientLanIp, to_string(ClientMap[i - 1].ClientConnectPort)) && ClientMap[i - 1].state != ClientSocketFlagStruct::Use)
{
*funlog << "Server cmd match one\n";
sectClient++;
if (!send_message(ClientSEIDMap[ClientMap[i - 1].SEID].ServerSocket, sendBuf)) // 发送指令至Client
{
*funlog << "send to Client error "
<< "error code:" << WSAGetLastError() << "\n";
if (!send_message(s, "\r\ncmd error\r\n"))
{
*funlog << "send to Server error "
<< "error code:" << WSAGetLastError() << "\n";
return;
}
}
}
}
{
std::lock_guard<std::mutex> lock(ServerSEIDMap[seid].OtherValueLock);
if (ServerSEIDMap[seid].isBack)
{
*funlog << "Server is exit\n";
*funlog << "cmod End\n";
return;
}
}
std::lock_guard<std::mutex> lockForServerSEIDMapLock_ServerSocketLock(ServerSEIDMap[seid].ServerSocketLock);
if (!send_message(s, "\r\nok\r\n") ||
!send_message(s, to_string(sectClient).c_str()) ||
!send_message(s, to_string(ClientMap.size()).c_str()))
{
*funlog << "send to Server error "
<< "error code:" << WSAGetLastError() << "\n";
}
}
else if (strcmp(cmods[1].c_str(), "show") == 0 && cmodsNum >= 3)
{
{
std::lock_guard<std::mutex> lock(ServerSEIDMap[seid].OtherValueLock);
if (ServerSEIDMap[seid].isBack)
{
*funlog << "Server is exit\n";
*funlog << "cmod End\n";
return;
}
}
std::lock_guard<std::mutex> lock(ServerSEIDMap[seid].ServerSocketLock);
if (!send_message(s, "\r\nsee\r\n"))
{
*funlog << "send to Server error "
<< "error code:" << WSAGetLastError() << "\n";
*funlog << "cmod End\n";
return;
}
for (int i = 2; i <= cmodsNum && (cmodsNum - i) >= 2; i += 3)
{
switch (StringToIntInComd[cmods[i]])
{
case 5:
if (!f.addRule(filter::ruleDataType::port, cmods[i + 1], cmods[i + 2]))
{
goto addRule_error;
}
break;
case 6:
if (!f.addRule(filter::ruleDataType::wanip, cmods[i + 1], cmods[i + 2]))
{
goto addRule_error;
}
break;
case 7:
if (!f.addRule(filter::ruleDataType::lanip, cmods[i + 1], cmods[i + 2]))
{
goto addRule_error;
}
break;
case 8:
if (!f.addRule(filter::ruleDataType::all, NULL, NULL))
{
goto addRule_error;
}
break;
default:
goto addRule_error;
}
}
int showClient = showForSend(seid, f);
{
std::lock_guard<std::mutex> lock(ServerSEIDMap[seid].OtherValueLock);
if (ServerSEIDMap[seid].isBack)
{
*funlog << "Server is exit\n";
*funlog << "cmod End\n";
return;
}
}
if (!send_message(s, "\r\nok\r\n") ||
!send_message(s, to_string(showClient).c_str()) ||
!send_message(s, to_string(ClientMap.size()).c_str()))
{
*funlog << "send to Server error "
<< "error code:" << WSAGetLastError() << "\n";
}
}
*funlog << "cmod End\n";
return;
}
string createSEID(SOCKET sock, string something = NULL)
{
MD5 m;
m.init();
return m.encode(StringTime(time(NULL)) + to_string(sock));
}
void joinClient(string ClientWanIp, string ClientLanIp, string ClientPort, unsigned long long int OnlineTime, unsigned long long int OfflineTime, string SEID)
{
std::lock_guard<std::mutex> lock(ClientMapLock);
ClientSocketFlagStruct temp;
temp.ClientWanIp = ClientWanIp;
temp.ClientLanIp = ClientLanIp;
temp.ClientConnectPort = atoi(ClientPort.c_str());
temp.OnlineTime = OnlineTime;
temp.OfflineTime = OfflineTime;
temp.state = ClientSocketFlagStruct::states::Online;
temp.SEID = SEID;
vector<ClientSocketFlagStruct>::iterator itr = find(ClientMap.begin(), ClientMap.end(), temp);
if (itr != ClientMap.end())
{
while (ClientMap[distance(ClientMap.begin(), itr)].state == ClientSocketFlagStruct::states::Use)
;
closesocket(ClientSEIDMap[ClientMap[distance(ClientMap.begin(), itr)].SEID].ServerSocket);
ClientMap[distance(ClientMap.begin(), itr)] = temp;
}
else
{
ClientMap.push_back(temp);
}
return;
}
// void SetColor(unsigned short forecolor = 4, unsigned short backgroudcolor = 0)
// {
// HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE); // 获取缓冲区句柄
// SetConsoleTextAttribute(hCon, forecolor | backgroudcolor); // 设置文本及背景色
// }
void ServerRS(SOCKET s)
{
string lastloginYZM;
string loginYZM;
lastloginYZM.clear();
bool loginTRUE = false;
MD5 m;
for (int i = 1; i <= 5 && !loginTRUE; i++)
{
m.init();
string recvBuf;
if (!receive_message(s, recvBuf))
{
return;
}
loginYZM = m.encode(StringTime(time(NULL)) + lastloginYZM + password);
if (recvBuf == loginYZM)
{
loginTRUE = true;
if (!send_message(s, "true"))
{
return;
}
break;
}
if (!send_message(s, "error"))
{
return;
}
lastloginYZM = loginYZM;
}
if (!loginTRUE)
{
auto funlog = prlog.getFunLog("ServerRS Temp");
*funlog << "login error\n";
return;
}
string SEID = createSEID(s, lastloginYZM + loginYZM);
auto funlog = prlog.getFunLog("ServerRS SEID:" + SEID);
{
std::lock_guard<std::mutex> lock(ServerSEIDMap[SEID].ServerSocketLock);
if (!send_message(s, SEID.c_str()))
{
*funlog << "send to Server error "
<< "error code:" << WSAGetLastError() << "\n";
return;
}
}
{
std::lock_guard<std::mutex> lock(ServerSEIDMap[SEID].OtherValueLock);
ServerSEIDMap[SEID].isSEIDExit = true;
ServerSEIDMap[SEID].SEID = SEID;
ServerSEIDMap[SEID].ServerSocket = s;
}
std::unique_lock<std::mutex> lock(ServerSEIDMap[SEID].isSocketExitLock);
ServerSEIDMap[SEID].cv.wait(lock, [SEID]
{ return ServerSEIDMap[SEID].isSocketExit; });
*funlog << "Server online\n";
try