11 Commits

Author SHA1 Message Date
KerwinKoo
f8d065b09e add domain check for server_addr field 2017-07-25 17:54:38 +08:00
KerwinKoo
2f2e64ae31 modify domain check code 2017-07-25 17:44:31 +08:00
KerwinKoo
a4812dd596 trans custom_domains to lower 2017-07-25 17:30:42 +08:00
KerwinKoo
2e420e921c fix dns error bug 2017-07-25 16:25:46 +08:00
KerwinKoo
99972f257a delete DNS of server addr 2017-07-25 13:57:07 +08:00
KerwinKoo
8e4ca75d36 Merge pull request #8 from KunTengRom/libdns
debug
2017-07-25 13:42:21 +08:00
KerwinKoo
360019d1a4 debug 2017-07-25 13:41:13 +08:00
KerwinKoo
204c086a03 fix remote_port is -1 will not be set
Signed-off-by: KerwinKoo <gukaiqiang@gmail.com>
2017-07-25 11:25:26 +08:00
KerwinKoo
316834ffd0 don't show port 2017-07-21 16:14:43 +08:00
KerwinKoo
aefbd2e062 add -r to show run id and port 2017-07-21 14:41:45 +08:00
KerwinKoo
ff6f60f079 add Openwrt Router check 2017-07-19 18:24:47 +08:00
7 changed files with 91 additions and 10 deletions

View File

