-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
157 lines (137 loc) · 3.77 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
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"github.com/aidansteele/flowdog/examples/account_id_emf"
"github.com/aidansteele/flowdog/examples/geneve_headers"
"github.com/aidansteele/flowdog/examples/lambda_acceptor"
"github.com/aidansteele/flowdog/examples/sts_rickroll"
"github.com/aidansteele/flowdog/examples/upsidedown"
"github.com/aidansteele/flowdog/examples/webfirehose"
"github.com/aidansteele/flowdog/gwlb"
"github.com/aidansteele/flowdog/gwlb/mirror"
"github.com/aidansteele/flowdog/gwlb/shark"
"github.com/aidansteele/flowdog/kmssigner"
"github.com/aidansteele/flowdog/mytls"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/firehose"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/aws/aws-sdk-go/service/kms/kmsiface"
"github.com/aws/aws-sdk-go/service/lambda"
_ "github.com/google/gopacket/layers"
"github.com/pkg/errors"
"net"
"net/http"
"os"
"time"
)
func main() {
ctx := context.Background()
sess, err := session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
})
if err != nil {
fmt.Printf("%+v\n", err)
panic(err)
}
err = setupTls(kms.New(sess))
if err != nil {
fmt.Printf("%+v\n", err)
panic(err)
}
conn, err := net.ListenUDP("udp", &net.UDPAddr{Port: 6081})
if err != nil {
panic(err)
}
wf := webfirehose.New(firehose.New(sess), ec2.New(sess), "webfirehose-Firehose-0k2UpKEJfMz0")
go func() {
err := wf.Run(ctx)
if err != nil {
fmt.Printf("%+v\n", err)
panic(err)
}
}()
chain := gwlb.Chain{
wf,
&geneve_headers.GeneveHeaders{},
&account_id_emf.AccountIdEmf{},
&sts_rickroll.StsRickroll{},
upsidedown.UpsideDown(),
//cloudfront_functions.NewRickroll(),
}
acceptor, err := lambda_acceptor.New(
lambda.New(sess),
os.Getenv("LAMBDA_ACCEPTOR_ARN"),
ec2.New(sess),
)
if err != nil {
fmt.Printf("%+v\n", err)
panic(err)
}
ctx := context.Background()
shark := shark.NewSharkServer()
sharkL, err := net.Listen("tcp", "127.0.0.1:7081")
if err != nil {
fmt.Printf("%+v\n", err)
panic(err)
}
mirrorCh := make(chan mirror.Packet)
go shark.Serve(ctx, sharkL, mirrorCh)
server := &gwlb.Server{
Handler: gwlb.DefaultHandler(chain, &tls.Config{KeyLogWriter: shark.KeyLogWriter()}),
Acceptor: acceptor,
KeyLogWriter: shark.KeyLogWriter(),
Mirror: mirrorCh,
}
go healthChecks()
err = server.Serve(ctx, conn)
if err != nil {
fmt.Printf("%+v\n", err)
panic(err)
}
}
func setupTls(kmsapi kmsiface.KMSAPI) error {
// TODO: env var or something
certBlock, _ := pem.Decode([]byte(`
-----BEGIN CERTIFICATE-----
MIIBfjCCASSgAwIBAgIBATAKBggqhkjOPQQDAjAcMRowGAYDVQQDExFBaWRhbiBL
TVMgVExTIENvLjAeFw0yMjAxMTEwMzU5NTBaFw0zMjAxMTEwMzU5NTBaMBwxGjAY
BgNVBAMTEUFpZGFuIEtNUyBUTFMgQ28uMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcD
QgAE7nApN4JApU+uocRZiefgFoHwpeTTHZkcqVUUTfJp67GmmV4WJ2MTXAH2W+MP
2M8d5cHmgcrSAi6vIzLjvRZBuKNXMFUwDgYDVR0PAQH/BAQDAgKEMBMGA1UdJQQM
MAoGCCsGAQUFBwMBMA8GA1UdEwEB/wQFMAMBAf8wHQYDVR0OBBYEFIAvKHJ7WiTA
vlpCx+VMI/+lwgfuMAoGCCqGSM49BAMCA0gAMEUCIFwVxZTneQupaLH2Cunk7FdE
nca45vDEVkjEZw7ERb7SAiEA5Sv3PbIBQSGihtWG4SOJ4tm8US29wM81w9Okl0vR
qpw=
-----END CERTIFICATE-----
`))
cert, err := x509.ParseCertificate(certBlock.Bytes)
if err != nil {
return errors.WithStack(err)
}
signer, err := kmssigner.New(kmsapi, os.Getenv("KEY_ID"))
if err != nil {
return errors.WithStack(err)
}
return mytls.SetIntermediateCA(
os.Getenv("INTERMEDIATE_CA_NAME"),
time.Now().AddDate(1, 0, 0),
cert,
signer,
)
}
func healthChecks() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("health checks")
w.Write([]byte("ok"))
})
err := http.ListenAndServe(":8080", nil)
if err != nil {
fmt.Printf("%+v\n", err)
panic(err)
}
}