- Added country of origin row for quickban list
This commit is contained in:
Toby Chui 2025-02-28 22:01:11 +08:00
parent 53657e8716
commit 3993ac954c
3 changed files with 60 additions and 3 deletions

View File

@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"net/http"
"sort"
"strings"
"github.com/google/uuid"
@ -545,3 +546,39 @@ func handleWhitelistEnable(w http.ResponseWriter, r *http.Request) {
utils.SendOK(w)
}
}
// List all quick ban ip address
func handleListQuickBan(w http.ResponseWriter, r *http.Request) {
currentSummary := statisticCollector.GetCurrentDailySummary()
type quickBanEntry struct {
IpAddr string
Count int
CountryCode string
}
result := []quickBanEntry{}
currentSummary.RequestClientIp.Range(func(key, value interface{}) bool {
ip := key.(string)
count := value.(int)
thisEntry := quickBanEntry{
IpAddr: ip,
Count: count,
}
//Get the country code
geoinfo, err := geodbStore.ResolveCountryCodeFromIP(ip)
if err == nil {
thisEntry.CountryCode = geoinfo.CountryIsoCode
}
result = append(result, thisEntry)
return true
})
//Sort result based on count
sort.Slice(result, func(i, j int) bool {
return result[i].Count > result[j].Count
})
js, _ := json.Marshal(result)
utils.SendJSONResponse(w, string(js))
}

View File

@ -114,6 +114,9 @@ func RegisterAccessRuleAPIs(authRouter *auth.RouterDef) {
authRouter.HandleFunc("/api/whitelist/ip/add", handleIpWhitelistAdd)
authRouter.HandleFunc("/api/whitelist/ip/remove", handleIpWhitelistRemove)
authRouter.HandleFunc("/api/whitelist/enable", handleWhitelistEnable)
/* Quick Ban List */
authRouter.HandleFunc("/api/quickban/list", handleListQuickBan)
}
// Register the APIs for path blocking rules management functions, WIP

View File

@ -694,6 +694,7 @@
<tr>
<th>IP</th>
<th>Access Count</th>
<th>Country of Origin</th>
<th>Blacklist</th>
</tr>
</thead>
@ -1489,15 +1490,30 @@
//Load the summary to ip access table
function initBlacklistQuickBanTable(){
$.get("/api/stats/summary", function(data){
initIpAccessTable(data.RequestClientIp);
$.get("/api/quickban/list", function(data){
//Convert the data to a dictionary
var ipAccessCounts = {};
access_ip_country_map = {};
data.forEach(function(entry){
ipAccessCounts[entry.IpAddr] = entry.Count
access_ip_country_map[entry.IpAddr] = entry.CountryCode;
});
initIpAccessTable(ipAccessCounts);
})
}
initBlacklistQuickBanTable();
function getCountryISOFromQuickBan(ip){
if (access_ip_country_map[ip] === "") {
return "LAN / Reserved";
}
return access_ip_country_map[ip];
}
var blacklist_entriesPerPage = 30;
var blacklist_currentPage = 1;
var blacklist_totalPages = 0;
var access_ip_country_map = {};
function initIpAccessTable(ipAccessCounts){
blacklist_totalPages = Math.ceil(Object.keys(ipAccessCounts).length / blacklist_entriesPerPage);
@ -1533,6 +1549,7 @@
var row = $("<tr>").appendTo(tableBody);
$("<td>").text(ip).appendTo(row);
$("<td>").text(accessCount).appendTo(row);
$("<td>").text(getCountryISOFromQuickBan(ip)).appendTo(row);
if (ipInBlacklist(ip)){
$("<td>").html(`<button class="ui basic green tiny icon button" title"Unban IP" onclick="handleUnban('${ip}');"><i class="green check icon"></i></button>`).appendTo(row);
}else{
@ -1542,7 +1559,7 @@
if (slicedEntries.length == 0){
var row = $("<tr>").appendTo(tableBody);
$("<td colspan='3'>").html(`
$("<td colspan='4'>").html(`
<i class="ui green circle check icon"></i> There are no HTTP requests recorded today
`).appendTo(row);