2015-10-16 22:59:24 +00:00
|
|
|
// Copyright (C) 2015 Audrius Butkevicius and Contributors (see the CONTRIBUTORS file).
|
|
|
|
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2019-11-21 07:41:15 +00:00
|
|
|
"context"
|
2015-10-16 22:59:24 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
2019-11-23 15:20:54 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2015-11-26 23:41:11 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/dialer"
|
2015-10-16 22:59:24 +00:00
|
|
|
syncthingprotocol "github.com/syncthing/syncthing/lib/protocol"
|
|
|
|
"github.com/syncthing/syncthing/lib/relay/protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
type staticClient struct {
|
2019-07-09 09:40:30 +00:00
|
|
|
commonClient
|
2015-10-16 22:59:24 +00:00
|
|
|
|
2019-07-09 09:40:30 +00:00
|
|
|
uri *url.URL
|
2015-10-16 22:59:24 +00:00
|
|
|
|
|
|
|
config *tls.Config
|
|
|
|
|
2015-11-23 21:14:46 +00:00
|
|
|
messageTimeout time.Duration
|
|
|
|
connectTimeout time.Duration
|
2015-10-16 22:59:24 +00:00
|
|
|
|
|
|
|
conn *tls.Conn
|
|
|
|
|
|
|
|
connected bool
|
|
|
|
latency time.Duration
|
|
|
|
}
|
|
|
|
|
2015-11-23 21:14:46 +00:00
|
|
|
func newStaticClient(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation, timeout time.Duration) RelayClient {
|
2019-07-09 09:40:30 +00:00
|
|
|
c := &staticClient{
|
|
|
|
uri: uri,
|
2015-10-16 22:59:24 +00:00
|
|
|
|
|
|
|
config: configForCerts(certs),
|
|
|
|
|
2015-11-23 21:14:46 +00:00
|
|
|
messageTimeout: time.Minute * 2,
|
|
|
|
connectTimeout: timeout,
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
2019-11-21 07:41:15 +00:00
|
|
|
c.commonClient = newCommonClient(invitations, c.serve, c.String())
|
2019-07-09 09:40:30 +00:00
|
|
|
return c
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 07:41:15 +00:00
|
|
|
func (c *staticClient) serve(ctx context.Context) error {
|
2019-11-26 07:39:51 +00:00
|
|
|
if err := c.connect(ctx); err != nil {
|
2016-03-18 07:25:37 +00:00
|
|
|
l.Infof("Could not connect to relay %s: %s", c.uri, err)
|
2019-07-09 09:40:30 +00:00
|
|
|
return err
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
l.Debugln(c, "connected", c.conn.RemoteAddr())
|
2019-07-09 09:40:30 +00:00
|
|
|
defer c.disconnect()
|
2015-10-16 22:59:24 +00:00
|
|
|
|
|
|
|
if err := c.join(); err != nil {
|
2016-03-18 07:25:37 +00:00
|
|
|
l.Infof("Could not join relay %s: %s", c.uri, err)
|
2019-07-09 09:40:30 +00:00
|
|
|
return err
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.conn.SetDeadline(time.Time{}); err != nil {
|
|
|
|
l.Infoln("Relay set deadline:", err)
|
2019-07-09 09:40:30 +00:00
|
|
|
return err
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
2016-05-17 00:05:38 +00:00
|
|
|
l.Infof("Joined relay %s://%s", c.uri.Scheme, c.uri.Host)
|
|
|
|
defer l.Infof("Disconnected from relay %s://%s", c.uri.Scheme, c.uri.Host)
|
2015-10-16 22:59:24 +00:00
|
|
|
|
|
|
|
c.mut.Lock()
|
|
|
|
c.connected = true
|
|
|
|
c.mut.Unlock()
|
|
|
|
|
|
|
|
messages := make(chan interface{})
|
|
|
|
errors := make(chan error, 1)
|
|
|
|
|
2019-11-21 07:41:15 +00:00
|
|
|
go messageReader(ctx, c.conn, messages, errors)
|
2015-10-16 22:59:24 +00:00
|
|
|
|
2015-11-23 21:14:46 +00:00
|
|
|
timeout := time.NewTimer(c.messageTimeout)
|
2015-10-16 22:59:24 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case message := <-messages:
|
2015-11-23 21:14:46 +00:00
|
|
|
timeout.Reset(c.messageTimeout)
|
2015-10-16 22:59:24 +00:00
|
|
|
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.Infoln("Relay write:", err)
|
2019-07-09 09:40:30 +00:00
|
|
|
return err
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
2019-07-09 09:40:30 +00:00
|
|
|
l.Debugln(c, "sent pong")
|
2015-10-16 22:59:24 +00:00
|
|
|
|
|
|
|
case protocol.SessionInvitation:
|
|
|
|
ip := net.IP(msg.Address)
|
|
|
|
if len(ip) == 0 || ip.IsUnspecified() {
|
2016-05-31 14:42:10 +00:00
|
|
|
msg.Address = remoteIPBytes(c.conn)
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
|
|
|
c.invitations <- msg
|
|
|
|
|
2015-11-20 23:42:49 +00:00
|
|
|
case protocol.RelayFull:
|
2016-03-18 07:25:37 +00:00
|
|
|
l.Infof("Disconnected from relay %s due to it becoming full.", c.uri)
|
2019-07-09 09:40:30 +00:00
|
|
|
return fmt.Errorf("relay full")
|
2015-11-20 23:42:49 +00:00
|
|
|
|
2015-10-16 22:59:24 +00:00
|
|
|
default:
|
|
|
|
l.Infoln("Relay: protocol error: unexpected message %v", msg)
|
2019-07-09 09:40:30 +00:00
|
|
|
return fmt.Errorf("protocol error: unexpected message %v", msg)
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 07:41:15 +00:00
|
|
|
case <-ctx.Done():
|
2015-10-16 22:59:24 +00:00
|
|
|
l.Debugln(c, "stopping")
|
2019-07-09 09:40:30 +00:00
|
|
|
return nil
|
2015-10-16 22:59:24 +00:00
|
|
|
|
|
|
|
case err := <-errors:
|
2019-07-09 09:40:30 +00:00
|
|
|
l.Infof("Disconnecting from relay %s due to error: %s", c.uri, err)
|
|
|
|
return err
|
2015-10-16 22:59:24 +00:00
|
|
|
|
|
|
|
case <-timeout.C:
|
|
|
|
l.Debugln(c, "timed out")
|
2019-07-09 09:40:30 +00:00
|
|
|
return fmt.Errorf("timed out")
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *staticClient) StatusOK() bool {
|
|
|
|
c.mut.RLock()
|
|
|
|
con := c.connected
|
|
|
|
c.mut.RUnlock()
|
|
|
|
return con
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *staticClient) Latency() time.Duration {
|
|
|
|
c.mut.RLock()
|
|
|
|
lat := c.latency
|
|
|
|
c.mut.RUnlock()
|
|
|
|
return lat
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *staticClient) String() string {
|
|
|
|
return fmt.Sprintf("StaticClient:%p@%s", c, c.URI())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *staticClient) URI() *url.URL {
|
|
|
|
return c.uri
|
|
|
|
}
|
|
|
|
|
2019-11-26 07:39:51 +00:00
|
|
|
func (c *staticClient) connect(ctx context.Context) error {
|
2015-10-16 22:59:24 +00:00
|
|
|
if c.uri.Scheme != "relay" {
|
2019-07-17 08:55:28 +00:00
|
|
|
return fmt.Errorf("unsupported relay scheme: %v", c.uri.Scheme)
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t0 := time.Now()
|
2020-01-23 21:37:35 +00:00
|
|
|
timeoutCtx, cancel := context.WithTimeout(ctx, c.connectTimeout)
|
2019-11-26 07:39:51 +00:00
|
|
|
defer cancel()
|
|
|
|
tcpConn, err := dialer.DialContext(timeoutCtx, "tcp", c.uri.Host)
|
2015-10-16 22:59:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.mut.Lock()
|
|
|
|
c.latency = time.Since(t0)
|
|
|
|
c.mut.Unlock()
|
|
|
|
|
|
|
|
conn := tls.Client(tcpConn, c.config)
|
|
|
|
|
2015-11-23 21:14:46 +00:00
|
|
|
if err := conn.SetDeadline(time.Now().Add(c.connectTimeout)); err != nil {
|
2015-10-16 22:59:24 +00:00
|
|
|
conn.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := performHandshakeAndValidation(conn, c.uri); err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.conn = conn
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-18 22:29:04 +00:00
|
|
|
func (c *staticClient) disconnect() {
|
|
|
|
l.Debugln(c, "disconnecting")
|
2015-10-16 22:59:24 +00:00
|
|
|
c.mut.Lock()
|
|
|
|
c.connected = false
|
|
|
|
c.mut.Unlock()
|
|
|
|
|
|
|
|
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 {
|
2019-07-17 08:55:28 +00:00
|
|
|
return fmt.Errorf("incorrect response code %d: %s", msg.Code, msg.Message)
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
2015-11-20 23:42:49 +00:00
|
|
|
case protocol.RelayFull:
|
|
|
|
return fmt.Errorf("relay full")
|
|
|
|
|
2015-10-16 22:59:24 +00:00
|
|
|
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 fmt.Errorf("protocol negotiation error")
|
|
|
|
}
|
|
|
|
|
|
|
|
q := uri.Query()
|
|
|
|
relayIDs := q.Get("id")
|
|
|
|
if relayIDs != "" {
|
|
|
|
relayID, err := syncthingprotocol.DeviceIDFromString(relayIDs)
|
|
|
|
if err != nil {
|
2019-11-23 15:20:54 +00:00
|
|
|
return errors.Wrap(err, "relay address contains invalid verification id")
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-11-21 07:41:15 +00:00
|
|
|
func messageReader(ctx context.Context, conn net.Conn, messages chan<- interface{}, errors chan<- error) {
|
2015-10-16 22:59:24 +00:00
|
|
|
for {
|
|
|
|
msg, err := protocol.ReadMessage(conn)
|
|
|
|
if err != nil {
|
|
|
|
errors <- err
|
|
|
|
return
|
|
|
|
}
|
2019-07-09 09:40:30 +00:00
|
|
|
select {
|
|
|
|
case messages <- msg:
|
2019-11-21 07:41:15 +00:00
|
|
|
case <-ctx.Done():
|
2019-07-09 09:40:30 +00:00
|
|
|
return
|
|
|
|
}
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
|
|
|
}
|