mirror of
https://github.com/TecharoHQ/anubis.git
synced 2026-04-25 09:32:43 +00:00
refactor: move ErrMisconfiguration to top level
Signed-off-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
package anubis
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrMisconfiguration = errors.New("[unexpected] policy: administrator misconfiguration")
|
||||||
|
)
|
||||||
@@ -8,15 +8,12 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/TecharoHQ/anubis"
|
||||||
"github.com/TecharoHQ/anubis/internal"
|
"github.com/TecharoHQ/anubis/internal"
|
||||||
"github.com/TecharoHQ/anubis/lib/policy/checker"
|
"github.com/TecharoHQ/anubis/lib/policy/checker"
|
||||||
"github.com/gaissmai/bart"
|
"github.com/gaissmai/bart"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
ErrMisconfiguration = errors.New("[unexpected] policy: administrator misconfiguration")
|
|
||||||
)
|
|
||||||
|
|
||||||
type RemoteAddrChecker struct {
|
type RemoteAddrChecker struct {
|
||||||
prefixTable *bart.Lite
|
prefixTable *bart.Lite
|
||||||
hash string
|
hash string
|
||||||
@@ -28,7 +25,7 @@ func NewRemoteAddrChecker(cidrs []string) (checker.Impl, error) {
|
|||||||
for _, cidr := range cidrs {
|
for _, cidr := range cidrs {
|
||||||
prefix, err := netip.ParsePrefix(cidr)
|
prefix, err := netip.ParsePrefix(cidr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%w: range %s not parsing: %w", ErrMisconfiguration, cidr, err)
|
return nil, fmt.Errorf("%w: range %s not parsing: %w", anubis.ErrMisconfiguration, cidr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
table.Insert(prefix)
|
table.Insert(prefix)
|
||||||
@@ -43,12 +40,12 @@ func NewRemoteAddrChecker(cidrs []string) (checker.Impl, error) {
|
|||||||
func (rac *RemoteAddrChecker) Check(r *http.Request) (bool, error) {
|
func (rac *RemoteAddrChecker) Check(r *http.Request) (bool, error) {
|
||||||
host := r.Header.Get("X-Real-Ip")
|
host := r.Header.Get("X-Real-Ip")
|
||||||
if host == "" {
|
if host == "" {
|
||||||
return false, fmt.Errorf("%w: header X-Real-Ip is not set", ErrMisconfiguration)
|
return false, fmt.Errorf("%w: header X-Real-Ip is not set", anubis.ErrMisconfiguration)
|
||||||
}
|
}
|
||||||
|
|
||||||
addr, err := netip.ParseAddr(host)
|
addr, err := netip.ParseAddr(host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, fmt.Errorf("%w: %s is not an IP address: %w", ErrMisconfiguration, host, err)
|
return false, fmt.Errorf("%w: %s is not an IP address: %w", anubis.ErrMisconfiguration, host, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return rac.prefixTable.Contains(addr), nil
|
return rac.prefixTable.Contains(addr), nil
|
||||||
@@ -71,7 +68,7 @@ func NewUserAgentChecker(rexStr string) (checker.Impl, error) {
|
|||||||
func NewHeaderMatchesChecker(header, rexStr string) (checker.Impl, error) {
|
func NewHeaderMatchesChecker(header, rexStr string) (checker.Impl, error) {
|
||||||
rex, err := regexp.Compile(strings.TrimSpace(rexStr))
|
rex, err := regexp.Compile(strings.TrimSpace(rexStr))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%w: regex %s failed parse: %w", ErrMisconfiguration, rexStr, err)
|
return nil, fmt.Errorf("%w: regex %s failed parse: %w", anubis.ErrMisconfiguration, rexStr, err)
|
||||||
}
|
}
|
||||||
return &HeaderMatchesChecker{strings.TrimSpace(header), rex, internal.FastHash(header + ": " + rexStr)}, nil
|
return &HeaderMatchesChecker{strings.TrimSpace(header), rex, internal.FastHash(header + ": " + rexStr)}, nil
|
||||||
}
|
}
|
||||||
@@ -96,7 +93,7 @@ type PathChecker struct {
|
|||||||
func NewPathChecker(rexStr string) (checker.Impl, error) {
|
func NewPathChecker(rexStr string) (checker.Impl, error) {
|
||||||
rex, err := regexp.Compile(strings.TrimSpace(rexStr))
|
rex, err := regexp.Compile(strings.TrimSpace(rexStr))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%w: regex %s failed parse: %w", ErrMisconfiguration, rexStr, err)
|
return nil, fmt.Errorf("%w: regex %s failed parse: %w", anubis.ErrMisconfiguration, rexStr, err)
|
||||||
}
|
}
|
||||||
return &PathChecker{rex, internal.FastHash(rexStr)}, nil
|
return &PathChecker{rex, internal.FastHash(rexStr)}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user