7acd029a5f0b54ce43db5c5823605d259108f993
[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
16 module("luci.controller.upnp", package.seeall)
17
18 function index()
19         if not nixio.fs.access("/etc/config/upnpd") then
20                 return
21         end
22
23         local page
24
25         page = entry({"admin", "services", "upnp"}, cbi("upnp/upnp"), _("UPNP"))
26         page.dependent = true
27
28         page = entry({"mini", "network", "upnp"}, cbi("upnp/upnpmini", {autoapply=true}), _("UPNP"))
29         page.dependent = true
30
31         entry({"admin", "services", "upnp", "status"}, call("act_status")).leaf = true
32         entry({"admin", "services", "upnp", "delete"}, call("act_delete")).leaf = true
33 end
34
35 function act_status()
36         local ipt = io.popen("iptables --line-numbers -t nat -xnvL MINIUPNPD")
37         if ipt then
38                 local fwd = { }
39                 while true do
40                         local ln = ipt:read("*l")
41                         if not ln then
42                                 break
43                         elseif ln:match("^%d+") then
44                                 local num, proto, extport, intaddr, intport =
45                                         ln:match("^(%d+).-([a-z]+).-dpt:(%d+) to:(%S-):(%d+)")
46
47                                 if num and proto and extport and intaddr and intport then
48                                         num     = tonumber(num)
49                                         extport = tonumber(extport)
50                                         intport = tonumber(intport)
51
52                                         fwd[#fwd+1] = {
53                                                 num     = num,
54                                                 proto   = proto:upper(),
55                                                 extport = extport,
56                                                 intaddr = intaddr,
57                                                 intport = intport
58                                         }
59                                 end
60                         end
61                 end
62
63                 ipt:close()
64
65                 luci.http.prepare_content("application/json")
66                 luci.http.write_json(fwd)
67         end
68 end
69
70 function act_delete(idx)
71         idx = tonumber(idx)
72         if idx and idx > 0 then
73                 luci.sys.call("iptables -t filter -D MINIUPNPD %d 2>/dev/null" % idx)
74                 luci.sys.call("iptables -t nat -D MINIUPNPD %d 2>/dev/null" % idx)
75                 return
76         end
77
78         luci.http.status(400, "Bad request")
79 end