13 Commits

Author SHA1 Message Date
KerwinKoo
bf4a66f554 using json_object_object_get_ex instead of json_object_object_get func 2017-07-03 17:53:21 +08:00
KerwinKoo
d2b85c64b6 rewrite debug info 2017-07-03 16:44:56 +08:00
KerwinKoo
2fc391d8a0 safe free 2017-07-03 16:35:23 +08:00
KerwinKoo
253432601e using SAFE_FREE instead of normal free 2017-07-03 16:27:04 +08:00
KerwinKoo
2f3eb22ad9 add service type check 2017-07-03 16:21:03 +08:00
KerwinKoo
0452348207 remove debug infor 2017-07-03 16:12:01 +08:00
KerwinKoo
cf4f937c25 gukq 20170703 xfrpc update
fix 25 bits length error bug

Signed-off-by: KerwinKoo <gukaiqiang@gmail.com>
2017-07-03 16:06:28 +08:00
KerwinKoo
9c2d183c84 dbg 2017-06-29 17:56:51 +08:00
KerwinKoo
b28fbd316f dbng 2017-06-29 17:53:40 +08:00
KerwinKoo
2e5637d916 dbg 2017-06-29 17:46:32 +08:00
KerwinKoo
2cb82c3c61 dbg 2017-06-29 17:34:24 +08:00
KerwinKoo
06582cc998 dbg 2017-06-29 17:24:21 +08:00
KerwinKoo
c8641376aa DBG 2017-06-29 17:19:35 +08:00
6 changed files with 79 additions and 94 deletions

View File

