-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadvanced.go
44 lines (38 loc) · 1.13 KB
/
advanced.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
package peregrine
import (
"github.com/RealFax/peregrine/internal/hack"
"github.com/gobwas/ws"
"net/http"
"net/url"
)
func RequestProxy(proxyFunc func(req *url.URL) error) func(uri []byte) error {
return func(uri []byte) error {
if len(uri) == 0 {
return ws.RejectConnectionError(ws.RejectionStatus(http.StatusBadRequest))
}
path, err := url.Parse(hack.Bytes2String(uri))
if err != nil {
return ws.RejectConnectionError(
ws.RejectionStatus(http.StatusBadRequest),
ws.RejectionReason(err.Error()),
)
}
return proxyFunc(path)
}
}
func HostProxy(proxyFunc func(host string) error) func(host []byte) error {
return func(host []byte) error {
if len(host) == 0 {
return ws.RejectConnectionError(ws.RejectionStatus(http.StatusBadRequest))
}
return proxyFunc(hack.Bytes2String(host))
}
}
func HeaderProxy(proxyFunc func(key, value string) error) func(key, value []byte) error {
return func(key, value []byte) error {
if len(key) == 0 || len(value) == 0 {
return ws.RejectConnectionError(ws.RejectionStatus(http.StatusBadRequest))
}
return proxyFunc(hack.Bytes2String(key), hack.Bytes2String(value))
}
}