-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
270 lines (220 loc) · 6.96 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
package main
import (
"crypto/tls"
"crypto/x509"
"flag"
"log"
"net"
"net/http"
"net/url"
"os"
"os/signal"
"strconv"
"strings"
"time"
"path"
"github.com/google/martian"
mapi "github.com/google/martian/api"
"github.com/google/martian/cors"
"github.com/google/martian/fifo"
"github.com/google/martian/har"
"github.com/google/martian/httpspec"
"github.com/google/martian/marbl"
"github.com/google/martian/martianhttp"
"github.com/google/martian/martianlog"
"github.com/google/martian/mitm"
"github.com/google/martian/servemux"
"github.com/google/martian/trafficshape"
"github.com/google/martian/verify"
_ "github.com/google/martian/body"
_ "github.com/google/martian/cookie"
_ "github.com/google/martian/failure"
_ "github.com/google/martian/martianurl"
_ "github.com/google/martian/method"
_ "github.com/google/martian/pingback"
_ "github.com/google/martian/port"
_ "github.com/google/martian/priority"
_ "github.com/google/martian/querystring"
_ "github.com/google/martian/skip"
_ "github.com/google/martian/stash"
_ "github.com/google/martian/static"
_ "github.com/google/martian/status"
)
var (
addr = flag.String("addr", ":8080", "host:port of the proxy")
apiAddr = flag.String("api-addr", ":8181", "host:port of the configuration API")
tlsAddr = flag.String("tls-addr", ":4443", "host:port of the proxy over TLS")
api = flag.String("api", "martian.proxy", "hostname for the API")
generateCA = flag.Bool("generate-ca-cert", false, "generate CA certificate and private key for MITM")
cert = flag.String("cert", "", "filepath to the CA certificate used to sign MITM certificates")
key = flag.String("key", "", "filepath to the private key of the CA used to sign MITM certificates")
organization = flag.String("organization", "Martian Proxy", "organization name for MITM certificates")
validity = flag.Duration("validity", time.Hour, "window of time that MITM certificates are valid")
allowCORS = flag.Bool("cors", false, "allow CORS requests to configure the proxy")
harLogging = flag.Bool("har", false, "enable HAR logging API")
marblLogging = flag.Bool("marbl", false, "enable MARBL logging API")
trafficShaping = flag.Bool("traffic-shaping", false, "enable traffic shaping API")
skipTLSVerify = flag.Bool("skip-tls-verify", false, "skip TLS server verification; insecure")
dsProxyURL = flag.String("downstream-proxy-url", "", "URL of downstream proxy")
)
func main() {
p := martian.NewProxy()
defer p.Close()
tr := &http.Transport{
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: time.Second,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: *skipTLSVerify,
},
}
p.SetRoundTripper(tr)
if *dsProxyURL != "" {
u, err := url.Parse(*dsProxyURL)
if err != nil {
log.Fatal(err)
}
p.SetDownstreamProxy(u)
}
mux := http.NewServeMux()
var x509c *x509.Certificate
var priv interface{}
if *generateCA {
var err error
x509c, priv, err = mitm.NewAuthority("martian.proxy", "Martian Authority", 30*24*time.Hour)
if err != nil {
log.Fatal(err)
}
} else if *cert != "" && *key != "" {
tlsc, err := tls.LoadX509KeyPair(*cert, *key)
if err != nil {
log.Fatal(err)
}
priv = tlsc.PrivateKey
x509c, err = x509.ParseCertificate(tlsc.Certificate[0])
if err != nil {
log.Fatal(err)
}
}
if x509c != nil && priv != nil {
mc, err := mitm.NewConfig(x509c, priv)
if err != nil {
log.Fatal(err)
}
mc.SetValidity(*validity)
mc.SetOrganization(*organization)
mc.SkipTLSVerify(*skipTLSVerify)
p.SetMITM(mc)
// Expose certificate authority.
ah := martianhttp.NewAuthorityHandler(x509c)
configure("/authority.cer", ah, mux)
// Start TLS listener for transparent MITM.
tl, err := net.Listen("tcp", *tlsAddr)
if err != nil {
log.Fatal(err)
}
go p.Serve(tls.NewListener(tl, mc.TLS()))
}
stack, fg := httpspec.NewStack("martian")
// wrap stack in a group so that we can forward API requests to the API port
// before the httpspec modifiers which include the via modifier which will
// trip loop detection
topg := fifo.NewGroup()
// Redirect API traffic to API server.
if *apiAddr != "" {
apip := strings.Replace(*apiAddr, ":", "", 1)
port, err := strconv.Atoi(apip)
if err != nil {
log.Fatal(err)
}
// Forward traffic that pattern matches in http.DefaultServeMux
apif := servemux.NewFilter(mux)
apif.SetRequestModifier(mapi.NewForwarder("", port))
topg.AddRequestModifier(apif)
}
topg.AddRequestModifier(stack)
topg.AddResponseModifier(stack)
p.SetRequestModifier(topg)
p.SetResponseModifier(topg)
m := martianhttp.NewModifier()
fg.AddRequestModifier(m)
fg.AddResponseModifier(m)
if *harLogging {
hl := har.NewLogger()
muxf := servemux.NewFilter(mux)
// Only append to HAR logs when the requests are not API requests,
// that is, they are not matched in http.DefaultServeMux
muxf.RequestWhenFalse(hl)
muxf.ResponseWhenFalse(hl)
stack.AddRequestModifier(muxf)
stack.AddResponseModifier(muxf)
configure("/logs", har.NewExportHandler(hl), mux)
configure("/logs/reset", har.NewResetHandler(hl), mux)
}
logger := martianlog.NewLogger()
logger.SetDecode(true)
stack.AddRequestModifier(logger)
stack.AddResponseModifier(logger)
if *marblLogging {
lsh := marbl.NewHandler()
lsm := marbl.NewModifier(lsh)
muxf := servemux.NewFilter(mux)
muxf.RequestWhenFalse(lsm)
muxf.ResponseWhenFalse(lsm)
stack.AddRequestModifier(muxf)
stack.AddResponseModifier(muxf)
// retrieve binary marbl logs
mux.Handle("/binlogs", lsh)
}
// Configure modifiers.
configure("/configure", m, mux)
// Verify assertions.
vh := verify.NewHandler()
vh.SetRequestVerifier(m)
vh.SetResponseVerifier(m)
configure("/verify", vh, mux)
// Reset verifications.
rh := verify.NewResetHandler()
rh.SetRequestVerifier(m)
rh.SetResponseVerifier(m)
configure("/verify/reset", rh, mux)
l, err := net.Listen("tcp", *addr)
if err != nil {
log.Fatal(err)
}
if *trafficShaping {
tsl := trafficshape.NewListener(l)
tsh := trafficshape.NewHandler(tsl)
configure("/shape-traffic", tsh, mux)
l = tsl
}
lAPI, err := net.Listen("tcp", *apiAddr)
if err != nil {
log.Fatal(err)
}
log.Printf("martian: starting proxy on %s and api on %s", l.Addr().String(), lAPI.Addr().String())
go p.Serve(l)
go http.Serve(lAPI, mux)
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, os.Interrupt, os.Kill)
<-sigc
log.Println("martian: shutting down")
}
func init() {
martian.Init()
}
// configure installs a configuration handler at path.
func configure(pattern string, handler http.Handler, mux *http.ServeMux) {
if *allowCORS {
handler = cors.NewHandler(handler)
}
// register handler for martian.proxy to be forwarded to
// local API server
mux.Handle(path.Join(*api, pattern), handler)
// register handler for local API server
p := path.Join("localhost"+*apiAddr, pattern)
mux.Handle(p, handler)
}