Documentation
¶
Index ¶
- func NewHandler(handler http.Handler, allow, deny Contains, opts ...Option) http.Handler
- func RemoteAddrExtractor(r *http.Request) (string, netip.Addr, error)
- func XForwardedForExtractor(r *http.Request) (string, netip.Addr, error)
- type ACL
- type AddressExtractor
- type Config
- type Contains
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewHandler ¶
NewHandler creates a new http.Handler that enforces allow and deny ACLs. The deny ACL takes precedence over the allow ACL. If no ACLs are supplied then the handler allows all requests. If the remote IP cannot be determined or parsed then the request is denied. If the request's remote IP address is not allowed by the ACL, a 403 Forbidden response is returned, otherwise the request is passed to the given handler.
func RemoteAddrExtractor ¶
RemoteAddrExtractor returns the remote IP address from an HTTP request. It is the default AddressExtractor and is suitable for when a server is directly exposed to the internet.
Types ¶
type ACL ¶
type ACL struct {
// contains filtered or unexported fields
}
ACL represents an IP address access control list.
type AddressExtractor ¶
AddressExtractor represents a function that extracts an IP address from an HTTP request.
type Config ¶
type Config struct {
Addresses []string `yaml:"addresses" cmd:"list of ip addresses or cidr prefixes"`
Direct bool `yaml:"direct" cmd:"set to true to use the requests.RemoteAddr"` // Use the requests.RemoteAddr
Proxy bool `yaml:"proxy" cmd:"set to true to use the X-Forwarded-For header"` // Use the X-Forwarded-For header
}
Config represents an IP address access control list configuration.
func (Config) AddressExtractor ¶
func (c Config) AddressExtractor() (AddressExtractor, error)
AddressExtractor returns an Option that sets the AddressExtractor.
type Contains ¶
Contains represents a function that returns whether the given IP address is in the ACL.
type Option ¶
type Option func(o *options)
Option represents an option for NewACLHandler.
func WithAddressExtractor ¶
func WithAddressExtractor(extractor AddressExtractor) Option
WithAddressExtractor returns an Option that sets the AddressExtractor.
func WithCounters ¶
func WithCounters(deniedCounter, notAllowedCounter, errorCounter webapp.CounterInc) Option
WithCounters returns an Option that sets three Counters: 1. one that is incremented when a request is denied because the IP address is in the deny ACL 2. one that is incremented if the address is not in the allow ACL 3. one that is incremented on error