2015-06-28 02:52:01 +02:00
|
|
|
// Copyright (C) 2015 Audrius Butkevicius and Contributors (see the CONTRIBUTORS file).
|
|
|
|
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
2015-09-22 19:38:46 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/relay/protocol"
|
2019-07-09 11:40:30 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
|
|
|
"github.com/syncthing/syncthing/lib/util"
|
|
|
|
|
|
|
|
"github.com/thejerf/suture"
|
2015-06-28 02:52:01 +02:00
|
|
|
)
|
|
|
|
|
2015-11-23 22:14:46 +01:00
|
|
|
type relayClientFactory func(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation, timeout time.Duration) RelayClient
|
2015-06-28 02:52:01 +02:00
|
|
|
|
2015-10-17 00:59:24 +02:00
|
|
|
var (
|
|
|
|
supportedSchemes = map[string]relayClientFactory{
|
|
|
|
"relay": newStaticClient,
|
|
|
|
"dynamic+http": newDynamicClient,
|
|
|
|
"dynamic+https": newDynamicClient,
|
2015-06-28 02:52:01 +02:00
|
|
|
}
|
2015-10-17 00:59:24 +02:00
|
|
|
)
|
2015-06-28 02:52:01 +02:00
|
|
|
|
2015-10-17 00:59:24 +02:00
|
|
|
type RelayClient interface {
|
2019-07-09 11:40:30 +02:00
|
|
|
suture.Service
|
2016-05-04 21:38:12 +02:00
|
|
|
Error() error
|
2015-10-17 00:59:24 +02:00
|
|
|
Latency() time.Duration
|
|
|
|
String() string
|
|
|
|
Invitations() chan protocol.SessionInvitation
|
|
|
|
URI() *url.URL
|
2015-06-28 02:52:01 +02:00
|
|
|
}
|
|
|
|
|
2015-11-23 22:14:46 +01:00
|
|
|
func NewClient(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation, timeout time.Duration) (RelayClient, error) {
|
2015-10-17 00:59:24 +02:00
|
|
|
factory, ok := supportedSchemes[uri.Scheme]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Unsupported scheme: %s", uri.Scheme)
|
2015-06-28 02:52:01 +02:00
|
|
|
}
|
|
|
|
|
2015-11-23 22:14:46 +01:00
|
|
|
return factory(uri, certs, invitations, timeout), nil
|
2015-07-20 11:56:10 +02:00
|
|
|
}
|
2019-07-09 11:40:30 +02:00
|
|
|
|
|
|
|
type commonClient struct {
|
|
|
|
util.ServiceWithError
|
|
|
|
|
|
|
|
invitations chan protocol.SessionInvitation
|
|
|
|
closeInvitationsOnFinish bool
|
|
|
|
mut sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCommonClient(invitations chan protocol.SessionInvitation, serve func(chan struct{}) error) commonClient {
|
|
|
|
c := commonClient{
|
|
|
|
invitations: invitations,
|
|
|
|
mut: sync.NewRWMutex(),
|
|
|
|
}
|
|
|
|
newServe := func(stop chan struct{}) error {
|
|
|
|
defer c.cleanup()
|
|
|
|
return serve(stop)
|
|
|
|
}
|
|
|
|
c.ServiceWithError = util.AsServiceWithError(newServe)
|
|
|
|
if c.invitations == nil {
|
|
|
|
c.closeInvitationsOnFinish = true
|
|
|
|
c.invitations = make(chan protocol.SessionInvitation)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commonClient) cleanup() {
|
|
|
|
c.mut.Lock()
|
|
|
|
if c.closeInvitationsOnFinish {
|
|
|
|
close(c.invitations)
|
|
|
|
}
|
|
|
|
c.mut.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commonClient) Invitations() chan protocol.SessionInvitation {
|
|
|
|
c.mut.RLock()
|
|
|
|
defer c.mut.RUnlock()
|
|
|
|
return c.invitations
|
|
|
|
}
|