smartdns.lua 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. --
  2. -- Copyright (C) 2018-2020 Ruilin Peng (Nick) <[email protected]>.
  3. --
  4. -- smartdns is free software: you can redistribute it and/or modify
  5. -- it under the terms of the GNU General Public License as published by
  6. -- the Free Software Foundation, either version 3 of the License, or
  7. -- (at your option) any later version.
  8. --
  9. -- smartdns is distributed in the hope that it will be useful,
  10. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. -- GNU General Public License for more details.
  13. --
  14. -- You should have received a copy of the GNU General Public License
  15. -- along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. module("luci.controller.smartdns", package.seeall)
  17. local smartdns = require "luci.model.smartdns"
  18. function index()
  19. if not nixio.fs.access("/etc/config/smartdns") then
  20. return
  21. end
  22. local page
  23. page = entry({"admin", "services", "smartdns"}, cbi("smartdns/smartdns"), _("SmartDNS"), 60)
  24. page.dependent = true
  25. page = entry({"admin", "services", "smartdns", "status"}, call("act_status"))
  26. page.leaf = true
  27. page = entry({"admin", "services", "smartdns", "upstream"}, cbi("smartdns/upstream"), nil)
  28. page.leaf = true
  29. end
  30. local function is_running()
  31. return luci.sys.call("pidof smartdns >/dev/null") == 0
  32. end
  33. function act_status()
  34. local e={}
  35. local ipv6_server;
  36. local redirect_mode="none";
  37. e.ipv6_works = 2;
  38. e.ipv4_works = 2;
  39. e.ipv6_server = 1;
  40. e.dnsmasq_forward = 0;
  41. redirect_mode = smartdns.get_config_option("smartdns", "smartdns", "redirect", nil);
  42. if redirect_mode == "redirect" then
  43. e.redirect = 1
  44. elseif redirect_mode == "dnsmasq-upstream" then
  45. e.redirect = 2
  46. else
  47. e.redirect = 0
  48. end
  49. e.local_port = smartdns.get_config_option("smartdns", "smartdns", "port", nil);
  50. ipv6_server = smartdns.get_config_option("smartdns", "smartdns", "ipv6_server", nil);
  51. if e.redirect == 1 then
  52. if e.local_port ~= nil and e.local_port ~= "53" then
  53. e.ipv4_works = luci.sys.call("iptables -t nat -nL PREROUTING 2>/dev/null | grep REDIRECT | grep dpt:53 | grep %q >/dev/null 2>&1" % e.local_port) == 0
  54. if ipv6_server == "1" then
  55. e.ipv6_works = luci.sys.call("ip6tables -t nat -nL PREROUTING 2>/dev/null| grep REDIRECT | grep dpt:53 | grep %q >/dev/null 2>&1" % e.local_port) == 0
  56. else
  57. e.ipv6_works = 2
  58. end
  59. else
  60. e.redirect = 0
  61. end
  62. elseif e.redirect == 2 then
  63. local str;
  64. local dnsmasq_server = luci.sys.exec("uci get dhcp.@dnsmasq[0].server")
  65. if e.local_port ~= nil then
  66. str = "127.0.0.1#" .. e.local_port
  67. if string.sub(dnsmasq_server,1,string.len(str)) == str then
  68. e.dnsmasq_forward = 1
  69. end
  70. end
  71. end
  72. e.running = is_running()
  73. luci.http.prepare_content("application/json")
  74. luci.http.write_json(e)
  75. end