-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrequest.go
122 lines (110 loc) · 3.8 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package masque
import (
"fmt"
"net/http"
"net/url"
"reflect"
"strconv"
"strings"
"github.com/dunglas/httpsfv"
"github.com/quic-go/quic-go/http3"
"github.com/yosida95/uritemplate/v3"
)
const requestProtocol = "connect-udp"
var capsuleProtocolHeaderValue string
func init() {
v, err := httpsfv.Marshal(httpsfv.NewItem(true))
if err != nil {
panic(fmt.Sprintf("failed to marshal capsule protocol header value: %v", err))
}
capsuleProtocolHeaderValue = v
}
// Request is the parsed CONNECT-UDP request returned from ParseRequest.
// Target is the target server that the client requests to connect to.
// It can either be DNS name:port or an IP:port.
type Request struct {
Target string
}
// RequestParseError is returned from ParseRequest if parsing the CONNECT-UDP request fails.
// It is recommended that the request is rejected with the corresponding HTTP status code.
type RequestParseError struct {
HTTPStatus int
Err error
}
func (e *RequestParseError) Error() string { return e.Err.Error() }
func (e *RequestParseError) Unwrap() error { return e.Err }
// ParseRequest parses a CONNECT-UDP request.
// The template is the URI template that clients will use to configure this UDP proxy.
func ParseRequest(r *http.Request, template *uritemplate.Template) (*Request, error) {
u, err := url.Parse(template.Raw())
if err != nil {
return nil, &RequestParseError{
HTTPStatus: http.StatusInternalServerError,
Err: fmt.Errorf("failed to parse template: %w", err),
}
}
if r.Method != http.MethodConnect {
return nil, &RequestParseError{
HTTPStatus: http.StatusMethodNotAllowed,
Err: fmt.Errorf("expected CONNECT request, got %s", r.Method),
}
}
if r.Proto != requestProtocol {
return nil, &RequestParseError{
HTTPStatus: http.StatusNotImplemented,
Err: fmt.Errorf("unexpected protocol: %s", r.Proto),
}
}
if r.Host != u.Host {
return nil, &RequestParseError{
HTTPStatus: http.StatusBadRequest,
Err: fmt.Errorf("host in :authority (%s) does not match template host (%s)", r.Host, u.Host),
}
}
// The capsule protocol header is optional, but if it's present,
// we need to validate its value.
capsuleHeaderValues, ok := r.Header[http3.CapsuleProtocolHeader]
if ok {
item, err := httpsfv.UnmarshalItem(capsuleHeaderValues)
if err != nil {
return nil, &RequestParseError{
HTTPStatus: http.StatusBadRequest,
Err: fmt.Errorf("invalid capsule header value: %s", capsuleHeaderValues),
}
}
if v, ok := item.Value.(bool); !ok {
return nil, &RequestParseError{
HTTPStatus: http.StatusBadRequest,
Err: fmt.Errorf("incorrect capsule header value type: %s", reflect.TypeOf(item.Value)),
}
} else if !v {
return nil, &RequestParseError{
HTTPStatus: http.StatusBadRequest,
Err: fmt.Errorf("incorrect capsule header value: %t", item.Value),
}
}
}
match := template.Match(r.URL.String())
targetHost := unescape(match.Get(uriTemplateTargetHost).String())
targetPortStr := match.Get(uriTemplateTargetPort).String()
if targetHost == "" || targetPortStr == "" {
return nil, &RequestParseError{
HTTPStatus: http.StatusBadRequest,
Err: fmt.Errorf("expected target_host and target_port"),
}
}
// IPv6 addresses need to be enclosed in [], otherwise resolving the address will fail.
if strings.Contains(targetHost, ":") {
targetHost = "[" + targetHost + "]"
}
targetPort, err := strconv.Atoi(targetPortStr)
if err != nil {
return nil, &RequestParseError{
HTTPStatus: http.StatusBadRequest,
Err: fmt.Errorf("failed to decode target_port: %w", err),
}
}
return &Request{Target: fmt.Sprintf("%s:%d", targetHost, targetPort)}, nil
}
func escape(s string) string { return strings.ReplaceAll(s, ":", "%3A") }
func unescape(s string) string { return strings.ReplaceAll(s, "%3A", ":") }