-
Notifications
You must be signed in to change notification settings - Fork 4
/
backend.go
407 lines (378 loc) · 10.5 KB
/
backend.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// MIT License
//
// Copyright (c) 2023 TTBT Enterprises LLC
// Copyright (c) 2023 Robin Thellend <rthellend@rthellend.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package proxy
import (
"context"
"crypto/tls"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"net/http/httputil"
"regexp"
"slices"
"strings"
"time"
"github.com/c2FmZQ/tlsproxy/proxy/internal/netw"
)
const (
hstsHeader = "Strict-Transport-Security"
hstsValue = "max-age=2592000" // 30 days
viaHeader = "Via"
hostHeader = "Host"
xForwardedForHeader = "X-Forwarded-For"
)
var (
commaRE = regexp.MustCompile(`, *`)
)
func (be *Backend) incInFlight(delta int) int {
be.mu.Lock()
defer be.mu.Unlock()
be.inFlight += delta
if be.inFlight == 0 && be.shutdown && be.httpServer != nil {
close(be.httpConnChan)
be.httpServer = nil
}
return be.inFlight
}
func (be *Backend) close(ctx context.Context) {
be.mu.Lock()
defer be.mu.Unlock()
if be.httpServer == nil {
return
}
if ctx == nil {
be.httpServer.Close()
close(be.httpConnChan)
be.httpServer = nil
return
}
go be.httpServer.Shutdown(ctx)
be.shutdown = true
if be.inFlight == 0 {
close(be.httpConnChan)
be.httpServer = nil
}
}
func (be *Backend) dial(proto string) (net.Conn, error) {
if len(be.Addresses) == 0 {
return nil, errors.New("no backend addresses")
}
var max int
for {
be.mu.Lock()
sz := len(be.Addresses)
if max == 0 {
max = sz
}
addr := be.Addresses[be.next]
be.next = (be.next + 1) % sz
be.mu.Unlock()
dialer := &net.Dialer{
Timeout: be.ForwardTimeout,
KeepAlive: 30 * time.Second,
}
var c net.Conn
var err error
if be.Mode == ModeTLS || be.Mode == ModeHTTPS {
var protos []string
if proto != "" {
protos = append(protos, proto)
}
tc := &tls.Config{
InsecureSkipVerify: be.InsecureSkipVerify,
ServerName: be.ForwardServerName,
NextProtos: protos,
}
if be.forwardRootCAs != nil {
tc.RootCAs = be.forwardRootCAs
}
c, err = tls.DialWithDialer(dialer, "tcp", addr, tc)
} else {
c, err = dialer.Dial("tcp", addr)
}
if err != nil {
max--
if max > 0 {
log.Printf("ERR dial %q: %v", addr, err)
continue
}
return nil, err
}
return c, nil
}
}
func (be *Backend) authorize(subject string) error {
if be.ClientAuth == nil || be.ClientAuth.ACL == nil {
return nil
}
if subject == "" || !slices.Contains(*be.ClientAuth.ACL, subject) {
return errAccessDenied
}
return nil
}
func (be *Backend) checkIP(addr net.Addr) error {
var ip net.IP
switch a := addr.(type) {
case *net.TCPAddr:
ip = a.IP
default:
return fmt.Errorf("can't get IP address from %T", addr)
}
if be.denyIPs != nil {
for _, n := range *be.denyIPs {
if n.Contains(ip) {
return errAccessDenied
}
}
}
if be.allowIPs != nil {
for _, n := range *be.allowIPs {
if n.Contains(ip) {
return nil
}
}
return errAccessDenied
}
return nil
}
func (be *Backend) bridgeConns(client, server net.Conn) error {
serverClose := true
if be.ServerCloseEndsConnection != nil {
serverClose = *be.ServerCloseEndsConnection
}
clientClose := false
if be.ClientCloseEndsConnection != nil {
clientClose = *be.ClientCloseEndsConnection
}
timeout := time.Minute
if be.HalfCloseTimeout != nil {
timeout = *be.HalfCloseTimeout
}
ch := make(chan error)
go func() {
ch <- forward(client, server, serverClose, timeout)
}()
var retErr error
if err := forward(server, client, clientClose, timeout); err != nil && !errors.Is(err, net.ErrClosed) {
retErr = fmt.Errorf("[ext➔ int]: %w", unwrapErr(err))
}
if err := <-ch; err != nil && !errors.Is(err, net.ErrClosed) {
retErr = fmt.Errorf("[int➔ ext]: %w", unwrapErr(err))
}
return retErr
}
func (be *Backend) localHandlersAndAuthz(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
h, exists := be.localHandlers[req.URL.Path]
if exists && h.ssoBypass {
h.handler.ServeHTTP(w, req)
return
}
if !be.enforceSSOPolicy(w, req) {
return
}
if exists && !h.ssoBypass {
h.handler.ServeHTTP(w, req)
return
}
if !exists {
if _, ok := be.localHandlers[req.URL.Path+"/"]; ok {
http.Redirect(w, req, req.URL.Path+"/", http.StatusMovedPermanently)
return
}
}
if next == nil {
http.NotFound(w, req)
return
}
next.ServeHTTP(w, req)
})
}
func (be *Backend) consoleHandler() http.Handler {
return be.userAuthentication(logHandler(be.localHandlersAndAuthz(nil)))
}
func (be *Backend) reverseProxy() http.Handler {
var rp http.Handler
if len(be.Addresses) == 0 {
rp = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
log.Printf("PRX %s ➔ %s %s ➔ status:%d", formatReqDesc(req), req.Method, req.URL, http.StatusNotFound)
http.NotFound(w, req)
})
} else {
rp = &httputil.ReverseProxy{
Director: be.reverseProxyDirector,
Transport: &cleanRoundTripper{
serverNames: be.ServerNames,
RoundTripper: &http.Transport{
DialContext: be.reverseProxyDial,
DialTLSContext: be.reverseProxyDial,
MaxIdleConns: 100,
IdleConnTimeout: 30 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
},
ModifyResponse: be.reverseProxyModifyResponse,
}
}
return be.userAuthentication(be.localHandlersAndAuthz(rp))
}
func (be *Backend) reverseProxyDial(ctx context.Context, network, addr string) (net.Conn, error) {
return be.dial("")
}
func (be *Backend) reverseProxyDirector(req *http.Request) {
req.URL.Scheme = "https"
if be.Mode == ModeHTTP {
req.URL.Scheme = "http"
}
host := req.Host
if host == "" {
host = be.ServerNames[0]
}
req.URL.Host = host
req.Host = host
req.Header.Set(hostHeader, host)
req.Header.Del(xForwardedForHeader)
req.Header.Del(xFCCHeader)
var via []string
if v := req.Header.Get(viaHeader); v != "" {
via = commaRE.Split(req.Header.Get(viaHeader), -1)
}
via = append(via, req.Proto+" "+req.Context().Value(connCtxKey).(*tls.Conn).LocalAddr().String())
req.Header.Set(viaHeader, strings.Join(via, ", "))
if req.TLS != nil && len(req.TLS.PeerCertificates) > 0 && be.ClientAuth != nil && len(be.ClientAuth.AddClientCertHeader) > 0 {
addXFCCHeader(req, be.ClientAuth.AddClientCertHeader)
}
}
func (be *Backend) reverseProxyModifyResponse(resp *http.Response) error {
req := resp.Request
var cl string
if resp.ContentLength != -1 {
cl = fmt.Sprintf(" content-length:%d", resp.ContentLength)
}
log.Printf("PRX %s ➔ %s %s ➔ status:%d%s", formatReqDesc(req), req.Method, req.URL, resp.StatusCode, cl)
if resp.StatusCode != http.StatusMisdirectedRequest && resp.Header.Get(hstsHeader) == "" {
resp.Header.Set(hstsHeader, hstsValue)
}
return nil
}
// cleanRoundTripper detects loop with the via http header, and ensures that the
// request's Host value is valid before sending the request to the backend
// server. If the Host has an unexpected value, it returns a 421 immediately.
type cleanRoundTripper struct {
serverNames []string
http.RoundTripper
}
func (rt *cleanRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
hops := commaRE.Split(req.Header.Get(viaHeader), -1)
_, me, _ := strings.Cut(hops[len(hops)-1], " ")
hops = hops[:len(hops)-1]
for _, via := range hops {
_, via, _ = strings.Cut(via, " ")
if via != me {
continue
}
if req.Body != nil {
req.Body.Close()
}
return makeResponse(req, http.StatusLoopDetected, nil, req.Header.Get(viaHeader)), nil
}
host := req.Host
if h, _, err := net.SplitHostPort(host); err == nil {
host = h
}
if !slices.Contains(rt.serverNames, host) {
if req.Body != nil {
req.Body.Close()
}
return makeResponse(req, http.StatusMisdirectedRequest, nil, ""), nil
}
return rt.RoundTripper.RoundTrip(req)
}
func makeResponse(req *http.Request, statusCode int, header http.Header, body string) *http.Response {
if header == nil {
header = http.Header{}
}
header.Set("content-type", "text/plain")
header.Set("content-length", fmt.Sprintf("%d", len(body)))
return &http.Response{
StatusCode: statusCode,
ProtoMajor: 1,
ProtoMinor: 1,
Header: header,
Body: io.NopCloser(strings.NewReader(body)),
ContentLength: int64(len(body)),
Request: req,
}
}
func forward(out net.Conn, in net.Conn, closeWhenDone bool, halfClosedTimeout time.Duration) error {
if _, err := io.Copy(out, in); err != nil || closeWhenDone {
out.Close()
in.Close()
return err
}
if err := closeWrite(out); err != nil {
out.Close()
in.Close()
return nil
}
if err := closeRead(in); err != nil {
out.Close()
in.Close()
return nil
}
// At this point, the connection is either half closed, or fully closed.
// If it is half closed, the remote end will get an EOF on the next
// read. It can still send data back in the other direction. There are
// some broken clients or network devices that never close their end of
// the connection. So, we need to set a deadline to avoid keeping
// connections open forever.
out.SetReadDeadline(time.Now().Add(halfClosedTimeout))
return nil
}
func closeWrite(c net.Conn) error {
type closeWriter interface {
CloseWrite() error
}
if cc, ok := c.(closeWriter); ok {
return cc.CloseWrite()
}
if cc, ok := c.(*netw.Conn); ok {
return closeWrite(cc.Conn)
}
return fmt.Errorf("unexpected type: %T", c)
}
func closeRead(c net.Conn) error {
type closeReader interface {
CloseRead() error
}
if cc, ok := c.(closeReader); ok {
return cc.CloseRead()
}
if cc, ok := c.(*netw.Conn); ok {
return closeRead(cc.Conn)
}
return nil
}