9 Commits

Author SHA1 Message Date
KerwinKoo
571de7556d add assert after strdup 2017-07-04 17:08:33 +08:00
KerwinKoo
2ffa39e7cb reset heartbeat interval to 30s 2017-07-04 16:37:11 +08:00
KerwinKoo
1aba513a05 update config.c 2017-07-04 16:32:54 +08:00
KerwinKoo
5d0d1f105e update code 2017-07-04 16:29:21 +08:00
KerwinKoo
cb95f86b08 update code 2017-07-04 16:27:42 +08:00
KerwinKoo
7028aa3baa fix bug 9+16*x bits bug 2017-07-04 16:18:12 +08:00
KerwinKoo
94314af51a add declearation warnming in cmake 2017-07-04 10:56:24 +08:00
KerwinKoo
b587a2d30f add tcp_mux in send_msg_frp_server func 2017-07-04 10:53:39 +08:00
KerwinKoo
f2e482aa2e add ftp support check 2017-07-04 10:50:27 +08:00
8 changed files with 106 additions and 122 deletions

View File

@@ -34,7 +34,7 @@ set(test_libs
event
)
ADD_DEFINITIONS(-Wall -g --std=gnu99 -Wmissing-declarations)
ADD_DEFINITIONS(-Wall -g --std=gnu99)
add_executable(xfrp_client ${src_xfrp_client})
target_link_libraries(xfrp_client ${libs})

View File

@@ -30,6 +30,7 @@
#include <string.h>
#include <signal.h>
#include <syslog.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
@@ -146,8 +147,7 @@ parse_commandline(int argc, char **argv)
case 'c':
if (optarg) {
confile = strdup(optarg); //never free it
if (! confile)
exit(0);
assert(confile);
flag = 1;
}

View File

