-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathhev-config.c
515 lines (423 loc) · 12.2 KB
/
hev-config.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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
============================================================================
Name : hev-config.c
Author : hev <[email protected]>
Copyright : Copyright (c) 2017 - 2024 hev
Description : Config
============================================================================
*/
#include <stdio.h>
#include <arpa/inet.h>
#include <yaml.h>
#include <hev-socks5.h>
#include <hev-socks5-proto.h>
#include "hev-logger.h"
#include "hev-config.h"
static unsigned int workers;
static int listen_ipv6_only;
static char listen_address[256];
static char listen_port[8];
static char udp_listen_address[256];
static char udp_listen_port[8];
static char bind_address[2][256];
static char bind_interface[256];
static char auth_file[1024];
static char username[256];
static char password[256];
static char log_file[1024];
static char pid_file[1024];
static int task_stack_size = 8192;
static int udp_recv_buffer_size = 524288;
static int connect_timeout = 5000;
static int read_write_timeout = 60000;
static int limit_nofile = 65535;
static int log_level = HEV_LOGGER_WARN;
static int domain_addr_type = HEV_SOCKS5_DOMAIN_ADDR_TYPE_UNSPEC;
static unsigned int socket_mark;
static int
hev_config_parse_main (yaml_document_t *doc, yaml_node_t *base)
{
yaml_node_pair_t *pair;
const char *addr = NULL;
const char *port = NULL;
const char *mark = NULL;
const char *udp_addr = NULL;
const char *udp_port = NULL;
const char *bind_saddr = NULL;
const char *bind_saddr4 = NULL;
const char *bind_saddr6 = NULL;
const char *bind_iface = NULL;
const char *addr_type = NULL;
if (!base || YAML_MAPPING_NODE != base->type)
return -1;
for (pair = base->data.mapping.pairs.start;
pair < base->data.mapping.pairs.top; pair++) {
yaml_node_t *node;
const char *key, *value;
if (!pair->key || !pair->value)
break;
node = yaml_document_get_node (doc, pair->key);
if (!node || YAML_SCALAR_NODE != node->type)
break;
key = (const char *)node->data.scalar.value;
node = yaml_document_get_node (doc, pair->value);
if (!node || YAML_SCALAR_NODE != node->type)
break;
value = (const char *)node->data.scalar.value;
if (0 == strcmp (key, "workers"))
workers = strtoul (value, NULL, 10);
else if (0 == strcmp (key, "port"))
port = value;
else if (0 == strcmp (key, "listen-address"))
addr = value;
else if (0 == strcmp (key, "udp-port"))
udp_port = value;
else if (0 == strcmp (key, "udp-listen-address"))
udp_addr = value;
else if (0 == strcmp (key, "listen-ipv6-only"))
listen_ipv6_only = (0 == strcasecmp (value, "true")) ? 1 : 0;
else if (0 == strcmp (key, "bind-address"))
bind_saddr = value;
else if (0 == strcmp (key, "bind-address-v4"))
bind_saddr4 = value;
else if (0 == strcmp (key, "bind-address-v6"))
bind_saddr6 = value;
else if (0 == strcmp (key, "bind-interface"))
bind_iface = value;
else if (0 == strcmp (key, "domain-address-type"))
addr_type = value;
else if (0 == strcmp (key, "mark"))
mark = value;
}
if (!workers) {
fprintf (stderr, "Can't found main.workers!\n");
return -1;
}
if (!port) {
fprintf (stderr, "Can't found main.port!\n");
return -1;
}
if (!addr) {
fprintf (stderr, "Can't found main.listen-address!\n");
return -1;
}
strncpy (listen_port, port, 8 - 1);
strncpy (listen_address, addr, 256 - 1);
if (udp_port)
strncpy (udp_listen_port, udp_port, 8 - 1);
if (udp_addr)
strncpy (udp_listen_address, udp_addr, 256 - 1);
if (bind_saddr4 && bind_saddr4[0] != '\0')
strncpy (bind_address[0], bind_saddr4, 256 - 1);
else if (bind_saddr && bind_saddr[0] != '\0')
strncpy (bind_address[0], bind_saddr, 256 - 1);
if (bind_saddr6 && bind_saddr6[0] != '\0')
strncpy (bind_address[1], bind_saddr6, 256 - 1);
else if (bind_saddr && bind_saddr[0] != '\0')
strncpy (bind_address[1], bind_saddr, 256 - 1);
if (bind_iface)
strncpy (bind_interface, bind_iface, 256 - 1);
if (addr_type) {
if (0 == strcmp (addr_type, "ipv4"))
domain_addr_type = HEV_SOCKS5_DOMAIN_ADDR_TYPE_IPV4;
else if (0 == strcmp (addr_type, "ipv6"))
domain_addr_type = HEV_SOCKS5_DOMAIN_ADDR_TYPE_IPV6;
}
if (mark)
socket_mark = strtoul (mark, NULL, 0);
return 0;
}
static int
hev_config_parse_auth (yaml_document_t *doc, yaml_node_t *base)
{
yaml_node_pair_t *pair;
const char *user = NULL, *pass = NULL, *file = NULL;
if (!base || YAML_MAPPING_NODE != base->type)
return -1;
for (pair = base->data.mapping.pairs.start;
pair < base->data.mapping.pairs.top; pair++) {
yaml_node_t *node;
const char *key, *value;
if (!pair->key || !pair->value)
break;
node = yaml_document_get_node (doc, pair->key);
if (!node || YAML_SCALAR_NODE != node->type)
break;
key = (const char *)node->data.scalar.value;
node = yaml_document_get_node (doc, pair->value);
if (!node || YAML_SCALAR_NODE != node->type)
break;
value = (const char *)node->data.scalar.value;
if (0 == strcmp (key, "username"))
user = value;
else if (0 == strcmp (key, "password"))
pass = value;
else if (0 == strcmp (key, "file"))
file = value;
}
if (file) {
strncpy (auth_file, file, 1023);
} else if (user && pass) {
strncpy (username, user, 255);
strncpy (password, pass, 255);
}
return 0;
}
static int
hev_config_parse_log_level (const char *value)
{
if (0 == strcmp (value, "debug"))
return HEV_LOGGER_DEBUG;
else if (0 == strcmp (value, "info"))
return HEV_LOGGER_INFO;
else if (0 == strcmp (value, "error"))
return HEV_LOGGER_ERROR;
return HEV_LOGGER_WARN;
}
static int
hev_config_parse_misc (yaml_document_t *doc, yaml_node_t *base)
{
yaml_node_pair_t *pair;
if (!base || YAML_MAPPING_NODE != base->type)
return -1;
for (pair = base->data.mapping.pairs.start;
pair < base->data.mapping.pairs.top; pair++) {
yaml_node_t *node;
const char *key, *value;
if (!pair->key || !pair->value)
break;
node = yaml_document_get_node (doc, pair->key);
if (!node || YAML_SCALAR_NODE != node->type)
break;
key = (const char *)node->data.scalar.value;
node = yaml_document_get_node (doc, pair->value);
if (!node || YAML_SCALAR_NODE != node->type)
break;
value = (const char *)node->data.scalar.value;
if (0 == strcmp (key, "task-stack-size"))
task_stack_size = strtoul (value, NULL, 10);
else if (0 == strcmp (key, "udp-recv-buffer-size"))
udp_recv_buffer_size = strtoul (value, NULL, 10);
else if (0 == strcmp (key, "connect-timeout"))
connect_timeout = strtoul (value, NULL, 10);
else if (0 == strcmp (key, "read-write-timeout"))
read_write_timeout = strtoul (value, NULL, 10);
else if (0 == strcmp (key, "pid-file"))
strncpy (pid_file, value, 1024 - 1);
else if (0 == strcmp (key, "log-file"))
strncpy (log_file, value, 1024 - 1);
else if (0 == strcmp (key, "log-level"))
log_level = hev_config_parse_log_level (value);
else if (0 == strcmp (key, "limit-nofile"))
limit_nofile = strtol (value, NULL, 10);
}
return 0;
}
static int
hev_config_parse_doc (yaml_document_t *doc)
{
yaml_node_t *root;
yaml_node_pair_t *pair;
root = yaml_document_get_root_node (doc);
if (!root || YAML_MAPPING_NODE != root->type)
return -1;
for (pair = root->data.mapping.pairs.start;
pair < root->data.mapping.pairs.top; pair++) {
yaml_node_t *node;
const char *key;
int res = 0;
if (!pair->key || !pair->value)
break;
node = yaml_document_get_node (doc, pair->key);
if (!node || YAML_SCALAR_NODE != node->type)
break;
key = (const char *)node->data.scalar.value;
node = yaml_document_get_node (doc, pair->value);
if (0 == strcmp (key, "main"))
res = hev_config_parse_main (doc, node);
else if (0 == strcmp (key, "auth"))
res = hev_config_parse_auth (doc, node);
else if (0 == strcmp (key, "misc"))
res = hev_config_parse_misc (doc, node);
if (res < 0)
return -1;
}
return 0;
}
int
hev_config_init_from_file (const char *path)
{
yaml_parser_t parser;
yaml_document_t doc;
FILE *fp;
int res = -1;
if (!yaml_parser_initialize (&parser))
goto exit;
fp = fopen (path, "r");
if (!fp) {
fprintf (stderr, "Open %s failed!\n", path);
goto exit_free_parser;
}
yaml_parser_set_input_file (&parser, fp);
if (!yaml_parser_load (&parser, &doc)) {
fprintf (stderr, "Parse %s failed!\n", path);
goto exit_close_fp;
}
res = hev_config_parse_doc (&doc);
yaml_document_delete (&doc);
exit_close_fp:
fclose (fp);
exit_free_parser:
yaml_parser_delete (&parser);
exit:
return res;
}
int
hev_config_init_from_str (const unsigned char *config_str,
unsigned int config_len)
{
yaml_parser_t parser;
yaml_document_t doc;
int res = -1;
if (!yaml_parser_initialize (&parser))
goto exit;
yaml_parser_set_input_string (&parser, config_str, config_len);
if (!yaml_parser_load (&parser, &doc)) {
fprintf (stderr, "Failed to parse config.");
goto exit_free_parser;
}
res = hev_config_parse_doc (&doc);
yaml_document_delete (&doc);
exit_free_parser:
yaml_parser_delete (&parser);
exit:
return res;
}
void
hev_config_fini (void)
{
}
unsigned int
hev_config_get_workers (void)
{
return workers;
}
const char *
hev_config_get_listen_address (void)
{
return listen_address;
}
const char *
hev_config_get_listen_port (void)
{
return listen_port;
}
const char *
hev_config_get_udp_listen_address (void)
{
if ('\0' == udp_listen_address[0])
return NULL;
return udp_listen_address;
}
const char *
hev_config_get_udp_listen_port (void)
{
if ('\0' == udp_listen_port[0])
return NULL;
return udp_listen_port;
}
int
hev_config_get_listen_ipv6_only (void)
{
return listen_ipv6_only;
}
const char *
hev_config_get_bind_address (int family)
{
int idx = family == AF_INET6;
if ('\0' == bind_address[idx][0])
return NULL;
return bind_address[idx];
}
const char *
hev_config_get_bind_interface (void)
{
if ('\0' == bind_interface[0])
return NULL;
return bind_interface;
}
int
hev_config_get_domain_address_type (void)
{
return domain_addr_type;
}
unsigned int
hev_config_get_socket_mark (void)
{
return socket_mark;
}
const char *
hev_config_get_auth_file (void)
{
if ('\0' == auth_file[0])
return NULL;
return auth_file;
}
const char *
hev_config_get_auth_username (void)
{
if ('\0' == username[0])
return NULL;
return username;
}
const char *
hev_config_get_auth_password (void)
{
if ('\0' == password[0])
return NULL;
return password;
}
int
hev_config_get_misc_task_stack_size (void)
{
return task_stack_size;
}
int
hev_config_get_misc_udp_recv_buffer_size (void)
{
return udp_recv_buffer_size;
}
int
hev_config_get_misc_connect_timeout (void)
{
return connect_timeout;
}
int
hev_config_get_misc_read_write_timeout (void)
{
return read_write_timeout;
}
int
hev_config_get_misc_limit_nofile (void)
{
return limit_nofile;
}
const char *
hev_config_get_misc_pid_file (void)
{
if ('\0' == pid_file[0])
return NULL;
return pid_file;
}
const char *
hev_config_get_misc_log_file (void)
{
if ('\0' == log_file[0])
return "stderr";
return log_file;
}
int
hev_config_get_misc_log_level (void)
{
return log_level;
}