-
Notifications
You must be signed in to change notification settings - Fork 0
/
connector.ixx
179 lines (147 loc) · 4.29 KB
/
connector.ixx
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
// LogEXE 启动/发送模块
//
// Powred By Yan
export module connector;
import <Windows.h>;
import "chiper.h";
import "global.h";
extern std::string MOR_key;
extern std::vector<char> MOR_salt;
extern bool LogEXE_Start;
HANDLE hPipe;
/*
* Windows系统下,管道名称命名规范:
* \\.\pipe\pipename
* 其中'\\.\pipe\'是固定的前缀,表示这是一个命名管道
*/
LPCWSTR pipeName = L"\\\\.\\pipe\\AOALPipe";
BOOL success;
char buffer[1024];
DWORD bytesWritten;
DWORD bytesRead;
// 互斥锁
std::mutex LogEXEMutex;
// 分割字符串,以实现包发送
std::vector<std::string> splitString(const std::string& str, int many) {
std::vector<std::string> chunks;
if (str.size() <= many) {
chunks.push_back(str);
chunks.push_back("quit");
return chunks;
}
for (size_t i = 0; i < str.size(); i += many) {
chunks.push_back(str.substr(i, many));
}
chunks.push_back("quit");
return chunks;
}
// 消息发送核心函数
bool send_massage(std::string str, int cut_s) {
int indexx = 0;
std::string vhash = vectorToHex(Vsha512WithSalt(stringToVectorChar(str), MOR_salt));
str = vectorToHex(aesProcess(stringToVectorChar(str), MOR_key, true));
std::vector<std::string> str_index = splitString(str, cut_s);
while (true) {
// 向服务端发送消息
if (str_index[indexx] == "quit") {
std::string endSignal;
// 发送终止字符串
endSignal = "Q" + vhash;
success = WriteFile(hPipe, endSignal.c_str(), (DWORD)(strlen(endSignal.c_str())), &bytesWritten, NULL);
FlushFileBuffers(hPipe);
break;
}
const char* message = str_index[indexx].c_str();
success = WriteFile(
hPipe, // 管道句柄
message, // 发送的数据
(DWORD)strlen(message), // 数据大小
&bytesWritten, // 写入的字节数
NULL // 不重叠
);
FlushFileBuffers(hPipe);
if (!success) {
//logger("Failed to send data for LogEXE.", false, 3, false, __FILE__, __func__);
CloseHandle(hPipe);
return false;
}
indexx++;
}
// 从服务端读取响应
success = ReadFile(
hPipe, // 管道句柄
buffer, // 接收缓冲区
sizeof(buffer), // 缓冲区大小
&bytesRead, // 读取的字节数
NULL // 不重叠
);
if (!success || bytesRead == 0) {
if (GetLastError() == ERROR_BROKEN_PIPE) {
//logger("LogEXE has disconnected.", false, 1, false, __FILE__, __func__);
}
else {
//logger("From LogEXE, Read message failed.", false, 3, false, __FILE__, __func__);
}
return false;
}
buffer[bytesRead] = '\0'; // 确保字符串结束符
std::string receivedData(buffer);
if (receivedData.find("OK") != std::string::npos) {
return true;
}
else {
return false;
}
}
// LogEXE 初始化启动函数
bool pipeClient() {
// 创建命名管道
hPipe = CreateNamedPipe(
pipeName,
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
1,
1024,
1024,
0,
NULL
);
if (hPipe == INVALID_HANDLE_VALUE) {
//logger("For LogEXE, Failed to create pipe.", false, 3, false, __FILE__, __func__);
return false;
}
//logger("For LogEXE, Named pipe created successfully.", false, 1, false, __FILE__, __func__);
// 等待连接
success = ConnectNamedPipe(hPipe, NULL) ?
TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
return true;
}
// LogEXE 启动封装函数
export void Start_LogEXE() {
std::lock_guard<std::mutex> guard(LogEXEMutex);
if (send_massage("CHECK", 100)) {
LogEXE_Start = true;
return;
}
HINSTANCE result = ShellExecute(NULL, L"open", L"LogEXE.exe", NULL, NULL, SW_SHOW);
if ((int)result <= 32) {
std::string error_text = "Start LogEXE error, ERROR Code: " + (int)result;
//logger(error_text, true, 3, false, __FILE__, __func__);
return;
}
else {
//logger("Successfully star LogEXE.", false, 1, false, __FILE__, __func__);
}
if (!pipeClient()) //logger("For LogEXE, Create pipe faild.", true, 3, false, __FILE__, __func__);
return;
}
// LogEXE 消息发送封装函数
export bool Send_LogEXE(std::string send_massage_data) {
std::lock_guard<std::mutex> guard(LogEXEMutex);
if (!LogEXE_Start) return false;
if (!send_massage("CHECK", 100)) {
//logger("LogEXE is not strated.", true, 3, false, __FILE__, __func__);
LogEXE_Start = false;
}
return send_massage(send_massage_data, 500);
}