Add redirect check

This commit is contained in:
Nick Peng
2018-08-07 00:38:13 +08:00
parent 5ab3cd86a5
commit 49510c9145
6 changed files with 67 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
-- Copyright 2018 Nick Peng (pymumu@gmail.com)
module("luci.controller.smartdns", package.seeall)
local smartdns = require "luci.model.smartdns"
function index()
if not nixio.fs.access("/etc/config/smartdns") then
@@ -17,9 +18,23 @@ end
local function is_running()
return luci.sys.call("pidof smartdns >/dev/null") == 0
end
function act_status()
local e={}
e.ipv6_works = 2;
e.ipv4_works = 2;
e.redirect = smartdns.get_config_option("smartdns", "smartdns", "redirect", nil);
e.local_port = smartdns.get_config_option("smartdns", "smartdns", "port", nil);
if e.redirect == "1" then
if e.local_port ~= nil and e.local_port ~= "53" then
e.ipv4_works = luci.sys.call("iptables -t nat -nL PREROUTING | grep REDIRECT | grep dpt:53 | grep %q >/dev/null 2>&1" % e.local_port) == 0
e.ipv6_works = luci.sys.call("ip6tables -t nat -nL PREROUTING | grep REDIRECT | grep dpt:53 | grep %q >/dev/null 2>&1" % e.local_port) == 0
else
e.redirect = 0
end
end
e.running = is_running()
luci.http.prepare_content("application/json")
luci.http.write_json(e)
end

View File

@@ -31,6 +31,12 @@ msgstr "本地端口"
msgid "Smartdns local server port"
msgstr "SmartDNS本地服务端口"
msgid "IPV4 53 Port Redirect Failure"
msgstr "IPV4 53端口重定向失败"
msgid "IPV6 53 Port Redirect Failure"
msgstr "IPV6 53端口重定向失败"
msgid "Redirect"
msgstr "重定向"

View File

@@ -0,0 +1,17 @@
-- Copyright 2018 Nick Peng (pymumu@gmail.com)
require ("nixio.fs")
require ("luci.http")
require ("luci.dispatcher")
require ("nixio.fs")
local uci = require "luci.model.uci".cursor()
module("luci.model.smartdns", package.seeall)
function get_config_option(module, section, option, default)
return uci:get_first(module, section, option) or default
end
return m

View File

@@ -3,12 +3,23 @@ XHR.poll(3, '<%=luci.dispatcher.build_url("admin", "services", "smartdns", "stat
function(x, data) {
var tb = document.getElementById('smartdns_status');
if (data && tb) {
var links = "";
if (data.running) {
var links = '<b><font color=green>SmartDNS - <%:RUNNING%></font></b></em>';
tb.innerHTML = links;
links = '<b><font color=green>SmartDNS - <%:RUNNING%></font></b></em>';
if (data.redirect && data.redirect == 1) {
if (data.ipv4_works == 0) {
links += "<br></br><b><font color=red><%:IPV4 53 Port Redirect Failure%></font></b>"
}
if (data.ipv6_works == 0) {
links += "<br></br><b><font color=red><%:IPV6 53 Port Redirect Failure%></font></b>"
}
}
} else {
tb.innerHTML = '<b><font color=red>SmartDNS - <%:NOT RUNNING%></font></b>';
links = '<b><font color=red>SmartDNS - <%:NOT RUNNING%></font></b>';
}
tb.innerHTML = links;
}
}
);

View File

@@ -18,6 +18,12 @@ set_iptable()
iptables -t nat -A PREROUTING -p udp -d $IP --dport 53 -j REDIRECT --to-ports $SMARTDNS_PORT >/dev/null 2>&1
done
IPS="`ifconfig | grep "inet6 addr" | grep -v " fe80::" | grep -v " ::1" | grep "Global" | awk '{print $3}'`"
for IP in $IPS
do
ip6tables -t nat -A PREROUTING -p udp -d $IP --dport 53 -j REDIRECT --to-ports $SMARTDNS_PORT >/dev/null 2>&1
done
}
clear_iptable()
@@ -27,6 +33,12 @@ clear_iptable()
do
iptables -t nat -D PREROUTING -p udp -d $IP --dport 53 -j REDIRECT --to-ports $SMARTDNS_PORT >/dev/null 2>&1
done
IPS="`ifconfig | grep "inet6 addr" | grep -v " fe80::" | grep -v " ::1" | grep "Global" | awk '{print $3}'`"
for IP in $IPS
do
ip6tables -t nat -D PREROUTING -p udp -d $IP --dport 53 -j REDIRECT --to-ports $SMARTDNS_PORT >/dev/null 2>&1
done
}