2015-01-16 22:38:38 +00:00
|
|
|
-- Copyright 2008 Steven Barth <steven@midlink.org>
|
|
|
|
-- Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
|
|
|
|
-- Licensed to the public under the Apache License 2.0.
|
2011-08-12 13:16:27 +00:00
|
|
|
|
2008-07-17 18:11:15 +00:00
|
|
|
module("luci.controller.upnp", package.seeall)
|
|
|
|
|
|
|
|
function index()
|
2009-07-19 00:24:58 +00:00
|
|
|
if not nixio.fs.access("/etc/config/upnpd") then
|
2008-07-17 18:11:15 +00:00
|
|
|
return
|
|
|
|
end
|
2010-11-07 19:30:55 +00:00
|
|
|
|
|
|
|
local page
|
|
|
|
|
2011-08-12 13:16:27 +00:00
|
|
|
page = entry({"admin", "services", "upnp"}, cbi("upnp/upnp"), _("UPNP"))
|
2008-07-17 18:11:15 +00:00
|
|
|
page.dependent = true
|
2010-11-07 19:30:55 +00:00
|
|
|
|
|
|
|
entry({"admin", "services", "upnp", "status"}, call("act_status")).leaf = true
|
|
|
|
entry({"admin", "services", "upnp", "delete"}, call("act_delete")).leaf = true
|
|
|
|
end
|
|
|
|
|
|
|
|
function act_status()
|
|
|
|
local ipt = io.popen("iptables --line-numbers -t nat -xnvL MINIUPNPD")
|
|
|
|
if ipt then
|
|
|
|
local fwd = { }
|
|
|
|
while true do
|
|
|
|
local ln = ipt:read("*l")
|
|
|
|
if not ln then
|
|
|
|
break
|
|
|
|
elseif ln:match("^%d+") then
|
|
|
|
local num, proto, extport, intaddr, intport =
|
|
|
|
ln:match("^(%d+).-([a-z]+).-dpt:(%d+) to:(%S-):(%d+)")
|
|
|
|
|
|
|
|
if num and proto and extport and intaddr and intport then
|
|
|
|
num = tonumber(num)
|
|
|
|
extport = tonumber(extport)
|
|
|
|
intport = tonumber(intport)
|
|
|
|
|
|
|
|
fwd[#fwd+1] = {
|
|
|
|
num = num,
|
|
|
|
proto = proto:upper(),
|
|
|
|
extport = extport,
|
|
|
|
intaddr = intaddr,
|
|
|
|
intport = intport
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ipt:close()
|
|
|
|
|
|
|
|
luci.http.prepare_content("application/json")
|
|
|
|
luci.http.write_json(fwd)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-13 18:33:27 +00:00
|
|
|
function act_delete(num)
|
|
|
|
local idx = tonumber(num)
|
|
|
|
local uci = luci.model.uci.cursor()
|
|
|
|
|
2010-11-07 19:30:55 +00:00
|
|
|
if idx and idx > 0 then
|
|
|
|
luci.sys.call("iptables -t filter -D MINIUPNPD %d 2>/dev/null" % idx)
|
|
|
|
luci.sys.call("iptables -t nat -D MINIUPNPD %d 2>/dev/null" % idx)
|
2013-01-13 18:33:27 +00:00
|
|
|
|
|
|
|
local lease_file = uci:get("upnpd", "config", "upnp_lease_file")
|
|
|
|
if lease_file and nixio.fs.access(lease_file) then
|
|
|
|
luci.sys.call("sed -i -e '%dd' %q" %{ idx, lease_file })
|
|
|
|
end
|
|
|
|
|
|
|
|
luci.http.status(200, "OK")
|
2010-11-07 19:30:55 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
luci.http.status(400, "Bad request")
|
|
|
|
end
|