mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2025-12-13 17:10:36 +00:00
Fixed linting issues
This commit is contained in:
parent
8d123e47bd
commit
545856f8a0
@ -6,15 +6,17 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/bitechdev/ResolveSpec/pkg/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
// IPBlacklist provides IP blocking functionality
|
// IPBlacklist provides IP blocking functionality
|
||||||
type IPBlacklist struct {
|
type IPBlacklist struct {
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
ips map[string]bool // Individual IPs
|
ips map[string]bool // Individual IPs
|
||||||
cidrs []*net.IPNet // CIDR ranges
|
cidrs []*net.IPNet // CIDR ranges
|
||||||
reason map[string]string
|
reason map[string]string
|
||||||
useProxy bool // Whether to check X-Forwarded-For headers
|
useProxy bool // Whether to check X-Forwarded-For headers
|
||||||
}
|
}
|
||||||
|
|
||||||
// BlacklistConfig configures the IP blacklist
|
// BlacklistConfig configures the IP blacklist
|
||||||
@ -92,7 +94,7 @@ func (bl *IPBlacklist) UnblockCIDR(cidr string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// IsBlocked checks if an IP is blacklisted
|
// IsBlocked checks if an IP is blacklisted
|
||||||
func (bl *IPBlacklist) IsBlocked(ip string) (bool, string) {
|
func (bl *IPBlacklist) IsBlocked(ip string) (blacklist bool, reason string) {
|
||||||
bl.mu.RLock()
|
bl.mu.RLock()
|
||||||
defer bl.mu.RUnlock()
|
defer bl.mu.RUnlock()
|
||||||
|
|
||||||
@ -178,7 +180,10 @@ func (bl *IPBlacklist) Middleware(next http.Handler) http.Handler {
|
|||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusForbidden)
|
w.WriteHeader(http.StatusForbidden)
|
||||||
json.NewEncoder(w).Encode(response)
|
err := json.NewEncoder(w).Encode(response)
|
||||||
|
if err != nil {
|
||||||
|
logger.Debug("Failed to write blacklist response: %v", err)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,13 +197,16 @@ func (bl *IPBlacklist) StatsHandler() http.Handler {
|
|||||||
ips, cidrs := bl.GetBlacklist()
|
ips, cidrs := bl.GetBlacklist()
|
||||||
|
|
||||||
stats := map[string]interface{}{
|
stats := map[string]interface{}{
|
||||||
"blocked_ips": ips,
|
"blocked_ips": ips,
|
||||||
"blocked_cidrs": cidrs,
|
"blocked_cidrs": cidrs,
|
||||||
"total_ips": len(ips),
|
"total_ips": len(ips),
|
||||||
"total_cidrs": len(cidrs),
|
"total_cidrs": len(cidrs),
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(stats)
|
err := json.NewEncoder(w).Encode(stats)
|
||||||
|
if err != nil {
|
||||||
|
logger.Debug("Failed to encode stats: %v", err)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/bitechdev/ResolveSpec/pkg/logger"
|
||||||
"golang.org/x/time/rate"
|
"golang.org/x/time/rate"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -113,10 +114,10 @@ func (rl *RateLimiter) MiddlewareWithKeyFunc(keyFunc func(*http.Request) string)
|
|||||||
|
|
||||||
// RateLimitInfo contains information about a specific IP's rate limit status
|
// RateLimitInfo contains information about a specific IP's rate limit status
|
||||||
type RateLimitInfo struct {
|
type RateLimitInfo struct {
|
||||||
IP string `json:"ip"`
|
IP string `json:"ip"`
|
||||||
TokensRemaining float64 `json:"tokens_remaining"`
|
TokensRemaining float64 `json:"tokens_remaining"`
|
||||||
Limit float64 `json:"limit"`
|
Limit float64 `json:"limit"`
|
||||||
Burst int `json:"burst"`
|
Burst int `json:"burst"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetTrackedIPs returns all IPs currently being tracked by the rate limiter
|
// GetTrackedIPs returns all IPs currently being tracked by the rate limiter
|
||||||
@ -140,18 +141,18 @@ func (rl *RateLimiter) GetRateLimitInfo(ip string) *RateLimitInfo {
|
|||||||
if !exists {
|
if !exists {
|
||||||
// Return default info for untracked IP
|
// Return default info for untracked IP
|
||||||
return &RateLimitInfo{
|
return &RateLimitInfo{
|
||||||
IP: ip,
|
IP: ip,
|
||||||
TokensRemaining: float64(rl.burst),
|
TokensRemaining: float64(rl.burst),
|
||||||
Limit: float64(rl.rate),
|
Limit: float64(rl.rate),
|
||||||
Burst: rl.burst,
|
Burst: rl.burst,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &RateLimitInfo{
|
return &RateLimitInfo{
|
||||||
IP: ip,
|
IP: ip,
|
||||||
TokensRemaining: limiter.Tokens(),
|
TokensRemaining: limiter.Tokens(),
|
||||||
Limit: float64(rl.rate),
|
Limit: float64(rl.rate),
|
||||||
Burst: rl.burst,
|
Burst: rl.burst,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,7 +176,10 @@ func (rl *RateLimiter) StatsHandler() http.Handler {
|
|||||||
if ip := r.URL.Query().Get("ip"); ip != "" {
|
if ip := r.URL.Query().Get("ip"); ip != "" {
|
||||||
info := rl.GetRateLimitInfo(ip)
|
info := rl.GetRateLimitInfo(ip)
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(info)
|
err := json.NewEncoder(w).Encode(info)
|
||||||
|
if err != nil {
|
||||||
|
logger.Debug("Failed to encode json: %v", err)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -186,13 +190,16 @@ func (rl *RateLimiter) StatsHandler() http.Handler {
|
|||||||
"total_tracked_ips": len(allInfo),
|
"total_tracked_ips": len(allInfo),
|
||||||
"rate_limit_config": map[string]interface{}{
|
"rate_limit_config": map[string]interface{}{
|
||||||
"requests_per_second": float64(rl.rate),
|
"requests_per_second": float64(rl.rate),
|
||||||
"burst": rl.burst,
|
"burst": rl.burst,
|
||||||
},
|
},
|
||||||
"tracked_ips": allInfo,
|
"tracked_ips": allInfo,
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(stats)
|
err := json.NewEncoder(w).Encode(stats)
|
||||||
|
if err != nil {
|
||||||
|
logger.Debug("Failed to encode json: %v", err)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user