forked from mufengXD/MinecraftSpeedProxy-for-Windows-GUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonline.c
266 lines (254 loc) · 6.91 KB
/
online.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <WinSock2.h>
#include <errno.h>
#include "cJSON.h"
#include "CheckLogin.h"
#include "websocket.h"
#include <windows.h>
#include <process.h>
#include "log.h"
typedef struct ONLINE_LINK
{
int sock;
char ip[32];
char username[32];
HANDLE clientpid;
HANDLE serverpid;
time_t starttime;
struct ONLINE_LINK *next;
} OL_L;
typedef void (*FUNC_ONLINE_HOOK)(const char *str);
HANDLE O_lock;
OL_L *head;
FUNC_ONLINE_HOOK List_Remove = NULL, List_Add = NULL;
extern char *Version;
int OnlineNumber = 0;
extern char OnlineLockInited;
void printonline();
void kickplayer(const char *playername);
void kicksock(int sock);
int GetOnlinePlayerNumber();
__declspec(dllexport) void __cdecl ShowPlayerInfo(const char *playername, HWND hwnd);
void __cdecl ShowPlayerInfo(const char *playername, HWND hwnd)
{
WaitForSingleObject(O_lock, INFINITE);
OL_L *temp = head;
char successinfo = 0;
while (temp != NULL)
{
if (!strcmp(playername, temp->username))
{
successinfo = 1;
char *str = (char *)malloc(1024);
sprintf(str, "用户名:%s\nIP:%s\n登陆时间:%s", temp->username, temp->ip, ctime(&(temp->starttime)));
MessageBox(hwnd, str, TEXT("用户信息"), MB_OK);
}
temp = temp->next;
}
if (successinfo == 0)
{
MessageBox(hwnd, "该玩家不在线", "错误", MB_OK);
}
ReleaseMutex(O_lock);
}
__declspec(dllexport) void __cdecl InitOnline(FUNC_ONLINE_HOOK add, FUNC_ONLINE_HOOK remove)
{
O_lock = CreateMutex(NULL, 0, NULL);
OnlineLockInited = 1;
if (add == NULL || remove == NULL)
{
MessageBox(NULL, TEXT("在线列表添加移除函数指针为NULL"), TEXT("ERROR"), MB_ICONERROR);
exit(1);
}
List_Add = add;
List_Remove = remove;
}
void kicksock(int sock)
{
WaitForSingleObject(O_lock, INFINITE);
OL_L *temp = head;
char successinfo = 0;
while (temp != NULL)
{
if (sock == temp->sock)
{
successinfo = 1;
if (0 == shutdown(temp->sock, SD_BOTH))
{
printf("已关闭IP=%s,username=%s玩家的连接\n", temp->ip, temp->username);
}
else
{
printf("未能成功关闭IP=%s,username=%s玩家的连接:%s\n", temp->ip, temp->username, strerror(errno));
}
}
temp = temp->next;
}
if (successinfo == 0)
{
printf("未找到%d的连接\n", sock);
}
ReleaseMutex(O_lock);
}
__declspec(dllexport) void __cdecl kickplayer(const char *playername)
{
WaitForSingleObject(O_lock, INFINITE);
OL_L *temp = head;
char successinfo = 0;
while (temp != NULL)
{
if (!strcmp(playername, temp->username))
{
successinfo = 1;
if (0 == shutdown(temp->sock, SD_BOTH))
{
logout(LOG_PLAYER, "已关闭IP=%s,username=%s玩家的连接\n", temp->ip, temp->username);
}
else
{
logout(LOG_PLAYER, "未能成功关闭IP=%s,username=%s玩家的连接:%s\n", temp->ip, temp->username, strerror(errno));
}
}
temp = temp->next;
}
if (successinfo == 0)
{
logout(LOG_PLAYER, "未找到%s的连接\n", playername);
}
ReleaseMutex(O_lock);
}
void printonline()
{
printf("==========开始列出在线玩家==========\n");
WaitForSingleObject(O_lock, INFINITE);
OL_L *temp = head;
char str[20];
printf("ID\tsock\tIP地址\t用户名\t时间\n");
int i = 1;
while (temp)
{
printf("%d\t%d\t%s\t%s\t%s", i, temp->sock, temp->ip, temp->username, ctime(&(temp->starttime)));
temp = temp->next;
i += 1;
}
ReleaseMutex(O_lock);
printf("==========列出在线玩家完成==========\n");
}
void removeip(int sock)
{
WaitForSingleObject(O_lock, INFINITE);
if (head == NULL)
{
//当前没有在线玩家
ReleaseMutex(O_lock);
// printf("[W] 试图移除非在线玩家\n");
// log_warn("试图移除非在线玩家");
logout(LOG_WARN, "试图移除非在线玩家");
return;
}
else
{
OL_L *temp = head;
if (temp->sock == sock)
{
if (strcmp(temp->username, "Not-Login") != 0)
{
OnlineNumber -= 1;
logout(LOG_PLAYER, "移除了玩家:%s于IP:%s的连接", temp->username, temp->ip);
List_Remove(temp->username);
}
head = temp->next;
free(temp);
ReleaseMutex(O_lock);
return;
}
while (temp->next)
{
if (temp->next->sock == sock)
{
OL_L *a = temp->next;
if (strcmp(temp->username, "Not-Login") != 0)
{
OnlineNumber -= 1;
logout(LOG_PLAYER, "移除了玩家:%s于IP:%s的连接", a->username, a->ip);
List_Remove(a->username);
}
temp->next = a->next;
free(a);
ReleaseMutex(O_lock);
return;
}
temp = temp->next;
}
ReleaseMutex(O_lock);
return;
}
}
void adduser(int sock, const char *username)
{
WaitForSingleObject(O_lock, INFINITE);
OL_L *temp = head;
while (temp)
{
if (temp->sock == sock)
{
strcpy(temp->username, username);
OnlineNumber += 1;
logout(LOG_PLAYER, "IP:%s 玩家%s登录到服务器", temp->ip, username);
List_Add(username);
ReleaseMutex(O_lock);
return;
}
temp = temp->next;
}
////////////////
ReleaseMutex(O_lock);
return;
}
void addip(int sock, const char *IP)
{
WaitForSingleObject(O_lock, INFINITE);
if (head == NULL)
{
//当前没有在线玩家
head = (OL_L *)malloc(sizeof(OL_L));
strcpy(head->ip, IP);
strcpy(head->username, "Not-Login");
head->starttime = time(NULL);
head->sock = sock;
head->next = NULL;
ReleaseMutex(O_lock);
return;
}
else
{
OL_L *temp = head;
while (temp)
{
if (temp->sock == sock)
{
logout(LOG_WARN, "重复的套接字添加,sock=%d,IP=%s\n", sock, IP);
ReleaseMutex(O_lock);
return;
}
temp = temp->next;
}
temp = head;
temp = (OL_L *)malloc(sizeof(OL_L));
strcpy(temp->ip, IP);
strcpy(temp->username, "Not-Login");
temp->sock = sock;
temp->starttime = time(NULL);
temp->next = head;
head = temp;
ReleaseMutex(O_lock);
return;
}
}
int GetOnlinePlayerNumber()
{
return OnlineNumber;
}