-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathss_connector.c
More file actions
315 lines (264 loc) · 8.63 KB
/
Copy pathss_connector.c
File metadata and controls
315 lines (264 loc) · 8.63 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
#ifdef ENABLE_SS
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#ifdef _WIN32
# include <malloc.h>
#endif
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include "connector.h"
#undef HAVE_CONFIG_H
#include "encrypt.h"
#include "utils.h"
#ifdef _MSC_VER
#define alloca _alloca
#endif
/*
* ss_conn
*/
typedef struct ss_conn_t {
struct bufferevent *client;
struct enc_ctx e_ctx;
struct enc_ctx d_ctx;
//enum socks5_conn_status status;
char host[256];
uint16_t port;
connect_callback cb;
void *arg;
} ss_conn;
int g_ss_method;
const char *g_ss_server;
int g_ss_port;
char *g_ss_key;
static void free_context(void *ctx)
{
ss_conn *conn = (ss_conn*)ctx;
// For both OpenSSL and mbed TLS, release is free pointer in struct and zero struct.
// And release a zeroed struct is safe.
cipher_context_release(&conn->e_ctx.evp);
cipher_context_release(&conn->d_ctx.evp);
free(conn);
}
static enum bufferevent_filter_result input_filter(struct evbuffer *src, struct evbuffer *dst, ev_ssize_t dst_limit, enum bufferevent_flush_mode mode, void *ctx)
{
int i;
ss_conn *conn = (ss_conn*)ctx;
#define CRYPT(method, crypt_ctx, extra_len) \
do { \
int iovec_len = evbuffer_peek(src, -1, NULL, NULL, 0); \
\
struct evbuffer_iovec *vec_src = alloca(sizeof(struct evbuffer_iovec) * iovec_len); \
struct evbuffer_iovec vec_dst[1]; \
\
evbuffer_peek(src, -1, NULL, vec_src, iovec_len); \
if (1 != evbuffer_reserve_space(dst, evbuffer_get_length(src) + (extra_len), vec_dst, 1)) { \
/* malloc space failed */ \
return BEV_ERROR; \
} \
vec_dst[0].iov_len = 0; \
\
for (i = 0; i < iovec_len; i++) { \
ssize_t r = vec_src[i].iov_len; \
char *buf = ss_ ## method((char*)vec_dst[0].iov_base + vec_dst[0].iov_len, vec_src[i].iov_base, &r, &(crypt_ctx)); \
if (!buf) { \
/* crypt error */ \
LOGE( # method " failed"); \
return BEV_ERROR; \
} \
\
vec_dst[0].iov_len += r; \
} \
\
evbuffer_drain(src, -1); \
if (-1 == evbuffer_commit_space(dst, vec_dst, 1)) { \
LOGE("evbuffer commit space failed"); \
return BEV_ERROR; \
} \
} while(0)
if (conn->d_ctx.aead)
{
struct evbuffer_iovec vec_dst[1];
char *ciphertext = evbuffer_pullup(src, -1);
if (1 != evbuffer_reserve_space(dst, evbuffer_get_length(src) + BLOCK_SIZE, vec_dst, 1)) {
/* malloc space failed */
return BEV_ERROR;
}
vec_dst[0].iov_len = 0;
int consume = evbuffer_get_length(src);
int produce = aead_decrypt(vec_dst[0].iov_base, ciphertext, &consume, &conn->e_ctx);
if (produce < 0) {
LOGE("aead decrypt failed");
return BEV_ERROR;
}
else if (produce == 0 && consume == 0) {
return BEV_NEED_MORE;
}
else {
evbuffer_drain(src, consume);
vec_dst[0].iov_len = produce;
if (-1 == evbuffer_commit_space(dst, vec_dst, 1)) {
LOGE("evbuffer commit space failed");
return BEV_ERROR;
}
}
}
else {
CRYPT(decrypt, conn->d_ctx, BLOCK_SIZE);
}
return BEV_OK;
}
static enum bufferevent_filter_result output_filter(struct evbuffer *src, struct evbuffer *dst, ev_ssize_t dst_limit, enum bufferevent_flush_mode mode, void *ctx)
{
int i;
ss_conn *conn = (ss_conn*)ctx;
/*{
int iovec_len = evbuffer_peek(src, -1, NULL, NULL, 0);
struct evbuffer_iovec vec_src[iovec_len];
struct evbuffer_iovec vec_dst[1];
evbuffer_peek(src, -1, NULL, vec_src, iovec_len);
if (1 != evbuffer_reserve_space(dst, evbuffer_get_length(src) + MAX_IV_LENGTH + BLOCK_SIZE, vec_dst, 1)) {
// malloc space failed
return BEV_ERROR;
}
vec_dst[0].iov_len = 0;
for (i = 0; i < iovec_len; i++)
{
ssize_t r = vec_src[i].iov_len;
char *buf = ss_encrypt((char*)vec_dst[0].iov_base + vec_dst[0].iov_len, vec_src[i].iov_base, &r, &conn->e_ctx);
if (!buf) {
// crypt error
LOGE("ss_encrypt failed");
return BEV_ERROR;
}
vec_dst[0].iov_len += r;
}
evbuffer_drain(src, -1);
if (-1 == evbuffer_commit_space(dst, vec_dst, 1)) {
return BEV_ERROR;
}
}*/
if (conn->e_ctx.aead)
{
int src_len = evbuffer_get_length(src);
do {
int consume = src_len > CHUNK_SIZE_MASK ? CHUNK_SIZE_MASK : src_len;
struct evbuffer_iovec vec_dst[1];
char *plaintext = evbuffer_pullup(src, consume);
if (1 != evbuffer_reserve_space(dst, consume + (CHUNK_SIZE_LEN + 2 * MAX_TAG_LENGTH + BLOCK_SIZE), vec_dst, 1)) {
/* malloc space failed */
return BEV_ERROR;
}
vec_dst[0].iov_len = 0;
//int consume = evbuffer_get_length(src);
int produce = aead_encrypt(vec_dst[0].iov_base, plaintext, &consume, &conn->d_ctx);
if (produce < 0) {
LOGE("aead encrypt failed");
return BEV_ERROR;
}
evbuffer_drain(src, consume);
vec_dst[0].iov_len = produce;
if (-1 == evbuffer_commit_space(dst, vec_dst, 1)) {
LOGE("evbuffer commit space failed");
return BEV_ERROR;
}
//LOGD("src %d consume %d", src_len, consume);
src_len -= consume;
} while(src_len > 0);
}
else
{
CRYPT(encrypt, conn->e_ctx, MAX_IV_LENGTH + BLOCK_SIZE);
}
return BEV_OK;
}
static void ss_eventcb(struct bufferevent *bev, short what, void *ctx)
{
struct bufferevent *bev_filter = NULL;
ss_conn *conn = (ss_conn*)ctx;
if (what & (BEV_EVENT_EOF|BEV_EVENT_ERROR)) {
char buf[4096] = {'\0'};
// during handshake, EOF is an error also
LOGE("bev %p (sock %d) event: 0x%hx %s", bev, bufferevent_getfd(bev), what, socket_error(buf, sizeof(buf)));
// error
conn->cb(NULL, conn->arg);
free_context(conn);
bufferevent_free(bev);
} else if (what & BEV_EVENT_CONNECTED) {
LOGD("upstrem connected with bev %p (sock %d)", bev, bufferevent_getfd(bev));
#if defined TCP_NODELAY
if (g_enable_nodelay == 1) {
int on = 1;
setsockopt(bufferevent_getfd(bev), IPPROTO_TCP, TCP_NODELAY, (void *)&on, sizeof(on));
}
#endif
/* TCP */
bufferevent_setcb(bev, NULL, NULL, NULL, NULL);
/* ss */
enc_ctx_init(g_ss_method, &conn->e_ctx, 1);
enc_ctx_init(g_ss_method, &conn->d_ctx, 0);
/* any changes in output evbuffer will call output_filter, so must be deferred */
bev_filter = bufferevent_filter_new(bev, input_filter, output_filter, BEV_OPT_CLOSE_ON_FREE/*|BEV_OPT_DEFER_CALLBACKS*/, free_context, ctx);
if (bev_filter) {
LOGD("create filter bev %p from bev %p ", bev_filter, bev);
/* same as SOCKS5 Requests
+------+----------+----------+
| ATYP | DST.ADDR | DST.PORT |
+------+----------+----------+
| 1 | Variable | 2 |
+------+----------+----------+
*/
char data[1 + 1 + 255 + 2] = {0x03, }; // DOMAINNAME: X'03
size_t domain_len = strlen(conn->host);
assert(domain_len <= 255);
uint16_t net_port = htons(conn->port);
data[1] = domain_len;
memcpy(data + 2, conn->host, domain_len);
memcpy(data + 2 + domain_len, &net_port, 2);
/* currently BEV_OPT_DEFER_CALLBACKS no effect with bufferevent_filter_new(), so defer it manually */
evbuffer_defer_callbacks(bufferevent_get_output(bev_filter), bufferevent_get_base(bev_filter));
bufferevent_write(bev_filter, data, 1 + 1 + domain_len + 2);
conn->cb(bev_filter, conn->arg);
} else {
LOGE("create filter event failed");
conn->cb(bev_filter, conn->arg);
free_context(conn);
bufferevent_free(bev);
}
}
}
void connect_ss(struct event_base *evbase, struct evdns_base *evdns_base, const char *hostname, int port, connect_callback cb, void *arg)
{
struct bufferevent *bev = NULL;
ss_conn *conn = (ss_conn*)calloc(1, sizeof(ss_conn));
if (NULL == conn) {
goto fail;
}
strcpy(conn->host, hostname);
conn->port = port;
conn->cb = cb;
conn->arg = arg;
assert(g_ss_server && g_ss_port != 0);
hostname = g_ss_server;
port =g_ss_port;
bev = bufferevent_socket_new(evbase, -1, BEV_OPT_CLOSE_ON_FREE/*|BEV_OPT_DEFER_CALLBACKS*/);
if (NULL == bev) {
LOGE("bufferevent create failed");
goto fail;
}
conn->client = bev;
bufferevent_setcb(bev, NULL, NULL, ss_eventcb, conn);
if (bufferevent_socket_connect_hostname(bev, evdns_base, PF_UNSPEC, hostname, port) < 0) {
LOGE("bufferevent connect ss server %s:%d failed", hostname, port);
goto fail;
}
return;
fail:
LOGE("connect host %s:%d failed", hostname, port);
if (bev)
bufferevent_free(bev);
cb(NULL, arg);
if (conn)
free_context(conn);
}
#endif