acl

package
v1.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 31, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	AllIPv4 = ParseIPNet("0.0.0.0/0")
	AllIPv6 = ParseIPNet("::/0")
)
View Source
var (
	Logger = log.Default()
)
View Source
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

func ParseIPNet(n string) *net.IPNet

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

type CountryRule struct {
	Items  map[string]struct{}
	HasAny bool
}

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 Model

type Model struct {
	Host    string
	IP      string
	Country string
}

type Option

type Option func(o *Options)

func AllowCountries

func AllowCountries(countries ...string) Option

AllowCountries allow countries

func AllowHosts

func AllowHosts(hosts ...string) Option

AllowHosts allow the hosts

func AllowIPNets

func AllowIPNets(nets ...string) Option

AllowIPNets allow IPNets

func DenyCountries

func DenyCountries(countries ...string) Option

DenyCountries deny countries

func DenyIPNets

func DenyIPNets(nets ...string) Option

DenyIPNets deny IPNets

func WithConfig

func WithConfig(file string) Option

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

func WithHostRedirect(u string, code int) Option

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

func WithHostWhitelist(paths ...string) Option

WithHostWhitelist sets the whitelist for AllowHosts,allowing specific paths to bypass host checking.

func WithLookupFunc

func WithLookupFunc(fn func(string) string) Option

WithLookupFunc setup custom lookup function to get country by ip address

func WithViewer

func WithViewer(v string) Option

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.

type Section

type Section int
const (
	SectionNA Section = iota // none
	SectionAH                // allow_hosts
	SectionAN                // allow_ipnets
	SectionDN                // deny_ipnets
	SectionAC                // allow_countries
	SectionDC                // deny_countries
	SectionWL                // host_whitelist
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL