Fix shellcheck issues

This commit is contained in:
Nick Peng
2019-12-19 22:51:11 +08:00
parent 0228a79991
commit aaeae7167b
3 changed files with 122 additions and 123 deletions

View File

@@ -34,14 +34,14 @@ test -x $SMARTDNS || exit 5
case $1 in case $1 in
start) start)
$SMARTDNS $SMART_DNS_OPTS $SMARTDNS "$SMART_DNS_OPTS"
while true; do while true; do
if [ -e "$PIDFILE" ]; then if [ -e "$PIDFILE" ]; then
break; break;
fi fi
sleep .5 sleep .5
done done
PID="`cat $PIDFILE 2>/dev/null`" PID="$(cat $PIDFILE 2>/dev/null)"
if [ -z "$PID" ]; then if [ -z "$PID" ]; then
echo "start smartdns server failed." echo "start smartdns server failed."
exit 1 exit 1
@@ -57,25 +57,25 @@ case $1 in
echo "smartdns server is stopped." echo "smartdns server is stopped."
exit 0 exit 0
fi fi
PID="`cat $PIDFILE 2>/dev/null`" PID="$(cat $PIDFILE 2>/dev/null)"
if [ ! -e "/proc/$PID" ] || [ -z "$PID" ]; then if [ ! -e "/proc/$PID" ] || [ -z "$PID" ]; then
echo "smartdns server is stopped" echo "smartdns server is stopped"
exit 0 exit 0
fi fi
kill -TERM $PID kill -TERM "$PID"
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "Stop smartdns server failed." echo "Stop smartdns server failed."
exit 1; exit 1;
fi fi
rm -f $PIDFILE rm -f "$PIDFILE"
echo "Stop smartdns server success." echo "Stop smartdns server success."
;; ;;
restart) restart)
$0 stop && sleep 1 && $0 start "$0" stop && sleep 1 && "$0" start
;; ;;
status) status)
PID="`cat $PIDFILE 2>/dev/null`" PID="$(cat "$PIDFILE" 2>/dev/null)"
if [ ! -e "/proc/$PID" ] || [ -z "$PID" ]; then if [ ! -e "/proc/$PID" ] || [ -z "$PID" ]; then
echo "smartdns server is not running." echo "smartdns server is not running."
exit 1 exit 1

View File

@@ -22,7 +22,6 @@ SERVICE_USE_PID=1
SERVICE_WRITE_PID=1 SERVICE_WRITE_PID=1
SERVICE_DAEMONIZE=1 SERVICE_DAEMONIZE=1
SERVICE_PID_FILE="/var/run/smartdns.pid" SERVICE_PID_FILE="/var/run/smartdns.pid"
BASECONFIGFILE="/etc/smartdns/smartdns.conf"
SMARTDNS_CONF_DIR="/etc/smartdns" SMARTDNS_CONF_DIR="/etc/smartdns"
SMARTDNS_VAR_CONF_DIR="/var/etc/smartdns" SMARTDNS_VAR_CONF_DIR="/var/etc/smartdns"
SMARTDNS_CONF="$SMARTDNS_VAR_CONF_DIR/smartdns.conf" SMARTDNS_CONF="$SMARTDNS_VAR_CONF_DIR/smartdns.conf"
@@ -37,18 +36,18 @@ 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 get dhcp.@dnsmasq[0].server 2>/dev/null)"
echo $OLD_SERVER | grep "^$addr" >/dev/null 2>&1 echo "$OLD_SERVER" | grep "^$addr" >/dev/null 2>&1
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
return return
fi fi
uci delete dhcp.@dnsmasq[0].server 2>/dev/null uci delete dhcp.@dnsmasq[0].server 2>/dev/null
uci add_list dhcp.@dnsmasq[0].server=$addr uci add_list dhcp.@dnsmasq[0].server="$addr"
for server in $OLD_SERVER; do for server in $OLD_SERVER; do
if [ "$server" = "$addr" ]; then if [ "$server" = "$addr" ]; then
continue continue
fi fi
uci add_list dhcp.@dnsmasq[0].server=$server uci add_list dhcp.@dnsmasq[0].server="$server"
done done
uci delete dhcp.@dnsmasq[0].resolvfile 2>/dev/null uci delete dhcp.@dnsmasq[0].resolvfile 2>/dev/null
uci set dhcp.@dnsmasq[0].noresolv=1 uci set dhcp.@dnsmasq[0].noresolv=1
@@ -60,14 +59,14 @@ 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 get dhcp.@dnsmasq[0].server 2>/dev/null)"
echo $OLD_SERVER | grep "^$addr" >/dev/null 2>&1 echo "$OLD_SERVER" | grep "^$addr" >/dev/null 2>&1
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
return return
fi fi
uci del_list dhcp.@dnsmasq[0].server=$addr 2>/dev/null uci del_list dhcp.@dnsmasq[0].server="$addr" 2>/dev/null
addrlist="`uci get dhcp.@dnsmasq[0].server 2>/dev/null`" addrlist="$(uci get dhcp.@dnsmasq[0].server 2>/dev/null)"
if [ -z "$addrlist" ] ; then if [ -z "$addrlist" ] ; then
uci set dhcp.@dnsmasq[0].resolvfile=/tmp/resolv.conf.auto 2>/dev/null uci set dhcp.@dnsmasq[0].resolvfile=/tmp/resolv.conf.auto 2>/dev/null
uci delete dhcp.@dnsmasq[0].noresolv 2>/dev/null uci delete dhcp.@dnsmasq[0].noresolv 2>/dev/null
@@ -81,26 +80,26 @@ set_iptable()
local ipv6_server=$1 local ipv6_server=$1
local tcp_server=$2 local tcp_server=$2
IPS="`ifconfig | grep "inet addr" | grep -v ":127" | grep "Bcast" | awk '{print $2}' | awk -F : '{print $2}'`" IPS="$(ifconfig | grep "inet addr" | grep -v ":127" | grep "Bcast" | awk '{print $2}' | awk -F : '{print $2}')"
for IP in $IPS for IP in $IPS
do do
if [ "$tcp_server" == "1" ]; then if [ "$tcp_server" = "1" ]; then
iptables -t nat -A PREROUTING -p tcp -d $IP --dport 53 -j REDIRECT --to-ports $SMARTDNS_PORT >/dev/null 2>&1 iptables -t nat -A PREROUTING -p tcp -d "$IP" --dport 53 -j REDIRECT --to-ports "$SMARTDNS_PORT" >/dev/null 2>&1
fi fi
iptables -t nat -A PREROUTING -p udp -d $IP --dport 53 -j REDIRECT --to-ports $SMARTDNS_PORT >/dev/null 2>&1 iptables -t nat -A PREROUTING -p udp -d "$IP" --dport 53 -j REDIRECT --to-ports "$SMARTDNS_PORT" >/dev/null 2>&1
done done
if [ "$ipv6_server" == 0 ]; then if [ "$ipv6_server" = 0 ]; then
return return
fi fi
IPS="`ifconfig | grep "inet6 addr" | grep -v " fe80::" | grep -v " ::1" | grep "Global" | awk '{print $3}'`" IPS="$(ifconfig | grep "inet6 addr" | grep -v " fe80::" | grep -v " ::1" | grep "Global" | awk '{print $3}')"
for IP in $IPS for IP in $IPS
do do
if [ "$tcp_server" == "1" ]; then if [ "$tcp_server" = "1" ]; then
ip6tables -t nat -A PREROUTING -p tcp -d $IP --dport 53 -j REDIRECT --to-ports $SMARTDNS_PORT >/dev/null 2>&1 ip6tables -t nat -A PREROUTING -p tcp -d "$IP" --dport 53 -j REDIRECT --to-ports "$SMARTDNS_PORT" >/dev/null 2>&1
fi fi
ip6tables -t nat -A PREROUTING -p udp -d $IP --dport 53 -j REDIRECT --to-ports $SMARTDNS_PORT >/dev/null 2>&1 ip6tables -t nat -A PREROUTING -p udp -d "$IP" --dport 53 -j REDIRECT --to-ports "$SMARTDNS_PORT" >/dev/null 2>&1
done done
} }
@@ -109,22 +108,22 @@ clear_iptable()
{ {
local OLD_PORT="$1" local OLD_PORT="$1"
local ipv6_server=$2 local ipv6_server=$2
IPS="`ifconfig | grep "inet addr" | grep -v ":127" | grep "Bcast" | awk '{print $2}' | awk -F : '{print $2}'`" IPS="$(ifconfig | grep "inet addr" | grep -v ":127" | grep "Bcast" | awk '{print $2}' | awk -F : '{print $2}')"
for IP in $IPS for IP in $IPS
do do
iptables -t nat -D PREROUTING -p udp -d $IP --dport 53 -j REDIRECT --to-ports $OLD_PORT >/dev/null 2>&1 iptables -t nat -D PREROUTING -p udp -d "$IP" --dport 53 -j REDIRECT --to-ports "$OLD_PORT" >/dev/null 2>&1
iptables -t nat -D PREROUTING -p tcp -d $IP --dport 53 -j REDIRECT --to-ports $OLD_PORT >/dev/null 2>&1 iptables -t nat -D PREROUTING -p tcp -d "$IP" --dport 53 -j REDIRECT --to-ports "$OLD_PORT" >/dev/null 2>&1
done done
if [ "$ipv6_server" == 0 ]; then if [ "$ipv6_server" = 0 ]; then
return return
fi fi
IPS="`ifconfig | grep "inet6 addr" | grep -v " fe80::" | grep -v " ::1" | grep "Global" | awk '{print $3}'`" IPS="$(ifconfig | grep "inet6 addr" | grep -v " fe80::" | grep -v " ::1" | grep "Global" | awk '{print $3}')"
for IP in $IPS for IP in $IPS
do do
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
} }
@@ -154,7 +153,7 @@ get_tz()
continue continue
fi fi
tz="`cat $tzfile 2>/dev/null`" tz="$(cat $tzfile 2>/dev/null)"
done done
if [ -z "$tz" ]; then if [ -z "$tz" ]; then
@@ -170,18 +169,18 @@ load_server()
local ADDITIONAL_ARGS="" local ADDITIONAL_ARGS=""
local DNS_ADDRESS="" local DNS_ADDRESS=""
config_get_bool "enabled" "$section" "enabled" "1" config_get_bool enabled "$section" "enabled" "1"
config_get "port" "$section" "port" "" config_get port "$section" "port" ""
config_get "type" "$section" "type" "udp" config_get type "$section" "type" "udp"
config_get "ip" "$section" "ip" "" config_get ip "$section" "ip" ""
config_get "tls_host_verify" "$section" "tls_host_verify" "" config_get tls_host_verify "$section" "tls_host_verify" ""
config_get "host_name" "$section" "host_name" "" config_get host_name "$section" "host_name" ""
config_get "http_host" "$section" "http_host" "" config_get http_host "$section" "http_host" ""
config_get "server_group" "$section" "server_group" "" config_get server_group "$section" "server_group" ""
config_get "blacklist_ip" "$section" "blacklist_ip" "0" config_get blacklist_ip "$section" "blacklist_ip" "0"
config_get "check_edns" "$section" "check_edns" "0" config_get check_edns "$section" "check_edns" "0"
config_get "spki_pin" "$section" "spki_pin" "" config_get spki_pin "$section" "spki_pin" ""
config_get "addition_arg" "$section" "addition_arg" "" config_get addition_arg "$section" "addition_arg" ""
if [ "$enabled" = "0" ]; then if [ "$enabled" = "0" ]; then
return return
@@ -200,8 +199,8 @@ load_server()
SERVER="server-https" SERVER="server-https"
fi fi
if [ ! -z "`echo $ip | grep ":" | grep -v "https://"`" ]; then if [ ! -z "$(echo "$ip" | grep ":" | grep -v "https://")" ]; then
if [ -z "`echo $ip | grep "\["`" ]; then if [ -z "$(echo "$ip" | grep "\\[")" ]; then
ip="[$ip]" ip="[$ip]"
fi fi
fi fi
@@ -253,54 +252,54 @@ load_second_server()
local ARGS="" local ARGS=""
local ADDR="" local ADDR=""
config_get_bool "seconddns_enabled" "$section" "seconddns_enabled" "0" config_get_bool seconddns_enabled "$section" "seconddns_enabled" "0"
if [ "$seconddns_enabled" = "0" ]; then if [ "$seconddns_enabled" = "0" ]; then
return return
fi fi
config_get "seconddns_port" "$section" "seconddns_port" "7053" config_get seconddns_port "$section" "seconddns_port" "7053"
config_get_bool "seconddns_no_speed_check" "$section" "seconddns_no_speed_check" "0" config_get_bool seconddns_no_speed_check "$section" "seconddns_no_speed_check" "0"
if [ "$seconddns_no_speed_check" = "1" ]; then if [ "$seconddns_no_speed_check" = "1" ]; then
ARGS="$ARGS -no-speed-check" ARGS="$ARGS -no-speed-check"
fi fi
config_get "seconddns_server_group" "$section" "seconddns_server_group" "" config_get seconddns_server_group "$section" "seconddns_server_group" ""
if [ ! -z "$seconddns_server_group" ]; then if [ ! -z "$seconddns_server_group" ]; then
ARGS="$ARGS -group $seconddns_server_group" ARGS="$ARGS -group $seconddns_server_group"
fi fi
config_get_bool "seconddns_no_rule_addr" "$section" "seconddns_no_rule_addr" "0" config_get_bool seconddns_no_rule_addr "$section" "seconddns_no_rule_addr" "0"
if [ "$seconddns_no_rule_addr" = "1" ]; then if [ "$seconddns_no_rule_addr" = "1" ]; then
ARGS="$ARGS -no-rule-addr" ARGS="$ARGS -no-rule-addr"
fi fi
config_get_bool "seconddns_no_rule_nameserver" "$section" "seconddns_no_rule_nameserver" "0" config_get_bool seconddns_no_rule_nameserver "$section" "seconddns_no_rule_nameserver" "0"
if [ "$seconddns_no_rule_nameserver" = "1" ]; then if [ "$seconddns_no_rule_nameserver" = "1" ]; then
ARGS="$ARGS -no-rule-nameserver" ARGS="$ARGS -no-rule-nameserver"
fi fi
config_get_bool "seconddns_no_rule_ipset" "$section" "seconddns_no_rule_ipset" "0" config_get_bool seconddns_no_rule_ipset "$section" "seconddns_no_rule_ipset" "0"
if [ "$seconddns_no_rule_ipset" = "1" ]; then if [ "$seconddns_no_rule_ipset" = "1" ]; then
ARGS="$ARGS -no-rule-ipset" ARGS="$ARGS -no-rule-ipset"
fi fi
config_get_bool "seconddns_no_rule_soa" "$section" "seconddns_no_rule_soa" "0" config_get_bool seconddns_no_rule_soa "$section" "seconddns_no_rule_soa" "0"
if [ "$seconddns_no_rule_soa" = "1" ]; then if [ "$seconddns_no_rule_soa" = "1" ]; then
ARGS="$ARGS -no-rule-soa" ARGS="$ARGS -no-rule-soa"
fi fi
config_get_bool "seconddns_no_dualstack_selection" "$section" "seconddns_no_dualstack_selection" "0" config_get_bool seconddns_no_dualstack_selection "$section" "seconddns_no_dualstack_selection" "0"
if [ "$seconddns_no_dualstack_selection" = "1" ]; then if [ "$seconddns_no_dualstack_selection" = "1" ]; then
ARGS="$ARGS -no-dualstack-selection" ARGS="$ARGS -no-dualstack-selection"
fi fi
config_get_bool "seconddns_no_cache" "$section" "seconddns_no_cache" "0" config_get_bool seconddns_no_cache "$section" "seconddns_no_cache" "0"
if [ "$seconddns_no_cache" = "1" ]; then if [ "$seconddns_no_cache" = "1" ]; then
ARGS="$ARGS -no-cache" ARGS="$ARGS -no-cache"
fi fi
config_get "ipv6_server" "$section" "ipv6_server" "1" config_get ipv6_server "$section" "ipv6_server" "1"
if [ "$ipv6_server" = "1" ]; then if [ "$ipv6_server" = "1" ]; then
ADDR="[::]" ADDR="[::]"
else else
@@ -322,21 +321,21 @@ load_service()
mkdir -p $SMARTDNS_VAR_CONF_DIR mkdir -p $SMARTDNS_VAR_CONF_DIR
rm -f $SMARTDNS_CONF_TMP rm -f $SMARTDNS_CONF_TMP
config_get_bool "enabled" "$section" "enabled" '0' config_get_bool enabled "$section" "enabled" '0'
config_get "server_name" "$section" "server_name" "" config_get server_name "$section" "server_name" ""
if [ ! -z "$server_name" ]; then if [ ! -z "$server_name" ]; then
conf_append "server-name" "$server_name" conf_append "server-name" "$server_name"
fi fi
config_get "coredump" "$section" "coredump" "0" config_get coredump "$section" "coredump" "0"
if [ "$coredump" = "1" ]; then if [ "$coredump" = "1" ]; then
COREDUMP="1" COREDUMP="1"
fi fi
config_get "port" "$section" "port" "6053" config_get port "$section" "port" "6053"
config_get "ipv6_server" "$section" "ipv6_server" "1" config_get ipv6_server "$section" "ipv6_server" "1"
config_get "tcp_server" "$section" "tcp_server" "1" config_get tcp_server "$section" "tcp_server" "1"
if [ "$ipv6_server" = "1" ]; then if [ "$ipv6_server" = "1" ]; then
conf_append "bind" "[::]:$port" conf_append "bind" "[::]:$port"
else else
@@ -350,69 +349,69 @@ load_service()
conf_append "bind-tcp" ":$port" conf_append "bind-tcp" ":$port"
fi fi
fi fi
config_get "dualstack_ip_selection" "$section" "dualstack_ip_selection" "0" config_get dualstack_ip_selection "$section" "dualstack_ip_selection" "0"
if [ "$dualstack_ip_selection" = "1" ]; then if [ "$dualstack_ip_selection" = "1" ]; then
conf_append "dualstack-ip-selection" "yes" conf_append "dualstack-ip-selection" "yes"
fi fi
config_get "prefetch_domain" "$section" "prefetch_domain" "0" config_get prefetch_domain "$section" "prefetch_domain" "0"
if [ "$prefetch_domain" = "1" ]; then if [ "$prefetch_domain" = "1" ]; then
conf_append "prefetch-domain" "yes" conf_append "prefetch-domain" "yes"
fi fi
SMARTDNS_PORT="$port" SMARTDNS_PORT="$port"
config_get "cache_size" "$section" "cache_size" "" config_get cache_size "$section" "cache_size" ""
if [ ! -z "$cache_size" ]; then if [ ! -z "$cache_size" ]; then
conf_append "cache-size" "$cache_size" conf_append "cache-size" "$cache_size"
fi fi
config_get "rr_ttl" "$section" "rr_ttl" "" config_get rr_ttl "$section" "rr_ttl" ""
if [ ! -z "$rr_ttl" ]; then if [ ! -z "$rr_ttl" ]; then
conf_append "rr-ttl" "$rr_ttl" conf_append "rr-ttl" "$rr_ttl"
fi fi
config_get "rr_ttl_min" "$section" "rr_ttl_min" "" config_get rr_ttl_min "$section" "rr_ttl_min" ""
if [ ! -z "$rr_ttl_min" ]; then if [ ! -z "$rr_ttl_min" ]; then
conf_append "rr-ttl-min" "$rr_ttl_min" conf_append "rr-ttl-min" "$rr_ttl_min"
fi fi
config_get "rr_ttl_max" "$section" "rr_ttl_max" "" config_get rr_ttl_max "$section" "rr_ttl_max" ""
if [ ! -z "$rr_ttl_max" ]; then if [ ! -z "$rr_ttl_max" ]; then
conf_append "rr-ttl-max" "$rr_ttl_max" conf_append "rr-ttl-max" "$rr_ttl_max"
fi fi
config_get "log_size" "$section" "log_size" "64K" config_get log_size "$section" "log_size" "64K"
if [ ! -z "$log_size" ]; then if [ ! -z "$log_size" ]; then
conf_append "log-size" "$log_size" conf_append "log-size" "$log_size"
fi fi
config_get "log_num" "$section" "log_num" "1" config_get log_num "$section" "log_num" "1"
if [ ! -z "$log_num" ]; then if [ ! -z $log_num ]; then
conf_append "log-num" "$log_num" conf_append "log-num" "$log_num"
fi fi
config_get "log_level" "$section" "log_level" "error" config_get log_level "$section" "log_level" "error"
if [ ! -z "$log_level" ]; then if [ ! -z "$log_level" ]; then
conf_append "log-level" "$log_level" conf_append "log-level" "$log_level"
fi fi
config_get "log_file" "$section" "log_file" "" config_get log_file "$section" "log_file" ""
if [ ! -z "$log_file" ]; then if [ ! -z "$log_file" ]; then
conf_append "log-file" "$log_file" conf_append "log-file" "$log_file"
fi fi
config_get "redirect" "$section" "redirect" "none" config_get redirect "$section" "redirect" "none"
config_get "old_redirect" "$section" "old_redirect" "none" config_get old_redirect "$section" "old_redirect" "none"
config_get "old_port" "$section" "old_port" "0" config_get old_port "$section" "old_port" "0"
config_get "old_enabled" "$section" "old_enabled" "0" config_get old_enabled "$section" "old_enabled" "0"
if [ "$old_redirect" != "$redirect" ] || [ "$old_port" != "$SMARTDNS_PORT" ] || [ "$old_enabled" = "1" -a "$enabled" = "0" ]; then if [ "$old_redirect" != "$redirect" ] || [ "$old_port" != "$SMARTDNS_PORT" ] || [ "$old_enabled" = "1" -a "$enabled" = "0" ]; then
if [ "$old_redirect" != "none" ]; then if [ "$old_redirect" != "none" ]; then
if [ "$old_port" != "0" ]; then if [ "$old_port" != "0" ]; then
clear_iptable "$old_port" "$ipv6_server" clear_iptable "$old_port" "$ipv6_server"
fi fi
if [ "$old_redirect" == "dnsmasq-upstream" ]; then if [ "$old_redirect" = "dnsmasq-upstream" ]; then
stop_forward_dnsmasq "$old_port" stop_forward_dnsmasq "$old_port"
fi fi
fi fi

View File

@@ -43,56 +43,56 @@ set_iptable()
redirect_tcp=1; redirect_tcp=1;
fi fi
IPS="`ifconfig | grep "inet addr" | grep -v ":127" | grep "Bcast" | awk '{print $2}' | awk -F: '{print $2}'`" IPS="$(ifconfig | grep "inet addr" | grep -v ":127" | grep "Bcast" | awk '{print $2}' | awk -F: '{print $2}')"
for IP in $IPS for IP in $IPS
do do
if [ $redirect_tcp -eq 1 ]; then if [ $redirect_tcp -eq 1 ]; then
iptables -t nat -A PREROUTING -p tcp -d $IP --dport 53 -j REDIRECT --to-ports $SMARTDNS_PORT > /dev/null 2>&1 iptables -t nat -A PREROUTING -p tcp -d "$IP" --dport 53 -j REDIRECT --to-ports "$SMARTDNS_PORT" > /dev/null 2>&1
fi fi
iptables -t nat -A PREROUTING -p udp -d $IP --dport 53 -j REDIRECT --to-ports $SMARTDNS_PORT > /dev/null 2>&1 iptables -t nat -A PREROUTING -p udp -d "$IP" --dport 53 -j REDIRECT --to-ports "$SMARTDNS_PORT" > /dev/null 2>&1
done done
} }
clear_iptable() clear_iptable()
{ {
IPS="`ifconfig | grep "inet addr" | grep -v ":127" | grep "Bcast" | awk '{print $2}' | awk -F: '{print $2}'`" IPS="$(ifconfig | grep "inet addr" | grep -v ":127" | grep "Bcast" | awk '{print $2}' | awk -F: '{print $2}')"
for IP in $IPS for IP in $IPS
do do
iptables -t nat -D PREROUTING -p tcp -d $IP --dport 53 -j REDIRECT --to-ports $SMARTDNS_PORT > /dev/null 2>&1 iptables -t nat -D PREROUTING -p tcp -d "$IP" --dport 53 -j REDIRECT --to-ports "$SMARTDNS_PORT" > /dev/null 2>&1
iptables -t nat -D PREROUTING -p udp -d $IP --dport 53 -j REDIRECT --to-ports $SMARTDNS_PORT > /dev/null 2>&1 iptables -t nat -D PREROUTING -p udp -d "$IP" --dport 53 -j REDIRECT --to-ports "$SMARTDNS_PORT" > /dev/null 2>&1
done done
} }
get_dnsmasq_cmd() get_dnsmasq_cmd()
{ {
CMD="`ps | grep -e '[a-zA-Z]\{0,2\} \{1,\}dnsmasq' | grep -v grep 2>/dev/null`" CMD="$(ps 2>/dev/null | grep -e '[a-zA-Z]\{0,2\} \{1,\}dnsmasq' | grep -v grep 2>/dev/null)"
if [ ! -z "$CMD" ]; then if [ ! -z "$CMD" ]; then
return return
fi fi
CMD="`ps | grep '/usr/sbin/dnsmasq' | grep -v grep 2>/dev/null`" CMD="$(ps 2>/dev/null | grep '/usr/sbin/dnsmasq' | grep -v grep 2>/dev/null)"
if [ ! -z "$CMD" ]; then if [ ! -z "$CMD" ]; then
return return
fi fi
CMD="`ps | grep 'dnsmasq' | grep -v grep 2>/dev/null`" CMD="$(ps 2>/dev/null | grep 'dnsmasq' | grep -v grep 2>/dev/null)"
if [ ! -z "$CMD" ]; then if [ ! -z "$CMD" ]; then
return return
fi fi
CMD="`ps ax | grep -e '[a-zA-Z]\{0,2\} \{1,\}dnsmasq' | grep -v grep 2>/dev/null`" CMD="$(ps ax 2>/dev/null | grep -e '[a-zA-Z]\{0,2\} \{1,\}dnsmasq' | grep -v grep 2>/dev/null)"
if [ ! -z "$CMD" ]; then if [ ! -z "$CMD" ]; then
return return
fi fi
CMD="`ps ax | grep /usr/sbin/dnsmasq | grep -v grep 2>/dev/null`" CMD="$(ps ax 2>/dev/null | grep /usr/sbin/dnsmasq | grep -v grep 2>/dev/null)"
if [ ! -z "$CMD" ]; then if [ ! -z "$CMD" ]; then
return return
fi fi
CMD="`ps ax | grep 'dnsmasq' | grep -v grep 2>/dev/null`" CMD="$(ps ax 2>/dev/null | grep 'dnsmasq' | grep -v grep 2>/dev/null)"
if [ ! -z "$CMD" ]; then if [ ! -z "$CMD" ]; then
return return
fi fi
@@ -108,15 +108,15 @@ restart_dnsmasq()
return 1 return 1
fi fi
PID=`echo "$CMD" | awk '{print $1}'` PID="$(echo "$CMD" | awk '{print $1}')"
if [ ! -d "/proc/$PID" ]; then if [ ! -d "/proc/$PID" ]; then
echo "dnsmasq is not running" echo "dnsmasq is not running"
return 1 return 1
fi fi
kill -9 $PID kill -9 "$PID"
DNSMASQ_CMD="`echo $CMD | awk '{for(i=5; i<=NF;i++)printf \$i " "}'`" DNSMASQ_CMD="$(echo "$CMD" | awk '{for(i=5; i<=NF;i++)printf $i " "}')"
$DNSMASQ_CMD $DNSMASQ_CMD
} }
@@ -124,21 +124,20 @@ restart_dnsmasq()
get_server_ip() get_server_ip()
{ {
CONF_FILE=$1 CONF_FILE=$1
IPS="`ifconfig | grep "inet addr" | grep -v ":127" | grep "Bcast" | awk '{print $2}' | awk -F: '{print $2}'`" IPS="$(ifconfig | grep "inet addr" | grep -v ":127" | grep "Bcast" | awk '{print $2}' | awk -F: '{print $2}')"
LOCAL_SERVER_IP=""
for IP in $IPS for IP in $IPS
do do
N=3 N=3
while [ $N -gt 0 ] while [ $N -gt 0 ]
do do
ADDR=`echo $IP | awk -F. "{for(i=1;i<=$N;i++)printf \\$i\".\"}"` ADDR="$(echo "$IP" | awk -F. "{for(i=1;i<="$N";i++)printf \$i\".\"}")"
grep "dhcp-range=" $CONF_FILE | grep $ADDR >/dev/null 2>&1 grep "dhcp-range=" "$CONF_FILE" | grep "$ADDR" >/dev/null 2>&1
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
SERVER_TAG="`grep "^dhcp-range *=" $CONF_FILE | grep $ADDR | awk -F= '{print $2}' | awk -F, '{print $1}'`" SERVER_TAG="$(grep "^dhcp-range *=" "$CONF_FILE" | grep "$ADDR" | awk -F= '{print $2}' | awk -F, '{print $1}')"
LOCAL_SERVER_IP="$IP" LOCAL_SERVER_IP="$IP"
return 0 return 1
fi fi
N="`expr $N - 1`" N=$((N-1))
done done
done done
@@ -152,19 +151,20 @@ set_dnsmasq_conf()
local CONF_FILE=$1 local CONF_FILE=$1
get_server_ip $CONF_FILE get_server_ip $CONF_FILE
if [ "$LOCAL_SERVER_IP" ] && [ "$SERVER_TAG" ]; then if [ "$LOCAL_SERVER_IP" ] && [ "$SERVER_TAG" ]; then
grep "dhcp-option *=" $CONF_FILE | grep "$SERVER_TAG,6,$LOCAL_SERVER_IP" > /dev/null 2>&1 grep "dhcp-option *=" "$CONF_FILE" | grep "$SERVER_TAG,6,$LOCAL_SERVER_IP" > /dev/null 2>&1
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
sed -i "/^dhcp-option *=$SERVER_TAG,6,/d" $CONF_FILE sed -i "/^dhcp-option *=$SERVER_TAG,6,/d" "$CONF_FILE"
echo "dhcp-option=$SERVER_TAG,6,$LOCAL_SERVER_IP" >> $CONF_FILE echo "dhcp-option=$SERVER_TAG,6,$LOCAL_SERVER_IP" >> "$CONF_FILE"
RESTART_DNSMASQ=1 RESTART_DNSMASQ=1
fi fi
fi fi
grep "^port *=0" $CONF_FILE > /dev/null 2>&1 grep "^port *=0" "$CONF_FILE" > /dev/null 2>&1
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
sed -i "/^port *=/d" $CONF_FILE sed -i "/^port *=/d" "$CONF_FILE"
echo "port=0" >> $CONF_FILE echo "port=0" >> "$CONF_FILE"
RESTART_DNSMASQ=1 RESTART_DNSMASQ=1
fi fi
} }
@@ -179,7 +179,7 @@ set_dnsmasq()
continue continue
fi fi
set_dnsmasq_conf $conf set_dnsmasq_conf "$conf"
done done
if [ $RESTART_DNSMASQ -ne 0 ]; then if [ $RESTART_DNSMASQ -ne 0 ]; then
@@ -193,18 +193,18 @@ clear_dnsmasq_conf()
local SERVER_TAG="" local SERVER_TAG=""
local CONF_FILE=$1 local CONF_FILE=$1
get_server_ip $CONF_FILE get_server_ip "$CONF_FILE"
if [ "$LOCAL_SERVER_IP" ] && [ "$SERVER_TAG" ]; then if [ "$LOCAL_SERVER_IP" ] && [ "$SERVER_TAG" ]; then
grep "dhcp-option *=" $CONF_FILE | grep "$SERVER_TAG,6,$LOCAL_SERVER_IP" > /dev/null 2>&1 grep "dhcp-option *=" "$CONF_FILE" | grep "$SERVER_TAG,6,$LOCAL_SERVER_IP" > /dev/null 2>&1
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
sed -i "/^dhcp-option *=$SERVER_TAG,6,/d" $CONF_FILE sed -i "/^dhcp-option *=$SERVER_TAG,6,/d" "$CONF_FILE"
RESTART_DNSMASQ=1 RESTART_DNSMASQ=1
fi fi
fi fi
grep "^port *=" $CONF_FILE > /dev/null 2>&1 grep "^port *=" "$CONF_FILE" > /dev/null 2>&1
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
sed -i "/^port *=/d" $CONF_FILE sed -i "/^port *=/d" "$CONF_FILE"
RESTART_DNSMASQ=1 RESTART_DNSMASQ=1
fi fi
} }
@@ -219,7 +219,7 @@ clear_dnsmasq()
continue continue
fi fi
clear_dnsmasq_conf $conf clear_dnsmasq_conf "$conf"
done done
if [ $RESTART_DNSMASQ -ne 0 ]; then if [ $RESTART_DNSMASQ -ne 0 ]; then
@@ -232,11 +232,11 @@ set_smartdns_port()
if [ "$SMARTDNS_WORKMODE" = "0" ]; then if [ "$SMARTDNS_WORKMODE" = "0" ]; then
return 0 return 0
elif [ "$SMARTDNS_WORKMODE" = "1" ]; then elif [ "$SMARTDNS_WORKMODE" = "1" ]; then
sed -i "s/^\(bind .*\):53 *\(.*\)\$/\1:$SMARTDNS_PORT \2/g" $SMARTDNS_CONF sed -i "s/^\\(bind .*\\):53 *\\(.*\\)\$/\\1:$SMARTDNS_PORT \\2/g" $SMARTDNS_CONF
sed -i "s/^\(bind-tcp .*\):53 *\(.*\)\$/\1:$SMARTDNS_PORT \2/g" $SMARTDNS_CONF sed -i "s/^\\(bind-tcp .*\\):53 *\\(.*\\)\$/\\1:$SMARTDNS_PORT \\2/g" $SMARTDNS_CONF
elif [ "$SMARTDNS_WORKMODE" = "2" ]; then elif [ "$SMARTDNS_WORKMODE" = "2" ]; then
sed -i "s/^\(bind .*\):$SMARTDNS_PORT *\(.*\)\$/\1:53 \2/g" $SMARTDNS_CONF sed -i "s/^\\(bind .*\\):$SMARTDNS_PORT *\\(.*\\)\$/\\1:53 \\2/g" $SMARTDNS_CONF
sed -i "s/^\(bind-tcp .*\):$SMARTDNS_PORT *\(.*\)\$/\1:53 \2/g" $SMARTDNS_CONF sed -i "s/^\\(bind-tcp .*\\):$SMARTDNS_PORT *\\(.*\\)\$/\\1:53 \\2/g" $SMARTDNS_CONF
else else
return 1 return 1
fi fi
@@ -286,7 +286,7 @@ get_tz()
continue continue
fi fi
tz="`cat $tzfile 2>/dev/null`" tz="$(cat $tzfile 2>/dev/null)"
done done
if [ -z "$tz" ]; then if [ -z "$tz" ]; then
@@ -311,7 +311,7 @@ case "$1" in
fi fi
;; ;;
status) status)
pid="`cat $SMARTDNS_PID |head -n 1 2>/dev/null`" pid="$(cat $SMARTDNS_PID |head -n 1 2>/dev/null)"
if [ -z "$pid" ]; then if [ -z "$pid" ]; then
echo "smartdns not running." echo "smartdns not running."
return 0 return 0
@@ -326,7 +326,7 @@ case "$1" in
;; ;;
stop) stop)
clear_rule clear_rule
pid="`cat $SMARTDNS_PID | head -n 1 2>/dev/null`" pid="$(cat "$SMARTDNS_PID" | head -n 1 2>/dev/null)"
if [ -z "$pid" ]; then if [ -z "$pid" ]; then
echo "smartdns not running." echo "smartdns not running."
return 0 return 0
@@ -336,7 +336,7 @@ case "$1" in
return 0; return 0;
fi fi
kill -9 $pid 2>/dev/null kill -9 "$pid" 2>/dev/null
;; ;;
restart) restart)
$0 stop $0 stop