forked from affordablemobiles/squid-mtls-relay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
278 lines (249 loc) · 6.98 KB
/
main.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package main
import (
"bufio"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"strings"
"sync"
"time"
)
var bufferPool sync.Pool
var proxyAddr string = mustGetenv("PROXY_ADDR")
var dnsSuffix string = mustGetenv("CERTIFICATE_DNS_SUFFIX")
func init() {
makeBuffer := func() interface{} { return make([]byte, 0, 32*1024) }
bufferPool = sync.Pool{New: makeBuffer}
}
func main() {
// Create a CA certificate pool and add cert.pem to it
caCert, err := os.ReadFile("cacert.pem")
if err != nil {
log.Fatal(err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
// Create the TLS Config with the CA pool and enable Client certificate validation
tlsConfig := &tls.Config{
MinVersion: tls.VersionTLS13,
ClientCAs: caCertPool,
ClientAuth: tls.RequireAndVerifyClientCert,
VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
if len(verifiedChains) > 0 {
if len(verifiedChains[0]) > 0 {
if len(verifiedChains[0][0].DNSNames) > 0 {
if strings.HasSuffix(verifiedChains[0][0].DNSNames[0], dnsSuffix) {
return nil
}
}
}
}
return fmt.Errorf("invalid certificate hostname")
},
}
port := os.Getenv("PORT")
if port == "" {
port = "8443"
log.Printf("Defaulting to port %s", port)
}
// Create a Server instance to listen on port 8443 with the TLS config
server := &http.Server{
Addr: ":" + port,
TLSConfig: tlsConfig,
Handler: http.HandlerFunc(proxyHandler),
}
// Listen to HTTPS connections with the server certificate and wait
log.Fatal(server.ListenAndServeTLS("cert.pem", "key.pem"))
}
func proxyHandler(w http.ResponseWriter, r *http.Request) {
// First, verify the mTLS CN / attributes
identity, ok := strings.CutSuffix(r.TLS.VerifiedChains[0][0].DNSNames[0], dnsSuffix)
if !ok {
http.Error(w, "Permission Denied", http.StatusForbidden)
return
}
log.Printf("%s %s %s %s", r.RemoteAddr, identity, r.Method, r.URL)
if r.Method == http.MethodConnect {
if r.ProtoMajor == 2 {
if len(r.URL.Scheme) > 0 || len(r.URL.Path) > 0 {
log.Printf("CONNECT request has :scheme or/and :path pseudo-header fields")
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
}
}
// Add a "Proxy-Authorization" header, to notify squid who we've got.
r.Header.Set("Proxy-Authorization", "Basic "+basicAuth(identity, "automatic"))
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
r.Header.Set("X-Forwarded-For", host)
r.Header.Set("X-Real-IP", host)
// Pass the connection to squid...
targetConn, err := net.DialTimeout("tcp", proxyAddr, time.Second*2)
if err != nil {
log.Printf("failed to connect to upstream proxy: %s", err)
return
}
defer targetConn.Close()
req := r
if r.Method == "CONNECT" {
req = &http.Request{
Method: "CONNECT",
URL: r.URL,
Host: r.Host,
Header: r.Header.Clone(),
}
} else {
r.URL.Scheme = "http"
}
err = req.WriteProxy(targetConn)
if err != nil {
log.Printf("failed to write request to upstream proxy: %s", err)
return
}
switch r.ProtoMajor {
case 1: // http1: hijack the whole flow
err = serveHijack(w, targetConn)
if err != nil {
log.Printf("%s", err)
}
case 2: // http2: keep reading from "request" and writing into same response
defer r.Body.Close()
wFlusher, ok := w.(http.Flusher)
if !ok {
log.Printf("ResponseWriter doesn't implement Flusher()")
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
// Okay to use and discard buffered reader here, because
// TLS server will not speak until spoken to.
br := bufio.NewReader(targetConn)
resp, err := http.ReadResponse(br, r)
if err != nil {
log.Printf("failed to read CONNECT response: %s", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
for header, values := range resp.Header {
for _, val := range values {
w.Header().Add(header, val)
}
}
w.WriteHeader(resp.StatusCode)
wFlusher.Flush()
if n := br.Buffered(); n > 0 {
rbuf, err := br.Peek(n)
if err != nil {
log.Printf("failed to write buffer: %s", err)
return
}
w.Write(rbuf)
wFlusher.Flush()
}
err = dualStream(targetConn, r.Body, w)
if err != nil {
log.Printf("%s", err)
}
default:
panic("There was a check for http version, yet it's incorrect")
}
}
// Provies an encoded basic authentication header value
func basicAuth(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
}
// Hijacks the connection from ResponseWriter, writes the response and proxies data between targetConn
// and hijacked connection.
func serveHijack(w http.ResponseWriter, targetConn net.Conn) error {
hijacker, ok := w.(http.Hijacker)
if !ok {
return fmt.Errorf("ResponseWriter does not implement Hijacker")
}
clientConn, bufReader, err := hijacker.Hijack()
if err != nil {
return fmt.Errorf("failed to hijack: %s", err)
}
defer clientConn.Close()
// bufReader may contain unprocessed buffered data from the client.
if bufReader != nil {
// snippet borrowed from `proxy` plugin
if n := bufReader.Reader.Buffered(); n > 0 {
rbuf, err := bufReader.Reader.Peek(n)
if err != nil {
return err
}
targetConn.Write(rbuf)
}
}
return dualStream(targetConn, clientConn, clientConn)
}
// Copies data target->clientReader and clientWriter->target, and flushes as needed
// Returns when clientWriter-> target stream is done.
// Caddy should finish writing target -> clientReader.
func dualStream(target net.Conn, clientReader io.ReadCloser, clientWriter io.Writer) error {
stream := func(w io.Writer, r io.Reader) error {
// copy bytes from r to w
buf := bufferPool.Get().([]byte)
buf = buf[0:cap(buf)]
_, _err := flushingIoCopy(w, r, buf)
if closeWriter, ok := w.(interface {
CloseWrite() error
}); ok {
closeWriter.CloseWrite()
}
return _err
}
go stream(target, clientReader)
return stream(clientWriter, target)
}
// flushingIoCopy is analogous to buffering io.Copy(), but also attempts to flush on each iteration.
// If dst does not implement http.Flusher(e.g. net.TCPConn), it will do a simple io.CopyBuffer().
// Reasoning: http2ResponseWriter will not flush on its own, so we have to do it manually.
func flushingIoCopy(dst io.Writer, src io.Reader, buf []byte) (written int64, err error) {
flusher, ok := dst.(http.Flusher)
if !ok {
return io.CopyBuffer(dst, src, buf)
}
for {
nr, er := src.Read(buf)
if nr > 0 {
nw, ew := dst.Write(buf[0:nr])
flusher.Flush()
if nw > 0 {
written += int64(nw)
}
if ew != nil {
err = ew
break
}
if nr != nw {
err = io.ErrShortWrite
break
}
}
if er != nil {
if er != io.EOF {
err = er
}
break
}
}
return
}
func mustGetenv(k string) string {
v := os.Getenv(k)
if v == "" {
log.Panicf("%s environment variable not set.", k)
}
return v
}