diff --git a/src/accesslist.go b/src/accesslist.go index 2df35d7..6cdb34e 100644 --- a/src/accesslist.go +++ b/src/accesslist.go @@ -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)) +} diff --git a/src/api.go b/src/api.go index 7afa247..112d5b0 100644 --- a/src/api.go +++ b/src/api.go @@ -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 diff --git a/src/web/components/access.html b/src/web/components/access.html index 6a59453..4ed89fa 100644 --- a/src/web/components/access.html +++ b/src/web/components/access.html @@ -694,6 +694,7 @@ IP Access Count + Country of Origin Blacklist @@ -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 = $("").appendTo(tableBody); $("").text(ip).appendTo(row); $("").text(accessCount).appendTo(row); + $("").text(getCountryISOFromQuickBan(ip)).appendTo(row); if (ipInBlacklist(ip)){ $("").html(``).appendTo(row); }else{ @@ -1542,7 +1559,7 @@ if (slicedEntries.length == 0){ var row = $("").appendTo(tableBody); - $("").html(` + $("").html(` There are no HTTP requests recorded today `).appendTo(row);