mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-19 20:32:35 +02:00
feat(rules): compile path rules directly to glob
This commit is contained in:
parent
dd65a8d04b
commit
ef95682116
3 changed files with 21 additions and 32 deletions
|
@ -5,6 +5,7 @@ import (
|
||||||
|
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
|
"github.com/gobwas/glob"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/net/types"
|
"github.com/yusing/go-proxy/internal/net/types"
|
||||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||||
|
@ -176,15 +177,15 @@ var checkers = map[string]struct {
|
||||||
"path": "the request path",
|
"path": "the request path",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
validate: validateURLPath,
|
validate: validateURLPathGlob,
|
||||||
builder: func(args any) CheckFunc {
|
builder: func(args any) CheckFunc {
|
||||||
pat := args.(string)
|
pat := args.(glob.Glob)
|
||||||
return func(cached Cache, r *http.Request) bool {
|
return func(cached Cache, r *http.Request) bool {
|
||||||
reqPath := r.URL.Path
|
reqPath := r.URL.Path
|
||||||
if len(reqPath) > 0 && reqPath[0] != '/' {
|
if len(reqPath) > 0 && reqPath[0] != '/' {
|
||||||
reqPath = "/" + reqPath
|
reqPath = "/" + reqPath
|
||||||
}
|
}
|
||||||
return strutils.GlobMatch(pat, reqPath)
|
return pat.Match(reqPath)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -4,8 +4,10 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gobwas/glob"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
gphttp "github.com/yusing/go-proxy/internal/net/gphttp"
|
gphttp "github.com/yusing/go-proxy/internal/net/gphttp"
|
||||||
"github.com/yusing/go-proxy/internal/net/types"
|
"github.com/yusing/go-proxy/internal/net/types"
|
||||||
|
@ -111,6 +113,20 @@ func validateURLPath(args []string) (any, gperr.Error) {
|
||||||
return p, nil
|
return p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// validateURLPathGlob returns []string with each element validated.
|
||||||
|
func validateURLPathGlob(args []string) (any, gperr.Error) {
|
||||||
|
p, err := validateURLPath(args)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
g, gErr := glob.Compile(p.(string))
|
||||||
|
if gErr != nil {
|
||||||
|
return nil, ErrInvalidArguments.With(gErr)
|
||||||
|
}
|
||||||
|
return g, nil
|
||||||
|
}
|
||||||
|
|
||||||
// validateURLPaths returns []string with each element validated.
|
// validateURLPaths returns []string with each element validated.
|
||||||
func validateURLPaths(paths []string) (any, gperr.Error) {
|
func validateURLPaths(paths []string) (any, gperr.Error) {
|
||||||
errs := gperr.NewBuilder("invalid url paths")
|
errs := gperr.NewBuilder("invalid url paths")
|
||||||
|
@ -133,7 +149,7 @@ func validateFSPath(args []string) (any, gperr.Error) {
|
||||||
if len(args) != 1 {
|
if len(args) != 1 {
|
||||||
return nil, ErrExpectOneArg
|
return nil, ErrExpectOneArg
|
||||||
}
|
}
|
||||||
p := path.Clean(args[0])
|
p := filepath.Clean(args[0])
|
||||||
if _, err := os.Stat(p); err != nil {
|
if _, err := os.Stat(p); err != nil {
|
||||||
return nil, ErrInvalidArguments.With(err)
|
return nil, ErrInvalidArguments.With(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,28 +0,0 @@
|
||||||
package strutils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/gobwas/glob"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
globPatterns = make(map[string]glob.Glob)
|
|
||||||
globPatternsMu sync.Mutex
|
|
||||||
)
|
|
||||||
|
|
||||||
func GlobMatch(pattern string, s string) bool {
|
|
||||||
if glob, ok := globPatterns[pattern]; ok {
|
|
||||||
return glob.Match(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
globPatternsMu.Lock()
|
|
||||||
defer globPatternsMu.Unlock()
|
|
||||||
|
|
||||||
glob, err := glob.Compile(pattern)
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
globPatterns[pattern] = glob
|
|
||||||
return glob.Match(s)
|
|
||||||
}
|
|
Loading…
Add table
Reference in a new issue