-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
72 lines (58 loc) · 1.45 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
package main
import (
"crypto/x509"
"net"
"os"
"os/signal"
"syscall"
"time"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/gomitmproxy"
"github.com/AdguardTeam/gomitmproxy/mitm"
btk "github.com/ghostsecurity/waf-btk/internal"
_ "net/http/pprof"
)
func main() {
// log.SetLevel(log.DEBUG)
// mitmproxy will generate it's own self-signed cert,
// but we need to provide a CA cert and private key
tlsCert, caPrivateKey, _ := btk.GenerateCACert()
caCert, err := x509.ParseCertificate(tlsCert)
if err != nil {
log.Fatal(err)
}
mitmConfig, err := mitm.NewConfig(caCert, caPrivateKey, nil)
if err != nil {
log.Fatal(err)
}
// sets the validity window of the proxy cert
// from: time.Now() - period
// to: time.Now() + period
// a 7 day period results in a 14 day validity window
mitmConfig.SetValidity(time.Hour * 24 * 7)
mitmConfig.SetOrganization(btk.PROXY_HOST)
// Prepare the proxy.
addr := &net.TCPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: 8888,
}
proxy := gomitmproxy.NewProxy(gomitmproxy.Config{
ListenAddr: addr,
APIHost: btk.PROXY_HOST,
MITMConfig: mitmConfig,
OnRequest: btk.RequestHandler,
// not used
// OnResponse: ResponseHandler,
// not used
// OnConnect: ConnectHandler,
})
err = proxy.Start()
if err != nil {
log.Fatal(err)
}
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM)
<-signalChannel
// Stop the proxy.
proxy.Close()
}