-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesocks.c
More file actions
355 lines (301 loc) · 8.39 KB
/
esocks.c
File metadata and controls
355 lines (301 loc) · 8.39 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
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
/*
* Watch out - this is a work in progress!
*
* Use of this source code is governed by a
* license that can be found in the LICENSE file.
*
*/
#include <sys/wait.h>
#include <signal.h>
#include "def.h"
#include "crypto.h"
#include "log.h"
#include "server.h"
#include "version.h"
#include "helper.h"
struct settings settings;
static int quit = 0;
static void settings_init(void);
static void usage(void);
static void fatal_error_cb(int err);
static void start_master_process(int workers);
static void save_pid(const char *pid_file);
static void remove_pid(const char *pid_file);
static void sig_handler(int sig);
static void fatal_error_cb(int err)
{
log_e("fata_error_cb got=%d\n", err);
}
static void settings_init(void)
{
#define DEFAULT_SERVER_PORT 1080
settings.listen_addr = "0.0.0.0";
settings.listen_port = DEFAULT_SERVER_PORT;
settings.server_addr = "0.0.0.0";
settings.server_port = DEFAULT_SERVER_PORT;
settings.passphrase = (u8 *)"too lame to set password";
// Timeout for connections made between clients and a server.
#define DEFAULT_CONNECTION_TIMEOUT 300
settings.connection_timeout = DEFAULT_CONNECTION_TIMEOUT;
settings.relay_mode = false;
settings.resolv_conf = "/etc/resolv.conf";
settings.nameserver = NULL;
// TOODO: Let users set up rate limit.
#define DEFAULT_RATE_LIMIT 20000
settings.rate_wlimit = DEFAULT_RATE_LIMIT;
settings.rate_rlimit = DEFAULT_RATE_LIMIT;
settings.proxy = NULL;
// TODO: dns ttl
#define DEFAULT_DNS_CACHE_TVAL 6500;
settings.dns_cache_tval = DEFAULT_DNS_CACHE_TVAL;
#define DEFAULT_WORKER_NUMBER 1
settings.workers = DEFAULT_WORKER_NUMBER;
settings.daemon_mode = false;
settings.pid_file = "/var/run/esocks.pid";
// TODO: refactor, this ain't good for security!
const u8 key16[16] = {
0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
};
const u8 ckey32[32] = {
0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
};
settings.key = ckey32;
settings.iv = key16;
settings.cipher_name = "aes-256-cfb";
}
static void usage(void)
{
printf("Esocks " ESOCKS_VERSION ", a socks5 proxy server\n"
"Usage: esocks [OPTIONS...]\n"
"\n"
"OPTIONS:\n"
" -c cipher name (default aes-256-cfb)\n"
" -C path to config file\n"
" -d dns cache timeout (default 6500 seconds)\n"
" -D enable daemon mode (default false)\n"
" -j connect to this port\n"
" -k password for AES enc/dec\n"
" -n worker number\n"
" -o path to resolver conf file (default /etc/resolv.conf)\n"
" -p bind to this port (default 1080)\n"
" -P write PID file (default /var/run/esocks.pid\n"
" -s bind to this address (default 0.0.0.0)\n"
" -u connect to this server address\n"
" -t timeout for connections (default 300 seconds)\n"
" -g nameserver\n"
" -V show version number\n"
);
exit(1);
}
static void sig_handler(int sig)
{
switch (sig) {
case SIGTERM:
case SIGINT:
quit++;
default:
break;
}
}
static void start_master_process(int num)
{
int j;
pid_t pid;
int workers = num;
pid_t pids[workers];
struct sigaction sa;
sigset_t orig_set, set;
sigemptyset(&set);
sigaddset(&set, SIGTERM);
sigaddset(&set, SIGINT);
if (sigprocmask(SIG_BLOCK, &set, &orig_set) == -1)
log_e("sigprocmask");
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = sig_handler;
if (sigaction(SIGTERM, &sa, NULL) == -1)
log_e("sigaction - SIGTERM");
if (sigaction(SIGINT, &sa, NULL) == -1)
log_e("sigaction - SIGINT");
log_i("fork(): parent pid=%ld", (long)getpid());
for (j = 0; j < workers; j++) {
pid = fork();
switch (pid) {
case -1:
log_e("fork(): failed while forking \"%ld\"", (long)getpid());
return;
case 0:
log_i("fork(): child pid=%ld", (long)getpid());
if (sigprocmask(SIG_SETMASK, &orig_set, NULL) == -1)
log_e("sigprocmask");
e_start_server();
exit(0);
default:
pids[j] = pid;
break;
}
}
for ( ;; ) {
if (quit)
break;
sigsuspend(&orig_set);
}
if (sigprocmask(SIG_SETMASK, &orig_set, NULL) == -1)
log_e("sigprocmask");
for (j = 0; j < workers; j++) {
int status;
kill(pids[j], SIGTERM);
if ((waitpid(pids[j], &status, 0)) == -1)
log_e("waitpid()");
}
}
static void save_pid(const char *pid_file)
{
FILE *fp;
fp = fopen(pid_file, "r");
if (fp != NULL) {
char buf[1024];
if (fgets(buf, sizeof(buf), fp) != NULL) {
char *endptr;
pid_t pid;
pid = strtoul(buf, &endptr, 10);
if (pid && kill((pid_t)pid, SIGTERM) == 0)
log_warn("The pid file contained pid: %u", pid);
}
fclose(fp);
}
fp = fopen(pid_file, "w");
if (fp != NULL) {
fprintf(fp, "%ld\n", (long)getpid());
fclose(fp);
} else
log_ex(1, "fopen(%ld, w)", (long)getpid());
}
static void remove_pid(const char *pid_file)
{
if (pid_file == NULL)
return;
if (unlink(pid_file) != 0)
log_ex(1, "unlink(%s)", pid_file);
}
int main(int argc, char **argv)
{
int port;
int cc = 0;
int daemon_nochdir = 0;
int daemon_noclose = 0;
_Bool load_config = false;
crypto_init();
settings_init();
char *shortopts =
"C:" /* TODO: support config file */
"c:" /* cipher name */
"d:" /* dns cache timeout */
"D" /* TODO: support daemon mode */
// "g:" /* TODO: make it comma-separate; nameservers */
"j:" /* Connect to this port */
"k:" /* password for AES enc/dec */
"n:" /* TODO: support worker number */
"o:" /* Path to resolver conf */
"p:" /* Bind to this port */
"P:" /* TODO: Save PID file */
// "r:" /* TODO: read rate limit */
"s:" /* Bind to this address */
"t:" /* Timeout for connections */
"u:" /* Connect to this address */
// "v" /* TODO: verbose */
"Vh" /* Show version number */
// "w:" /* TODO: write rate limit */
;
while (cc != -1) {
cc = getopt(argc, argv, shortopts);
switch (cc) {
case 'c':
settings.cipher_name = optarg;
break;
case 'C':
load_config = true;
settings.config_file = optarg;
break;
case 'd':
settings.dns_cache_tval = atol(optarg);
break;
case 'D':
settings.daemon_mode = true;
break;
case 'j':
port = atoi(optarg);
if (port < 1 || port > 65535)
usage();
settings.server_port = port;
settings.relay_mode = true;
break;
case 'k':
settings.passphrase = (u8 *)optarg;
break;
case 'n':
settings.workers = atoi(optarg);
break;
case 'o':
settings.resolv_conf = optarg;
break;
case 'p':
port = atoi(optarg);
if (port < 1 || port > 65535)
usage();
settings.listen_port = port;
break;
case 'P':
settings.pid_file = optarg;
break;
case 's':
settings.listen_addr = optarg;
break;
case 't':
settings.connection_timeout = atol(optarg);
break;
case 'u':
settings.server_addr = optarg;
settings.relay_mode = true;
break;
case 'V':
printf("esocks %s\n", ESOCKS_VERSION);
exit(0);
case 'h':
usage();
break;
case '?':
usage();
}
}
if (load_config)
if (e_parse_conf_file(&settings, settings.config_file) != 0)
log_ex(1, "cannot find config file: %s", settings.config_file);
settings.plen = strlen((char *)settings.passphrase);
settings.cipher = EVP_get_cipherbyname(settings.cipher_name);
if (settings.cipher == NULL)
log_ex(1, "failed to set cipher: %s", settings.cipher_name);
settings.dgst = EVP_md5();
EVP_BytesToKey(settings.cipher, settings.dgst, NULL,
settings.passphrase, settings.plen, 1, (u8 *)settings.key,
(u8 *)settings.iv);
DEBUG ? NULL : event_set_fatal_callback(fatal_error_cb);
save_pid(settings.pid_file);
if (settings.daemon_mode)
daemonize(daemon_nochdir, daemon_noclose);
if (settings.relay_mode)
log_i("listening on %s:%d and connect to %s:%d",
settings.listen_addr, settings.listen_port, settings.server_addr,
settings.server_port);
else
log_i("listening on %s:%d", settings.listen_addr, settings.listen_port);
log_d(DEBUG, "running in debug mode, timeout=%d mode=%s",
settings.connection_timeout, settings.cipher_name);
start_master_process(settings.workers);
remove_pid(settings.pid_file);
exit(0);
}