applications/luci-upnp: enable/disable service, live status, allow removing single...
[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 $Id$
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.i18n = "upnp"
26         page.dependent = true
27
28         page = entry({"mini", "network", "upnp"}, cbi("upnp/upnpmini", {autoapply=true}), "UPNP")
29         page.i18n = "upnp"
30         page.dependent = true
31
32         entry({"admin", "services", "upnp", "status"}, call("act_status")).leaf = true
33         entry({"admin", "services", "upnp", "delete"}, call("act_delete")).leaf = true
34 end
35
36 function act_status()
37         local ipt = io.popen("iptables --line-numbers -t nat -xnvL MINIUPNPD")
38         if ipt then
39                 local fwd = { }
40                 while true do
41                         local ln = ipt:read("*l")
42                         if not ln then
43                                 break
44                         elseif ln:match("^%d+") then
45                                 local num, proto, extport, intaddr, intport =
46                                         ln:match("^(%d+).-([a-z]+).-dpt:(%d+) to:(%S-):(%d+)")
47
48                                 if num and proto and extport and intaddr and intport then
49                                         num     = tonumber(num)
50                                         extport = tonumber(extport)
51                                         intport = tonumber(intport)
52
53                                         fwd[#fwd+1] = {
54                                                 num     = num,
55                                                 proto   = proto:upper(),
56                                                 extport = extport,
57                                                 intaddr = intaddr,
58                                                 intport = intport
59                                         }
60                                 end
61                         end
62                 end
63
64                 ipt:close()
65
66                 luci.http.prepare_content("application/json")
67                 luci.http.write_json(fwd)
68         end
69 end
70
71 function act_delete()
72         local path = luci.dispatcher.context.requestpath
73         local idx = tonumber(path[#path])
74
75         if idx and idx > 0 then
76                 luci.sys.call("iptables -t filter -D MINIUPNPD %d 2>/dev/null" % idx)
77                 luci.sys.call("iptables -t nat -D MINIUPNPD %d 2>/dev/null" % idx)
78                 return
79         end
80
81         luci.http.status(400, "Bad request")
82 end