-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
static.go
246 lines (195 loc) · 5.52 KB
/
static.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
// Copyright (C) 2015 Audrius Butkevicius and Contributors (see the CONTRIBUTORS file).
package client
import (
"context"
"crypto/tls"
"fmt"
"net"
"net/url"
"time"
"github.com/pkg/errors"
"github.com/syncthing/syncthing/lib/dialer"
syncthingprotocol "github.com/syncthing/syncthing/lib/protocol"
"github.com/syncthing/syncthing/lib/relay/protocol"
)
type staticClient struct {
commonClient
uri *url.URL
config *tls.Config
messageTimeout time.Duration
connectTimeout time.Duration
conn *tls.Conn
}
func newStaticClient(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation, timeout time.Duration) *staticClient {
c := &staticClient{
uri: uri,
config: configForCerts(certs),
messageTimeout: time.Minute * 2,
connectTimeout: timeout,
}
c.commonClient = newCommonClient(invitations, c.serve, c.String())
return c
}
func (c *staticClient) serve(ctx context.Context) error {
if err := c.connect(ctx); err != nil {
l.Debugf("Could not connect to relay %s: %s", c.uri, err)
return err
}
l.Debugln(c, "connected", c.conn.RemoteAddr())
defer c.disconnect()
if err := c.join(); err != nil {
l.Debugf("Could not join relay %s: %s", c.uri, err)
return err
}
if err := c.conn.SetDeadline(time.Time{}); err != nil {
l.Debugln("Relay set deadline:", err)
return err
}
l.Infof("Joined relay %s://%s", c.uri.Scheme, c.uri.Host)
messages := make(chan interface{})
errorsc := make(chan error, 1)
go messageReader(ctx, c.conn, messages, errorsc)
timeout := time.NewTimer(c.messageTimeout)
for {
select {
case message := <-messages:
timeout.Reset(c.messageTimeout)
l.Debugf("%s received message %T", c, message)
switch msg := message.(type) {
case protocol.Ping:
if err := protocol.WriteMessage(c.conn, protocol.Pong{}); err != nil {
l.Debugln("Relay write:", err)
return err
}
l.Debugln(c, "sent pong")
case protocol.SessionInvitation:
ip := net.IP(msg.Address)
if len(ip) == 0 || ip.IsUnspecified() {
msg.Address = remoteIPBytes(c.conn)
}
select {
case c.invitations <- msg:
case <-ctx.Done():
l.Debugln(c, "stopping")
return ctx.Err()
}
case protocol.RelayFull:
l.Debugf("Disconnected from relay %s due to it becoming full.", c.uri)
return errors.New("relay full")
default:
l.Debugln("Relay: protocol error: unexpected message %v", msg)
return fmt.Errorf("protocol error: unexpected message %v", msg)
}
case <-ctx.Done():
l.Debugln(c, "stopping")
return ctx.Err()
case err := <-errorsc:
l.Debugf("Disconnecting from relay %s due to error: %s", c.uri, err)
return err
case <-timeout.C:
l.Debugln(c, "timed out")
return errors.New("timed out")
}
}
}
func (c *staticClient) String() string {
return fmt.Sprintf("StaticClient:%p@%s", c, c.URI())
}
func (c *staticClient) URI() *url.URL {
return c.uri
}
func (c *staticClient) connect(ctx context.Context) error {
if c.uri.Scheme != "relay" {
return fmt.Errorf("unsupported relay scheme: %v", c.uri.Scheme)
}
timeoutCtx, cancel := context.WithTimeout(ctx, c.connectTimeout)
defer cancel()
tcpConn, err := dialer.DialContext(timeoutCtx, "tcp", c.uri.Host)
if err != nil {
return err
}
// Copy the TLS config and set the server name we're connecting to. In
// many cases this will be an IP address, in which case it's a no-op. In
// other cases it will be a hostname, which will cause the TLS stack to
// send SNI.
cfg := c.config
if host, _, err := net.SplitHostPort(c.uri.Host); err == nil {
cfg = cfg.Clone()
cfg.ServerName = host
}
conn := tls.Client(tcpConn, cfg)
if err := conn.SetDeadline(time.Now().Add(c.connectTimeout)); err != nil {
conn.Close()
return err
}
if err := performHandshakeAndValidation(conn, c.uri); err != nil {
conn.Close()
return err
}
c.conn = conn
return nil
}
func (c *staticClient) disconnect() {
l.Debugln(c, "disconnecting")
c.conn.Close()
}
func (c *staticClient) join() error {
if err := protocol.WriteMessage(c.conn, protocol.JoinRelayRequest{}); err != nil {
return err
}
message, err := protocol.ReadMessage(c.conn)
if err != nil {
return err
}
switch msg := message.(type) {
case protocol.Response:
if msg.Code != 0 {
return &incorrectResponseCodeErr{msg.Code, msg.Message}
}
case protocol.RelayFull:
return errors.New("relay full")
default:
return fmt.Errorf("protocol error: expecting response got %v", msg)
}
return nil
}
func performHandshakeAndValidation(conn *tls.Conn, uri *url.URL) error {
if err := conn.Handshake(); err != nil {
return err
}
cs := conn.ConnectionState()
if !cs.NegotiatedProtocolIsMutual || cs.NegotiatedProtocol != protocol.ProtocolName {
return errors.New("protocol negotiation error")
}
q := uri.Query()
relayIDs := q.Get("id")
if relayIDs != "" {
relayID, err := syncthingprotocol.DeviceIDFromString(relayIDs)
if err != nil {
return errors.Wrap(err, "relay address contains invalid verification id")
}
certs := cs.PeerCertificates
if cl := len(certs); cl != 1 {
return fmt.Errorf("unexpected certificate count: %d", cl)
}
remoteID := syncthingprotocol.NewDeviceID(certs[0].Raw)
if remoteID != relayID {
return fmt.Errorf("relay id does not match. Expected %v got %v", relayID, remoteID)
}
}
return nil
}
func messageReader(ctx context.Context, conn net.Conn, messages chan<- interface{}, errors chan<- error) {
for {
msg, err := protocol.ReadMessage(conn)
if err != nil {
errors <- err
return
}
select {
case messages <- msg:
case <-ctx.Done():
return
}
}
}