-
Notifications
You must be signed in to change notification settings - Fork 3
/
proxy.go
198 lines (166 loc) · 4.1 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
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
package devproxy
import (
"context"
"errors"
"io"
"log"
"net"
"net/http"
"net/http/httputil"
"os"
"strings"
"sync"
)
var (
connEstablishedReply = []byte("HTTP/1.1 200 Connection established\r\n\r\n")
)
// Proxy is the development proxy server
type Proxy struct {
*httputil.ReverseProxy
middlewares http.Handler
logger *log.Logger
debug bool
hosts []Spoofer
}
func (p *Proxy) ServeHTTP(w http.ResponseWriter, req *http.Request) {
p.middlewares.ServeHTTP(w, req)
}
// Spoofer represent a spoofing rule.
type Spoofer interface {
Match(string) bool
Replace(string) string
}
type option func(*Proxy)
// WithDebugOutput prints each request headers to stdout
func WithDebugOutput(d bool) option {
return func(p *Proxy) {
p.debug = d
}
}
// WithHosts configures which hosts to spoof
func WithHosts(h []Spoofer) option {
return func(p *Proxy) {
p.hosts = h
}
}
// New creates a new development proxy
func New(opts ...option) *Proxy {
director := func(req *http.Request) {
if host, match := req.Context().Value(hostKeyMatch).(string); match {
req.Host = host
req.URL.Host = host
}
}
p := &Proxy{
logger: log.New(os.Stdout, "[devproxy] ", log.LstdFlags),
ReverseProxy: &httputil.ReverseProxy{
Director: director,
BufferPool: buffers,
ErrorLog: log.New(os.Stderr, "[devproxy] ", log.LstdFlags),
},
}
for _, o := range opts {
o(p)
}
p.middlewares = http.Handler(p.ReverseProxy)
p.middlewares = connectMiddleware(p.middlewares)
if p.debug {
p.middlewares = debugMiddleware(p.logger, p.middlewares)
}
p.middlewares = detectMiddleware(p.hosts, p.middlewares)
return p
}
type hostKey int
var hostKeyMatch hostKey = 1
func detectMiddleware(hosts []Spoofer, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
for _, s := range hosts {
host := normalizeHost(req)
if s.Match(host) {
host = s.Replace(host)
ctx := context.WithValue(req.Context(), hostKeyMatch, host)
req = req.WithContext(ctx)
break
}
}
next.ServeHTTP(w, req)
})
}
func debugMiddleware(log *log.Logger, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if host, match := req.Context().Value(hostKeyMatch).(string); match {
log.Printf("Host spoof matched: %s -> %s\n", req.URL.Host, host)
}
b, err := httputil.DumpRequest(req, false)
if err == nil {
log.Printf("new request:\n%s", b)
}
next.ServeHTTP(w, req)
})
}
func connectMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.Method == http.MethodConnect {
if err := handleConnect(w, req); err != nil {
http.Error(w, err.Error(), 500)
}
return
}
next.ServeHTTP(w, req)
})
}
func handleConnect(w http.ResponseWriter, req *http.Request) error {
// according to the RFC, the requested URL will always
// have a port and has a form:
// www.example.com:80
// The request URL will then be: //www.example.com:80
host, match := req.Context().Value(hostKeyMatch).(string)
if !match {
host = req.URL.Host
}
origin, err := net.Dial("tcp", host)
if err != nil {
return err
}
defer origin.Close()
hj, ok := w.(http.Hijacker)
if !ok {
return errors.New("could not hijack connection")
}
client, _, err := hj.Hijack()
if err != nil {
return err
}
defer client.Close()
client.Write(connEstablishedReply)
close := sync.Once{}
cp := func(dst io.WriteCloser, src io.ReadCloser) {
b := buffers.Get()
defer buffers.Put(b)
io.CopyBuffer(dst, src, b)
close.Do(func() {
dst.Close()
src.Close()
})
}
go cp(origin, client)
cp(client, origin)
return nil
}
// normalizeHost will always return a host string
// of the form host:port for a giver request
func normalizeHost(req *http.Request) string {
if hostHasPort(req.Host) {
return req.Host
}
if hostHasPort(req.URL.Host) {
return req.URL.Host
}
if req.URL.Scheme == "https" {
return req.Host + ":443"
}
return req.Host + ":80"
}
func hostHasPort(host string) bool {
return strings.LastIndex(host, ":") > strings.LastIndex(host, "]")
}