-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathcontrol.c
More file actions
293 lines (250 loc) · 8.38 KB
/
control.c
File metadata and controls
293 lines (250 loc) · 8.38 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
/* vim: set et ts=4 sts=4 sw=4 : */
/********************************************************************\
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License*
* along with this program; if not, contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 59 Temple Place - Suite 330 Fax: +1-617-542-2652 *
* Boston, MA 02111-1307, USA gnu@gnu.org *
* *
\********************************************************************/
/** @file control.c
@brief xfrp control protocol implemented
@author Copyright (C) 2016 Dengfeng Liu <liudengfeng@kunteng.org>
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <json-c/json.h>
#include <syslog.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <event2/listener.h>
#include <event2/util.h>
#include <event2/event.h>
#include <event2/event_struct.h>
#include <openssl/md5.h>
#include "debug.h"
#include "client.h"
#include "uthash.h"
#include "config.h"
#include "const.h"
#include "msg.h"
#include "control.h"
#include "uthash.h"
static char *calc_md5(const char *data, int datalen)
{
unsigned char digest[16] = {0};
char *out = (char*)malloc(33);
MD5_CTX md5;
MD5_Init(&md5);
MD5_Update(&md5, data, datalen);
MD5_Final(digest, &md5);
for (int n = 0; n < 16; ++n) {
snprintf(&(out[n*2]), 3, "%02x", (unsigned int)digest[n]);
}
return out;
}
static char *get_auth_key(const char *name, const char *token)
{
char seed[128] = {0};
snprintf(seed, 128, "%s%s%ld", name, token, time(NULL));
return calc_md5(seed, strlen(seed));
}
static struct control_request *
get_control_request(enum msg_type type, const struct proxy_client *client)
{
if (!client)
return NULL;
struct control_request *req = calloc(sizeof(struct control_request), 1);
long ntime = time(NULL);
req->type = type;
req->proxy_name = strdup(client->name);
#define STRDUP(v) v?strdup(v):NULL
switch(type) {
case NewCtlConn:
req->use_encryption = client->bconf->use_encryption;
req->use_gzip = client->bconf->use_gzip;
req->pool_count = client->bconf->pool_count;
req->privilege_mode = client->bconf->privilege_mode;
req->proxy_type = STRDUP(client->bconf->type);
req->host_header_rewrite = STRDUP(client->bconf->host_header_rewrite);
req->http_username = STRDUP(client->bconf->http_username);
req->http_password = STRDUP(client->bconf->http_password);
req->subdomain = STRDUP(client->bconf->subdomain);
if (req->privilege_mode) {
req->remote_port = client->remote_port;
req->custom_domains = STRDUP(client->custom_domains);
req->locations = STRDUP(client->locations);
}
break;
case NewWorkConn:
break;
case NoticeUserConn:
break;
case NewCtlConnRes:
break;
case HeartbeatReq:
break;
case HeartbeatRes:
break;
case NewWorkConnUdp:
break;
}
req->privilege_mode = client->bconf->privilege_mode;
req->timestamp = ntime;
if (req->privilege_mode) {
req->privilege_key = get_auth_key(client->name, client->bconf->privilege_token);
} else {
req->auth_key = get_auth_key(client->name, client->bconf->auth_token);
}
return req;
}
static void
control_request_free(struct control_request *req)
{
if (!req)
return;
if (req->proxy_name) free(req->proxy_name);
if (req->auth_key) free(req->auth_key);
if (req->privilege_key) free(req->privilege_key);
if (req->proxy_type) free(req->proxy_type);
if (req->custom_domains) free(req->custom_domains);
if (req->locations) free(req->locations);
if (req->host_header_rewrite) free(req->host_header_rewrite);
if (req->http_username) free(req->http_username);
if (req->http_password) free(req->http_password);
if (req->subdomain) free(req->subdomain);
free(req);
}
void send_msg_frp_server(enum msg_type type, const struct proxy_client *client, struct bufferevent *bev)
{
char *msg = NULL;
struct control_request *req = get_control_request(type, client); // get control request by client
int len = control_request_marshal(req, &msg); // marshal control request to json string
assert(msg);
struct bufferevent *bout = NULL;
if (bev) {
bout = bev;
} else {
bout = client->ctl_bev;
}
bufferevent_write(bout, msg, len);
bufferevent_write(bout, "\n", 1);
debug(LOG_DEBUG, "Send msg to frp server [%s]", msg);
free(msg);
control_request_free(req); // free control request
}
// connect to server
struct bufferevent *connect_server(struct event_base *base, const char *name, const int port)
{
struct bufferevent *bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
assert(bev);
if (bufferevent_socket_connect_hostname(bev, NULL, AF_INET, name, port)<0) {
bufferevent_free(bev);
return NULL;
}
return bev;
}
static void set_heartbeat_interval(struct event *timeout)
{
struct timeval tv;
struct common_conf *c_conf = get_common_config();
evutil_timerclear(&tv);
tv.tv_sec = c_conf->heartbeat_interval;
event_add(timeout, &tv);
}
static void hb_sender_cb(evutil_socket_t fd, short event, void *arg)
{
struct proxy_client *client = arg;
send_msg_frp_server(HeartbeatReq, client, NULL);
set_heartbeat_interval(client->ev_timeout);
}
static void heartbeat_sender(struct proxy_client *client)
{
client->ev_timeout = evtimer_new(client->base, hb_sender_cb, client);
set_heartbeat_interval(client->ev_timeout);
}
static void process_frp_msg(char *res, struct proxy_client *client)
{
struct control_response *c_res = control_response_unmarshal(res);
if (c_res == NULL)
return;
switch(c_res->type) {
case HeartbeatRes:
break;
case NoticeUserConn:
// when user connect
start_frp_tunnel(client);
break;
default:
break;
}
control_response_free(c_res);
}
static void login_xfrp_read_msg_cb(struct bufferevent *bev, void *ctx)
{
struct evbuffer *input = bufferevent_get_input(bev);
int len = evbuffer_get_length(input);
if (len <= 0)
return;
char *buf = calloc(1, len+1);
if (evbuffer_remove(input, buf, len) > 0) {
process_frp_msg(buf, ctx);
}
free(buf);
}
static void login_xfrp_event_cb(struct bufferevent *bev, short what, void *ctx)
{
struct proxy_client *client = ctx;
struct common_conf *c_conf = get_common_config();
if (what & (BEV_EVENT_EOF|BEV_EVENT_ERROR)) {
if (client->ctl_bev != bev) {
debug(LOG_ERR, "Error: should be equal");
bufferevent_free(client->ctl_bev);
client->ctl_bev = NULL;
}
debug(LOG_ERR, "Proxy [%s]: connect server [%s:%d] error", client->name, c_conf->server_addr, c_conf->server_port);
bufferevent_free(bev);
free_proxy_client(client);
} else if (what & BEV_EVENT_CONNECTED) {
debug(LOG_INFO, "Proxy [%s] connected: send msg to frp server", client->name);
bufferevent_setcb(bev, login_xfrp_read_msg_cb, NULL, login_xfrp_event_cb, client);
bufferevent_enable(bev, EV_READ|EV_WRITE);
send_msg_frp_server(NewCtlConn, client, NULL);
}
}
static void login_frp_server(struct proxy_client *client)
{
struct common_conf *c_conf = get_common_config();
struct bufferevent *bev = connect_server(client->base, c_conf->server_addr, c_conf->server_port);
if (!bev) {
debug(LOG_DEBUG, "Connect server [%s:%d] failed", c_conf->server_addr, c_conf->server_port);
return;
}
debug(LOG_INFO, "Proxy [%s]: connect server [%s:%d] ......", client->name, c_conf->server_addr, c_conf->server_port);
client->ctl_bev = bev;
bufferevent_enable(bev, EV_WRITE);
bufferevent_setcb(bev, NULL, NULL, login_xfrp_event_cb, client);
}
void control_process(struct proxy_client *client)
{
login_frp_server(client);
heartbeat_sender(client);
}