2015-06-28 12:09:53 -07:00
|
|
|
// Copyright (C) 2015 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2015-07-17 13:22:07 -07:00
|
|
|
package relay
|
2015-06-28 12:09:53 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2015-10-16 15:59:24 -07:00
|
|
|
|
2015-06-28 12:09:53 -07:00
|
|
|
"net/url"
|
2015-09-20 06:30:25 -07:00
|
|
|
"sort"
|
2015-06-28 12:09:53 -07:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
2015-09-20 06:30:25 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/events"
|
2015-09-22 10:38:46 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/relay/client"
|
|
|
|
"github.com/syncthing/syncthing/lib/relay/protocol"
|
2015-06-28 12:09:53 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
|
|
|
|
|
|
|
"github.com/thejerf/suture"
|
|
|
|
)
|
|
|
|
|
2015-09-20 06:30:25 -07:00
|
|
|
const (
|
|
|
|
eventBroadcasterCheckInterval = 10 * time.Second
|
|
|
|
)
|
|
|
|
|
2015-09-14 11:19:39 -07:00
|
|
|
type Svc struct {
|
|
|
|
*suture.Supervisor
|
|
|
|
cfg *config.Wrapper
|
|
|
|
tlsCfg *tls.Config
|
|
|
|
|
|
|
|
tokens map[string]suture.ServiceToken
|
2015-10-16 15:59:24 -07:00
|
|
|
clients map[string]client.RelayClient
|
2015-09-14 11:19:39 -07:00
|
|
|
mut sync.RWMutex
|
|
|
|
invitations chan protocol.SessionInvitation
|
|
|
|
conns chan *tls.Conn
|
|
|
|
}
|
|
|
|
|
2015-09-14 01:21:55 -07:00
|
|
|
func NewSvc(cfg *config.Wrapper, tlsCfg *tls.Config) *Svc {
|
2015-09-14 11:19:39 -07:00
|
|
|
conns := make(chan *tls.Conn)
|
2015-09-14 01:21:55 -07:00
|
|
|
|
2015-07-17 13:22:07 -07:00
|
|
|
svc := &Svc{
|
|
|
|
Supervisor: suture.New("Svc", suture.Spec{
|
2015-06-28 12:09:53 -07:00
|
|
|
Log: func(log string) {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln(log)
|
2015-06-28 12:09:53 -07:00
|
|
|
},
|
|
|
|
FailureBackoff: 5 * time.Minute,
|
|
|
|
FailureDecay: float64((10 * time.Minute) / time.Second),
|
|
|
|
FailureThreshold: 5,
|
|
|
|
}),
|
|
|
|
cfg: cfg,
|
|
|
|
tlsCfg: tlsCfg,
|
|
|
|
|
2015-09-14 01:21:55 -07:00
|
|
|
tokens: make(map[string]suture.ServiceToken),
|
2015-10-16 15:59:24 -07:00
|
|
|
clients: make(map[string]client.RelayClient),
|
2015-09-14 01:21:55 -07:00
|
|
|
mut: sync.NewRWMutex(),
|
2015-06-28 12:09:53 -07:00
|
|
|
invitations: make(chan protocol.SessionInvitation),
|
2015-09-14 01:21:55 -07:00
|
|
|
conns: conns,
|
2015-06-28 12:09:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
rcfg := cfg.Raw()
|
|
|
|
svc.CommitConfiguration(rcfg, rcfg)
|
|
|
|
cfg.Subscribe(svc)
|
|
|
|
|
|
|
|
receiver := &invitationReceiver{
|
|
|
|
tlsCfg: tlsCfg,
|
|
|
|
conns: conns,
|
|
|
|
invitations: svc.invitations,
|
2015-08-23 00:39:53 -07:00
|
|
|
stop: make(chan struct{}),
|
2015-06-28 12:09:53 -07:00
|
|
|
}
|
|
|
|
|
2015-09-20 06:30:25 -07:00
|
|
|
eventBc := &eventBroadcaster{
|
2015-10-20 06:14:05 -07:00
|
|
|
svc: svc,
|
|
|
|
stop: make(chan struct{}),
|
2015-09-20 06:30:25 -07:00
|
|
|
}
|
|
|
|
|
2015-08-23 00:39:53 -07:00
|
|
|
svc.Add(receiver)
|
2015-09-20 06:30:25 -07:00
|
|
|
svc.Add(eventBc)
|
2015-06-28 12:09:53 -07:00
|
|
|
|
|
|
|
return svc
|
|
|
|
}
|
|
|
|
|
2015-07-17 13:22:07 -07:00
|
|
|
func (s *Svc) VerifyConfiguration(from, to config.Configuration) error {
|
2015-06-28 12:09:53 -07:00
|
|
|
for _, addr := range to.Options.RelayServers {
|
|
|
|
_, err := url.Parse(addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-17 13:22:07 -07:00
|
|
|
func (s *Svc) CommitConfiguration(from, to config.Configuration) bool {
|
2015-07-23 12:39:44 -07:00
|
|
|
existing := make(map[string]*url.URL, len(to.Options.RelayServers))
|
|
|
|
|
2015-06-28 12:09:53 -07:00
|
|
|
for _, addr := range to.Options.RelayServers {
|
|
|
|
uri, err := url.Parse(addr)
|
|
|
|
if err != nil {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln("Failed to parse relay address", addr, err)
|
2015-06-28 12:09:53 -07:00
|
|
|
continue
|
|
|
|
}
|
2015-07-23 12:39:44 -07:00
|
|
|
existing[uri.String()] = uri
|
|
|
|
}
|
|
|
|
|
2015-08-23 00:39:53 -07:00
|
|
|
s.mut.Lock()
|
|
|
|
|
2015-07-23 12:39:44 -07:00
|
|
|
for key, uri := range existing {
|
|
|
|
_, ok := s.tokens[key]
|
2015-06-28 12:09:53 -07:00
|
|
|
if !ok {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln("Connecting to relay", uri)
|
2015-10-16 15:59:24 -07:00
|
|
|
c, err := client.NewClient(uri, s.tlsCfg.Certificates, s.invitations)
|
|
|
|
if err != nil {
|
|
|
|
l.Debugln("Failed to connect to relay", uri, err)
|
|
|
|
continue
|
|
|
|
}
|
2015-07-23 12:39:44 -07:00
|
|
|
s.tokens[key] = s.Add(c)
|
|
|
|
s.clients[key] = c
|
2015-06-28 12:09:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-23 12:39:44 -07:00
|
|
|
for key, token := range s.tokens {
|
|
|
|
_, ok := existing[key]
|
2015-06-28 12:09:53 -07:00
|
|
|
if !ok {
|
|
|
|
err := s.Remove(token)
|
2015-07-23 12:39:44 -07:00
|
|
|
delete(s.tokens, key)
|
|
|
|
delete(s.clients, key)
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln("Disconnecting from relay", key, err)
|
2015-06-28 12:09:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-23 00:39:53 -07:00
|
|
|
s.mut.Unlock()
|
|
|
|
|
2015-06-28 12:09:53 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-09-20 06:30:25 -07:00
|
|
|
type Status struct {
|
|
|
|
URL string
|
|
|
|
OK bool
|
|
|
|
Latency int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Relays return the list of relays that currently have an OK status.
|
|
|
|
func (s *Svc) Relays() []string {
|
2015-09-09 03:58:57 -07:00
|
|
|
if s == nil {
|
|
|
|
// A nil client does not have a status, really. Yet we may be called
|
|
|
|
// this way, for raisins...
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-28 12:09:53 -07:00
|
|
|
s.mut.RLock()
|
2015-09-20 06:30:25 -07:00
|
|
|
relays := make([]string, 0, len(s.clients))
|
2015-10-16 15:59:24 -07:00
|
|
|
for _, client := range s.clients {
|
|
|
|
relays = append(relays, client.URI().String())
|
2015-09-20 06:30:25 -07:00
|
|
|
}
|
|
|
|
s.mut.RUnlock()
|
|
|
|
|
|
|
|
sort.Strings(relays)
|
|
|
|
|
|
|
|
return relays
|
|
|
|
}
|
|
|
|
|
|
|
|
// RelayStatus returns the latency and OK status for a given relay.
|
|
|
|
func (s *Svc) RelayStatus(uri string) (time.Duration, bool) {
|
|
|
|
if s == nil {
|
|
|
|
// A nil client does not have a status, really. Yet we may be called
|
|
|
|
// this way, for raisins...
|
|
|
|
return time.Hour, false
|
2015-06-28 12:09:53 -07:00
|
|
|
}
|
2015-09-20 06:30:25 -07:00
|
|
|
|
|
|
|
s.mut.RLock()
|
2015-10-16 15:59:24 -07:00
|
|
|
for _, client := range s.clients {
|
|
|
|
if client.URI().String() == uri {
|
|
|
|
return client.Latency(), client.StatusOK()
|
|
|
|
}
|
2015-09-20 06:30:25 -07:00
|
|
|
}
|
2015-10-16 15:59:24 -07:00
|
|
|
s.mut.RUnlock()
|
2015-09-20 06:30:25 -07:00
|
|
|
|
2015-10-16 15:59:24 -07:00
|
|
|
return time.Hour, false
|
2015-06-28 12:09:53 -07:00
|
|
|
}
|
|
|
|
|
2015-09-14 11:19:39 -07:00
|
|
|
// Accept returns a new *tls.Conn. The connection is already handshaken.
|
|
|
|
func (s *Svc) Accept() *tls.Conn {
|
2015-09-14 01:21:55 -07:00
|
|
|
return <-s.conns
|
|
|
|
}
|
|
|
|
|
2015-06-28 12:09:53 -07:00
|
|
|
type invitationReceiver struct {
|
|
|
|
invitations chan protocol.SessionInvitation
|
|
|
|
tlsCfg *tls.Config
|
2015-09-14 11:19:39 -07:00
|
|
|
conns chan<- *tls.Conn
|
2015-06-28 12:09:53 -07:00
|
|
|
stop chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *invitationReceiver) Serve() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case inv := <-r.invitations:
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln("Received relay invitation", inv)
|
2015-06-28 12:09:53 -07:00
|
|
|
conn, err := client.JoinSession(inv)
|
|
|
|
if err != nil {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugf("Failed to join relay session %s: %v", inv, err)
|
2015-06-28 12:09:53 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var tc *tls.Conn
|
|
|
|
|
|
|
|
if inv.ServerSocket {
|
|
|
|
tc = tls.Server(conn, r.tlsCfg)
|
|
|
|
} else {
|
|
|
|
tc = tls.Client(conn, r.tlsCfg)
|
|
|
|
}
|
|
|
|
err = tc.Handshake()
|
|
|
|
if err != nil {
|
|
|
|
l.Infof("TLS handshake (BEP/relay %s): %v", inv, err)
|
|
|
|
tc.Close()
|
|
|
|
continue
|
|
|
|
}
|
2015-09-14 11:19:39 -07:00
|
|
|
r.conns <- tc
|
2015-08-23 00:39:53 -07:00
|
|
|
|
2015-06-28 12:09:53 -07:00
|
|
|
case <-r.stop:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *invitationReceiver) Stop() {
|
2015-08-23 00:39:53 -07:00
|
|
|
close(r.stop)
|
2015-06-28 12:09:53 -07:00
|
|
|
}
|
2015-07-23 12:39:44 -07:00
|
|
|
|
2015-09-20 06:30:25 -07:00
|
|
|
// The eventBroadcaster sends a RelayStateChanged event when the relay status
|
|
|
|
// changes. We need this somewhat ugly polling mechanism as there's currently
|
|
|
|
// no way to get the event feed directly from the relay lib. This may be
|
2015-10-16 15:59:24 -07:00
|
|
|
// something to revisit later, possibly.
|
2015-09-20 06:30:25 -07:00
|
|
|
type eventBroadcaster struct {
|
|
|
|
svc *Svc
|
|
|
|
stop chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *eventBroadcaster) Serve() {
|
|
|
|
timer := time.NewTicker(eventBroadcasterCheckInterval)
|
|
|
|
defer timer.Stop()
|
|
|
|
|
|
|
|
var prevOKRelays []string
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timer.C:
|
|
|
|
curOKRelays := e.svc.Relays()
|
|
|
|
|
|
|
|
changed := len(curOKRelays) != len(prevOKRelays)
|
|
|
|
if !changed {
|
|
|
|
for i := range curOKRelays {
|
|
|
|
if curOKRelays[i] != prevOKRelays[i] {
|
|
|
|
changed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if changed {
|
|
|
|
events.Default.Log(events.RelayStateChanged, map[string][]string{
|
|
|
|
"old": prevOKRelays,
|
|
|
|
"new": curOKRelays,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
prevOKRelays = curOKRelays
|
|
|
|
|
|
|
|
case <-e.stop:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *eventBroadcaster) Stop() {
|
|
|
|
close(e.stop)
|
|
|
|
}
|