mirror of
https://github.com/TecharoHQ/anubis.git
synced 2026-04-13 20:18:45 +00:00
feat(checker): port path checker
Signed-off-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
37
lib/checker/path/checker.go
Normal file
37
lib/checker/path/checker.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package path
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/TecharoHQ/anubis"
|
||||
"github.com/TecharoHQ/anubis/internal"
|
||||
"github.com/TecharoHQ/anubis/lib/checker"
|
||||
)
|
||||
|
||||
func New(rexStr string) (checker.Interface, error) {
|
||||
rex, err := regexp.Compile(strings.TrimSpace(rexStr))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: regex %s failed parse: %w", anubis.ErrMisconfiguration, rexStr, err)
|
||||
}
|
||||
return &Checker{rex, internal.FastHash(rexStr)}, nil
|
||||
}
|
||||
|
||||
type Checker struct {
|
||||
regexp *regexp.Regexp
|
||||
hash string
|
||||
}
|
||||
|
||||
func (c *Checker) Check(r *http.Request) (bool, error) {
|
||||
if c.regexp.MatchString(r.URL.Path) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (c *Checker) Hash() string {
|
||||
return c.hash
|
||||
}
|
||||
Reference in New Issue
Block a user