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