mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-24 09:48:49 +02:00
feat(rules): add protocol matching functionality
- Introduced a new checker for HTTP protocols (http, https, h3) in the routing rules. - Added corresponding test cases to validate protocol matching behavior in requests.
This commit is contained in:
@@ -31,6 +31,7 @@ const (
|
|||||||
OnCookie = "cookie"
|
OnCookie = "cookie"
|
||||||
OnForm = "form"
|
OnForm = "form"
|
||||||
OnPostForm = "postform"
|
OnPostForm = "postform"
|
||||||
|
OnProto = "proto"
|
||||||
OnMethod = "method"
|
OnMethod = "method"
|
||||||
OnHost = "host"
|
OnHost = "host"
|
||||||
OnPath = "path"
|
OnPath = "path"
|
||||||
@@ -226,6 +227,41 @@ var checkers = map[string]struct {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
OnProto: {
|
||||||
|
help: Help{
|
||||||
|
command: OnProto,
|
||||||
|
args: map[string]string{
|
||||||
|
"proto": "the http protocol (http, https, h3)",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
validate: func(args []string) (any, gperr.Error) {
|
||||||
|
if len(args) != 1 {
|
||||||
|
return nil, ErrExpectOneArg
|
||||||
|
}
|
||||||
|
proto := args[0]
|
||||||
|
if proto != "http" && proto != "https" && proto != "h3" {
|
||||||
|
return nil, ErrInvalidArguments.Withf("proto: %q", proto)
|
||||||
|
}
|
||||||
|
return proto, nil
|
||||||
|
},
|
||||||
|
builder: func(args any) CheckFunc {
|
||||||
|
proto := args.(string)
|
||||||
|
switch proto {
|
||||||
|
case "http":
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) bool {
|
||||||
|
return r.TLS == nil
|
||||||
|
}
|
||||||
|
case "https":
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) bool {
|
||||||
|
return r.TLS != nil
|
||||||
|
}
|
||||||
|
default: // h3
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) bool {
|
||||||
|
return r.TLS != nil && r.ProtoMajor == 3
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
OnMethod: {
|
OnMethod: {
|
||||||
help: Help{
|
help: Help{
|
||||||
command: OnMethod,
|
command: OnMethod,
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package rules_test
|
package rules_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/tls"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -65,6 +66,30 @@ func genCorrectnessTestCases(field string, genRequest func(k, v string) *http.Re
|
|||||||
|
|
||||||
func TestOnCorrectness(t *testing.T) {
|
func TestOnCorrectness(t *testing.T) {
|
||||||
tests := []testCorrectness{
|
tests := []testCorrectness{
|
||||||
|
{
|
||||||
|
name: "proto_match_http",
|
||||||
|
checker: "proto http",
|
||||||
|
input: &http.Request{TLS: nil},
|
||||||
|
want: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "proto_match_https",
|
||||||
|
checker: "proto https",
|
||||||
|
input: &http.Request{TLS: &tls.ConnectionState{}},
|
||||||
|
want: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "proto_match_h3",
|
||||||
|
checker: "proto h3",
|
||||||
|
input: &http.Request{TLS: &tls.ConnectionState{}, ProtoMajor: 3},
|
||||||
|
want: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "proto_no_match_h3",
|
||||||
|
checker: "proto h3",
|
||||||
|
input: &http.Request{TLS: &tls.ConnectionState{}, ProtoMajor: 2},
|
||||||
|
want: false,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "method_match",
|
name: "method_match",
|
||||||
checker: "method GET",
|
checker: "method GET",
|
||||||
|
|||||||
Reference in New Issue
Block a user