1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2008 Jo-Philipp Wich <jow@openwrt.org>
3 -- Licensed to the public under the Apache License 2.0.
5 module("luci.controller.upnp", package.seeall)
8 if not nixio.fs.access("/etc/config/upnpd") then
14 page = entry({"admin", "services", "upnp"}, cbi("upnp/upnp"), _("UPnP"))
17 entry({"admin", "services", "upnp", "status"}, call("act_status")).leaf = true
18 entry({"admin", "services", "upnp", "delete"}, post("act_delete")).leaf = true
22 local uci = luci.model.uci.cursor()
23 local lease_file = uci:get("upnpd", "config", "upnp_lease_file")
25 local ipt = io.popen("iptables --line-numbers -t nat -xnvL MINIUPNPD 2>/dev/null")
27 local upnpf = lease_file and io.open(lease_file, "r")
30 local ln = ipt:read("*l")
33 elseif ln:match("^%d+") then
34 local num, proto, extport, intaddr, intport =
35 ln:match("^(%d+).-([a-z]+).-dpt:(%d+) to:(%S-):(%d+)")
38 if num and proto and extport and intaddr and intport then
40 extport = tonumber(extport)
41 intport = tonumber(intport)
44 local uln = upnpf:read("*l")
45 if uln then descr = uln:match(string.format("^%s:%d:%s:%d:%%d*:(.*)$", proto:upper(), extport, intaddr, intport)) end
46 if not descr then descr = "" end
51 proto = proto:upper(),
61 if upnpf then upnpf:close() end
64 luci.http.prepare_content("application/json")
65 luci.http.write_json(fwd)
69 function act_delete(num)
70 local idx = tonumber(num)
71 local uci = luci.model.uci.cursor()
73 if idx and idx > 0 then
74 luci.sys.call("iptables -t filter -D MINIUPNPD %d 2>/dev/null" % idx)
75 luci.sys.call("iptables -t nat -D MINIUPNPD %d 2>/dev/null" % idx)
77 local lease_file = uci:get("upnpd", "config", "upnp_lease_file")
78 if lease_file and nixio.fs.access(lease_file) then
79 luci.sys.call("sed -i -e '%dd' %q" %{ idx, lease_file })
82 luci.http.status(200, "OK")
86 luci.http.status(400, "Bad request")