2015-01-16 22:38:38 +00:00
|
|
|
-- Copyright 2008 Steven Barth <steven@midlink.org>
|
2018-07-12 14:50:40 +00:00
|
|
|
-- Copyright 2011-2018 Jo-Philipp Wich <jo@mein.io>
|
2015-01-16 22:38:38 +00:00
|
|
|
-- Licensed to the public under the Apache License 2.0.
|
2010-10-30 00:46:41 +00:00
|
|
|
|
2008-05-25 17:00:30 +00:00
|
|
|
module("luci.controller.admin.network", package.seeall)
|
2008-05-22 14:04:03 +00:00
|
|
|
|
|
|
|
function index()
|
2010-10-25 22:26:08 +00:00
|
|
|
local page
|
|
|
|
|
2011-11-07 10:44:20 +00:00
|
|
|
-- if page.inreq then
|
2019-10-31 17:46:51 +00:00
|
|
|
page = entry({"admin", "network", "switch"}, view("network/switch"), _("Switch"), 20)
|
|
|
|
page.uci_depends = { network = { ["@switch[0]"] = "switch" } }
|
2011-11-07 10:38:41 +00:00
|
|
|
|
2019-10-31 17:46:51 +00:00
|
|
|
page = entry({"admin", "network", "wireless"}, view("network/wireless"), _('Wireless'), 15)
|
|
|
|
page.uci_depends = { wireless = { ["@wifi-device[0]"] = "wifi-device" } }
|
|
|
|
page.leaf = true
|
2011-11-07 10:38:41 +00:00
|
|
|
|
2019-08-20 13:39:16 +00:00
|
|
|
page = entry({"admin", "network", "iface_down"}, post("iface_down"), nil)
|
|
|
|
page.leaf = true
|
|
|
|
|
|
|
|
page = entry({"admin", "network", "network"}, view("network/interfaces"), _("Interfaces"), 10)
|
2011-11-07 10:38:41 +00:00
|
|
|
page.leaf = true
|
|
|
|
page.subindex = true
|
2009-10-29 02:23:58 +00:00
|
|
|
|
2019-10-31 17:46:51 +00:00
|
|
|
page = node("admin", "network", "dhcp")
|
|
|
|
page.uci_depends = { dhcp = true }
|
|
|
|
page.target = view("network/dhcp")
|
|
|
|
page.title = _("DHCP and DNS")
|
|
|
|
page.order = 30
|
|
|
|
|
|
|
|
page = node("admin", "network", "hosts")
|
|
|
|
page.uci_depends = { dhcp = true }
|
|
|
|
page.target = view("network/hosts")
|
|
|
|
page.title = _("Hostnames")
|
|
|
|
page.order = 40
|
2010-11-15 22:04:56 +00:00
|
|
|
|
2011-11-07 10:38:41 +00:00
|
|
|
page = node("admin", "network", "routes")
|
2019-07-07 17:11:37 +00:00
|
|
|
page.target = view("network/routes")
|
2011-11-07 10:38:41 +00:00
|
|
|
page.title = _("Static Routes")
|
|
|
|
page.order = 50
|
2010-11-15 22:04:56 +00:00
|
|
|
|
2011-11-07 10:38:41 +00:00
|
|
|
page = node("admin", "network", "diagnostics")
|
|
|
|
page.target = template("admin_network/diagnostics")
|
|
|
|
page.title = _("Diagnostics")
|
|
|
|
page.order = 60
|
2008-09-06 18:21:58 +00:00
|
|
|
|
2015-10-20 20:17:23 +00:00
|
|
|
page = entry({"admin", "network", "diag_ping"}, post("diag_ping"), nil)
|
2011-11-07 10:38:41 +00:00
|
|
|
page.leaf = true
|
2010-11-07 23:31:19 +00:00
|
|
|
|
2015-10-20 20:17:23 +00:00
|
|
|
page = entry({"admin", "network", "diag_nslookup"}, post("diag_nslookup"), nil)
|
2010-11-07 23:31:19 +00:00
|
|
|
page.leaf = true
|
2008-09-06 18:21:58 +00:00
|
|
|
|
2015-10-20 20:17:23 +00:00
|
|
|
page = entry({"admin", "network", "diag_traceroute"}, post("diag_traceroute"), nil)
|
2011-11-07 10:38:41 +00:00
|
|
|
page.leaf = true
|
2012-11-11 12:20:56 +00:00
|
|
|
|
2015-10-20 20:17:23 +00:00
|
|
|
page = entry({"admin", "network", "diag_ping6"}, post("diag_ping6"), nil)
|
2012-11-11 12:20:56 +00:00
|
|
|
page.leaf = true
|
|
|
|
|
2015-10-20 20:17:23 +00:00
|
|
|
page = entry({"admin", "network", "diag_traceroute6"}, post("diag_traceroute6"), nil)
|
2012-11-11 12:20:56 +00:00
|
|
|
page.leaf = true
|
2011-11-07 10:44:20 +00:00
|
|
|
-- end
|
2009-05-13 23:46:56 +00:00
|
|
|
end
|
2009-09-26 11:10:01 +00:00
|
|
|
|
2019-08-20 13:39:16 +00:00
|
|
|
local function addr2dev(addr, src)
|
|
|
|
local ip = require "luci.ip"
|
|
|
|
local route = ip.route(addr, src)
|
|
|
|
if not src and route and route.src then
|
|
|
|
route = ip.route(addr, route.src:string())
|
|
|
|
end
|
|
|
|
return route and route.dev
|
|
|
|
end
|
|
|
|
|
|
|
|
function iface_down(iface, force)
|
|
|
|
local netmd = require "luci.model.network".init()
|
|
|
|
local peer = luci.http.getenv("REMOTE_ADDR")
|
|
|
|
local serv = luci.http.getenv("SERVER_ADDR")
|
|
|
|
|
|
|
|
if force ~= "force" and serv and peer then
|
|
|
|
local dev = addr2dev(peer, serv)
|
|
|
|
if dev then
|
|
|
|
local nets = netmd:get_networks()
|
|
|
|
local outnet = nil
|
|
|
|
local _, net, ai
|
|
|
|
|
|
|
|
for _, net in ipairs(nets) do
|
|
|
|
if net:contains_interface(dev) then
|
|
|
|
outnet = net
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if outnet:name() == iface then
|
|
|
|
luci.http.status(409, "Is inbound interface")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local peeraddr = outnet:get("peeraddr")
|
|
|
|
for _, ai in ipairs(peeraddr and nixio.getaddrinfo(peeraddr) or {}) do
|
|
|
|
local peerdev = addr2dev(ai.address)
|
|
|
|
for _, net in ipairs(peerdev and nets or {}) do
|
|
|
|
if net:contains_interface(peerdev) and net:name() == iface then
|
|
|
|
luci.http.status(409, "Is inbound interface")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if netmd:get_network(iface) then
|
|
|
|
luci.sys.call("env -i /sbin/ifdown %s >/dev/null 2>/dev/null"
|
|
|
|
% luci.util.shellquote(iface))
|
|
|
|
luci.http.status(200, "Shut down")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
luci.http.status(404, "No such interface")
|
|
|
|
end
|
|
|
|
|
2012-11-21 19:29:47 +00:00
|
|
|
function diag_command(cmd, addr)
|
2010-11-24 20:09:37 +00:00
|
|
|
if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
|
|
|
|
luci.http.prepare_content("text/plain")
|
|
|
|
|
2018-04-05 07:32:22 +00:00
|
|
|
local util = io.popen(cmd % luci.util.shellquote(addr))
|
2010-11-24 20:09:37 +00:00
|
|
|
if util then
|
|
|
|
while true do
|
|
|
|
local ln = util:read("*l")
|
|
|
|
if not ln then break end
|
|
|
|
luci.http.write(ln)
|
|
|
|
luci.http.write("\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
util:close()
|
|
|
|
end
|
|
|
|
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
luci.http.status(500, "Bad address")
|
|
|
|
end
|
|
|
|
|
2012-11-21 19:29:47 +00:00
|
|
|
function diag_ping(addr)
|
2018-04-05 07:32:22 +00:00
|
|
|
diag_command("ping -c 5 -W 1 %s 2>&1", addr)
|
2010-11-24 20:09:37 +00:00
|
|
|
end
|
|
|
|
|
2012-11-21 19:29:47 +00:00
|
|
|
function diag_traceroute(addr)
|
2018-04-05 07:32:22 +00:00
|
|
|
diag_command("traceroute -q 1 -w 1 -n %s 2>&1", addr)
|
2010-11-24 20:09:37 +00:00
|
|
|
end
|
|
|
|
|
2012-11-21 19:29:47 +00:00
|
|
|
function diag_nslookup(addr)
|
2018-04-05 07:32:22 +00:00
|
|
|
diag_command("nslookup %s 2>&1", addr)
|
2010-11-24 20:09:37 +00:00
|
|
|
end
|
2012-11-11 12:20:56 +00:00
|
|
|
|
2012-11-21 19:29:47 +00:00
|
|
|
function diag_ping6(addr)
|
2018-04-05 07:32:22 +00:00
|
|
|
diag_command("ping6 -c 5 %s 2>&1", addr)
|
2012-11-11 12:20:56 +00:00
|
|
|
end
|
|
|
|
|
2012-11-21 19:29:47 +00:00
|
|
|
function diag_traceroute6(addr)
|
2018-04-05 07:32:22 +00:00
|
|
|
diag_command("traceroute6 -q 1 -w 2 -n %s 2>&1", addr)
|
2012-11-11 12:20:56 +00:00
|
|
|
end
|