forked from zalando/skipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade.go
191 lines (169 loc) · 5.34 KB
/
upgrade.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package proxy
import (
"bufio"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strings"
"sync"
log "github.com/sirupsen/logrus"
)
// isUpgradeRequest returns true if and only if there is a "Connection"
// key with the value "Upgrade" in Headers of the given request.
func isUpgradeRequest(req *http.Request) bool {
for _, h := range req.Header[http.CanonicalHeaderKey("Connection")] {
if strings.Contains(strings.ToLower(h), "upgrade") {
return true
}
}
return false
}
// getUpgradeRequest returns the protocol name from the upgrade header
func getUpgradeRequest(req *http.Request) string {
for _, h := range req.Header[http.CanonicalHeaderKey("Connection")] {
if strings.Contains(strings.ToLower(h), "upgrade") {
return strings.Join(req.Header[h], " ")
}
}
return ""
}
// UpgradeProxy stores everything needed to make the connection upgrade.
type upgradeProxy struct {
backendAddr *url.URL
reverseProxy *httputil.ReverseProxy
insecure bool
tlsClientConfig *tls.Config
}
// TODO: add user here
type auditLog struct {
Method string `json:"method"`
Path string `json:"path"`
Query string `json:"query"`
Fragment string `json:"fragment"`
}
// serveHTTP establishes a bidirectional connection, creates an
// auditlog for the request target, copies the data back and force and
// write data to an auditlog. It will not return until the connection
// is closed.
func (p *upgradeProxy) serveHTTP(w http.ResponseWriter, req *http.Request) {
backendConn, err := p.dialBackend(req)
if err != nil {
log.Errorf("Error connecting to backend: %s", err)
w.WriteHeader(http.StatusServiceUnavailable)
w.Write([]byte(http.StatusText(http.StatusServiceUnavailable)))
return
}
defer backendConn.Close()
err = req.Write(backendConn)
if err != nil {
log.Errorf("Error writing request to backend: %s", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
return
}
// Audit-Log
auditlog := &auditLog{req.Method, req.URL.Path, req.URL.RawQuery, req.URL.Fragment}
auditJSON, err := json.Marshal(auditlog)
_, err = os.Stderr.Write(auditJSON)
if err != nil {
log.Errorf("Could not write audit-log, caused by: %v", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
return
}
resp, err := http.ReadResponse(bufio.NewReader(backendConn), req)
if err != nil {
log.Errorf("Error reading response from backend: %s", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
return
}
if resp.StatusCode == http.StatusUnauthorized {
log.Errorf("Got unauthorized error from backend for: %s %s", req.Method, req.URL)
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(http.StatusText(http.StatusUnauthorized)))
return
}
requestHijackedConn, _, err := w.(http.Hijacker).Hijack()
if err != nil {
log.Errorf("Error hijacking request connection: %s", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
return
}
defer requestHijackedConn.Close()
// NOTE: from this point forward, we own the connection and we can't use
// w.Header(), w.Write(), or w.WriteHeader any more
err = resp.Write(requestHijackedConn)
if err != nil {
log.Errorf("Error writing backend response to client: %s", err)
return
}
var wg sync.WaitGroup
wg.Add(2)
copyAsync(&wg, backendConn, requestHijackedConn, os.Stdout)
copyAsync(&wg, requestHijackedConn, backendConn)
log.Debugf("Successfully upgraded to protocol %s by user request", getUpgradeRequest(req))
// Wait for goroutine to finish, such that the established connection does not break.
wg.Wait()
}
func (p *upgradeProxy) dialBackend(req *http.Request) (net.Conn, error) {
dialAddr := canonicalAddr(req.URL)
switch p.backendAddr.Scheme {
case "http":
return net.Dial("tcp", dialAddr)
case "https":
tlsConn, err := tls.Dial("tcp", dialAddr, p.tlsClientConfig)
if err != nil {
return nil, err
}
if !p.insecure {
hostToVerify, _, err := net.SplitHostPort(dialAddr)
if err != nil {
return nil, err
}
err = tlsConn.VerifyHostname(hostToVerify)
if err != nil {
tlsConn.Close()
return nil, err
}
}
return tlsConn, nil
default:
return nil, fmt.Errorf("unknown scheme: %s", p.backendAddr.Scheme)
}
}
func copyAsync(wg *sync.WaitGroup, src io.Reader, dst ...io.Writer) {
go func() {
w := io.MultiWriter(dst...)
_, err := io.Copy(w, src)
if err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
log.Errorf("error proxying data from src to dst: %v", err)
}
wg.Done()
}()
}
// FROM: http://golang.org/src/net/http/client.go
// Given a string of the form "host", "host:port", or "[ipv6::address]:port",
// return true if the string includes a port.
func hasPort(s string) bool { return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") }
// FROM: http://golang.org/src/net/http/transport.go
var portMap = map[string]string{
"http": "80",
"https": "443",
}
// FROM: http://golang.org/src/net/http/transport.go
// canonicalAddr returns url.Host but always with a ":port" suffix
func canonicalAddr(url *url.URL) string {
addr := url.Host
if !hasPort(addr) {
return addr + ":" + portMap[url.Scheme]
}
return addr
}