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"
|
2022-08-16 08:01:49 +00:00
|
|
|
"errors"
|
2015-10-16 22:59:24 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-07-21 07:49:09 +00:00
|
|
|
func newStaticClient(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation, timeout time.Duration) *staticClient {
|
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 {
|
2021-05-10 20:26:25 +00:00
|
|
|
l.Debugf("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 {
|
2021-05-10 20:26:25 +00:00
|
|
|
l.Debugf("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 {
|
2021-05-10 20:26:25 +00:00
|
|
|
l.Debugln("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)
|
2015-10-16 22:59:24 +00:00
|
|
|
|
|
|
|
messages := make(chan interface{})
|
2020-03-03 21:40:00 +00:00
|
|
|
errorsc := make(chan error, 1)
|
2015-10-16 22:59:24 +00:00
|
|
|
|
2020-03-03 21:40:00 +00:00
|
|
|
go messageReader(ctx, c.conn, messages, errorsc)
|
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 {
|
2021-05-10 20:26:25 +00:00
|
|
|
l.Debugln("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
|
|
|
}
|
2021-05-10 20:25:43 +00:00
|
|
|
select {
|
|
|
|
case c.invitations <- msg:
|
|
|
|
case <-ctx.Done():
|
|
|
|
l.Debugln(c, "stopping")
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2015-10-16 22:59:24 +00:00
|
|
|
|
2015-11-20 23:42:49 +00:00
|
|
|
case protocol.RelayFull:
|
2021-05-10 20:26:25 +00:00
|
|
|
l.Debugf("Disconnected from relay %s due to it becoming full.", c.uri)
|
2020-03-03 21:40:00 +00:00
|
|
|
return errors.New("relay full")
|
2015-11-20 23:42:49 +00:00
|
|
|
|
2015-10-16 22:59:24 +00:00
|
|
|
default:
|
2021-05-10 20:26:25 +00:00
|
|
|
l.Debugln("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")
|
2021-05-10 20:26:25 +00:00
|
|
|
return ctx.Err()
|
2015-10-16 22:59:24 +00:00
|
|
|
|
2020-03-03 21:40:00 +00:00
|
|
|
case err := <-errorsc:
|
2021-05-10 20:26:25 +00:00
|
|
|
l.Debugf("Disconnecting from relay %s due to error: %s", c.uri, err)
|
2019-07-09 09:40:30 +00:00
|
|
|
return err
|
2015-10-16 22:59:24 +00:00
|
|
|
|
|
|
|
case <-timeout.C:
|
|
|
|
l.Debugln(c, "timed out")
|
2020-03-03 21:40:00 +00:00
|
|
|
return errors.New("timed out")
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-11-22 07:31:03 +00:00
|
|
|
// 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)
|
2015-10-16 22:59:24 +00:00
|
|
|
|
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.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 {
|
2020-06-16 07:27:34 +00:00
|
|
|
return &incorrectResponseCodeErr{msg.Code, msg.Message}
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
2015-11-20 23:42:49 +00:00
|
|
|
case protocol.RelayFull:
|
2020-03-03 21:40:00 +00:00
|
|
|
return errors.New("relay full")
|
2015-11-20 23:42:49 +00:00
|
|
|
|
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()
|
2022-07-28 15:14:49 +00:00
|
|
|
if cs.NegotiatedProtocol != protocol.ProtocolName {
|
2020-03-03 21:40:00 +00:00
|
|
|
return errors.New("protocol negotiation error")
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
q := uri.Query()
|
|
|
|
relayIDs := q.Get("id")
|
|
|
|
if relayIDs != "" {
|
|
|
|
relayID, err := syncthingprotocol.DeviceIDFromString(relayIDs)
|
|
|
|
if err != nil {
|
2022-08-16 08:01:49 +00:00
|
|
|
return fmt.Errorf("relay address contains invalid verification id: %w", err)
|
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
|
|
|
}
|
|
|
|
}
|