Documentation
¶
Index ¶
- func DefaultBlockingResponse(c fox.Context)
- func Middleware(db *geoip2.Reader, opts ...Option) fox.MiddlewareFunc
- type Filter
- type IPFilter
- type Option
- func WithBlacklistedCountries(codes ...string) Option
- func WithClientIPResolver(resolver fox.ClientIPResolver) Option
- func WithFilter(f ...Filter) Option
- func WithLogHandler(handler slog.Handler) Option
- func WithResponse(handler fox.HandlerFunc) Option
- func WithWhitelistedCountries(codes ...string) Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultBlockingResponse ¶
DefaultBlockingResponse is the default response for blocked IPs. It responds with a 403 Forbidden http status.
func Middleware ¶
func Middleware(db *geoip2.Reader, opts ...Option) fox.MiddlewareFunc
Middleware creates a middleware function for the IP filter. The middleware is intended to work with MaxMind GeoLite2 or GeoIP2 databases. It should work with other MMDB databases but has not been tested. Note that blacklist and whitelist options are mutually exclusive. Either it is a whitelist, and all requests are denied except for IPs that have a country code associated in the whitelist, OR it is a blacklist, and all requests are allowed except IPs that have a country code associated in the blacklist.
Types ¶
type IPFilter ¶
type IPFilter struct {
// contains filtered or unexported fields
}
func New ¶
New creates a new IPFilter with the provided GeoIP2 reader and options. The ip filter is intended to work with MaxMind GeoLite2 or GeoIP2 databases. It should work with other MMDB databases but has not been tested. Note that blacklist and whitelist options are mutually exclusive. Either it is a whitelist, and all requests are denied except for IPs that have a country code associated in the whitelist, OR it is a blacklist, and all requests are allowed except IPs that have a country code associated in the blacklist.
func (*IPFilter) Allowed ¶
Allowed checks if the given IP address is allowed based on the filter's configuration.
func (*IPFilter) FilterIP ¶
func (f *IPFilter) FilterIP(next fox.HandlerFunc) fox.HandlerFunc
FilterIP is a middleware function that filters requests based on the IP address.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
func WithBlacklistedCountries ¶
WithBlacklistedCountries sets the blacklist with the provided country codes. It clears any existing whitelist. Requests from countries in the blacklist will be denied.
func WithClientIPResolver ¶ added in v0.19.0
func WithClientIPResolver(resolver fox.ClientIPResolver) Option
WithClientIPResolver sets a custom resolver to determine the client IP address. This is for advanced use case, you should configure the resolver with Fox's router option using fox.WithClientIPResolver.
func WithFilter ¶
WithFilter appends the provided filters to the middleware's filter list. A filter returning true will exclude the request from using the ip filter handler. If no filters are provided, all requests will be handled. Keep in mind that filters are invoked for each request, so they should be simple and efficient.
func WithLogHandler ¶
WithLogHandler sets a custom log handler for structured logging.
func WithResponse ¶
func WithResponse(handler fox.HandlerFunc) Option
WithResponse sets a custom response handler for blocked requests.
func WithWhitelistedCountries ¶
WithWhitelistedCountries sets the whitelist with the provided country codes. It clears any existing blacklist. Requests from countries not in the whitelist will be denied.