forked from semigodking/redsocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shadowsocks.c
435 lines (381 loc) · 13.1 KB
/
shadowsocks.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
/* redsocks2 - transparent TCP-to-proxy redirector
* Copyright (C) 2013-2017 Zhuofei Wang <[email protected]>
*
* This code is based on redsocks project developed by Leonid Evdokimov.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/socket.h>
#include <event2/bufferevent.h>
#include <event2/bufferevent_struct.h>
#include "utils.h"
#include "log.h"
#include "redsocks.h"
#include "encrypt.h"
#include "shadowsocks.h"
typedef enum ss_state_t {
ss_new,
ss_connected,
ss_MAX,
} ss_state;
typedef struct ss_client_t {
struct enc_ctx e_ctx;
struct enc_ctx d_ctx;
short e_ctx_init;
short d_ctx_init;
} ss_client;
typedef struct ss_instance_t {
int method;
enc_info info;
} ss_instance;
void redsocks_event_error(struct bufferevent *buffev, short what, void *_arg);
int ss_is_valid_cred(const char *method, const char *password)
{
if (!method || !password)
return 0;
if (strlen(method) > 255) {
log_error(LOG_WARNING, "Shadowsocks encryption method can't be more than 255 chars.");
return 0;
}
if (strlen(password) > 255) {
log_error(LOG_WARNING, "Shadowsocks encryption password can't be more than 255 chars.");
return 0;
}
return 1;
}
static void ss_client_init(redsocks_client *client)
{
client->state = ss_new;
}
static void ss_client_fini(redsocks_client *client)
{
ss_client *sclient = (void*)(client + 1);
if (sclient->e_ctx_init) {
enc_ctx_free(&sclient->e_ctx);
sclient->e_ctx_init = 0;
}
if (sclient->d_ctx_init) {
enc_ctx_free(&sclient->d_ctx);
sclient->d_ctx_init = 0;
}
}
static void encrypt_mem(redsocks_client * client,
char * data, size_t len,
struct evbuffer * buf_out, int decrypt)
{
ss_client *sclient = (void*)(client + 1);
struct evbuffer_iovec vec;
size_t required;
int rc;
if (!len || !data)
return;
if (decrypt)
required = ss_calc_buffer_size(&sclient->d_ctx, len);
else
required = ss_calc_buffer_size(&sclient->e_ctx, len);
if (required && evbuffer_reserve_space(buf_out, required, &vec, 1) == 1)
{
if (decrypt)
rc = ss_decrypt(&sclient->d_ctx, data, len, vec.iov_base, &vec.iov_len);
else
rc = ss_encrypt(&sclient->e_ctx, data, len, vec.iov_base, &vec.iov_len);
if (!rc)
vec.iov_len = 0;
evbuffer_commit_space(buf_out, &vec, 1);
}
}
static void encrypt_buffer(redsocks_client *client,
struct bufferevent * from,
struct bufferevent * to)
{
// To reduce memory copy, just encrypt one block a time
struct evbuffer * buf_in = bufferevent_get_input(from);
size_t input_size = evbuffer_get_contiguous_space(buf_in);
char * input;
if (!input_size)
return;
input = (char *)evbuffer_pullup(buf_in, input_size);
encrypt_mem(client, input, input_size, bufferevent_get_output(to), 0);
evbuffer_drain(buf_in, input_size);
}
static void decrypt_buffer(redsocks_client * client,
struct bufferevent * from,
struct bufferevent * to)
{
// To reduce memory copy, just decrypt one block a time
struct evbuffer * buf_in = bufferevent_get_input(from);
size_t input_size = evbuffer_get_contiguous_space(buf_in);
char * input;
if (!input_size)
return;
input = (char *)evbuffer_pullup(buf_in, input_size);
encrypt_mem(client, input, input_size, bufferevent_get_output(to), 1);
evbuffer_drain(buf_in, input_size);
}
static void ss_client_writecb(struct bufferevent *buffev, void *_arg)
{
redsocks_client *client = _arg;
struct bufferevent * from = client->relay;
struct bufferevent * to = buffev;
size_t input_size = evbuffer_get_contiguous_space(bufferevent_get_input(from));
size_t output_size = evbuffer_get_length(bufferevent_get_output(to));
assert(buffev == client->client);
redsocks_touch_client(client);
if (process_shutdown_on_write_(client, from, to))
return;
if (client->state == ss_connected)
{
/* encrypt and forward data received from client side */
if (output_size < get_write_hwm(to))
{
if (input_size)
decrypt_buffer(client, from, to);
if (!(client->relay_evshut & EV_READ) && bufferevent_enable(from, EV_READ) == -1)
redsocks_log_errno(client, LOG_ERR, "bufferevent_enable");
}
}
else
{
redsocks_drop_client(client);
}
}
static void ss_client_readcb(struct bufferevent *buffev, void *_arg)
{
redsocks_client *client = _arg;
struct bufferevent * from = buffev;
struct bufferevent * to = client->relay;
size_t output_size = evbuffer_get_length(bufferevent_get_output(to));
assert(buffev == client->client);
redsocks_touch_client(client);
if (client->state == ss_connected)
{
/* encrypt and forward data to the other side */
if (output_size < get_write_hwm(to))
{
encrypt_buffer(client, from, to);
if (bufferevent_enable(from, EV_READ) == -1)
redsocks_log_errno(client, LOG_ERR, "bufferevent_enable");
}
else
{
if (bufferevent_disable(from, EV_READ) == -1)
redsocks_log_errno(client, LOG_ERR, "bufferevent_disable");
}
}
else
{
redsocks_drop_client(client);
}
}
static void ss_relay_writecb(struct bufferevent *buffev, void *_arg)
{
redsocks_client *client = _arg;
struct bufferevent * from = client->client;
struct bufferevent * to = buffev;
size_t input_size = evbuffer_get_contiguous_space(bufferevent_get_input(from));
size_t output_size = evbuffer_get_length(bufferevent_get_output(to));
assert(buffev == client->relay);
redsocks_touch_client(client);
if (process_shutdown_on_write_(client, from, to))
return;
if (client->state == ss_connected)
{
/* encrypt and forward data received from client side */
if (output_size < get_write_hwm(to))
{
if (input_size)
encrypt_buffer(client, from, to);
if (!(client->client_evshut & EV_READ) && bufferevent_enable(from, EV_READ) == -1)
redsocks_log_errno(client, LOG_ERR, "bufferevent_enable");
}
}
else
{
redsocks_drop_client(client);
}
}
static void ss_relay_readcb(struct bufferevent *buffev, void *_arg)
{
redsocks_client *client = _arg;
struct bufferevent * from = buffev;
struct bufferevent * to = client->client;
size_t input_size = evbuffer_get_contiguous_space(bufferevent_get_input(from));
size_t output_size = evbuffer_get_length(bufferevent_get_output(to));
assert(buffev == client->relay);
redsocks_touch_client(client);
if (client->state == ss_connected)
{
/* decrypt and forward data to client side */
if (output_size < get_write_hwm(to))
{
if (input_size)
decrypt_buffer(client, from, to);
if (bufferevent_enable(from, EV_READ) == -1)
redsocks_log_errno(client, LOG_ERR, "bufferevent_enable");
}
else
{
if (bufferevent_disable(from, EV_READ) == -1)
redsocks_log_errno(client, LOG_ERR, "bufferevent_disable");
}
}
else
{
redsocks_drop_client(client);
}
}
static void ss_relay_connected(struct bufferevent *buffev, void *_arg)
{
redsocks_client *client = _arg;
assert(buffev == client->relay);
assert(client->state == ss_new);
redsocks_touch_client(client);
if (!red_is_socket_connected_ok(buffev)) {
redsocks_log_error(client, LOG_DEBUG, "failed to connect to destination");
redsocks_drop_client(client);
return;
}
client->relay_connected = 1;
client->state = ss_connected;
/* We do not need to detect timeouts any more.
The two peers will handle it. */
bufferevent_set_timeouts(client->relay, NULL, NULL);
if (redsocks_start_relay(client))
// redsocks_start_relay() drops client on failure
return;
/* overwrite theread callback to my function */
bufferevent_setcb(client->client, ss_client_readcb,
ss_client_writecb,
redsocks_event_error,
client);
bufferevent_setcb(client->relay, ss_relay_readcb,
ss_relay_writecb,
redsocks_event_error,
client);
// Write any data received from client side to relay.
if (evbuffer_get_length(bufferevent_get_input(client->client)))
ss_relay_writecb(client->relay, client);
return;
}
static int ss_connect_relay(redsocks_client *client)
{
char * interface = client->instance->config.interface;
ss_client *sclient = (void*)(client + 1);
ss_instance * ss = (ss_instance *)(client->instance+1);
ss_header header;
struct timeval tv;
size_t len = 0;
size_t header_len = 0;
char buff[64+sizeof(header)];
if (enc_ctx_init(&ss->info, &sclient->e_ctx, 1)) {
log_error(LOG_ERR, "Shadowsocks failed to initialize encryption context.");
redsocks_drop_client(client);
return -1;
}
sclient->e_ctx_init = 1;
if (enc_ctx_init(&ss->info, &sclient->d_ctx, 0)) {
log_error(LOG_ERR, "Shadowsocks failed to initialize decryption context.");
redsocks_drop_client(client);
return -1;
}
sclient->d_ctx_init = 1;
/* build and send header */
if (client->destaddr.ss_family == AF_INET) {
struct sockaddr_in * addr = (struct sockaddr_in *)&client->destaddr;
header.v4.addr_type = ss_addrtype_ipv4;
header.v4.addr = addr->sin_addr.s_addr;
header.v4.port = addr->sin_port;
header_len = sizeof(ss_header_ipv4);
}
else if (client->destaddr.ss_family == AF_INET6) {
struct sockaddr_in6 * addr = (struct sockaddr_in6 *)&client->destaddr;
header.v6.addr_type = ss_addrtype_ipv6;
header.v6.addr = addr->sin6_addr;
header.v6.port = addr->sin6_port;
header_len = sizeof(ss_header_ipv6);
}
else {
log_error(LOG_ERR, "Unsupported address family: %d", client->destaddr.ss_family);
redsocks_drop_client(client);
return -1;
}
len += header_len;
size_t sz = sizeof(buff);
if (!ss_encrypt(&sclient->e_ctx, (char *)&header, len, &buff[0], &sz)) {
log_error(LOG_ERR, "Encryption error.");
redsocks_drop_client(client);
return -1;
}
len = sz;
tv.tv_sec = client->instance->config.timeout;
tv.tv_usec = 0;
client->relay = red_connect_relay_tfo(
interface,
&client->instance->config.relayaddr,
NULL,
ss_relay_connected,
redsocks_event_error,
client,
&tv,
&buff[0],
&sz);
if (!client->relay) {
redsocks_drop_client(client);
return -1;
}
else if (sz && sz != len) {
log_error(LOG_ERR, "Unexpected length of data sent.");
redsocks_drop_client(client);
return -1;
}
return 0;
}
static int ss_instance_init(struct redsocks_instance_t *instance)
{
ss_instance * ss = (ss_instance *)(instance+1);
const redsocks_config *config = &instance->config;
char buf1[RED_INET_ADDRSTRLEN];
int valid_cred = ss_is_valid_cred(config->login, config->password);
if (!valid_cred
|| (ss->method = enc_init(&ss->info, config->password, config->login), ss->method == -1))
{
log_error(LOG_ERR, "Invalided encrytion method or password.");
return -1;
}
else
{
log_error(LOG_INFO, "%s @ %s: encryption method: %s",
instance->relay_ss->name,
red_inet_ntop(&instance->config.bindaddr, buf1, sizeof(buf1)),
config->login);
}
return 0;
}
static void ss_instance_fini(struct redsocks_instance_t *instance)
{
}
relay_subsys shadowsocks_subsys =
{
.name = "shadowsocks",
.payload_len = sizeof(ss_client),
.instance_payload_len = sizeof(ss_instance),
.init = ss_client_init,
.fini = ss_client_fini,
.connect_relay = ss_connect_relay,
.instance_init = ss_instance_init,
.instance_fini = ss_instance_fini,
};
/* vim:set tabstop=4 softtabstop=4 shiftwidth=4: */
/* vim:set foldmethod=marker foldlevel=32 foldmarker={,}: */