1 -- Copyright 2018-2019 Dirk Brenken (dev@brenken.org)
2 -- This is free software, licensed under the Apache License, Version 2.0
4 module("luci.controller.banip", package.seeall)
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()
13 if not nixio.fs.access("/etc/config/banip") then
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
32 function ban_action(name)
33 if name == "do_reload" then
34 luci.sys.call("/etc/init.d/banip reload >/dev/null 2>&1")
36 luci.http.prepare_content("text/plain")
40 function status_update()
44 rt_file = uci:get("banip", "global", "ban_rtfile") or "/tmp/ban_runtime.json"
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)
54 local content = util.trim(util.exec("logread -e 'banIP-' 2>/dev/null")) or ""
57 content = "No banIP related logs yet!"
62 function ipset_view(ipset, filter)
65 if not (ipset or filter) then
69 if filter == "false" then
70 content = util.trim(util.exec("ipset -L " .. ipset .. " 2>/dev/null"))
72 content = util.trim(util.exec("ipset -L " .. ipset .. " 2>/dev/null | grep -e 'packets [1-9]\\|^[A-Z]'"))
76 content = "IPSet is empty!"
81 function ripe_view(query, input)
84 if not (query or input) then
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"))
91 content = "No response!"