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
|
package proxy
import (
"bytes"
"context"
"fmt"
"io"
"net"
"net/http"
"sync"
"time"
"github.com/shiv/internal/cert"
"github.com/shiv/internal/events"
internalhttp "github.com/shiv/internal/http"
"github.com/shiv/internal/logger"
)
type Proxy struct {
addr string
certAuth *cert.CA
bus *events.Bus
client *http.Client
mu sync.Mutex
srv *http.Server
conns sync.Map // active hijacked net.Conn
}
const maxBodySize = 10 << 20
func New(addr string, bus *events.Bus) (*Proxy, error) {
ca, err := cert.Load()
if err != nil {
return nil, fmt.Errorf("proxy: load CA: %w", err)
}
p := &Proxy{
addr: addr,
certAuth: ca,
bus: bus,
client: &http.Client{
Transport: &http.Transport{
ResponseHeaderTimeout: 30 * time.Second,
},
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
},
}
bus.Register(p)
return p, nil
}
func (p *Proxy) CA() *cert.CA {
return p.certAuth
}
func (p *Proxy) trackConn(c net.Conn) {
p.conns.Store(c, struct{}{})
}
func (p *Proxy) untrackConn(c net.Conn) {
p.conns.Delete(c)
}
func (p *Proxy) closeAllConns() {
p.conns.Range(func(k, _ any) bool {
k.(net.Conn).SetDeadline(time.Now())
return true
})
}
func (p *Proxy) Start(newAddr string) error {
ln, err := net.Listen("tcp", newAddr)
if err != nil {
return fmt.Errorf("proxy: listen on %s: %w", newAddr, err)
}
newSrv := &http.Server{
Handler: p,
ReadTimeout: 60 * time.Second,
WriteTimeout: 60 * time.Second,
IdleTimeout: 120 * time.Second,
}
p.mu.Lock()
oldSrv := p.srv
p.addr = newAddr
p.srv = newSrv
p.mu.Unlock()
if oldSrv != nil {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_ = oldSrv.Shutdown(ctx)
}
p.closeAllConns()
go func() {
if err := newSrv.Serve(ln); err != nil && err != http.ErrServerClosed {
logger.Error("proxy: Serve: %v", err)
}
}()
return nil
}
func (p *Proxy) Stop() {
p.mu.Lock()
srv := p.srv
p.mu.Unlock()
if srv != nil {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_ = srv.Shutdown(ctx)
p.mu.Lock()
p.srv = nil
p.mu.Unlock()
}
p.closeAllConns()
}
// ObserveProxyRestart implements events.ProxyCommandObserver.
func (p *Proxy) ObserveProxyRestart(e events.ProxyRestartEvent) {
_ = p.Start(e.Addr)
}
// ObserveProxyStop implements events.ProxyCommandObserver.
func (p *Proxy) ObserveProxyStop(_ events.ProxyStopEvent) {
p.Stop()
}
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodConnect {
p.handleConnect(w, r)
return
}
reqBody, err := io.ReadAll(io.LimitReader(r.Body, maxBodySize))
if err != nil {
http.Error(w, "failed to read request body", http.StatusBadRequest)
return
}
r.Body = io.NopCloser(bytes.NewReader(reqBody))
result := p.bus.EmitRequest(events.RequestEvent{Request: r, Body: reqBody})
if result.Drop {
http.Error(w, "request dropped", http.StatusForbidden)
return
}
interceptedReq, reqBody := result.Request, result.Body
interceptedReq.Body = io.NopCloser(bytes.NewReader(reqBody))
start := time.Now()
resp, err := forward(interceptedReq, p.client)
if err != nil {
http.Error(w, "bad gateway", http.StatusBadGateway)
return
}
defer resp.Body.Close()
var respBuf bytes.Buffer
reader := io.TeeReader(io.LimitReader(resp.Body, maxBodySize), &respBuf)
for k, vals := range resp.Header {
for _, v := range vals {
w.Header().Add(k, v)
}
}
w.WriteHeader(resp.StatusCode)
_, _ = io.Copy(w, reader)
respBody := respBuf.Bytes()
elapsed := time.Since(start).Milliseconds()
logBody := internalhttp.Decompress(resp.Header, respBody)
if internalhttp.IsBinary(resp.Header) {
logBody = nil
}
p.bus.EmitResponse(events.ResponseEvent{
Timestamp: start,
Host: interceptedReq.Host,
Proto: "HTTP/1.1",
Method: interceptedReq.Method,
URL: interceptedReq.URL.String(),
ReqHeaders: interceptedReq.Header,
ReqBody: reqBody,
StatusCode: resp.StatusCode,
RespHeaders: resp.Header,
RespBody: logBody,
DurationMs: elapsed,
TLS: false,
})
}
|