applications/luci-upnp: also purge entry from lease file when deleting leases, remove...
[project/luci.git] / applications / luci-upnp / luasrc / controller / upnp.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 ]]--
14
15 module("luci.controller.upnp", package.seeall)
16
17 function index()
18         if not nixio.fs.access("/etc/config/upnpd") then
19                 return
20         end
21
22         local page
23
24         page = entry({"admin", "services", "upnp"}, cbi("upnp/upnp"), _("UPNP"))
25         page.dependent = true
26
27         entry({"admin", "services", "upnp", "status"}, call("act_status")).leaf = true
28         entry({"admin", "services", "upnp", "delete"}, call("act_delete")).leaf = true
29 end
30
31 function act_status()
32         local ipt = io.popen("iptables --line-numbers -t nat -xnvL MINIUPNPD")
33         if ipt then
34                 local fwd = { }
35                 while true do
36                         local ln = ipt:read("*l")
37                         if not ln then
38                                 break
39                         elseif ln:match("^%d+") then
40                                 local num, proto, extport, intaddr, intport =
41                                         ln:match("^(%d+).-([a-z]+).-dpt:(%d+) to:(%S-):(%d+)")
42
43                                 if num and proto and extport and intaddr and intport then
44                                         num     = tonumber(num)
45                                         extport = tonumber(extport)
46                                         intport = tonumber(intport)
47
48                                         fwd[#fwd+1] = {
49                                                 num     = num,
50                                                 proto   = proto:upper(),
51                                                 extport = extport,
52                                                 intaddr = intaddr,
53                                                 intport = intport
54                                         }
55                                 end
56                         end
57                 end
58
59                 ipt:close()
60
61                 luci.http.prepare_content("application/json")
62                 luci.http.write_json(fwd)
63         end
64 end
65
66 function act_delete(num)
67         local idx = tonumber(num)
68         local uci = luci.model.uci.cursor()
69
70         if idx and idx > 0 then
71                 luci.sys.call("iptables -t filter -D MINIUPNPD %d 2>/dev/null" % idx)
72                 luci.sys.call("iptables -t nat -D MINIUPNPD %d 2>/dev/null" % idx)
73
74                 local lease_file = uci:get("upnpd", "config", "upnp_lease_file")
75                 if lease_file and nixio.fs.access(lease_file) then
76                         luci.sys.call("sed -i -e '%dd' %q" %{ idx, lease_file })
77                 end
78
79                 luci.http.status(200, "OK")
80                 return
81         end
82
83         luci.http.status(400, "Bad request")
84 end