-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathproxy.go
102 lines (89 loc) · 1.97 KB
/
proxy.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
package beyond
import (
"net/http"
"net/http/httputil"
"net/url"
"sync"
"github.com/koding/websocketproxy"
)
var (
hostProxy = sync.Map{}
)
func http2ws(r *http.Request) (*url.URL, error) {
target := "wss://" + hostRewrite(r.Host) + r.URL.RequestURI()
return url.Parse(target)
}
func nexthop(w http.ResponseWriter, r *http.Request) {
var (
nextHost = hostRewrite(r.Host)
nextProxy http.Handler
)
v, ok := hostProxy.Load(nextHost)
if ok {
nextProxy, ok = v.(*httputil.ReverseProxy)
}
if !ok && *learnNexthops {
nextProxy = learn(nextHost)
if nextProxy != nil {
hostProxy.Store(nextHost, nextProxy)
ok = true
}
}
if !ok || nextProxy == nil {
// unconfigured
errorHandler(w, 404, *fouroFourMessage)
return
}
if r.Header.Get("Upgrade") == "websocket" {
nextProxy, _ = websocketproxyNew(r)
}
nextProxy.ServeHTTP(w, r)
}
func newSHRP(target *url.URL) *httputil.ReverseProxy {
p := httputil.NewSingleHostReverseProxy(target)
p.ModifyResponse = func(resp *http.Response) error {
logRoundtrip(resp)
return nil
}
return p
}
func reproxy() error {
cleanup := map[string]bool{}
hostProxy.Range(func(key interface{}, value interface{}) bool {
if key, ok := key.(string); ok {
cleanup[key] = true
}
return true
})
var lerr error
sites.RLock()
for _, v := range sites.m {
for x := range v {
u, err := url.Parse(x)
if err != nil {
lerr = err
} else {
delete(cleanup, u.Host)
hostProxy.Store(u.Host, newSHRP(u))
}
}
}
sites.RUnlock()
for key := range cleanup {
hostProxy.Delete(key)
}
return lerr
}
func websocketproxyDirector(incoming *http.Request, out http.Header) {
out.Set("User-Agent", incoming.UserAgent())
out.Set("X-Forwarded-Proto", "https")
}
func websocketproxyNew(r *http.Request) (*websocketproxy.WebsocketProxy, error) {
ws, err := http2ws(r)
p := websocketproxy.NewProxy(ws)
p.Director = websocketproxyDirector
return p, err
}
func websocketproxyCheckOrigin(r *http.Request) bool {
return true
}