luci: fix auto setting issue

This commit is contained in:
Nick Peng
2022-08-31 22:05:26 +08:00
parent e6d533e2c5
commit 7c4ce074a5
9 changed files with 71 additions and 56 deletions

View File

@@ -269,6 +269,11 @@ msgstr "SmartDNS官方网站"
msgid "Smartdns local server port" msgid "Smartdns local server port"
msgstr "SmartDNS本地服务端口" msgstr "SmartDNS本地服务端口"
msgid ""
"Smartdns local server port, smartdns will be automatically set as main dns "
"when the port is 53."
msgstr "SmartDNS本地服务端口当端口号设置为53时smartdns将会自动配置为主dns。"
msgid "Smartdns server name" msgid "Smartdns server name"
msgstr "SmartDNS的服务器名称默认为smartdns留空为主机名" msgstr "SmartDNS的服务器名称默认为smartdns留空为主机名"

View File

@@ -45,7 +45,8 @@ o.datatype = "hostname"
o.rempty = false o.rempty = false
---- Port ---- Port
o = s:taboption("settings", Value, "port", translate("Local Port"), translate("Smartdns local server port")) o = s:taboption("settings", Value, "port", translate("Local Port"),
translate("Smartdns local server port, smartdns will be automatically set as main dns when the port is 53."))
o.placeholder = 53 o.placeholder = 53
o.default = 53 o.default = 53
o.datatype = "port" o.datatype = "port"

View File

@@ -269,6 +269,11 @@ msgstr "SmartDNS官方网站"
msgid "Smartdns local server port" msgid "Smartdns local server port"
msgstr "SmartDNS本地服务端口" msgstr "SmartDNS本地服务端口"
msgid ""
"Smartdns local server port, smartdns will be automatically set as main dns "
"when the port is 53."
msgstr "SmartDNS本地服务端口当端口号设置为53时smartdns将会自动配置为主dns。"
msgid "Smartdns server name" msgid "Smartdns server name"
msgstr "SmartDNS的服务器名称默认为smartdns留空为主机名" msgstr "SmartDNS的服务器名称默认为smartdns留空为主机名"

View File

@@ -122,7 +122,8 @@
o.rempty = false; o.rempty = false;
// Port; // Port;
o = s.taboption("settings", form.Value, "port", _("Local Port"), _("Smartdns local server port")); o = s.taboption("settings", form.Value, "port", _("Local Port"),
_("Smartdns local server port, smartdns will be automatically set as main dns when the port is 53."));
o.placeholder = 53; o.placeholder = 53;
o.default = 53; o.default = 53;
o.datatype = "port"; o.datatype = "port";

View File