@@ -102,7 +102,8 @@ static const char *get_valid_type(const char *val)
if (MATCH_VALUE("tcp") ||
MATCH_VALUE("http") ||
MATCH_VALUE("https") ||
MATCH_VALUE("udp")) { // will add ftp support in here
MATCH_VALUE("udp") ||
MATCH_VALUE("ftp") ) {
return val;
}
@@ -133,6 +134,7 @@ static void dump_proxy_service(const int index, struct proxy_service *ps)
if (NULL == ps->proxy_type) {
ps->proxy_type = strdup("tcp");
assert(ps->proxy_type);
}
debug(LOG_DEBUG,
@@ -157,6 +159,7 @@ static struct proxy_service *new_proxy_service(const char *name)
{
if (! name)
return NULL;
struct proxy_service *ps = calloc(sizeof(struct proxy_service), 1);
assert(ps);
assert(c_conf);
@@ -187,22 +190,21 @@ proxy_service_handler(void *user, const char *sect, const char *nm, const char *
struct proxy_service *ps = NULL;
char *section = NULL;
section = strdup(sect);
assert(section);
if (strlen(sect) == 25) {//fix proxy name length = 25 bug, not find the reason
section = calloc(1, 26);
memset(section, '_', 26);
memcpy(section, sect, 25);
} else {
section = strdup(sect);
}
if (strcmp(section, "common") == 0)
if (strcmp(section, "common") == 0) {
SAFE_FREE(section);
return 0;
}
HASH_FIND_STR(p_services, section, ps);
if (!ps) {
ps = new_proxy_service(section);
assert(ps);
if (! ps) {
debug(LOG_ERR, "cannot create proxy service, it should not happenned!");
exit(0);
}
HASH_ADD_KEYPTR(hh, p_services, ps->proxy_name, strlen(ps->proxy_name), ps);
}
@@ -213,11 +215,14 @@ proxy_service_handler(void *user, const char *sect, const char *nm, const char *
if (MATCH_NAME("type")) {
if (! get_valid_type(value)) {
debug(LOG_ERR, "proxy service type %s is not supportted", value);
SAFE_FREE(section);
exit(0);
}
ps->proxy_type = strdup(value);
assert(ps->proxy_type);
} else if (MATCH_NAME("local_ip")) {
ps->local_ip = strdup(value);
assert(ps->local_ip);
} else if (MATCH_NAME("local_port")) {
ps->local_port = atoi(value);
} else if (MATCH_NAME("use_encryption")) {
@@ -226,23 +231,29 @@ proxy_service_handler(void *user, const char *sect, const char *nm, const char *
ps->remote_port = atoi(value);
} else if (MATCH_NAME("http_user")) {
ps->http_user = strdup(value);
assert(ps->http_user);
} else if (MATCH_NAME("http_pwd")) {
ps->http_pwd = strdup(value);
assert(ps->http_pwd);
} else if (MATCH_NAME("subdomain")) {
ps->subdomain= strdup(value);
ps->subdomain = strdup(value);
assert(ps->http_pwd);
} else if (MATCH_NAME("custom_domains")) {
ps->custom_domains= strdup(value);
ps->custom_domains = strdup(value);
assert(ps->custom_domains);
} else if (MATCH_NAME("locations")) {
ps->locations= strdup(value);
ps->locations = strdup(value);
assert(ps->locations);
} else if (MATCH_NAME("host_header_rewrite")) {
ps->host_header_rewrite= strdup(value);
ps->host_header_rewrite = strdup(value);
assert(ps->host_header_rewrite);
} else if (MATCH_NAME("use_encryption")) {
ps->use_encryption = TO_BOOL(value);
} else if (MATCH_NAME("use_compression")) {
ps->use_compression = TO_BOOL(value);
}
free(section);
SAFE_FREE(section);
return 1;
}
@@ -254,25 +265,31 @@ static int common_handler(void *user, const char *section, const char *name, con
if (MATCH("common", "server_addr")) {
SAFE_FREE(config->server_addr);
config->server_addr = strdup(value);
assert(config->server_addr);
} else if (MATCH("common", "server_port")) {
config->server_port = atoi(value);
} else if (MATCH("common", "http_proxy")) {
SAFE_FREE(config->http_proxy);
config->http_proxy = strdup(value);
assert(config->http_proxy);
} else if (MATCH("common", "log_file")) {
SAFE_FREE(config->log_file);
config->log_file = strdup(value);
assert(config->log_file);
} else if (MATCH("common", "log_way")) {
SAFE_FREE(config->log_way);
config->log_way = strdup(value);
assert(config->log_way);
} else if (MATCH("common", "log_level")) {
SAFE_FREE(config->log_level);
config->log_level = strdup(value);
assert(config->log_level);
} else if (MATCH("common", "log_max_days")) {
config->log_max_days = atoi(value);
} else if (MATCH("common", "privilege_token")) {
SAFE_FREE(config->privilege_token);
config->privilege_token = strdup(value);
assert(config->privilege_token);
} else if (MATCH("common", "heartbeat_interval")) {
config->heartbeat_interval = atoi(value);
} else if (MATCH("common", "heartbeat_timeout")) {
@@ -280,9 +297,11 @@ static int common_handler(void *user, const char *section, const char *name, con
} else if (MATCH("common", "auth_token")) {
SAFE_FREE(config->auth_token);
config->auth_token = strdup(value);
assert(config->auth_token);
} else if (MATCH("common", "user")) {
SAFE_FREE(config->user);
config->user = strdup(value);
assert(config->user);
} else if (MATCH("common", "tcp_mux")) {
config->tcp_mux = 0; // set tcp_mux to default: false
}
@@ -295,13 +314,17 @@ static void init_common_conf(struct common_conf *config)
return;
config->server_addr = strdup("0.0.0.0");
assert(config->server_addr);
config->server_port = 7000;
config->log_file = strdup("console");
assert(config->log_file);
config->log_way = strdup("console");
assert(config->log_way);
config->log_level = strdup("info");
assert(config->log_level);
config->log_max_days = 3;
config->heartbeat_interval = 10;
config->heartbeat_timeout = 30;
config->heartbeat_interval = 30;
config->heartbeat_timeout = 60;
config->tcp_mux = 0;
config->user = NULL;
}

100
control.c
View File

@@ -35,7 +35,6 @@
#include <json-c/json.h>
#include <syslog.h>
#include <pthread.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
@@ -200,50 +199,15 @@ static size_t request(struct bufferevent *bev, struct frame *f)
goto REQ_END;
}
// #define DEV_DEBUG 1
#ifdef DEV_DEBUG
/* debug showing */
debug(LOG_DEBUG, "send request byte:");
unsigned int i = 0;
if (f->len) {
printf("[");
for(i = 0; i<f->len; i++) {
printf("%u ", f->data[i]);
}
printf("]\n");
}
#endif // DEV_DEBUG
struct common_conf *c = get_common_config();
if ( ! c)
goto REQ_END;
int headersize = get_header_size();
size_t len = (1<<16) + headersize;
memset(request_buf, 0, len);
if (c->tcp_mux) {
request_buf[VERI] = f->ver;
request_buf[CMDI] = f->cmd;
*((ushort *)(request_buf + 2)) = f->len;
*((uint32_t *)(request_buf + 4)) = f->sid;
// insert data to request buffer
if (f->data != NULL && f->len > 0) { //TODO: ENCODE when control
memcpy(request_buf + DATAI, f->data, f->len);
}
write_len = (size_t) (headersize + f->len);
} else {
memcpy(request_buf, f->data, f->len);
write_len = (size_t)f->len;
}
write_len = (size_t)f->len;
if ( 0 == write_len)
goto REQ_END;;
bufferevent_write(bout, request_buf, write_len);
memset(request_buf, 0, len);
bufferevent_write(bout, f->data, write_len);
REQ_END:
return write_len;
@@ -680,8 +644,6 @@ static unsigned char
if (buf_len > split_lv) {
if (! is_logged()) {
if (buf[0] == 49) {
debug(LOG_DEBUG, "mulity raw login-response...");
msg_size_t data_len_bigend;
data_len_bigend = *(msg_size_t *)(buf + MSG_LEN_I);
msg_size_t data_len = msg_ntoh(data_len_bigend);
@@ -769,7 +731,6 @@ static void recv_cb(struct bufferevent *bev, void *ctx)
if (read_n) {
unsigned char *raw_buf_p = buf;
for( ; raw_buf_p && read_n ; ) {
// #define CONN_DEBUG 1
#ifdef CONN_DEBUG
unsigned int i = 0;
@@ -829,7 +790,7 @@ static void connect_event_cb (struct bufferevent *bev, short what, void *ctx)
start_base_connect();
close_main_control();
} else if (what & BEV_EVENT_CONNECTED) {
// recv frpc login-response message before recv othfer fprs messages,
// recv login-response message before recving othfer fprs messages,
bufferevent_setcb(bev, recv_cb, NULL, connect_event_cb, NULL);
bufferevent_enable(bev, EV_READ|EV_WRITE|EV_PERSIST);
@@ -936,31 +897,22 @@ void send_msg_frp_server(struct bufferevent *bev,
req_msg.type = type;
req_msg.data_len = msg_len;
char frame_type = 0;
struct frame *f = NULL;
f = new_frame(frame_type, sid); // frame_type not truely matter, it will reset by set_frame_cmd
assert(f);
if (msg) {
req_msg.data_p = strdup(msg);
assert(req_msg.data_p);
}
unsigned char *puck_buf = NULL;
size_t pack_buf_len = pack(&req_msg, &puck_buf);
if ( ! pack_buf_len || ! puck_buf) {
debug(LOG_ERR, "login buffer pack failed!");
return;
debug(LOG_ERR, "send buffer pack failed!");
goto S_M_F_END;
}
// #define SEND_MSG_DEBUG 1
#ifdef SEND_MSG_DEBUG
debug(LOG_DEBUG, "puck result:");
size_t j = 0;
for(j=0; j<pack_buf_len; j++) {
printf("%d ", (unsigned char)puck_buf[j]);
}
printf("\n\n");
#endif // SEND_MSG_DEBUG
char frame_type = 0;
struct frame *f = NULL;
f = new_frame(frame_type, sid); // frame_type not truely matter, it will reset by set_frame_cmd
assert(f);
#ifdef ENCRYPTO
debug(LOG_DEBUG, "start encode message ...");
@@ -987,25 +939,29 @@ void send_msg_frp_server(struct bufferevent *bev,
f->data = puck_buf;
}
switch (type)
{
case TypeLogin:
case TypePong:
case TypePing:
case TypeNewProxy:
if (get_common_config()->tcp_mux) {
switch (type)
{
case TypeLogin:
case TypePong:
case TypePing:
case TypeNewProxy:
frame_type = cmdPSH;
break;
default:
break;
}
} else {
frame_type = cmdPSH;
break;
default:
break;
}
set_frame_cmd(f, frame_type);
request(bout, f);
S_M_F_END:
SAFE_FREE(req_msg.data_p);
SAFE_FREE(puck_buf);
f->data = NULL;
SAFE_FREE(f->data);
free_frame(f);
}

View File

@@ -31,8 +31,10 @@ struct frp_coder *new_coder(const char *privilege_token, const char *salt)
assert(enc);
enc->privilege_token = privilege_token ? strdup(privilege_token):"\0";
assert(enc->privilege_token);
enc->key_len = block_size;
enc->salt = strdup(salt);
assert(enc->salt);
enc->key = encrypt_key(enc->privilege_token, strlen(enc->privilege_token), enc->salt);
enc->iv = calloc(block_size, 1);
encrypt_iv(enc->iv, block_size);

View File

@@ -20,6 +20,7 @@ struct frame *new_frame(char cmd, uint32_t sid) {
f->cmd = cmd;
f->sid = sid;
f->len = 0;
f->data = NULL;
}
return f;
@@ -34,7 +35,7 @@ struct frame *raw_frame(unsigned char *buf, const size_t buf_len)
}
char ver = buf[VERI];
char cmd = buf[CMDI];
uint32_t sid = *(uint32_t *)(buf + SIDI);
uint32_t sid = htonl(*(uint32_t *)(buf + SIDI));
struct frame *f = new_frame(cmd, sid);
f->ver = ver;

15
login.c
View File

@@ -37,6 +37,7 @@ void init_login()
{
if (! c_login)
c_login = calloc(sizeof(struct login), 1);
assert(c_login);
struct common_conf *c_conf = get_common_config();
@@ -48,9 +49,12 @@ void init_login()
}
c_login->version = strdup(PROTOCOL_VERESION);
assert(c_login->version);
c_login->hostname = NULL;
c_login->os = strdup(uname_buf.sysname);
c_login->os = strdup(uname_buf.sysname);
assert(c_login->os);
c_login->arch = strdup(uname_buf.machine);
assert(c_login->arch);
c_login->user = NULL;
c_login->timestamp = 0;
@@ -64,11 +68,6 @@ void init_login()
int login_resp_check(struct login_resp *lr)
{
debug(LOG_DEBUG, "xfrp login response: run_id: [%s], version: [%s], error: [%s]",
lr->run_id,
lr->version,
lr->error);
if (lr->run_id == NULL || strlen(lr->run_id) <= 1) {
if (lr->error && strlen(lr->error) > 0) {
debug(LOG_ERR, "login response error: %s", lr->error);
@@ -77,9 +76,13 @@ int login_resp_check(struct login_resp *lr)
c_login->logged = 0;
} else {
c_login->logged = 1;
debug(LOG_DEBUG, "xfrp login response: run_id: [%s], version: [%s]",
lr->run_id,
lr->version);
SAFE_FREE(c_login->run_id);
c_login->run_id = strdup(lr->run_id);
assert(c_login->run_id);
}
return c_login->logged;

41
msg.c
View File

@@ -75,6 +75,7 @@ static void fill_custom_domains(struct json_object *j_ctl_req, const char *custo
struct json_object *jarray_cdomains = json_object_new_array();
assert(jarray_cdomains);
char *tmp = strdup(custom_domains);
assert(tmp);
char *tok = tmp, *end = tmp;
while (tok != NULL) {
strsep(&end, ",");
@@ -133,6 +134,7 @@ size_t login_request_marshal(char **msg)
struct common_conf *cf = get_common_config();
char *auth_key = get_auth_key(cf->privilege_token, &lg->timestamp);
lg->privilege_key = strdup(auth_key);
assert(lg->privilege_key);
JSON_MARSHAL_TYPE(j_login_req, "version", string, lg->version);
JSON_MARSHAL_TYPE(j_login_req, "hostname", string, SAFE_JSON_STRING(lg->hostname));
@@ -150,6 +152,7 @@ size_t login_request_marshal(char **msg)
if (tmp && strlen(tmp) > 0) {
nret = strlen(tmp);
*msg = strdup(tmp);
assert(*msg);
}
json_object_put(j_login_req);
SAFE_FREE(auth_key);
@@ -193,6 +196,7 @@ int new_proxy_service_marshal(const struct proxy_service *np_req, char **msg)
if (tmp && strlen(tmp) > 0) {
nret = strlen(tmp);
*msg = strdup(tmp);
assert(*msg);
}
json_object_put(j_np_req);
@@ -212,6 +216,7 @@ int new_work_conn_marshal(const struct work_conn *work_c, char **msg)
if (tmp && strlen(tmp) > 0) {
nret = strlen(tmp);
*msg = strdup(tmp);
assert(*msg);
}
json_object_put(j_new_work_conn);
@@ -227,25 +232,25 @@ struct login_resp *login_resp_unmarshal(const char *jres)
return NULL;
struct login_resp *lr = calloc(1, sizeof(struct login_resp));
if (lr == NULL) {
goto END_ERROR;
}
assert(lr);
struct json_object *l_version = NULL;
if (! json_object_object_get_ex(j_lg_res, "version", &l_version))
goto END_ERROR;
lr->version = strdup(json_object_get_string(l_version));
assert(lr->version);
struct json_object *l_run_id = NULL;
if (! json_object_object_get_ex(j_lg_res, "run_id", &l_run_id))
goto END_ERROR;
lr->run_id = strdup(json_object_get_string(l_run_id));
assert(lr->run_id);
struct json_object *l_error = NULL;
if(! json_object_object_get_ex(j_lg_res, "error", &l_error))
goto END_ERROR;
lr->error = strdup(json_object_get_string(l_error));
assert(lr->error);
END_ERROR:
json_object_put(j_lg_res);
@@ -259,13 +264,14 @@ struct start_work_conn_resp *start_work_conn_resp_unmarshal(const char *resp_msg
return NULL;
struct start_work_conn_resp *sr = calloc(1, sizeof(struct start_work_conn_resp));
if (! sr)
goto START_W_C_R_END;
assert(sr);
struct json_object *pn = NULL;
if(! json_object_object_get_ex(j_start_w_res, "proxy_name", &pn))
goto START_W_C_R_END;
sr->proxy_name = strdup(json_object_get_string(pn));
assert(sr->proxy_name);
START_W_C_R_END:
json_object_put(j_start_w_res);
@@ -278,9 +284,7 @@ struct control_response *control_response_unmarshal(const char *jres)
if (is_error(j_ctl_res))
return NULL;
struct control_response *ctl_res = calloc(sizeof(struct control_response), 1);
if (ctl_res == NULL) {
goto END_ERROR;
}
assert(ctl_res);
struct json_object *jtype = NULL;
if(! json_object_object_get_ex(j_ctl_res, "type", &jtype))
@@ -293,9 +297,11 @@ struct control_response *control_response_unmarshal(const char *jres)
ctl_res->code = json_object_get_int(jcode);
struct json_object *jmsg = NULL;
if(json_object_object_get_ex(j_ctl_res, "msg", &jmsg))
if(json_object_object_get_ex(j_ctl_res, "msg", &jmsg)) {
ctl_res->msg = strdup(json_object_get_string(jmsg));
assert(ctl_res->msg);
}
END_ERROR:
json_object_put(j_ctl_res);
return ctl_res;
@@ -357,20 +363,13 @@ size_t pack(struct message *req_msg, unsigned char **ret_buf)
else
data_len_bigend = req_msg->data_len;
size_t buf_len = TYPE_LEN + sizeof(data_len_bigend) + req_msg->data_len;
*ret_buf = calloc(buf_len, 1);
if (*ret_buf == NULL) {
return 0;
}
*ret_buf = calloc(1, buf_len);
assert(*ret_buf);
*(*ret_buf + MSG_TYPE_I) = req_msg->type;
*(msg_size_t *)(*ret_buf + MSG_LEN_I) = data_len_bigend;
snprintf((char *)*ret_buf + TYPE_LEN + sizeof(data_len_bigend),
req_msg->data_len + 1,
"%s",
req_msg->data_p);
memcpy((char *)*ret_buf+TYPE_LEN+sizeof(data_len_bigend), req_msg->data_p, req_msg->data_len);
return buf_len;
}