Update my email addresses in the license headers
[project/luci.git] / applications / luci-app-upnp / luasrc / controller / upnp.lua
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.
4
5 module("luci.controller.upnp", package.seeall)
6
7 function index()
8         if not nixio.fs.access("/etc/config/upnpd") then
9                 return
10         end
11
12         local page
13
14         page = entry({"admin", "services", "upnp"}, cbi("upnp/upnp"), _("UPNP"))
15         page.dependent = true
16
17         entry({"admin", "services", "upnp", "status"}, call("act_status")).leaf = true
18         entry({"admin", "services", "upnp", "delete"}, call("act_delete")).leaf = true
19 end
20
21 function act_status()
22         local ipt = io.popen("iptables --line-numbers -t nat -xnvL MINIUPNPD")
23         if ipt then
24                 local fwd = { }
25                 while true do
26                         local ln = ipt:read("*l")
27                         if not ln then
28                                 break
29                         elseif ln:match("^%d+") then
30                                 local num, proto, extport, intaddr, intport =
31                                         ln:match("^(%d+).-([a-z]+).-dpt:(%d+) to:(%S-):(%d+)")
32
33                                 if num and proto and extport and intaddr and intport then
34                                         num     = tonumber(num)
35                                         extport = tonumber(extport)
36                                         intport = tonumber(intport)
37
38                                         fwd[#fwd+1] = {
39                                                 num     = num,
40                                                 proto   = proto:upper(),
41                                                 extport = extport,
42                                                 intaddr = intaddr,
43                                                 intport = intport
44                                         }
45                                 end
46                         end
47                 end
48
49                 ipt:close()
50
51                 luci.http.prepare_content("application/json")
52                 luci.http.write_json(fwd)
53         end
54 end
55
56 function act_delete(num)
57         local idx = tonumber(num)
58         local uci = luci.model.uci.cursor()
59
60         if idx and idx > 0 then
61                 luci.sys.call("iptables -t filter -D MINIUPNPD %d 2>/dev/null" % idx)
62                 luci.sys.call("iptables -t nat -D MINIUPNPD %d 2>/dev/null" % idx)
63
64                 local lease_file = uci:get("upnpd", "config", "upnp_lease_file")
65                 if lease_file and nixio.fs.access(lease_file) then
66                         luci.sys.call("sed -i -e '%dd' %q" %{ idx, lease_file })
67                 end
68
69                 luci.http.status(200, "OK")
70                 return
71         end
72
73         luci.http.status(400, "Bad request")
74 end