@@ -31,22 +31,21 @@ CUSTOM_CONF="$SMARTDNS_CONF_DIR/custom.conf"
SMARTDNS_CONF_TMP="${SMARTDNS_CONF}.tmp" SMARTDNS_CONF_TMP="${SMARTDNS_CONF}.tmp"
COREDUMP="0" COREDUMP="0"
RESPAWN="1" RESPAWN="1"
UPSTREAM_SERVER_NUM=0
set_forward_dnsmasq() set_forward_dnsmasq()
{ {
local PORT="$1" local PORT="$1"
addr="127.0.0.1#$PORT" addr="127.0.0.1#$PORT"
OLD_SERVER="$(uci get dhcp.@dnsmasq[0].server 2>/dev/null)" OLD_SERVER="$(uci -q get dhcp.@dnsmasq[0].server)"
if echo "$OLD_SERVER" | grep "^$addr" >/dev/null 2>&1; then if echo "$OLD_SERVER" | grep "^$addr" >/dev/null 2>&1; then
return return
fi fi
uci delete dhcp.@dnsmasq[0].server 2>/dev/null uci -q delete dhcp.@dnsmasq[0].server
uci add_list dhcp.@dnsmasq[0].server="$addr" uci -q add_list dhcp.@dnsmasq[0].server="$addr"
uci set dhcp.@dnsmasq[0].noresolv=1 uci -q set dhcp.@dnsmasq[0].noresolv=1
uci set dhcp.@dnsmasq[0].rebind_protection=0 uci -q set dhcp.@dnsmasq[0].rebind_protection=0
uci set dhcp.@dnsmasq[0].domainneeded=0 uci -q set dhcp.@dnsmasq[0].domainneeded=0
uci commit dhcp uci commit dhcp
/etc/init.d/dnsmasq restart /etc/init.d/dnsmasq restart
} }
@@ -55,15 +54,15 @@ stop_forward_dnsmasq()
{ {
local OLD_PORT="$1" local OLD_PORT="$1"
addr="127.0.0.1#$OLD_PORT" addr="127.0.0.1#$OLD_PORT"
OLD_SERVER="$(uci get dhcp.@dnsmasq[0].server 2>/dev/null)" OLD_SERVER="$(uci -q get dhcp.@dnsmasq[0].server)"
if ! echo "$OLD_SERVER" | grep "^$addr" >/dev/null 2>&1; then if ! echo "$OLD_SERVER" | grep "^$addr" >/dev/null 2>&1; then
return return
fi fi
uci delete dhcp.@dnsmasq[0].server 2>/dev/null uci -q delete dhcp.@dnsmasq[0].server
uci delete dhcp.@dnsmasq[0].noresolv 2>/dev/null uci -q delete dhcp.@dnsmasq[0].noresolv
uci set dhcp.@dnsmasq[0].rebind_protection=1 uci -q set dhcp.@dnsmasq[0].rebind_protection=1
uci set dhcp.@dnsmasq[0].domainneeded=1 uci -q set dhcp.@dnsmasq[0].domainneeded=1
uci commit dhcp uci commit dhcp
/etc/init.d/dnsmasq restart /etc/init.d/dnsmasq restart
} }
@@ -85,17 +84,26 @@ stop_set_dnsmasq()
[ "$port" == "$old_port" ] || stop_forward_dnsmasq "$old_port" [ "$port" == "$old_port" ] || stop_forward_dnsmasq "$old_port"
} }
set_main_dns() set_main_dns()
{ {
uci set dhcp.@dnsmasq[0].port=0 local hostip
hostip="$(uci -q get network.lan.ipaddr)"
if [ -z "$hostip" ]; then
return
fi
uci -q set dhcp.@dnsmasq[0].port=0
uci -q set dhcp.lan.dhcp_option="6,$hostip"
uci commit dhcp uci commit dhcp
/etc/init.d/dnsmasq restart /etc/init.d/dnsmasq restart
} }
stop_main_dns() stop_main_dns()
{ {
uci delete dhcp.@dnsmasq[0].port 2>/dev/null uci -q delete dhcp.@dnsmasq[0].port
uci -q delete dhcp.lan.dhcp_option
uci commit dhcp uci commit dhcp
/etc/init.d/dnsmasq restart /etc/init.d/dnsmasq restart
} }
@@ -119,7 +127,6 @@ clear_iptable()
ip6tables -t nat -D PREROUTING -p udp -d "$IP" --dport 53 -j REDIRECT --to-ports "$OLD_PORT" >/dev/null 2>&1 ip6tables -t nat -D PREROUTING -p udp -d "$IP" --dport 53 -j REDIRECT --to-ports "$OLD_PORT" >/dev/null 2>&1
ip6tables -t nat -D PREROUTING -p tcp -d "$IP" --dport 53 -j REDIRECT --to-ports "$OLD_PORT" >/dev/null 2>&1 ip6tables -t nat -D PREROUTING -p tcp -d "$IP" --dport 53 -j REDIRECT --to-ports "$OLD_PORT" >/dev/null 2>&1
done done
} }
service_triggers() { service_triggers() {
@@ -208,21 +215,6 @@ load_server()
[ "$type" = "https" ] && DNS_ADDRESS="$ip" [ "$type" = "https" ] && DNS_ADDRESS="$ip"
conf_append "$SERVER" "$DNS_ADDRESS $ADDITIONAL_ARGS $addition_arg" conf_append "$SERVER" "$DNS_ADDRESS $ADDITIONAL_ARGS $addition_arg"
((UPSTREAM_SERVER_NUM=UPSTREAM_SERVER_NUM+1))
}
load_resolv_conf()
{
resolve_file="$(uci get dhcp.@dnsmasq[0].resolvfile 2>/dev/null)"
[ -z "$dhcp.@dnsmasq[0].resolvfile" ] && return 1
[ -e "$resolve_file" ] || return 1
while read line
do
IP="$(echo "$line" | grep "nameserver " | awk '{print $2}')"
[ -z "$IP" ] && continue
conf_append "server" "$IP"
done < $resolve_file
} }
load_second_server() load_second_server()
@@ -279,8 +271,13 @@ load_service()
{ {
local section="$1" local section="$1"
args="" args=""
dnsmasq_lease_file="$(uci get dhcp.@dnsmasq[0].leasefile 2>/dev/null)" dnsmasq_lease_file="$(uci -q get dhcp.@dnsmasq[0].leasefile)"
dnsmasq_port="$(uci get dhcp.@dnsmasq[0].port 2>/dev/null)" dnsmasq_port="$(uci -q get dhcp.@dnsmasq[0].port)"
resolve_file="$(uci -q get dhcp.@dnsmasq[0].resolvfile)"
[ -z "$dnsmasq_lease_file" ] && dnsmasq_lease_file="/tmp/dhcp.leases"
[ -z "$dnsmasq_port" ] && dnsmasq_port="53"
[ -z "$resolve_file" ] && resolve_file="/tmp/resolv.conf.d/resolv.conf.auto"
qtype_soa_list="" qtype_soa_list=""
mkdir -p $SMARTDNS_VAR_CONF_DIR mkdir -p $SMARTDNS_VAR_CONF_DIR
@@ -319,7 +316,7 @@ load_service()
config_get force_https_soa "$section" "force_https_soa" "0" config_get force_https_soa "$section" "force_https_soa" "0"
[ "$force_https_soa" = "1" ] && qtype_soa_list="$qtype_soa_list 65" [ "$force_https_soa" = "1" ] && qtype_soa_list="$qtype_soa_list 65"
config_get auto_set_dnsmasq "$section" "auto_set_dnsmasq" "" config_get auto_set_dnsmasq "$section" "auto_set_dnsmasq" "0"
config_get rr_ttl "$section" "rr_ttl" "" config_get rr_ttl "$section" "rr_ttl" ""
[ -z "$rr_ttl" ] || conf_append "rr-ttl" "$rr_ttl" [ -z "$rr_ttl" ] || conf_append "rr-ttl" "$rr_ttl"
@@ -349,6 +346,7 @@ load_service()
config_get old_port "$section" "old_port" "0" config_get old_port "$section" "old_port" "0"
[ -z "$qtype_soa_list" ] || conf_append "force-qtype-SOA" "$qtype_soa_list" [ -z "$qtype_soa_list" ] || conf_append "force-qtype-SOA" "$qtype_soa_list"
[ -e "$resolve_file" ] && conf_append "resolv-file" "$resolve_file"
# upgrade old configuration # upgrade old configuration
if [ "$redirect" = "redirect" ] || [ "$redirect" = "dnsmasq-upstream" ] || [ "$redirect" = "none" ]; then if [ "$redirect" = "redirect" ] || [ "$redirect" = "dnsmasq-upstream" ] || [ "$redirect" = "none" ]; then
@@ -357,16 +355,15 @@ load_service()
stop_forward_dnsmasq "$port" stop_forward_dnsmasq "$port"
stop_forward_dnsmasq "$old_port" stop_forward_dnsmasq "$old_port"
[ "$redirect" = "none" ] || { [ "$redirect" = "none" ] || {
uci delete smartdns.@smartdns[0].port="53" 2>/dev/null uci -q delete smartdns.@smartdns[0].port="53"
port="53" port="53"
} }
uci delete smartdns.@smartdns[0].redirect 2>/dev/null uci -q delete smartdns.@smartdns[0].redirect
uci delete smartdns.@smartdns[0].old_redirect 2>/dev/null uci -q delete smartdns.@smartdns[0].old_redirect
uci delete smartdns.@smartdns[0].old_enabled 2>/dev/null uci -q delete smartdns.@smartdns[0].old_enabled
fi fi
SMARTDNS_PORT="$port" SMARTDNS_PORT="$port"
[ -z "${dnsmasq_port}" ] && dnsmasq_port="53"
[ "$old_port" == "53" ] && [ "$dnsmasq_port" = "0" ] && { [ "$old_port" == "53" ] && [ "$dnsmasq_port" = "0" ] && {
[ "$SMARTDNS_PORT" = "53" ] || stop_main_dns [ "$SMARTDNS_PORT" = "53" ] || stop_main_dns
[ "$enabled" = "0" ] && stop_main_dns [ "$enabled" = "0" ] && stop_main_dns
@@ -376,8 +373,8 @@ load_service()
[ "$SMARTDNS_PORT" = "53" ] || stop_set_dnsmasq "$SMARTDNS_PORT" "$old_port" [ "$SMARTDNS_PORT" = "53" ] || stop_set_dnsmasq "$SMARTDNS_PORT" "$old_port"
} }
uci delete smartdns.@smartdns[0].old_port 2>/dev/null uci -q delete smartdns.@smartdns[0].old_port
uci set smartdns.@smartdns[0].old_port="$SMARTDNS_PORT" 2>/dev/null uci -q set smartdns.@smartdns[0].old_port="$SMARTDNS_PORT"
uci commit smartdns uci commit smartdns
[ "$enabled" -gt 0 ] || return 1 [ "$enabled" -gt 0 ] || return 1
@@ -407,8 +404,6 @@ load_service()
config_foreach load_server "server" config_foreach load_server "server"
[ "$UPSTREAM_SERVER_NUM" -gt "0" ] || load_resolv_conf
{ {
echo "conf-file $ADDRESS_CONF" echo "conf-file $ADDRESS_CONF"
echo "conf-file $BLACKLIST_IP_CONF" echo "conf-file $BLACKLIST_IP_CONF"
@@ -435,9 +430,10 @@ load_service()
unload_service() unload_service()
{ {
local section="$1" local section="$1"
dnsmasq_port="$(uci get dhcp.@dnsmasq[0].port 2>/dev/null)" dnsmasq_port="$(uci -q get dhcp.@dnsmasq[0].port)"
config_get port "$section" "port" "53" config_get port "$section" "port" "53"
config_get old_port "$section" "old_port" "0" config_get old_port "$section" "old_port" "0"
config_get auto_set_dnsmasq "$section" "auto_set_dnsmasq" "0"
[ -z "${dnsmasq_port}" ] && dnsmasq_port="53" [ -z "${dnsmasq_port}" ] && dnsmasq_port="53"
[ "$old_port" = "53" ] && [ "$dnsmasq_port" = "0" ] && stop_main_dns [ "$old_port" = "53" ] && [ "$dnsmasq_port" = "0" ] && stop_main_dns
[ "$auto_set_dnsmasq" = "1" ] && { [ "$auto_set_dnsmasq" = "1" ] && {
@@ -445,6 +441,12 @@ unload_service()
} }
} }
service_stopped()
{
config_load "smartdns"
config_foreach unload_service "smartdns"
}
start_service() start_service()
{ {
config_load "smartdns" config_load "smartdns"
@@ -456,9 +458,3 @@ reload_service()
stop stop
start start
} }
stop_service()
{
config_load "smartdns"
config_foreach unload_service "smartdns"
}

View File

@@ -32,6 +32,7 @@
#define DEFAULT_DNS_CACHE_SIZE 512 #define DEFAULT_DNS_CACHE_SIZE 512
#define DNS_MAX_REPLY_IP_NUM 8 #define DNS_MAX_REPLY_IP_NUM 8
#define DNS_RESOLV_FILE "/etc/resolv.conf"
/* ipset */ /* ipset */
struct dns_ipset_table { struct dns_ipset_table {
@@ -133,6 +134,7 @@ char dns_conf_user[DNS_CONF_USRNAME_LEN];
int dns_save_fail_packet; int dns_save_fail_packet;
char dns_save_fail_packet_dir[DNS_MAX_PATH]; char dns_save_fail_packet_dir[DNS_MAX_PATH];
char dns_resolv_file[DNS_MAX_PATH];
/* ECS */ /* ECS */
struct dns_edns_client_subnet dns_conf_ipv4_ecs; struct dns_edns_client_subnet dns_conf_ipv4_ecs;
@@ -1926,6 +1928,7 @@ static struct config_item _config_item[] = {
CONF_STRING("ca-path", (char *)&dns_conf_ca_path, DNS_MAX_PATH), CONF_STRING("ca-path", (char *)&dns_conf_ca_path, DNS_MAX_PATH),
CONF_STRING("user", (char *)&dns_conf_user, sizeof(dns_conf_user)), CONF_STRING("user", (char *)&dns_conf_user, sizeof(dns_conf_user)),
CONF_YESNO("debug-save-fail-packet", &dns_save_fail_packet), CONF_YESNO("debug-save-fail-packet", &dns_save_fail_packet),
CONF_STRING("resolv-file", (char *)&dns_resolv_file, sizeof(dns_resolv_file)),
CONF_STRING("debug-save-fail-packet-dir", (char *)&dns_save_fail_packet_dir, sizeof(dns_save_fail_packet_dir)), CONF_STRING("debug-save-fail-packet-dir", (char *)&dns_save_fail_packet_dir, sizeof(dns_save_fail_packet_dir)),
CONF_CUSTOM("conf-file", config_addtional_file, NULL), CONF_CUSTOM("conf-file", config_addtional_file, NULL),
CONF_END(), CONF_END(),
@@ -2106,6 +2109,10 @@ static int _dns_conf_load_post(void)
dns_conf_local_ttl = dns_conf_rr_ttl_min; dns_conf_local_ttl = dns_conf_rr_ttl_min;
} }
if (dns_resolv_file[0] == '\0') {
safe_strncpy(dns_resolv_file, DNS_RESOLV_FILE, sizeof(dns_resolv_file));
}
return 0; return 0;
} }

View File

@@ -325,6 +325,7 @@ extern char dns_conf_sni_proxy_ip[DNS_MAX_IPLEN];
extern int dns_save_fail_packet; extern int dns_save_fail_packet;
extern char dns_save_fail_packet_dir[DNS_MAX_PATH]; extern char dns_save_fail_packet_dir[DNS_MAX_PATH];
extern char dns_resolv_file[DNS_MAX_PATH];
void dns_server_load_exit(void); void dns_server_load_exit(void);

View File

@@ -793,7 +793,7 @@ static int _dns_setup_dns_raw_packet(struct dns_server_post_context *context)
/* encode to binary data */ /* encode to binary data */
int encode_len = dns_encode(context->inpacket, context->inpacket_maxlen, context->packet); int encode_len = dns_encode(context->inpacket, context->inpacket_maxlen, context->packet);
if (encode_len <= 0) { if (encode_len <= 0) {
tlog(TLOG_ERROR, "encode raw packet failed for %s", context->request->domain); tlog(TLOG_DEBUG, "encode raw packet failed for %s", context->request->domain);
return -1; return -1;
} }

View File

@@ -44,7 +44,6 @@
#include <sys/types.h> #include <sys/types.h>
#include <ucontext.h> #include <ucontext.h>
#define RESOLVE_FILE "/etc/resolv.conf"
#define MAX_LINE_LEN 1024 #define MAX_LINE_LEN 1024
#define MAX_KEY_LEN 64 #define MAX_KEY_LEN 64
#define SMARTDNS_PID_FILE "/var/run/smartdns.pid" #define SMARTDNS_PID_FILE "/var/run/smartdns.pid"
@@ -173,9 +172,9 @@ static int _smartdns_load_from_resolv(void)
int filed_num = 0; int filed_num = 0;
int line_num = 0; int line_num = 0;
fp = fopen(RESOLVE_FILE, "r"); fp = fopen(dns_resolv_file, "r");
if (fp == NULL) { if (fp == NULL) {
tlog(TLOG_ERROR, "open %s failed, %s", RESOLVE_FILE, strerror(errno)); tlog(TLOG_ERROR, "open %s failed, %s", dns_resolv_file, strerror(errno));
return -1; return -1;
} }