2015-10-16 22:59:24 +00:00
|
|
|
// Copyright (C) 2015 Audrius Butkevicius and Contributors (see the CONTRIBUTORS file).
|
|
|
|
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"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"
|
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type staticClient struct {
|
|
|
|
uri *url.URL
|
|
|
|
invitations chan protocol.SessionInvitation
|
|
|
|
|
|
|
|
closeInvitationsOnFinish bool
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
stop chan struct{}
|
|
|
|
stopped chan struct{}
|
|
|
|
|
|
|
|
conn *tls.Conn
|
|
|
|
|
|
|
|
mut sync.RWMutex
|
2016-05-04 19:38:12 +00:00
|
|
|
err error
|
2015-10-16 22:59:24 +00:00
|
|
|
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 {
|
2015-10-16 22:59:24 +00:00
|
|
|
closeInvitationsOnFinish := false
|
|
|
|
if invitations == nil {
|
|
|
|
closeInvitationsOnFinish = true
|
|
|
|
invitations = make(chan protocol.SessionInvitation)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &staticClient{
|
|
|
|
uri: uri,
|
|
|
|
invitations: invitations,
|
|
|
|
|
|
|
|
closeInvitationsOnFinish: closeInvitationsOnFinish,
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
stop: make(chan struct{}),
|
|
|
|
stopped: make(chan struct{}),
|
|
|
|
|
2016-05-04 19:38:12 +00:00
|
|
|
mut: sync.NewRWMutex(),
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *staticClient) Serve() {
|
2016-11-11 22:23:48 +00:00
|
|
|
defer c.cleanup()
|
2015-10-16 22:59:24 +00:00
|
|
|
c.stop = make(chan struct{})
|
|
|
|
c.stopped = make(chan struct{})
|
|
|
|
defer close(c.stopped)
|
|
|
|
|
|
|
|
if err := c.connect(); err != nil {
|
2016-03-18 07:25:37 +00:00
|
|
|
l.Infof("Could not connect to relay %s: %s", c.uri, err)
|
2016-05-04 19:38:12 +00:00
|
|
|
c.setError(err)
|
2015-10-16 22:59:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
l.Debugln(c, "connected", c.conn.RemoteAddr())
|
|
|
|
|
|
|
|
if err := c.join(); err != nil {
|
|
|
|
c.conn.Close()
|
2016-03-18 07:25:37 +00:00
|
|
|
l.Infof("Could not join relay %s: %s", c.uri, err)
|
2016-05-04 19:38:12 +00:00
|
|
|
c.setError(err)
|
2015-10-16 22:59:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.conn.SetDeadline(time.Time{}); err != nil {
|
|
|
|
c.conn.Close()
|
|
|
|
l.Infoln("Relay set deadline:", err)
|
2016-05-04 19:38:12 +00:00
|
|
|
c.setError(err)
|
2015-10-16 22:59:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
go messageReader(c.conn, messages, errors)
|
|
|
|
|
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)
|
2016-05-04 19:38:12 +00:00
|
|
|
c.setError(err)
|
2015-12-18 22:29:04 +00:00
|
|
|
c.disconnect()
|
|
|
|
} else {
|
|
|
|
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)
|
2016-05-04 19:38:12 +00:00
|
|
|
c.setError(fmt.Errorf("Relay full"))
|
2015-12-18 22:29:04 +00:00
|
|
|
c.disconnect()
|
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)
|
2016-05-04 19:38:12 +00:00
|
|
|
c.setError(fmt.Errorf("protocol error: unexpected message %v", msg))
|
2015-12-18 22:29:04 +00:00
|
|
|
c.disconnect()
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case <-c.stop:
|
|
|
|
l.Debugln(c, "stopping")
|
2016-05-04 19:38:12 +00:00
|
|
|
c.setError(nil)
|
2015-12-18 22:29:04 +00:00
|
|
|
c.disconnect()
|
2015-10-16 22:59:24 +00:00
|
|
|
|
2015-12-18 22:29:04 +00:00
|
|
|
// We always exit via this branch of the select, to make sure the
|
|
|
|
// the reader routine exits.
|
2015-10-16 22:59:24 +00:00
|
|
|
case err := <-errors:
|
2015-12-18 22:29:04 +00:00
|
|
|
close(errors)
|
|
|
|
close(messages)
|
|
|
|
c.mut.Lock()
|
|
|
|
if c.connected {
|
|
|
|
c.conn.Close()
|
|
|
|
c.connected = false
|
2016-03-18 07:25:37 +00:00
|
|
|
l.Infof("Disconnecting from relay %s due to error: %s", c.uri, err)
|
2016-05-04 19:38:12 +00:00
|
|
|
c.err = err
|
|
|
|
} else {
|
|
|
|
c.err = nil
|
2015-12-18 22:29:04 +00:00
|
|
|
}
|
|
|
|
c.mut.Unlock()
|
2015-10-16 22:59:24 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
case <-timeout.C:
|
|
|
|
l.Debugln(c, "timed out")
|
2015-12-18 22:29:04 +00:00
|
|
|
c.disconnect()
|
2016-05-04 19:38:12 +00:00
|
|
|
c.setError(fmt.Errorf("timed out"))
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *staticClient) Stop() {
|
|
|
|
if c.stop == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
close(c.stop)
|
|
|
|
<-c.stopped
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *staticClient) Invitations() chan protocol.SessionInvitation {
|
|
|
|
c.mut.RLock()
|
|
|
|
inv := c.invitations
|
|
|
|
c.mut.RUnlock()
|
|
|
|
return inv
|
|
|
|
}
|
|
|
|
|
2016-11-11 22:23:48 +00:00
|
|
|
func (c *staticClient) cleanup() {
|
|
|
|
c.mut.Lock()
|
|
|
|
if c.closeInvitationsOnFinish {
|
|
|
|
close(c.invitations)
|
|
|
|
c.invitations = make(chan protocol.SessionInvitation)
|
|
|
|
}
|
|
|
|
c.mut.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-10-16 22:59:24 +00:00
|
|
|
func (c *staticClient) connect() error {
|
|
|
|
if c.uri.Scheme != "relay" {
|
|
|
|
return fmt.Errorf("Unsupported relay schema: %v", c.uri.Scheme)
|
|
|
|
}
|
|
|
|
|
|
|
|
t0 := time.Now()
|
2015-11-26 23:41:11 +00:00
|
|
|
tcpConn, err := dialer.DialTimeout("tcp", c.uri.Host, c.connectTimeout)
|
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()
|
|
|
|
}
|
|
|
|
|
2016-05-04 19:38:12 +00:00
|
|
|
func (c *staticClient) setError(err error) {
|
|
|
|
c.mut.Lock()
|
|
|
|
c.err = err
|
|
|
|
c.mut.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *staticClient) Error() error {
|
|
|
|
c.mut.RLock()
|
|
|
|
err := c.err
|
|
|
|
c.mut.RUnlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-16 22:59:24 +00:00
|
|
|
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 fmt.Errorf("Incorrect response code %d: %s", msg.Code, msg.Message)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return fmt.Errorf("relay address contains invalid verification id: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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(conn net.Conn, messages chan<- interface{}, errors chan<- error) {
|
|
|
|
for {
|
|
|
|
msg, err := protocol.ReadMessage(conn)
|
|
|
|
if err != nil {
|
|
|
|
errors <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
messages <- msg
|
|
|
|
}
|
|
|
|
}
|