Merge pull request #1735 from sumpfralle/olsr-jsoninfo-parser-handle-empty-result
[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_refresh" then
34                 luci.sys.call("/etc/init.d/banip refresh >/dev/null 2>&1")
35         elseif name == "do_reload" then
36                 luci.sys.call("/etc/init.d/banip reload >/dev/null 2>&1")
37         end
38         luci.http.prepare_content("text/plain")
39         luci.http.write("0")
40 end
41
42 function status_update()
43         local rt_file
44         local content
45
46         rt_file = uci:get("banip", "global", "ban_rtfile") or "/tmp/ban_runtime.json"
47
48         if nixio.fs.access(rt_file) then
49                 content = json.parse(nixio.fs.readfile(rt_file) or "")
50                 http.prepare_content("application/json")
51                 http.write_json(content)
52         end
53 end
54
55 function log_view()
56         local content = util.trim(util.exec("logread -e 'banIP-' 2>/dev/null")) or ""
57
58         if content == "" then
59                 content = "No banIP related logs yet!"
60         end
61         http.write(content)
62 end
63
64 function ipset_view(ipset, filter)
65         local content
66
67         if not (ipset or filter) then
68                 return
69         end
70
71         if filter == "false" then
72                 content = util.trim(util.exec("ipset -L " .. ipset .. " 2>/dev/null"))
73         else
74                 content = util.trim(util.exec("ipset -L " .. ipset .. " 2>/dev/null | grep -e 'packets [1-9]\\|^[A-Z]'"))
75         end
76
77         if content == "" then
78                 content = "IPSet is empty!"
79         end
80         http.write(content)
81 end
82
83 function ripe_view(query, input)
84         local content
85
86         if not (query or input) then
87                 return
88         end
89
90         content = util.trim(util.exec("uclient-fetch --no-check-certificate -O- https://stat.ripe.net/data/" ..query.. "/data.json?resource=" ..input.. " 2>/dev/null"))
91         
92         if content == "" then
93                 content = "No response!"
94         end
95         http.write(content)
96 end