@@ -39,6 +39,7 @@
#include "commandline.h"
#include "debug.h"
#include "version.h"
#include "utils.h"
typedef void signal_func (int);
@@ -122,6 +123,7 @@ usage(const char *appname)
fprintf(stdout, " -d <level> Debug level\n");
fprintf(stdout, " -h Print usage\n");
fprintf(stdout, " -v Print version information\n");
fprintf(stdout, " -r Print run id of client\n");
fprintf(stdout, "\n");
}
@@ -134,7 +136,7 @@ parse_commandline(int argc, char **argv)
int c;
int flag = 0;
while (-1 != (c = getopt(argc, argv, "c:hfd:sw:vx:i:a:"))) {
while (-1 != (c = getopt(argc, argv, "c:hfd:sw:vrx:i:a:"))) {
switch (c) {
@@ -168,7 +170,25 @@ parse_commandline(int argc, char **argv)
fprintf(stdout, "version: " VERSION "\n");
exit(1);
break;
case 'r':
{
char ifname[16] = {0};
if(get_net_ifname(ifname, 16)){
debug(LOG_ERR, "error: get device sign ifname failed!");
exit(0);
}
char if_mac[64] = {0};
if(get_net_mac(ifname, if_mac, sizeof(if_mac))) {
debug(LOG_ERR, "error: Hard ware MAC address of [%s] get failed!", ifname);
exit(0);
}
fprintf(stdout, "run ID:%s\n", if_mac);
exit(1);
break;
}
default:
usage(argv[0]);
exit(1);

View File

@@ -311,8 +311,13 @@ 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")) {
SAFE_FREE(config->server_addr);
config->server_addr = strdup(value);
int addr_len = strlen(value) + 1;
config->server_addr = (char *)calloc(1, addr_len);
assert(config->server_addr);
if(dns_unified(value, config->server_addr, addr_len)) {
debug(LOG_ERR, "error: server_addr [%s] is invalid!", value);
exit(0);
}
} else if (MATCH("common", "server_port")) {
config->server_port = atoi(value);
} else if (MATCH("common", "http_proxy")) {
@@ -375,6 +380,7 @@ static void init_common_conf(struct common_conf *config)
config->tcp_mux = 0;
config->user = NULL;
config->server_ip = NULL;
config->is_router = 0;
}
// it should be free after using
@@ -425,3 +431,8 @@ void load_config(const char *confile)
dump_all_ps();
}
int is_running_in_router()
{
return c_conf->is_router;
}

View File

@@ -45,20 +45,23 @@ struct base_conf{
// common config
struct common_conf {
char *server_addr; /* default 0.0.0.0 */
char *server_addr; /* default 0.0.0.0 */
char *server_ip;
int server_port; /* default 7000 */
int server_port; /* default 7000 */
char *http_proxy;
char *log_file; /* default consol */
char *log_way; /* default console */
char *log_level; /* default info */
char *log_file; /* default consol */
char *log_way; /* default console */
char *log_level; /* default info */
int log_max_days; /* default 3 */
char *privilege_token;
char *auth_token;
int heartbeat_interval; /* default 10 */
int heartbeat_timeout; /* default 30 */
int tcp_mux; /* default 0 */
int tcp_mux; /* default 0 */
char *user;
/* private fields */
int is_router; // to sign router (Openwrt/LEDE) or not
};
struct common_conf *get_common_config();
@@ -73,5 +76,6 @@ struct proxy_client *get_all_pc();
void load_config(const char *confile);
char *get_ftp_data_proxy_name(const char *ftp_proxy_name);
void set_common_server_ip(const char *ip);
int is_running_in_router();
#endif //_CONFIG_H_

View File

@@ -73,6 +73,11 @@ void init_login()
exit(0);
}
if (strcmp(ifname, "br-lan") == 0) {
c_conf->is_router = 1;
debug(LOG_DEBUG, "working in router");
}
char if_mac[64] = {0};
if(get_net_mac(ifname, if_mac, sizeof(if_mac))) {
debug(LOG_ERR, "error: Hard ware MAC address of [%s] get failed!", ifname);

17
msg.c
View File

@@ -42,6 +42,7 @@
#include "common.h"
#include "login.h"
#include "client.h"
#include "utils.h"
#define JSON_MARSHAL_TYPE(jobj,key,jtype,item) \
json_object_object_add(jobj, key, json_object_new_##jtype((item)));
@@ -80,7 +81,14 @@ static void fill_custom_domains(struct json_object *j_ctl_req, const char *custo
char *tok = tmp, *end = tmp;
while (tok != NULL) {
strsep(&end, ",");
json_object_array_add(jarray_cdomains, json_object_new_string(tok));
int dname_len = strlen(tok) + 1;
char *dname_buf = (char *)calloc(1, dname_len);
assert(dname_buf);
dns_unified(tok, dname_buf, dname_len);
json_object_array_add(jarray_cdomains, json_object_new_string(dname_buf));
free(dname_buf);
tok = end;
}
SAFE_FREE(tmp);
@@ -172,7 +180,6 @@ 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", int, np_req->remote_port);
if (is_ftp_proxy(np_req)) {
JSON_MARSHAL_TYPE(j_np_req, "remote_data_port", int, np_req->remote_data_port);
@@ -180,8 +187,14 @@ int new_proxy_service_marshal(const struct proxy_service *np_req, char **msg)
if (np_req->custom_domains) {
fill_custom_domains(j_np_req, np_req->custom_domains);
json_object_object_add(j_np_req, "remote_port", NULL);
} else {
json_object_object_add(j_np_req, "custom_domains", NULL);
if (np_req->remote_port != -1) {
JSON_MARSHAL_TYPE(j_np_req, "remote_port", int, np_req->remote_port);
} else {
json_object_object_add(j_np_req, "remote_port", NULL);
}
}
JSON_MARSHAL_TYPE(j_np_req, "subdomain", string, SAFE_JSON_STRING(np_req->subdomain));

27
utils.c
View File

@@ -7,6 +7,7 @@
#include <sys/time.h>
#include <sys/stat.h>
#include <errno.h>
#include <ctype.h>
#include <net/if.h>
#include <sys/ioctl.h>
@@ -180,4 +181,30 @@ int get_net_ifname(char *if_buf, int blen)
freeifaddrs(ifaddr);
return ret;
}
// e.g. wWw.Baidu.com/China will be trans into www.baidu.com/China
// return: 0:check and trant succeed, 1:failed or domain name is invalid
int dns_unified(const char *dname, char *udname_buf, int udname_buf_len)
{
if (! dname || ! udname_buf || udname_buf_len < strlen(dname)+1)
return 1;
int has_dot = 0;
int dlen = strlen(dname);
int i = 0;
for(i=0; i<dlen; i++) {
if(dname[i] == '/')
break;
if (dname[i] == '.' && i != dlen-1)
has_dot = 1;
udname_buf[i] = tolower(dname[i]);
}
if (! has_dot) //domain name should have 1 dot leastly
return 1;
return 0;
}

View File

@@ -9,5 +9,6 @@ int is_valid_ip_address(const char *ip_address);
int show_net_ifname();
int get_net_ifname(char *if_buf, int blen);
int get_net_mac(char *net_if_name, char *mac, int mac_len);
int dns_unified(const char *dname, char *udname_buf, int udname_buf_len);
#endif //_UTILS_H_