-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.cpp
More file actions
299 lines (252 loc) · 6.29 KB
/
server.cpp
File metadata and controls
299 lines (252 loc) · 6.29 KB
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
#include "server.h"
#include "debug_utility.h"
#include "socket_utility.h"
#include <xtl.h>
#include <winsockx.h>
#include <stdio.h>
#include <string.h>
#include <bearssl.h>
#if !(SERVER_RSA || SERVER_EC || SERVER_MIXED)
#define SERVER_RSA 1
#define SERVER_EC 0
#define SERVER_MIXED 0
#endif
#if SERVER_RSA
#include "chain-rsa.h"
#include "key-rsa.h"
#define SKEY RSA
#elif SERVER_EC
#include "chain-ec.h"
#include "key-ec.h"
#define SKEY EC
#elif SERVER_MIXED
#include "chain-ec+rsa.h"
#include "key-ec.h"
#define SKEY EC
#else
#error Must use one of RSA, EC or MIXED chains.
#endif
#define MAX_NUM_THREADS 5
typedef struct {
int client_socket;
int thread_complete;
uint16_t port;
} thread_info_t;
typedef struct {
int active;
thread_info_t data;
HANDLE thread;
} pthread_info_t;
static pthread_info_t threads[MAX_NUM_THREADS];
static volatile bool server_running = false;
static int listen_socket_global = INVALID_SOCKET;
static const char *HTTP_RES =
"HTTP/1.1 200 OK\r\n"
"Cache-Control: no-store, no-cache, must-revalidate\r\n"
"Pragma: no-cache\r\n"
"Expires: 0\r\n"
//"Content-Length: 46\r\n"
"Connection: close\r\n"
"Content-Type: text/html; charset=iso-8859-1\r\n"
"\r\n"
"<head><link rel=\"stylesheet\" href=\"https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css\"></head>"
"<html>\r\n"
"<body>\r\n"
"<main class=\"container\"><h1>BearSSL Test by EqUiNoX</h1><a href=\"https://doomonline1.vercel.app/dos.html\" target=\"_blank\">Play Doom</a></main>\r\n"
"</body>\r\n"
"</html>\r\n";
static void client_process(void* data)
{
thread_info_t *thread_info = (thread_info_t*)data;
unsigned char io_buffer[BR_SSL_BUFSIZE_BIDI];
br_ssl_server_context server_context;
#if SERVER_RSA /* RSA */
#if SERVER_PROFILE_MIN_FS
#if SERVER_CHACHA20
br_ssl_server_init_mine2c(&server_context, CHAIN, CHAIN_LEN, &SKEY);
#else
br_ssl_server_init_mine2g(&server_context, CHAIN, CHAIN_LEN, &SKEY);
#endif
#elif SERVER_PROFILE_MIN_NOFS
br_ssl_server_init_minr2g(&server_context, CHAIN, CHAIN_LEN, &SKEY);
#else
br_ssl_server_init_full_rsa(&server_context, CHAIN, CHAIN_LEN, &SKEY);
#endif
#elif SERVER_EC /* EC */
#if SERVER_PROFILE_MIN_FS
#if SERVER_CHACHA20
br_ssl_server_init_minf2c(&server_context, CHAIN, CHAIN_LEN, &SKEY);
#else
br_ssl_server_init_minf2g(&server_context, CHAIN, CHAIN_LEN, &SKEY);
#endif
#elif SERVER_PROFILE_MIN_NOFS
br_ssl_server_init_minv2g(&server_context, CHAIN, CHAIN_LEN, &SKEY);
#else
br_ssl_server_init_full_ec(&server_context, CHAIN, CHAIN_LEN, BR_KEYTYPE_EC, &SKEY);
#endif
#else /* SERVER_MIXED */
#if SERVER_PROFILE_MIN_FS
#if SERVER_CHACHA20
br_ssl_server_init_minf2c(&server_context, CHAIN, CHAIN_LEN, &SKEY);
#else
br_ssl_server_init_minf2g(&server_context, CHAIN, CHAIN_LEN, &SKEY);
#endif
#elif SERVER_PROFILE_MIN_NOFS
br_ssl_server_init_minu2g(&server_context, CHAIN, CHAIN_LEN, &SKEY);
#else
br_ssl_server_init_full_ec(&server_context, CHAIN, CHAIN_LEN, BR_KEYTYPE_RSA, &SKEY);
#endif
#endif
br_ssl_engine_set_buffer(&server_context.eng, io_buffer, BR_SSL_BUFSIZE_BIDI, 1);
br_ssl_server_reset(&server_context);
br_sslio_context io_context;
br_sslio_init(&io_context, &server_context.eng, socket_utility::socket_read, &thread_info->client_socket, socket_utility::socket_write, &thread_info->client_socket);
bool client_dropped = false;
int feed_count = 0;
while (true)
{
unsigned char current_char;
if (br_sslio_read(&io_context, ¤t_char, 1) < 0)
{
client_dropped = true;
break;
}
if (current_char == 0x0D)
{
continue;
}
if (current_char == 0x0A)
{
if (feed_count == 1)
{
break;
}
feed_count = 1;
}
else
{
feed_count = 0;
}
}
if (client_dropped == false)
{
br_sslio_write_all(&io_context, HTTP_RES, strlen(HTTP_RES));
}
int error = br_ssl_engine_last_error(&server_context.eng);
if (error == 0)
{
debug_utility::debug_print("SSL closed (correctly).\n");
}
else
{
debug_utility::debug_print("SSL error: %d\n", error);
}
br_sslio_close(&io_context);
closesocket(thread_info->client_socket);
thread_info->thread_complete = 1;
}
static uint64_t WINAPI process(void* data)
{
uint16_t port = (uint16_t)data;
int listen_socket = socket_utility::host_bind(port);
if (listen_socket == INVALID_SOCKET)
{
server_running = false;
return EXIT_FAILURE;
}
listen_socket_global = listen_socket;
while (server_running)
{
int read_status = socket_utility::get_read_status(listen_socket);
if (read_status == SOCKET_ERROR)
{
break;
}
int client_socket = INVALID_SOCKET;
if (read_status == 1)
{
client_socket = socket_utility::accept_client(listen_socket);
}
else
{
Sleep(50);
continue;
}
if (client_socket == INVALID_SOCKET)
{
continue;
}
int i;
for (i = 0; i < MAX_NUM_THREADS; i++)
{
if (threads[i].active == 0)
{
break;
}
if (threads[i].data.thread_complete == 1)
{
CloseHandle(threads[i].thread);
memset(&threads[i], 0, sizeof(pthread_info_t));
break;
}
}
if (i == MAX_NUM_THREADS)
{
closesocket(client_socket);
continue;
}
threads[i].active = 1;
threads[i].data.client_socket = client_socket;
threads[i].data.port = port;
threads[i].thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)client_process, (void*)&threads[i].data, 0, NULL);
}
closesocket(listen_socket);
listen_socket_global = INVALID_SOCKET;
return 0;
}
server::server()
: thread(NULL), running(false)
{
}
void server::start(uint16_t port)
{
memset(threads, 0, sizeof(threads));
running = true;
server_running = true;
thread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)process, (void*)port, 0, NULL);
}
void server::stop()
{
if (!running)
{
return;
}
running = false;
server_running = false;
// Close the listening socket to unblock accept
if (listen_socket_global != INVALID_SOCKET)
{
closesocket(listen_socket_global);
}
// Wait for the main server thread to finish
if (thread != NULL)
{
WaitForSingleObject(thread, 5000);
CloseHandle(thread);
thread = NULL;
}
// Wait for all client threads to complete
for (int i = 0; i < MAX_NUM_THREADS; i++)
{
if (threads[i].active && threads[i].thread != NULL)
{
WaitForSingleObject(threads[i].thread, 2000);
CloseHandle(threads[i].thread);
}
}
memset(threads, 0, sizeof(threads));
}
bool server::is_running()
{
return running;
}