From f5708fd53977522d1f7c037e25bc60df34098c52 Mon Sep 17 00:00:00 2001 From: yusing Date: Thu, 9 Jan 2025 19:05:18 +0800 Subject: [PATCH] add rule.on directives "cookie", "form", "postform" --- internal/route/rules/do.go | 4 --- internal/route/rules/help.go | 10 ++++-- internal/route/rules/on.go | 61 +++++++++++++++++++++++++++++++++--- 3 files changed, 64 insertions(+), 11 deletions(-) diff --git a/internal/route/rules/do.go b/internal/route/rules/do.go index 819e27b..5d93fd5 100644 --- a/internal/route/rules/do.go +++ b/internal/route/rules/do.go @@ -7,7 +7,6 @@ import ( "strings" E "github.com/yusing/go-proxy/internal/error" - "github.com/yusing/go-proxy/internal/logging" 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/types" @@ -225,9 +224,6 @@ func buildCmd(executors []*CommandExecutor) (*CommandExecutor, error) { return &CommandExecutor{ HandlerFunc: func(w http.ResponseWriter, r *http.Request) { for _, exec := range executors { - logging.Debug(). - Str("directive", exec.directive). - Msg("executing command") exec.HandlerFunc(w, r) } }, diff --git a/internal/route/rules/help.go b/internal/route/rules/help.go index 9f2b559..cdff5c9 100644 --- a/internal/route/rules/help.go +++ b/internal/route/rules/help.go @@ -3,8 +3,9 @@ package rules import "strings" type Help struct { - command string - args map[string]string // args[arg] -> description + command string + description string + args map[string]string // args[arg] -> description } /* @@ -23,6 +24,11 @@ func (h *Help) String() string { sb.WriteString(arg) sb.WriteString("> ") } + if h.description != "" { + sb.WriteString("\n\t") + sb.WriteString(h.description) + sb.WriteRune('\n') + } sb.WriteRune('\n') for arg, desc := range h.args { sb.WriteRune('\t') diff --git a/internal/route/rules/on.go b/internal/route/rules/on.go index 326513d..5f9fccd 100644 --- a/internal/route/rules/on.go +++ b/internal/route/rules/on.go @@ -18,11 +18,14 @@ type ( ) const ( - OnHeader = "header" - OnQuery = "query" - OnMethod = "method" - OnPath = "path" - OnRemote = "remote" + OnHeader = "header" + OnQuery = "query" + OnCookie = "cookie" + OnForm = "form" + OnPostForm = "postform" + OnMethod = "method" + OnPath = "path" + OnRemote = "remote" ) 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 }, }, + 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: { help: Help{ command: OnMethod, @@ -71,6 +119,9 @@ var checkers = map[string]struct { OnPath: { help: Help{ command: OnPath, + description: `The path can be a glob pattern, e.g.: + /path/to + /path/to/*`, args: map[string]string{ "path": "the request path, must start with /", },