diff --git a/geoblock.go b/geoblock.go index a47a569..637f827 100644 --- a/geoblock.go +++ b/geoblock.go @@ -18,6 +18,13 @@ import ( "time" ) +const ( + // CountryUnknown represents an unknown country code + CountryUnknown = "UNKNOWN" + // DefaultActionAllow represents the default allow action + DefaultActionAllow = "allow" +) + // Config holds the plugin configuration type Config struct { AllowedCountries []string `json:"allowedCountries,omitempty"` @@ -49,7 +56,7 @@ func CreateConfig() *Config { DatabaseURL: "", DatabasePath: "/tmp/ipinfo_lite.json", CacheDuration: 60, - DefaultAction: "allow", + DefaultAction: DefaultActionAllow, BlockMessage: "Access denied from your country", BlockPageTitle: "Access Denied", BlockPageBody: "", @@ -159,12 +166,6 @@ type prometheusMetrics struct { counters map[string]int64 // key: "country|organization|action" } -type prometheusMetricKey struct { - Country string - Organization string - Action string -} - // New creates a new GeoBlock plugin func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) { if config.QueryURL == "" { @@ -179,8 +180,8 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h config.CacheDuration = 60 } - if config.DefaultAction != "allow" && config.DefaultAction != "block" { - config.DefaultAction = "allow" + if config.DefaultAction != DefaultActionAllow && config.DefaultAction != "block" { + config.DefaultAction = DefaultActionAllow } if config.BlockMessage == "" { @@ -269,7 +270,7 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h func (g *GeoBlock) ServeHTTP(rw http.ResponseWriter, req *http.Request) { // Check if this is a Prometheus metrics request if g.config.PrometheusMetricsPath != "" && req.URL.Path == g.config.PrometheusMetricsPath { - g.servePrometheusMetrics(rw, req) + g.servePrometheusMetrics(rw) return } @@ -286,8 +287,8 @@ func (g *GeoBlock) ServeHTTP(rw http.ResponseWriter, req *http.Request) { } // On error, apply default action if g.config.DefaultAction == "block" { - g.blockRequest(rw, "UNKNOWN", "") - g.recordMetrics("UNKNOWN", "", "blocked") + g.blockRequest(rw, CountryUnknown, "") + g.recordMetrics(CountryUnknown, "", "blocked") return } g.next.ServeHTTP(rw, req) @@ -354,7 +355,7 @@ func (g *GeoBlock) getGeoInfo(ip string) (*geoInfo, error) { // Use local database if available if g.localDB != nil && len(g.localDB.ranges) > 0 { country := g.lookupLocalDatabase(ip) - if country != "" && country != "UNKNOWN" { + if country != "" && country != CountryUnknown { info = &geoInfo{Country: country, Organization: ""} // Try to get organization from API if apiInfo, apiErr := g.queryGeoIP(ip); apiErr == nil { @@ -415,7 +416,7 @@ func (g *GeoBlock) queryGeoIP(ip string) (*geoInfo, error) { if g.config.LogBlocked { fmt.Printf("[GeoBlock] Warning: Could not extract country from API response. Raw response: %s\n", string(body)) } - return &geoInfo{Country: "UNKNOWN", Organization: ""}, nil + return &geoInfo{Country: CountryUnknown, Organization: ""}, nil } // Extract organization information @@ -512,70 +513,21 @@ func (g *GeoBlock) generateBlockPage(country string) string { // If custom body is provided, use it if body != "" { - return fmt.Sprintf(` + return g.generateCustomBlockPage(title, message, body, country) + } + + // Default block page + return g.generateDefaultBlockPage(title, message, country) +} + +func (g *GeoBlock) generateCustomBlockPage(title, message, body, country string) string { + return fmt.Sprintf(`