From 6dbf0fae07b528c4ab6b09d840ae2aebba836700 Mon Sep 17 00:00:00 2001 From: Meitian Huang <_@freeaddr.info> Date: Sat, 10 Jan 2015 17:24:24 +1100 Subject: [PATCH] Refine nameserver settings * Use the nameserver given in the configuration file if any. * Use Google Public DNS only if the user does not nominate a nameserver. --- src/jconf.c | 2 ++ src/jconf.h | 1 + src/server.c | 13 +++++++++++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/jconf.c b/src/jconf.c index e01c87e1..b4e84d81 100644 --- a/src/jconf.c +++ b/src/jconf.c @@ -168,6 +168,8 @@ jconf_t *read_jconf(const char * file) conf.fast_open = value->u.boolean; } else if (strcmp(name, "nofile") == 0) { conf.nofile = value->u.integer; + } else if (strcmp(name, "nameserver") == 0) { + conf.nameserver = to_string(value); } } } else { diff --git a/src/jconf.h b/src/jconf.h index af8af52a..e510b586 100644 --- a/src/jconf.h +++ b/src/jconf.h @@ -43,6 +43,7 @@ typedef struct { char *timeout; int fast_open; int nofile; + char *nameserver; } jconf_t; jconf_t *read_jconf(const char * file); diff --git a/src/server.c b/src/server.c index 448fe8dd..93a28ee6 100644 --- a/src/server.c +++ b/src/server.c @@ -971,8 +971,7 @@ int main(int argc, char **argv) const char *server_port = NULL; char * nameservers[MAX_DNS_NUM + 1]; - int nameserver_num = 1; - nameservers[0] = "8.8.8.8"; + int nameserver_num = 0; int option_index = 0; static struct option long_options[] = @@ -1078,6 +1077,9 @@ int main(int argc, char **argv) set_nofile(nofile); } #endif + if (conf->nameserver != NULL) { + nameservers[nameserver_num++] = conf->nameserver; + } } if (server_num == 0 || server_port == NULL || password == NULL) { @@ -1122,6 +1124,9 @@ int main(int argc, char **argv) struct ev_loop *loop = EV_DEFAULT; // setup udns + if (nameserver_num == 0) { + nameservers[nameserver_num++] = "8.8.8.8"; + } resolv_init(loop, nameservers, nameserver_num); // inilitialize listen context @@ -1144,6 +1149,10 @@ int main(int argc, char **argv) setnonblocking(listenfd); LOGI("server listening at port %s", server_port); + for (int i = 0; i < nameserver_num; i++) { + LOGI("using nameserver: %s", nameservers[i]); + } + struct listen_ctx *listen_ctx = &listen_ctx_list[index]; // Setup proxy context