2015-06-28 00:52:01 +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-06-28 00:52:01 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
2015-09-22 17:38:46 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/relay/protocol"
|
2020-12-22 19:17:14 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/svcutil"
|
2019-07-09 09:40:30 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
|
|
|
|
2020-11-17 12:19:04 +00:00
|
|
|
"github.com/thejerf/suture/v4"
|
2015-06-28 00:52:01 +00:00
|
|
|
)
|
|
|
|
|
2015-11-23 21:14:46 +00:00
|
|
|
type relayClientFactory func(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation, timeout time.Duration) RelayClient
|
2015-06-28 00:52:01 +00:00
|
|
|
|
2015-10-16 22:59:24 +00:00
|
|
|
var (
|
|
|
|
supportedSchemes = map[string]relayClientFactory{
|
|
|
|
"relay": newStaticClient,
|
|
|
|
"dynamic+http": newDynamicClient,
|
|
|
|
"dynamic+https": newDynamicClient,
|
2015-06-28 00:52:01 +00:00
|
|
|
}
|
2015-10-16 22:59:24 +00:00
|
|
|
)
|
2015-06-28 00:52:01 +00:00
|
|
|
|
2015-10-16 22:59:24 +00:00
|
|
|
type RelayClient interface {
|
2019-07-09 09:40:30 +00:00
|
|
|
suture.Service
|
2016-05-04 19:38:12 +00:00
|
|
|
Error() error
|
2015-10-16 22:59:24 +00:00
|
|
|
Latency() time.Duration
|
|
|
|
String() string
|
|
|
|
Invitations() chan protocol.SessionInvitation
|
|
|
|
URI() *url.URL
|
2015-06-28 00:52:01 +00:00
|
|
|
}
|
|
|
|
|
2021-05-10 20:25:43 +00:00
|
|
|
func NewClient(uri *url.URL, certs []tls.Certificate, timeout time.Duration) (RelayClient, error) {
|
2015-10-16 22:59:24 +00:00
|
|
|
factory, ok := supportedSchemes[uri.Scheme]
|
|
|
|
if !ok {
|
2020-03-03 21:40:00 +00:00
|
|
|
return nil, fmt.Errorf("unsupported scheme: %s", uri.Scheme)
|
2015-06-28 00:52:01 +00:00
|
|
|
}
|
|
|
|
|
2021-05-10 20:25:43 +00:00
|
|
|
invitations := make(chan protocol.SessionInvitation)
|
2015-11-23 21:14:46 +00:00
|
|
|
return factory(uri, certs, invitations, timeout), nil
|
2015-07-20 09:56:10 +00:00
|
|
|
}
|
2019-07-09 09:40:30 +00:00
|
|
|
|
|
|
|
type commonClient struct {
|
2020-12-22 19:17:14 +00:00
|
|
|
svcutil.ServiceWithError
|
2019-07-09 09:40:30 +00:00
|
|
|
|
2021-05-10 20:25:43 +00:00
|
|
|
invitations chan protocol.SessionInvitation
|
|
|
|
mut sync.RWMutex
|
2019-07-09 09:40:30 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 07:41:15 +00:00
|
|
|
func newCommonClient(invitations chan protocol.SessionInvitation, serve func(context.Context) error, creator string) commonClient {
|
2019-07-09 09:40:30 +00:00
|
|
|
c := commonClient{
|
|
|
|
invitations: invitations,
|
|
|
|
mut: sync.NewRWMutex(),
|
|
|
|
}
|
2021-05-10 20:25:43 +00:00
|
|
|
c.ServiceWithError = svcutil.AsService(serve, creator)
|
2019-07-09 09:40:30 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commonClient) Invitations() chan protocol.SessionInvitation {
|
|
|
|
c.mut.RLock()
|
|
|
|
defer c.mut.RUnlock()
|
|
|
|
return c.invitations
|
|
|
|
}
|