@@ -36,7 +36,6 @@ struct event_base;
struct base_conf;
struct bufferevent;
struct event;
struct new_proxy;
struct proxy_service;
struct proxy_client {
@@ -63,7 +62,6 @@ struct proxy_client {
//provate arguments
UT_hash_handle hh;
struct new_proxy *n_proxy;
int connected;
int work_started;
struct proxy_service *ps;
@@ -71,43 +69,6 @@ struct proxy_client {
size_t data_tail_size;
};
// When frpc login success, send this message to frps for running a new proxy.
// type NewProxy struct {
// ProxyName string `json:"proxy_name"`
// ProxyType string `json:"proxy_type"`
// UseEncryption bool `json:"use_encryption"`
// UseCompression bool `json:"use_compression"`
// // tcp and udp only
// RemotePort int64 `json:"remote_port"`
// // http and https only
// CustomDomains []string `json:"custom_domains"`
// SubDomain string `json:"subdomain"`
// Locations []string `json:"locations"`
// HostHeaderRewrite string `json:"host_header_rewrite"`
// HttpUser string `json:"http_user"`
// HttpPwd string `json:"http_pwd"`
// }
struct new_proxy {
char *proxy_name;
char *proxy_type;
int use_encryption;
int use_compression;
// tcp and udp only
int64_t remote_port;
// http and https only
char *custom_domains;
char *subdomain;
char *locations;
char *host_header_rewrite;
char *http_user;
char *http_pwd;
};
struct proxy_service {
char *proxy_name;
char *proxy_type;
@@ -126,7 +87,6 @@ struct proxy_service {
char *http_user;
char *http_pwd;
//provate arguments
UT_hash_handle hh;
};

View File

@@ -145,7 +145,10 @@ parse_commandline(int argc, char **argv)
case 'c':
if (optarg) {
confile = strdup(optarg);
confile = strdup(optarg); //never free it
if (! confile)
exit(0);
flag = 1;
}
break;

View File

@@ -93,14 +93,18 @@ static int is_true(const char *val)
return 0;
}
static char *get_valid_type(const char *val)
static const char *get_valid_type(const char *val)
{
if (!val)
return NULL;
#define MATCH_VALUE(s) strcmp(val, s) == 0
if (MATCH_VALUE("tcp") || MATCH_VALUE("http") || MATCH_VALUE("https") || MATCH_VALUE("udp")) {
return strdup(val);
if (MATCH_VALUE("tcp") ||
MATCH_VALUE("http") ||
MATCH_VALUE("https") ||
MATCH_VALUE("udp")) { // will add ftp support in here
return val;
}
return NULL;
@@ -151,11 +155,15 @@ static void dump_all_ps()
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);
ps->proxy_name = strdup(name);
assert(ps->proxy_name);
ps->proxy_type = NULL;
ps->use_encryption = 0;
ps->local_port = -1;
@@ -173,17 +181,29 @@ static struct proxy_service *new_proxy_service(const char *name)
return ps;
}
static int proxy_service_handler(void *user, const char *section, const char *nm, const char *value)
static int
proxy_service_handler(void *user, const char *sect, const char *nm, const char *value)
{
struct proxy_service *ps = NULL;
char *section = NULL;
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)
return 0;
HASH_FIND_STR(p_services, section, ps);
if (!ps)
{
if (!ps) {
ps = new_proxy_service(section);
assert(ps);
HASH_ADD_KEYPTR(hh, p_services, ps->proxy_name, strlen(ps->proxy_name), ps);
}
@@ -191,7 +211,11 @@ static int proxy_service_handler(void *user, const char *section, const char *nm
#define TO_BOOL(v) strcmp(value, "true") ? 0:1
if (MATCH_NAME("type")) {
ps->proxy_type = get_valid_type(value);
if (! get_valid_type(value)) {
debug(LOG_ERR, "proxy service type %s is not supportted", value);
exit(0);
}
ps->proxy_type = strdup(value);
} else if (MATCH_NAME("local_ip")) {
ps->local_ip = strdup(value);
} else if (MATCH_NAME("local_port")) {
@@ -218,6 +242,7 @@ static int proxy_service_handler(void *user, const char *section, const char *nm
ps->use_compression = TO_BOOL(value);
}
free(section);
return 1;
}
@@ -227,32 +252,36 @@ static int common_handler(void *user, const char *section, const char *name, con
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
if (MATCH("common", "server_addr")) {
if (config->server_addr) free(config->server_addr);
SAFE_FREE(config->server_addr);
config->server_addr = strdup(value);
} 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);
} else if (MATCH("common", "log_file")) {
if (config->log_file) free(config->log_file);
SAFE_FREE(config->log_file);
config->log_file = strdup(value);
} else if (MATCH("common", "log_way")) {
if (config->log_way) free(config->log_way);
SAFE_FREE(config->log_way);
config->log_way = strdup(value);
} else if (MATCH("common", "log_level")) {
if (config->log_level) free(config->log_level);
SAFE_FREE(config->log_level);
config->log_level = strdup(value);
} 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);
} else if (MATCH("common", "heartbeat_interval")) {
config->heartbeat_interval = atoi(value);
} else if (MATCH("common", "heartbeat_timeout")) {
config->heartbeat_timeout = atoi(value);
} else if (MATCH("common", "auth_token")) {
SAFE_FREE(config->auth_token);
config->auth_token = strdup(value);
} else if (MATCH("common", "user")) {
SAFE_FREE(config->user);
config->user = strdup(value);
} else if (MATCH("common", "tcp_mux")) {
config->tcp_mux = 0; // set tcp_mux to default: false

View File

@@ -115,14 +115,14 @@ static void client_start_event_cb(struct bufferevent *bev, short what, void *ctx
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);
debug(LOG_ERR, "Proxy connect server [%s:%d] error", c_conf->server_addr, c_conf->server_port);
bufferevent_free(bev);
free_proxy_client(client);
} else if (what & BEV_EVENT_CONNECTED) {
bufferevent_setcb(bev, recv_cb, NULL, client_start_event_cb, client);
bufferevent_enable(bev, EV_READ|EV_WRITE);
sync_new_work_connection(bev);
debug(LOG_INFO, "new proxy connected");
debug(LOG_INFO, "proxy service connected");
}
}
@@ -139,7 +139,7 @@ static void new_client_connect()
return;
}
debug(LOG_INFO, "Proxy [%s]: connect server [%s:%d] ......", client->name, c_conf->server_addr, c_conf->server_port);
debug(LOG_INFO, "work connection: connect server [%s:%d] ......", c_conf->server_addr, c_conf->server_port);
client->ctl_bev = bev;
bufferevent_enable(bev, EV_WRITE);
@@ -157,7 +157,7 @@ static void start_proxy_services()
HASH_ITER(hh, all_ps, ps, tmp) {
if(ps == NULL) {
debug(LOG_ERR, "pc is null!");
debug(LOG_ERR, "proxy service is invalid!");
return;
}
send_new_proxy(ps);
@@ -375,7 +375,6 @@ static void sync_new_work_connection(struct bufferevent *bev)
debug(LOG_ERR, "new work connection request run_id marshal failed!");
return;
}
debug(LOG_DEBUG, "marshal new work connection:%s", new_work_conn_request_message);
send_msg_frp_server(bev, TypeNewWorkConn, new_work_conn_request_message, nret, f->sid);
request(bout, f);
@@ -463,7 +462,6 @@ raw_message(struct message *msg, struct bufferevent *bev, struct proxy_client *c
case TypeReqWorkConn:
if (! is_client_connected()) {
debug(LOG_DEBUG, "recv the client work connect start request ...");
start_proxy_services();
client_connected(1);
ping(bev);
@@ -635,7 +633,7 @@ static size_t data_handler(unsigned char *buf, ushort len, struct proxy_client *
debug(LOG_ERR, "message received format invalid");
goto DATA_H_END;
}
debug(LOG_DEBUG, "recv <---- %s" ,msg->data_p);
debug(LOG_DEBUG, "recv <---- %c: %s", msg->type, msg->data_p);
if (msg->data_p == NULL)
goto DATA_H_END;
@@ -687,7 +685,6 @@ static unsigned char
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);
debug(LOG_DEBUG, "raw data len = %u", data_len);
split_len = raw_static_size + data_len;
splited = 1;
@@ -713,7 +710,6 @@ static unsigned char
char msg_type = buf[0];
int type_valid = msg_type_valid_check(msg_type);
if (type_valid) {
debug(LOG_DEBUG, "buffer raw type [%c]", msg_type);
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);
@@ -933,7 +929,7 @@ void send_msg_frp_server(struct bufferevent *bev,
if ( ! bout) {
return;
}
debug(LOG_DEBUG, "send message to frps ... [type: %c %s]", type, msg);
debug(LOG_DEBUG, "send ----> [%c: %s]", type, msg);
struct message req_msg;
req_msg.data_p = NULL;
@@ -1035,17 +1031,21 @@ void start_login_frp_server(struct event_base *base)
void send_new_proxy(struct proxy_service *ps)
{
if (! ps) {
debug(LOG_ERR, "proxy service is invalid!");
return;
}
debug(LOG_DEBUG, "control proxy client: [%s]", ps->proxy_name);
char *new_proxy_msg = NULL;
int len = new_proxy_service_marshal(ps, &new_proxy_msg);
if ( ! new_proxy_msg) {
debug(LOG_ERR, "proxy service request marshal failed");
assert(new_proxy_msg);
return;
}
send_msg_frp_server(NULL, TypeNewProxy, new_proxy_msg, len, main_ctl->session_id);
free(new_proxy_msg);
SAFE_FREE(new_proxy_msg);
}
void init_main_control()

View File

@@ -30,7 +30,7 @@ struct frp_coder *new_coder(const char *privilege_token, const char *salt)
struct frp_coder *enc = calloc(sizeof(struct frp_coder), 1);
assert(enc);
enc->privilege_token = privilege_token != NULL ? strdup(privilege_token):"\0";
enc->privilege_token = privilege_token ? strdup(privilege_token):"\0";
enc->key_len = block_size;
enc->salt = strdup(salt);
enc->key = encrypt_key(enc->privilege_token, strlen(enc->privilege_token), enc->salt);

49
msg.c
View File

@@ -81,7 +81,7 @@ static void fill_custom_domains(struct json_object *j_ctl_req, const char *custo
json_object_array_add(jarray_cdomains, json_object_new_string(tok));
tok = end;
}
free(tmp);
SAFE_FREE(tmp);
json_object_object_add(j_ctl_req, "custom_domains", jarray_cdomains);
}
@@ -152,7 +152,7 @@ size_t login_request_marshal(char **msg)
*msg = strdup(tmp);
}
json_object_put(j_login_req);
free(auth_key);
SAFE_FREE(auth_key);
return nret;
}
@@ -168,7 +168,7 @@ int new_proxy_service_marshal(const struct proxy_service *np_req, char **msg)
JSON_MARSHAL_TYPE(j_np_req, "proxy_type", string, np_req->proxy_type);
JSON_MARSHAL_TYPE(j_np_req, "use_encryption", boolean, np_req->use_encryption);
JSON_MARSHAL_TYPE(j_np_req, "use_compression", boolean, np_req->use_compression);
JSON_MARSHAL_TYPE(j_np_req, "remote_port", int64, np_req->remote_port);
JSON_MARSHAL_TYPE(j_np_req, "remote_port", int, np_req->remote_port);
if (np_req->custom_domains) {
fill_custom_domains(j_np_req, np_req->custom_domains);
@@ -231,22 +231,20 @@ struct login_resp *login_resp_unmarshal(const char *jres)
goto END_ERROR;
}
struct json_object *l_version = json_object_object_get(j_lg_res, "version");
if (is_error(l_version)) {
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));
struct json_object *l_run_id = json_object_object_get(j_lg_res, "run_id");
if (is_error(l_run_id)) {
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));
struct json_object *l_error = json_object_object_get(j_lg_res, "error");
if (is_error(l_error)) {
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));
END_ERROR:
@@ -264,10 +262,9 @@ struct start_work_conn_resp *start_work_conn_resp_unmarshal(const char *resp_msg
if (! sr)
goto START_W_C_R_END;
struct json_object *pn = json_object_object_get(j_start_w_res, "proxy_name");
if (is_error(pn))
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));
START_W_C_R_END:
@@ -285,19 +282,18 @@ struct control_response *control_response_unmarshal(const char *jres)
goto END_ERROR;
}
struct json_object *jtype = json_object_object_get(j_ctl_res, "type");
if (jtype == NULL) {
struct json_object *jtype = NULL;
if(! json_object_object_get_ex(j_ctl_res, "type", &jtype))
goto END_ERROR;
}
ctl_res->type = json_object_get_int(jtype);
struct json_object *jcode = json_object_object_get(j_ctl_res, "code");
if (jcode == NULL)
struct json_object *jcode = NULL;
if(! json_object_object_get_ex(j_ctl_res, "code", &jcode))
goto END_ERROR;
ctl_res->code = json_object_get_int(jcode);
struct json_object *jmsg = json_object_object_get(j_ctl_res, "msg");
if (jmsg)
struct json_object *jmsg = NULL;
if(json_object_object_get_ex(j_ctl_res, "msg", &jmsg))
ctl_res->msg = strdup(json_object_get_string(jmsg));
END_ERROR:
@@ -310,9 +306,8 @@ void control_response_free(struct control_response *res)
if (!res)
return;
if (res->msg) free(res->msg);
free(res);
SAFE_FREE(res->msg);
SAFE_FREE(res);
}
int msg_type_valid_check(char msg_type)
@@ -337,8 +332,6 @@ struct message *unpack(unsigned char *recv_msg, const ushort len)
return NULL;
}
debug(LOG_DEBUG, "unpacked message type: %c", msg->type);
msg_size_t data_len_bigend;
data_len_bigend = *(msg_size_t *)(recv_msg + MSG_LEN_I);
msg->data_len = msg_ntoh(data_len_bigend);