Translated using Weblate (Japanese)
[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
17         local e = entry({"admin", "services", "banip"}, firstchild(), _("banIP"), 40)
18         e.dependent = false
19         e.acl_depends = { "luci-app-banip" }
20
21         entry({"admin", "services", "banip", "tab_from_cbi"}, cbi("banip/overview_tab", {hideresetbtn=true, hidesavebtn=true}), _("Overview"), 10).leaf = true
22         entry({"admin", "services", "banip", "ipset"}, template("banip/ipsetview"), _("IPSet-Lookup"), 20).leaf = true
23         entry({"admin", "services", "banip", "ripe"}, template("banip/ripeview"), _("RIPE-Lookup"), 30).leaf = true
24         entry({"admin", "services", "banip", "log"}, template("banip/logview"), _("View Logfile"), 40).leaf = true
25         entry({"admin", "services", "banip", "advanced"}, firstchild(), _("Advanced"), 100)
26         entry({"admin", "services", "banip", "advanced", "blacklist"}, form("banip/blacklist_tab"), _("Edit Blacklist"), 110).leaf = true
27         entry({"admin", "services", "banip", "advanced", "whitelist"}, form("banip/whitelist_tab"), _("Edit Whitelist"), 120).leaf = true
28         entry({"admin", "services", "banip", "advanced", "configuration"}, form("banip/configuration_tab"), _("Edit Configuration"), 130).leaf = true
29         entry({"admin", "services", "banip", "ipsetview"}, call("ipset_view"), nil).leaf = true
30         entry({"admin", "services", "banip", "ripeview"}, call("ripe_view"), nil).leaf = true
31         entry({"admin", "services", "banip", "logview"}, call("log_view"), nil).leaf = true
32         entry({"admin", "services", "banip", "status"}, call("status_update"), nil).leaf = true
33         entry({"admin", "services", "banip", "action"}, call("ban_action"), nil).leaf = true
34 end
35
36 function ban_action(name)
37         if name == "do_refresh" then
38                 luci.sys.call("/etc/init.d/banip refresh >/dev/null 2>&1")
39         elseif name == "do_reload" then
40                 luci.sys.call("/etc/init.d/banip reload >/dev/null 2>&1")
41         end
42         luci.http.prepare_content("text/plain")
43         luci.http.write("0")
44 end
45
46 function status_update()
47         local rt_file
48         local content
49
50         rt_file = uci:get("banip", "global", "ban_rtfile") or "/tmp/ban_runtime.json"
51
52         if nixio.fs.access(rt_file) then
53                 content = json.parse(nixio.fs.readfile(rt_file) or "")
54                 http.prepare_content("application/json")
55                 http.write_json(content)
56         end
57 end
58
59 function log_view()
60         local content = util.trim(util.exec("logread -e 'banIP-' 2>/dev/null")) or ""
61
62         if content == "" then
63                 content = "No banIP related logs yet!"
64         end
65         http.write(content)
66 end
67
68 function ipset_view(ipset, filter)
69         local content
70
71         if not (ipset or filter) then
72                 return
73         end
74
75         if filter == "false" then
76                 content = util.trim(util.exec("ipset -L " .. ipset .. " 2>/dev/null"))
77         else
78                 content = util.trim(util.exec("ipset -L " .. ipset .. " 2>/dev/null | grep -e 'packets [1-9]\\|^[A-Z]'"))
79         end
80
81         if content == "" then
82                 content = "IPSet is empty!"
83         end
84         http.write(content)
85 end
86
87 function ripe_view(query, input)
88         local content
89
90         if not (query or input) then
91                 return
92         end
93
94         content = util.trim(util.exec("uclient-fetch --no-check-certificate -O- https://stat.ripe.net/data/" ..query.. "/data.json?resource=" ..input.. " 2>/dev/null"))
95         
96         if content == "" then
97                 content = "No response!"
98         end
99         http.write(content)
100 end