add rule.on directives "cookie", "form", "postform"

This commit is contained in:
yusing 2025-01-09 19:05:18 +08:00
parent 5769abb626
commit f5708fd539
3 changed files with 64 additions and 11 deletions

View file

@ -7,7 +7,6 @@ import (
"strings" "strings"
E "github.com/yusing/go-proxy/internal/error" E "github.com/yusing/go-proxy/internal/error"
"github.com/yusing/go-proxy/internal/logging"
gphttp "github.com/yusing/go-proxy/internal/net/http" gphttp "github.com/yusing/go-proxy/internal/net/http"
"github.com/yusing/go-proxy/internal/net/http/reverseproxy" "github.com/yusing/go-proxy/internal/net/http/reverseproxy"
"github.com/yusing/go-proxy/internal/net/types" "github.com/yusing/go-proxy/internal/net/types"
@ -225,9 +224,6 @@ func buildCmd(executors []*CommandExecutor) (*CommandExecutor, error) {
return &CommandExecutor{ return &CommandExecutor{
HandlerFunc: func(w http.ResponseWriter, r *http.Request) { HandlerFunc: func(w http.ResponseWriter, r *http.Request) {
for _, exec := range executors { for _, exec := range executors {
logging.Debug().
Str("directive", exec.directive).
Msg("executing command")
exec.HandlerFunc(w, r) exec.HandlerFunc(w, r)
} }
}, },

View file

@ -3,8 +3,9 @@ package rules
import "strings" import "strings"
type Help struct { type Help struct {
command string command string
args map[string]string // args[arg] -> description description string
args map[string]string // args[arg] -> description
} }
/* /*
@ -23,6 +24,11 @@ func (h *Help) String() string {
sb.WriteString(arg) sb.WriteString(arg)
sb.WriteString("> ") sb.WriteString("> ")
} }
if h.description != "" {
sb.WriteString("\n\t")
sb.WriteString(h.description)
sb.WriteRune('\n')
}
sb.WriteRune('\n') sb.WriteRune('\n')
for arg, desc := range h.args { for arg, desc := range h.args {
sb.WriteRune('\t') sb.WriteRune('\t')

View file

@ -18,11 +18,14 @@ type (
) )
const ( const (
OnHeader = "header" OnHeader = "header"
OnQuery = "query" OnQuery = "query"
OnMethod = "method" OnCookie = "cookie"
OnPath = "path" OnForm = "form"
OnRemote = "remote" OnPostForm = "postform"
OnMethod = "method"
OnPath = "path"
OnRemote = "remote"
) )
var checkers = map[string]struct { var checkers = map[string]struct {
@ -56,6 +59,51 @@ var checkers = map[string]struct {
return r.URL.Query().Get(args.(StrTuple).First) == args.(StrTuple).Second return r.URL.Query().Get(args.(StrTuple).First) == args.(StrTuple).Second
}, },
}, },
OnCookie: {
help: Help{
command: OnCookie,
args: map[string]string{
"key": "the cookie key",
"value": "the cookie value",
},
},
validate: toStrTuple,
check: func(r *http.Request, args any) bool {
cookies := r.CookiesNamed(args.(StrTuple).First)
for _, cookie := range cookies {
if cookie.Value == args.(StrTuple).Second {
return true
}
}
return false
},
},
OnForm: {
help: Help{
command: OnForm,
args: map[string]string{
"key": "the form key",
"value": "the form value",
},
},
validate: toStrTuple,
check: func(r *http.Request, args any) bool {
return r.FormValue(args.(StrTuple).First) == args.(StrTuple).Second
},
},
OnPostForm: {
help: Help{
command: OnPostForm,
args: map[string]string{
"key": "the form key",
"value": "the form value",
},
},
validate: toStrTuple,
check: func(r *http.Request, args any) bool {
return r.PostFormValue(args.(StrTuple).First) == args.(StrTuple).Second
},
},
OnMethod: { OnMethod: {
help: Help{ help: Help{
command: OnMethod, command: OnMethod,
@ -71,6 +119,9 @@ var checkers = map[string]struct {
OnPath: { OnPath: {
help: Help{ help: Help{
command: OnPath, command: OnPath,
description: `The path can be a glob pattern, e.g.:
/path/to
/path/to/*`,
args: map[string]string{ args: map[string]string{
"path": "the request path, must start with /", "path": "the request path, must start with /",
}, },