-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
57 lines (46 loc) · 1.41 KB
/
main.go
File metadata and controls
57 lines (46 loc) · 1.41 KB
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
package main
import (
"context"
"flag"
"log"
"net"
"net/http"
"github.com/elazarl/goproxy"
)
func main() {
patches := patchTLSServerName()
defer patches.Reset()
listenEndpoint := flag.String("l", "127.0.0.1:8080", "proxy listen address")
dnsEndpoint := flag.String("d", "8.8.8.8", "dns (over udp) server address")
flag.Parse()
dnsAddrPort, err := parseAddrPortWithDefaultPort(*dnsEndpoint, 53);
if err != nil {
panic(err)
}
net.DefaultResolver = &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{}
return d.DialContext(ctx, "udp", dnsAddrPort.String())
},
}
log.Printf("using %v to resolve domains\n", dnsAddrPort)
cert, err := loadCert()
if err != nil {
panic(err)
}
var customAlwaysMitm goproxy.FuncHttpsHandler = func(req string, ctx *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) {
return &goproxy.ConnectAction{Action: goproxy.ConnectMitm, TLSConfig: goproxy.TLSConfigFromCA(cert)}, req
};
proxy := goproxy.NewProxyHttpServer()
proxy.CertStore = newCertStorage()
proxy.OnRequest().HandleConnect(customAlwaysMitm)
proxy.OnRequest().DoFunc(func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
if req.URL.Scheme == "http" {
req.Host = "localhost"
}
return req, nil
})
log.Printf("listen at %v\n", *listenEndpoint)
panic(http.ListenAndServe(*listenEndpoint, proxy))
}