-
Notifications
You must be signed in to change notification settings - Fork 244
/
preflight.go
160 lines (132 loc) · 3.37 KB
/
preflight.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
package preflight
import (
"crypto/tls"
"crypto/x509"
"fmt"
"net"
"net/http"
"net/url"
"strconv"
"sync"
"time"
"github.com/ethereum/go-ethereum/log"
"github.com/status-im/status-go/server/pairing"
"github.com/status-im/status-go/timesource"
"go.uber.org/zap"
"github.com/status-im/status-go/logutils"
"github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/server"
)
const (
outboundCheck = "/outbound_check"
headerPing = "ping"
headerPong = "pong"
)
func preflightHandler(w http.ResponseWriter, r *http.Request) {
ping := r.Header.Get(headerPing)
if ping == "" {
http.Error(w, "no value in 'ping' header", http.StatusBadRequest)
}
w.Header().Set(headerPong, ping)
}
func makeCert(address net.IP) (*tls.Certificate, []byte, error) {
now := timesource.GetCurrentTime()
log.Debug("makeCert", "system time", time.Now().String(), "timesource time", now.String())
notBefore := now.Add(-pairing.CertificateMaxClockDrift)
notAfter := now.Add(pairing.CertificateMaxClockDrift)
return server.GenerateTLSCert(notBefore, notAfter, []net.IP{address}, []string{})
}
func makeAndStartServer(cert *tls.Certificate, address net.IP) (string, func() error, error) {
wg := sync.WaitGroup{}
wg.Add(1)
waitForPortSet := func(int) {
wg.Done()
}
s := server.NewServer(
cert,
address.String(),
waitForPortSet,
logutils.ZapLogger().Named("Preflight Server"),
)
s.SetHandlers(server.HandlerPatternMap{outboundCheck: preflightHandler})
err := s.Start()
if err != nil {
return "", nil, err
}
wg.Wait()
return s.GetHostname() + ":" + strconv.Itoa(s.GetPort()), s.Stop, nil
}
func makeClient(certPem []byte) (*http.Client, error) {
rootCAs, err := x509.SystemCertPool()
if err != nil {
return nil, err
}
if ok := rootCAs.AppendCertsFromPEM(certPem); !ok {
return nil, fmt.Errorf("failed to append certPem to rootCAs")
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
InsecureSkipVerify: false, // MUST BE FALSE
RootCAs: rootCAs,
Time: timesource.GetCurrentTime,
},
}
return &http.Client{Transport: tr}, nil
}
func makeOutboundCheck(c *http.Client, host string) error {
u := url.URL{
Scheme: "https",
Host: host,
Path: outboundCheck,
}
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return err
}
ping, err := common.RandomAlphanumericString(64)
if err != nil {
return err
}
req.Header.Set(headerPing, ping)
resp, err := c.Do(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("response status not ok, received '%d' : '%s'", resp.StatusCode, resp.Status)
}
pong := resp.Header.Get(headerPong)
if pong != ping {
return fmt.Errorf("ping should match pong: ping '%s', pong '%s'", ping, pong)
}
return nil
}
func CheckOutbound() error {
// cert stuff
outboundIP, err := server.GetOutboundIP()
if err != nil {
return err
}
cert, certPem, err := makeCert(outboundIP)
if err != nil {
return err
}
// server stuff
host, stop, err := makeAndStartServer(cert, outboundIP)
if err != nil {
return err
}
defer func() {
err := stop()
if err != nil {
logutils.ZapLogger().Error("error while stopping preflight serve", zap.Error(err))
}
}()
// Client stuff
c, err := makeClient(certPem)
if err != nil {
return err
}
return makeOutboundCheck(c, host)
}