-
Notifications
You must be signed in to change notification settings - Fork 1
/
ServerV2.cpp
418 lines (410 loc) · 15.7 KB
/
ServerV2.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
#include "stdafx.h"
int quit = 0, ret = 0;
thread_local int temp = 0;
WSADATA data;
SOCKET ListenSocket = INVALID_SOCKET;
struct addrinfo *result = NULL, *ptr = NULL, hints;
Timer timer;
char bind_port[] = "6112";
int ClientThread(SOCKET ClientSocket)
{
char game[4];
unsigned char *recvbuf = new unsigned char[1024];
//extern char *PORT;
printf("Client connected.\n");
temp = recv(ClientSocket, (char*)recvbuf, 1, 0);
if (temp > 0)
{
printf("Mode received: ");
}
else
{
printf("recv failed: %d\n", WSAGetLastError());
closesocket(ClientSocket);
delete[] recvbuf;
return 1;
}
switch (recvbuf[0]) //Authentication and mode choice
{
case 0x01:
{
std::cout << "Game\n";
temp = recv(ClientSocket, (char*)recvbuf, recvbuflen, 0);
if (temp > 0)
{
packets.write((char*)recvbuf, temp);
packets << std::endl;
game[0] = recvbuf[15];
game[1] = recvbuf[14];
game[2] = recvbuf[13];
game[3] = recvbuf[12];
std::cout << "Client Connected as " << game << std::endl;
}
else
{
printf("recv failed: %d\n", WSAGetLastError());
closesocket(ClientSocket);
delete[] recvbuf;
return 1;
}
temp = send(ClientSocket, (char*)SID_PING_DATA.data(), SID_PING_DATA.size(), 0);
if (temp == SOCKET_ERROR)
{
printf("send failed: %d\n", WSAGetLastError());
closesocket(ClientSocket);
delete[] recvbuf;
return 1;
}
timer.start();
temp = recv(ClientSocket, (char*)recvbuf, recvbuflen, 0);
if (temp > 0)
{
timer.stop();
int time = timer.elapsedMilliseconds();
std::cout << "Ping is " << time << "ms\n";
verbyte = recvbuf[16] & 0xFF;
if((strncmp("W3XP", game, 4) == 0) || (strncmp("WAR3", game, 4) == 0))
{
if(verbyte == 0xD2)
{
std::cout << "Beta 1.34 detected.\n";
temp = send(ClientSocket, (char*)SID_AUTH_INFO_W3BETA.data(), SID_AUTH_INFO_W3BETA.size(), 0);
nls = 1;
}
else
{
std::cout << "Client version byte is 0x" << std::hex << (int)recvbuf[16] << std::dec << std::endl;
temp = send(ClientSocket, (char*)SID_AUTH_INFO.data(), SID_AUTH_INFO.size(), 0);
}
}
else if(strncmp("W3DM", game, 4) == 0)
{
temp = send(ClientSocket, (char*)SID_AUTH_INFO_W3DEMO.data(), SID_AUTH_INFO_W3DEMO.size(), 0);
demo = 1;
}
else
{
temp = send(ClientSocket, (char*)SID_AUTH_INFO_NOTW3.data(), SID_AUTH_INFO_NOTW3.size(), 0);
}
if (temp == SOCKET_ERROR)
{
printf("send failed: %d\n", WSAGetLastError());
closesocket(ClientSocket);
delete[] recvbuf;
return 1;
}
temp = recv(ClientSocket, (char*)recvbuf, recvbuflen, 0);
if (temp > 0)
{
packets.write((char*)recvbuf, temp);
packets << std::endl;
temp = send(ClientSocket, (char*)SID_AUTH_CHECK, sizeof(SID_AUTH_CHECK), 0);
if (temp == SOCKET_ERROR)
{
printf("send failed: %d\n", WSAGetLastError());
closesocket(ClientSocket);
delete[] recvbuf;
return 1;
}
ret = GameLoop(ClientSocket, game);
if(ret != 0)
return ret;
}
else if (temp == 0)
std::cout << "Connection Closing\n";
else
{
printf("recv failed: %d\n", WSAGetLastError());
closesocket(ClientSocket);
delete[] recvbuf;
return 1;
}
}
else if (temp == 0)
std::cout << "Connection Closing\n";
else
{
printf("recv failed: %d\n", WSAGetLastError());
closesocket(ClientSocket);
delete[] recvbuf;
return 1;
}
delete[] recvbuf;
closesocket(ClientSocket);
break;
}
case 0x02: //BNFTP clients (v2)
{
std::cout << "FTP\n";
temp = recv(ClientSocket, (char*)recvbuf, recvbuflen, 0);
if (temp > 0)
{
printf("FTP: bytes received: %d\n", temp);
packets.write((char*)recvbuf, temp);
packets << std::endl;
temp = send(ClientSocket, (char*)SERVER_TOKEN, sizeof(SERVER_TOKEN), 0);
printf("FTP: Token sent\n");
if (temp == SOCKET_ERROR)
{
printf("FTP: Token send failed: %d\n", WSAGetLastError());
delete[] recvbuf;
closesocket(ClientSocket);
return 1;
}
temp = recv(ClientSocket, (char*)recvbuf, recvbuflen, 0);
if (temp > 0)
{
printf("FTP: Bytes received: %d\n", temp);
packets.write((char*)recvbuf, temp);
packets << std::endl;
}
else
{
printf("FTP: recv failed: %d\n", WSAGetLastError());
delete[] recvbuf;
closesocket(ClientSocket);
return 1;
}
char filename[temp-52];
int i;
for (i=52;i<temp;i++)
filename[i-52] = recvbuf[i];
std::cout << "Client requested file: " << filename << std::endl;
std::ifstream file(filename, std::ios::binary);
char fdata[32*1024];
file.seekg(((recvbuf[0]*32+recvbuf[1])*32+recvbuf[2])*32+recvbuf[3]);
if ((((recvbuf[0]*32+recvbuf[1])*32+recvbuf[2])*32+recvbuf[3])!=0)
std::cout<<"Starting point set at "<<((recvbuf[0]*32+recvbuf[1])*32+recvbuf[2])*32+recvbuf[3]<<std::endl;
int fsize = file.readsome(fdata, sizeof(fdata));
std::cout << "Size of file is: " << fsize << std::endl;
if(!file.good())
{
std::cout << "bad file: " << filename << std::endl;
closesocket(ClientSocket);
file.close();
break;
}
file.close();
int filenamelen = sizeof(filename);
char response_template[20 + filenamelen] = {0};
/*no header length
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0, //FILETIME\n
0, 0, 0, 0 //Structure
};*/
response_template[3] = (fsize >> 24) & 0xFF;
response_template[2] = (fsize >> 16) & 0xFF;
response_template[1] = (fsize >> 8) & 0xFF;
response_template[0] = fsize & 0xFF;
for (i = 0; i < filenamelen; i++)
{
response_template[20+i]=filename[i];
}
FILETIME filetime;
HANDLE filems = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
GetFileTime(filems, NULL, NULL, &filetime);
response_template[19] = (filetime.dwHighDateTime >> 24) & 0xFF;
response_template[18] = (filetime.dwHighDateTime >> 16) & 0xFF;
response_template[17] = (filetime.dwHighDateTime >> 8) & 0xFF;
response_template[16] = filetime.dwHighDateTime & 0xFF;
response_template[15] = (filetime.dwLowDateTime >> 24) & 0xFF;
response_template[14] = (filetime.dwLowDateTime >> 16) & 0xFF;
response_template[13] = (filetime.dwLowDateTime >> 8) & 0xFF;
response_template[12] = filetime.dwLowDateTime & 0xFF;
/*const char response[] = { 90, 0 //Header Length
0, 1, //Size
0, 0, //Ad banner ID or 0,0
0, 0, //Ad banner extension or 0,0
//0, 0, 2, 5, 0, 8, 1, 0, //FILETIME struct
//Filename here
0}; //file here*/
char response[24 + filenamelen];
response[3] = ((24 + filenamelen) >> 24) & 0xFF;
response[2] = ((24 + filenamelen) >> 16) & 0xFF;
response[1] = ((24 + filenamelen) >> 8) & 0xFF;
response[0] = (24 + filenamelen) & 0xFF;
for (i = 0; i < 20 + filenamelen; i++)
{
response[i + 4] = response_template[i];
}
char response1[fsize];
for (i = 0; i < fsize; i++)
{
response1[i] = fdata[i];
}
temp = send(ClientSocket, response, sizeof(response), 0);
std::cout << "FTP: File info packet sent\n";
if (temp == SOCKET_ERROR)
{
printf("FTP: Send failed: %d\n", WSAGetLastError());
delete[] recvbuf;
closesocket(ClientSocket);
return 1;
}
temp = send(ClientSocket, response1, sizeof(response1), 0);
std::cout << "FTP: File packet sent\n";
if (temp == SOCKET_ERROR)
{
printf("FTP: Send failed: %d\n", WSAGetLastError());
delete[] recvbuf;
closesocket(ClientSocket);
return 1;
}
/*temp = recv(ClientSocket, recvbuf, recvbuflen, 0);
if (temp < 0)
std::cout << "FTP: client closed the connection" << std::endl;*/
temp = shutdown(ClientSocket, SD_BOTH);
if (temp == SOCKET_ERROR)
{
printf("FTP: shutdown failed: %d\n", WSAGetLastError());
delete[] recvbuf;
closesocket(ClientSocket);
return 1;
}
else
std::cout << "FTP: closed the connection" << std::endl;
}
else
{
printf("FTP: recv failed: %d\n", WSAGetLastError());
delete[] recvbuf;
closesocket(ClientSocket);
return 1;
}
delete[] recvbuf;
closesocket(ClientSocket);
break;
}
case 0x03: //Telnet clients, shouldn't ever be accessed
case 0x63:
{
delete[] recvbuf;
ret = Telnet(ClientSocket);
closesocket(ClientSocket);
if (ret != 0)
return ret;
break;
}
default: //Unsupported
{
std::cout << "Requested unsupported protocol: " << std::hex << (int)recvbuf[0] << std::dec << std::endl;
delete[] recvbuf;
closesocket(ClientSocket);
break;
}
}
return 0;
}
int MTListenSocketThread(LPVOID pParam)
{
struct addrinfo *result = NULL, *ptr = NULL, hints;
int temp = 0;
WSADATA data;
if(WSAStartup(MAKEWORD(2,2), &data))
{
std::cout << "Socket failure.\n";
return 1;
}
std::cout << "Socket started\n";
ZeroMemory(&hints, sizeof (hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
// Resolve the local address and port to be used by the ListenSocket
temp = getaddrinfo(NULL, bind_port, &hints, &result);
if (temp != 0)
{
printf("getaddrinfo failed: %d\n", temp);
WSACleanup();
return 1;
}
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET)
{
printf("Error at socket(): %d\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}
temp = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen);
freeaddrinfo(result);
if (temp == SOCKET_ERROR)
{
printf("bind failed with error: %d\n", WSAGetLastError());
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
return 1;
}
if ( listen( ListenSocket, SOMAXCONN ) == SOCKET_ERROR )
{
printf( "Listen failed with error: %d\n", WSAGetLastError() );
closesocket(ListenSocket);
WSACleanup();
return 1;
}
while(!quit)
{
SOCKET ClientSocket=accept(ListenSocket,
NULL,NULL);
std::thread child(ClientThread, ClientSocket);
child.detach();
}
return 0;
}
int main()
{
int nRetCode = 0;
std::ifstream ub("users.log", std::ios::binary);
std::string buf = "";
char ch = ub.get();
do
{
//if ((ch == 0x0A || ch == 0x0D) && buf.length() >= 80)
if (ch == 0x0A && buf.length() >= 80)
{
users[buf.substr(0,16).c_str()]= std::tuple<std::string, std::string, std::string>
(buf.substr(16,32), buf.substr(48,32), buf.substr(80));
buf = "";
}
else
{
buf += ch;
}
/*while (buf.length() < 80)
{
ub >> buf2;
buf += ('\n' + buf2);
}*/
ch = ub.get();
} while(ch != EOF);
ub.close();
/*users["Bogdanbot"] = std::tuple<std::string, std::string, std::string>
("00000000000000000000000000000000", "00000000000000000000000000000000", "Bogdanbot@178.149.9.75");*/
std::cout << "Press q and Enter to terminate program\r\n";
std::thread main_thread(MTListenSocketThread, (void*)NULL);
main_thread.detach();
while(getchar()!='q');
quit = 1;
if (main_thread.joinable())
main_thread.join();
std::ofstream u("users.log", std::ios::trunc | std::ios::binary);
for(auto iter = users.begin(); iter != users.end(); ++iter)
{
u.write(iter->first.c_str(), 16);
u.write(std::get<0>(iter->second).c_str(), 32);
u.write(std::get<1>(iter->second).c_str(), 32);
char *email = std::get<2>(iter->second).c_str();
u.write(email, strlen(email));
u << (char)0x0A;
}
closesocket(ListenSocket);
WSACleanup();
delete [] recvbuf1;
delete [] recvbuf2;
return nRetCode;
}