2015-01-16 22:38:38 +00:00
|
|
|
-- Copyright 2008 Steven Barth <steven@midlink.org>
|
2015-01-16 22:46:42 +00:00
|
|
|
-- Copyright 2008 Jo-Philipp Wich <jow@openwrt.org>
|
2015-01-16 22:38:38 +00:00
|
|
|
-- 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
|
|
|
|
|
2015-09-27 23:50:37 +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
|
2015-10-20 22:11:28 +00:00
|
|
|
entry({"admin", "services", "upnp", "delete"}, post("act_delete")).leaf = true
|
2010-11-07 19:30:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function act_status()
|
luci-app-upnp: Adding and displaying "Description" to upnp data
Getting the Description data from upnp_lease_file. This data often displays the Application Name which made the upnp call. If the upnp_lease_file doesn't exist, it'll just return a blank entry under "Description".
upnp_lease_file order example: TCP:33333:192.168.0.100:33333:1485578298:NAT-PMP 33333 tcp
As an optimisation, since the upnp_lease_file has only active leases and is ordered by epoch timestamp (5th column above), and since "iptables --line-numbers -t nat -xnvL MINIUPNPD" has active leases and is also displayed in order of rule applied (time). This means the order of these two sources will be the same. This prevents us from "searching" the upnp_lease_file for every rule, and instead for the n'th rule, look at the n'th upnp_lease_file line. As a result we only need to read in one line at a time. For a safety, the upnp_lease_file description is always checked to see if it matches the rule it's being assigned to. If it doesn't match it'll return blank. This means we'll never put an incorrect description to a upnp rule, even if someone messes with the upnp_lease_file.
This is the case on my system, more testing may be necessary? If this is false we'll need to loop over the upnp_lease_file for every rule, or read in the whole upnp_lease_file once for the iptables loop.
The Description column is added to the upnp_status, and the "Delete Redirect" renamed to "Delete" to make more horizontal space in the table.
Signed-off-by: Cody R. Brown <dev@codybrown.ca>
2017-01-28 04:32:21 +00:00
|
|
|
local uci = luci.model.uci.cursor()
|
|
|
|
local lease_file = uci:get("upnpd", "config", "upnp_lease_file")
|
2018-06-17 17:31:43 +00:00
|
|
|
|
|
|
|
local ipv4_hints = luci.sys.net.ipv4_hints()
|
2018-04-05 07:32:22 +00:00
|
|
|
|
2015-02-28 18:05:42 +00:00
|
|
|
local ipt = io.popen("iptables --line-numbers -t nat -xnvL MINIUPNPD 2>/dev/null")
|
2010-11-07 19:30:55 +00:00
|
|
|
if ipt then
|
luci-app-upnp: Adding and displaying "Description" to upnp data
Getting the Description data from upnp_lease_file. This data often displays the Application Name which made the upnp call. If the upnp_lease_file doesn't exist, it'll just return a blank entry under "Description".
upnp_lease_file order example: TCP:33333:192.168.0.100:33333:1485578298:NAT-PMP 33333 tcp
As an optimisation, since the upnp_lease_file has only active leases and is ordered by epoch timestamp (5th column above), and since "iptables --line-numbers -t nat -xnvL MINIUPNPD" has active leases and is also displayed in order of rule applied (time). This means the order of these two sources will be the same. This prevents us from "searching" the upnp_lease_file for every rule, and instead for the n'th rule, look at the n'th upnp_lease_file line. As a result we only need to read in one line at a time. For a safety, the upnp_lease_file description is always checked to see if it matches the rule it's being assigned to. If it doesn't match it'll return blank. This means we'll never put an incorrect description to a upnp rule, even if someone messes with the upnp_lease_file.
This is the case on my system, more testing may be necessary? If this is false we'll need to loop over the upnp_lease_file for every rule, or read in the whole upnp_lease_file once for the iptables loop.
The Description column is added to the upnp_status, and the "Delete Redirect" renamed to "Delete" to make more horizontal space in the table.
Signed-off-by: Cody R. Brown <dev@codybrown.ca>
2017-01-28 04:32:21 +00:00
|
|
|
local upnpf = lease_file and io.open(lease_file, "r")
|
2010-11-07 19:30:55 +00:00
|
|
|
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+)")
|
luci-app-upnp: Adding and displaying "Description" to upnp data
Getting the Description data from upnp_lease_file. This data often displays the Application Name which made the upnp call. If the upnp_lease_file doesn't exist, it'll just return a blank entry under "Description".
upnp_lease_file order example: TCP:33333:192.168.0.100:33333:1485578298:NAT-PMP 33333 tcp
As an optimisation, since the upnp_lease_file has only active leases and is ordered by epoch timestamp (5th column above), and since "iptables --line-numbers -t nat -xnvL MINIUPNPD" has active leases and is also displayed in order of rule applied (time). This means the order of these two sources will be the same. This prevents us from "searching" the upnp_lease_file for every rule, and instead for the n'th rule, look at the n'th upnp_lease_file line. As a result we only need to read in one line at a time. For a safety, the upnp_lease_file description is always checked to see if it matches the rule it's being assigned to. If it doesn't match it'll return blank. This means we'll never put an incorrect description to a upnp rule, even if someone messes with the upnp_lease_file.
This is the case on my system, more testing may be necessary? If this is false we'll need to loop over the upnp_lease_file for every rule, or read in the whole upnp_lease_file once for the iptables loop.
The Description column is added to the upnp_status, and the "Delete Redirect" renamed to "Delete" to make more horizontal space in the table.
Signed-off-by: Cody R. Brown <dev@codybrown.ca>
2017-01-28 04:32:21 +00:00
|
|
|
local descr = ""
|
2010-11-07 19:30:55 +00:00
|
|
|
|
|
|
|
if num and proto and extport and intaddr and intport then
|
|
|
|
num = tonumber(num)
|
|
|
|
extport = tonumber(extport)
|
|
|
|
intport = tonumber(intport)
|
2018-04-05 07:32:22 +00:00
|
|
|
|
luci-app-upnp: Adding and displaying "Description" to upnp data
Getting the Description data from upnp_lease_file. This data often displays the Application Name which made the upnp call. If the upnp_lease_file doesn't exist, it'll just return a blank entry under "Description".
upnp_lease_file order example: TCP:33333:192.168.0.100:33333:1485578298:NAT-PMP 33333 tcp
As an optimisation, since the upnp_lease_file has only active leases and is ordered by epoch timestamp (5th column above), and since "iptables --line-numbers -t nat -xnvL MINIUPNPD" has active leases and is also displayed in order of rule applied (time). This means the order of these two sources will be the same. This prevents us from "searching" the upnp_lease_file for every rule, and instead for the n'th rule, look at the n'th upnp_lease_file line. As a result we only need to read in one line at a time. For a safety, the upnp_lease_file description is always checked to see if it matches the rule it's being assigned to. If it doesn't match it'll return blank. This means we'll never put an incorrect description to a upnp rule, even if someone messes with the upnp_lease_file.
This is the case on my system, more testing may be necessary? If this is false we'll need to loop over the upnp_lease_file for every rule, or read in the whole upnp_lease_file once for the iptables loop.
The Description column is added to the upnp_status, and the "Delete Redirect" renamed to "Delete" to make more horizontal space in the table.
Signed-off-by: Cody R. Brown <dev@codybrown.ca>
2017-01-28 04:32:21 +00:00
|
|
|
if upnpf then
|
|
|
|
local uln = upnpf:read("*l")
|
|
|
|
if uln then descr = uln:match(string.format("^%s:%d:%s:%d:%%d*:(.*)$", proto:upper(), extport, intaddr, intport)) end
|
|
|
|
if not descr then descr = "" end
|
|
|
|
end
|
2018-06-17 17:31:43 +00:00
|
|
|
|
|
|
|
local host_hint, _, e
|
|
|
|
|
|
|
|
for _,e in pairs(ipv4_hints) do
|
|
|
|
if e[1] == intaddr then
|
|
|
|
host_hint = e[2]
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2010-11-07 19:30:55 +00:00
|
|
|
|
|
|
|
fwd[#fwd+1] = {
|
|
|
|
num = num,
|
|
|
|
proto = proto:upper(),
|
|
|
|
extport = extport,
|
|
|
|
intaddr = intaddr,
|
2018-06-17 17:31:43 +00:00
|
|
|
host_hint = host_hint,
|
luci-app-upnp: Adding and displaying "Description" to upnp data
Getting the Description data from upnp_lease_file. This data often displays the Application Name which made the upnp call. If the upnp_lease_file doesn't exist, it'll just return a blank entry under "Description".
upnp_lease_file order example: TCP:33333:192.168.0.100:33333:1485578298:NAT-PMP 33333 tcp
As an optimisation, since the upnp_lease_file has only active leases and is ordered by epoch timestamp (5th column above), and since "iptables --line-numbers -t nat -xnvL MINIUPNPD" has active leases and is also displayed in order of rule applied (time). This means the order of these two sources will be the same. This prevents us from "searching" the upnp_lease_file for every rule, and instead for the n'th rule, look at the n'th upnp_lease_file line. As a result we only need to read in one line at a time. For a safety, the upnp_lease_file description is always checked to see if it matches the rule it's being assigned to. If it doesn't match it'll return blank. This means we'll never put an incorrect description to a upnp rule, even if someone messes with the upnp_lease_file.
This is the case on my system, more testing may be necessary? If this is false we'll need to loop over the upnp_lease_file for every rule, or read in the whole upnp_lease_file once for the iptables loop.
The Description column is added to the upnp_status, and the "Delete Redirect" renamed to "Delete" to make more horizontal space in the table.
Signed-off-by: Cody R. Brown <dev@codybrown.ca>
2017-01-28 04:32:21 +00:00
|
|
|
intport = intport,
|
|
|
|
descr = descr
|
2010-11-07 19:30:55 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
luci-app-upnp: Adding and displaying "Description" to upnp data
Getting the Description data from upnp_lease_file. This data often displays the Application Name which made the upnp call. If the upnp_lease_file doesn't exist, it'll just return a blank entry under "Description".
upnp_lease_file order example: TCP:33333:192.168.0.100:33333:1485578298:NAT-PMP 33333 tcp
As an optimisation, since the upnp_lease_file has only active leases and is ordered by epoch timestamp (5th column above), and since "iptables --line-numbers -t nat -xnvL MINIUPNPD" has active leases and is also displayed in order of rule applied (time). This means the order of these two sources will be the same. This prevents us from "searching" the upnp_lease_file for every rule, and instead for the n'th rule, look at the n'th upnp_lease_file line. As a result we only need to read in one line at a time. For a safety, the upnp_lease_file description is always checked to see if it matches the rule it's being assigned to. If it doesn't match it'll return blank. This means we'll never put an incorrect description to a upnp rule, even if someone messes with the upnp_lease_file.
This is the case on my system, more testing may be necessary? If this is false we'll need to loop over the upnp_lease_file for every rule, or read in the whole upnp_lease_file once for the iptables loop.
The Description column is added to the upnp_status, and the "Delete Redirect" renamed to "Delete" to make more horizontal space in the table.
Signed-off-by: Cody R. Brown <dev@codybrown.ca>
2017-01-28 04:32:21 +00:00
|
|
|
if upnpf then upnpf:close() end
|
2010-11-07 19:30:55 +00:00
|
|
|
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
|
2018-04-05 07:32:22 +00:00
|
|
|
luci.sys.call("sed -i -e '%dd' %s" %{ idx, luci.util.shellquote(lease_file) })
|
2013-01-13 18:33:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
luci.http.status(200, "OK")
|
2010-11-07 19:30:55 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
luci.http.status(400, "Bad request")
|
|
|
|
end
|