Documentation
¶
Index ¶
- Variables
- func New(opts ...Option) xun.Middleware
- func ParseIPNet(n string) *net.IPNet
- type CountryRule
- type Model
- type Option
- func AllowCountries(countries ...string) Option
- func AllowHosts(hosts ...string) Option
- func AllowIPNets(nets ...string) Option
- func DenyCountries(countries ...string) Option
- func DenyIPNets(nets ...string) Option
- func WithConfig(file string) Option
- func WithHostRedirect(u string, code int) Option
- func WithHostWhitelist(paths ...string) Option
- func WithLookupFunc(fn func(string) string) Option
- func WithViewer(v string) Option
- type Options
- type Section
Constants ¶
This section is empty.
Variables ¶
var ( AllIPv4 = ParseIPNet("0.0.0.0/0") AllIPv6 = ParseIPNet("::/0") )
var ErrInvalidRemoteAddr = errors.New("acl: invalid_remote_addr")
var (
Logger = log.Default()
)
var (
ReloadInterval = 1 * time.Minute
)
Functions ¶
func New ¶
func New(opts ...Option) xun.Middleware
New returns a new ACL middleware that applies access rules based on the provided options. It dynamically reloads rules if a configuration file is specified, enabling real-time updates.
func ParseIPNet ¶
ParseIPNet parses the given IP address and returns its IPNet. If the IP address does not have a netmask, the default netmask is used.
Types ¶
type CountryRule ¶
CountryRule represents a rule for managing country-based access control. It includes a map of country codes and a flag indicating if any country is allowed.
func (*CountryRule) Has ¶
func (cr *CountryRule) Has(name string) bool
type Option ¶
type Option func(o *Options)
func AllowCountries ¶
AllowCountries allow countries
func WithConfig ¶
WithConfig use the config file to load the options. It will watch the file and reload the options automatically. After WithConfig, all options will be discard, and overwritten by the config file Example:
[allow_hosts] yaitoo.cn www.yaitoo.cn [allow_ipnets] 89.207.132.170/24 ::1 [deny_ipnets] 0.0.0.0/0 [allow_countries] CN [deny_countries] * [host_redirect] url=http://abc.com status_code=301
func WithHostRedirect ¶
WithHostRedirect sets the redirect URL and status code for host redirection. It configures the Options to redirect requests to the specified URL with the given status code.
func WithHostWhitelist ¶ added in v1.1.3
WithHostWhitelist sets the whitelist for AllowHosts,allowing specific paths to bypass host checking.
func WithLookupFunc ¶
WithLookupFunc setup custom lookup function to get country by ip address
func WithViewer ¶
WithViewer render the viewer to current visitor when he is denied
type Options ¶
type Options struct {
AllowHosts map[string]struct{}
HostRedirectURL string
HostRedirectStatusCode int
HostWhitelist []string
AllowIPNets []*net.IPNet
DenyIPNets []*net.IPNet
AllowCountries *CountryRule
DenyCountries *CountryRule
LookupFunc func(ip string) string
ViewerName string
Config string
}
Options represents the configuration options for the ACL middleware. It includes various settings such as allowed hosts, IP networks, countries, and a lookup function for determining the country of an IP address.
func NewOptions ¶
func NewOptions() *Options
NewOptions creates and returns a new instance of Options with default values. It initializes the necessary fields, including maps and slices, required for the ACL middleware configuration.