forked from chrislusf/teeproxy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
teeproxy.go
177 lines (157 loc) · 5.38 KB
/
teeproxy.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
package main
import (
"bytes"
"crypto/tls"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/http/httputil"
"runtime"
"time"
)
var (
listen = flag.String("l", ":8080", "port to accept requests")
tlsKey = flag.String("key.file", "", "path to the TLS private key file")
tlsCertificate = flag.String("cert.file", "", "path to the TLS certificate file")
targetHost = flag.String("a", "localhost:8080", "where target traffic goes. http://localhost:8080/")
targetHostRewrite = flag.Bool("a.rewrite", false, "rewrite the host header when proxying target traffic")
targetTimeout = flag.Int("a.timeout", 3, "timeout in seconds for target traffic")
isTargetTLS = flag.Bool("a.tls", false, "proxies to target over TLS")
isTargetTLSInsecure = flag.Bool("a.tls.insecure", false, "ignores certificate checking on target")
alternateHost = flag.String("b", "localhost:8081", "where alternate traffic goes, response is ignored. http://localhost:8081/")
alternateHostRewrite = flag.Bool("b.rewrite", false, "rewrite the host header when proxying alternate site traffic")
alternateTimeout = flag.Int("b.timeout", 3, "timeout in seconds for alternate site traffic")
isAlternateTLS = flag.Bool("b.tls", false, "proxies to alternate over TLS")
isAlternateTLSInsecure = flag.Bool("b.tls.insecure", false, "ignores certificate checking on alternate")
)
func main() {
flag.Parse()
l, err := listener()
if err != nil {
fmt.Printf("Failed to listen on %s, %s\n", *listen, err)
return
}
fmt.Printf("Listening on %s and proxying to %s / %s\n", *listen, *targetHost, *alternateHost)
http.Serve(l, http.HandlerFunc(handler))
}
// listener returns either an HTTP or HTTPS listener.
func listener() (net.Listener, error) {
if *tlsKey == "" {
return net.Listen("tcp", *listen)
}
cert, err := tls.LoadX509KeyPair(*tlsCertificate, *tlsKey)
if err != nil {
return nil, err
}
config := &tls.Config{Certificates: []tls.Certificate{cert}}
return tls.Listen("tcp", *listen, config)
}
// recovery is pretty much copied from golang:net/http/server.go
func recovery(req *http.Request) {
if r := recover(); r != nil {
if err := recover(); err != nil && err != http.ErrAbortHandler {
const size = 64 << 10
buf := make([]byte, size)
buf = buf[:runtime.Stack(buf, false)]
log.Printf("panic serving %s: %v\n%s\n", req.RemoteAddr, err, buf)
}
}
}
// handler duplicates the incoming request across the target and alternate, discarding the alternates response.
func handler(w http.ResponseWriter, r *http.Request) {
defer recovery(r)
targetRequest, alternateRequest, err := proxiedRequests(r)
// alternate request
go func() {
defer recovery(r)
_, err := request(*alternateHost, *isAlternateTLS, *isAlternateTLSInsecure, alternateRequest)
if err != nil {
log.Printf("Failed to receive from alternate %s, %v\n", *alternateHost, err)
}
}()
// target request
resp, err := request(*targetHost, *isTargetTLS, *isTargetTLSInsecure, targetRequest)
if err != nil {
log.Printf("Failed to receive from target %s, %v\n", *targetHost, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
for k, v := range resp.Header {
w.Header()[k] = v
}
w.WriteHeader(resp.StatusCode)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Failed to read response body from target proxy: %v", err)
return
}
resp.Body.Close()
_, err = w.Write(body)
if err != nil {
log.Printf("Failed to write response body from target proxy: %v", err)
}
}
// copyRequest copyies the given request using the given body, re-writing the host when rewriteHost is true.
func copyRequest(request *http.Request, body io.ReadCloser, rewriteHost bool, host string) *http.Request {
r := http.Request{
Method: request.Method,
URL: request.URL,
Proto: request.Proto,
ProtoMajor: request.ProtoMajor,
ProtoMinor: request.ProtoMinor,
Header: request.Header,
Body: body,
Host: request.Host,
ContentLength: request.ContentLength,
Close: true,
}
if rewriteHost {
r.Host = host
}
return &r
}
// proxiedRequests creates the `target` and `alternate` requests from the given request.
func proxiedRequests(r *http.Request) (*http.Request, *http.Request, error) {
// Duplicate the request body.
b1 := new(bytes.Buffer)
b2 := new(bytes.Buffer)
w := io.MultiWriter(b1, b2)
_, err := io.Copy(w, r.Body)
r.Body.Close()
// Duplicate the request, using the duplicated body for each.
r1 := copyRequest(r, ioutil.NopCloser(b1), *targetHostRewrite, *targetHost)
r2 := copyRequest(r, ioutil.NopCloser(b2), *alternateHostRewrite, *alternateHost)
return r1, r2, err
}
// request invokes the request upon the host.
func request(host string, useTLS bool, insecureSkip bool, r *http.Request) (*http.Response, error) {
// Create the TCP connection.
tcpConn, err := net.DialTimeout("tcp", host, time.Duration(time.Duration(*alternateTimeout)*time.Second))
if err != nil {
return nil, err
}
// Wrap it with TLS.
if useTLS {
config := &tls.Config{
InsecureSkipVerify: true,
}
tcpConn = tls.Client(tcpConn, config)
err = tcpConn.(*tls.Conn).Handshake()
}
// Bundle it with HTTP.
var resp *http.Response
conn := httputil.NewClientConn(tcpConn, nil)
err = conn.Write(r)
if err == nil {
resp, err = conn.Read(r)
}
conn.Close()
if err == httputil.ErrPersistEOF {
err = nil
}
return resp, err
}