luci-app-olsr: handle empty result for non-status tables
[oweals/luci.git] / applications / luci-app-banip / luasrc / controller / banip.lua
1 -- Copyright 2018-2019 Dirk Brenken (dev@brenken.org)
2 -- This is free software, licensed under the Apache License, Version 2.0
3
4 module("luci.controller.banip", package.seeall)
5
6 local util  = require("luci.util")
7 local http  = require("luci.http")
8 local i18n  = require("luci.i18n")
9 local json  = require("luci.jsonc")
10 local uci   = require("luci.model.uci").cursor()
11
12 function index()
13         if not nixio.fs.access("/etc/config/banip") then
14                 return
15         end
16         entry({"admin", "services", "banip"}, firstchild(), _("banIP"), 40).dependent = false
17         entry({"admin", "services", "banip", "tab_from_cbi"}, cbi("banip/overview_tab", {hideresetbtn=true, hidesavebtn=true}), _("Overview"), 10).leaf = true
18         entry({"admin", "services", "banip", "ipset"}, template("banip/ipsetview"), _("IPSet-Lookup"), 20).leaf = true
19         entry({"admin", "services", "banip", "ripe"}, template("banip/ripeview"), _("RIPE-Lookup"), 30).leaf = true
20         entry({"admin", "services", "banip", "log"}, template("banip/logview"), _("View Logfile"), 40).leaf = true
21         entry({"admin", "services", "banip", "advanced"}, firstchild(), _("Advanced"), 100)
22         entry({"admin", "services", "banip", "advanced", "blacklist"}, form("banip/blacklist_tab"), _("Edit Blacklist"), 110).leaf = true
23         entry({"admin", "services", "banip", "advanced", "whitelist"}, form("banip/whitelist_tab"), _("Edit Whitelist"), 120).leaf = true
24         entry({"admin", "services", "banip", "advanced", "configuration"}, form("banip/configuration_tab"), _("Edit Configuration"), 130).leaf = true
25         entry({"admin", "services", "banip", "ipsetview"}, call("ipset_view"), nil).leaf = true
26         entry({"admin", "services", "banip", "ripeview"}, call("ripe_view"), nil).leaf = true
27         entry({"admin", "services", "banip", "logview"}, call("log_view"), nil).leaf = true
28         entry({"admin", "services", "banip", "status"}, call("status_update"), nil).leaf = true
29         entry({"admin", "services", "banip", "action"}, call("ban_action"), nil).leaf = true
30 end
31
32 function ban_action(name)
33         if name == "do_reload" then
34                 luci.sys.call("/etc/init.d/banip reload >/dev/null 2>&1")
35         end
36         luci.http.prepare_content("text/plain")
37         luci.http.write("0")
38 end
39
40 function status_update()
41         local rt_file
42         local content
43
44         rt_file = uci:get("banip", "global", "ban_rtfile") or "/tmp/ban_runtime.json"
45
46         if nixio.fs.access(rt_file) then
47                 content = json.parse(nixio.fs.readfile(rt_file) or "")
48                 http.prepare_content("application/json")
49                 http.write_json(content)
50         end
51 end
52
53 function log_view()
54         local content = util.trim(util.exec("logread -e 'banIP-' 2>/dev/null")) or ""
55
56         if content == "" then
57                 content = "No banIP related logs yet!"
58         end
59         http.write(content)
60 end
61
62 function ipset_view(ipset, filter)
63         local content
64
65         if not (ipset or filter) then
66                 return
67         end
68
69         if filter == "false" then
70                 content = util.trim(util.exec("ipset -L " .. ipset .. " 2>/dev/null"))
71         else
72                 content = util.trim(util.exec("ipset -L " .. ipset .. " 2>/dev/null | grep -e 'packets [1-9]\\|^[A-Z]'"))
73         end
74
75         if content == "" then
76                 content = "IPSet is empty!"
77         end
78         http.write(content)
79 end
80
81 function ripe_view(query, input)
82         local content
83
84         if not (query or input) then
85                 return
86         end
87
88         content = util.trim(util.exec("uclient-fetch --no-check-certificate -O- https://stat.ripe.net/data/" ..query.. "/data.json?resource=" ..input.. " 2>/dev/null"))
89         
90         if content == "" then
91                 content = "No response!"
92         end
93         http.write(content)
94 end