2015-10-17 00:59:24 +02:00
|
|
|
// Copyright (C) 2015 Audrius Butkevicius and Contributors (see the CONTRIBUTORS file).
|
|
|
|
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2019-11-21 08:41:15 +01:00
|
|
|
"context"
|
2015-10-17 00:59:24 +02:00
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
2020-03-03 22:40:00 +01:00
|
|
|
"errors"
|
2015-10-17 00:59:24 +02:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/osutil"
|
2019-05-29 09:56:40 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/rand"
|
2015-10-17 00:59:24 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/relay/protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
type dynamicClient struct {
|
2019-07-09 11:40:30 +02:00
|
|
|
commonClient
|
|
|
|
|
|
|
|
pooladdr *url.URL
|
|
|
|
certs []tls.Certificate
|
|
|
|
timeout time.Duration
|
|
|
|
|
2015-10-17 00:59:24 +02:00
|
|
|
client RelayClient
|
|
|
|
}
|
|
|
|
|
2015-11-23 22:14:46 +01:00
|
|
|
func newDynamicClient(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation, timeout time.Duration) RelayClient {
|
2019-07-09 11:40:30 +02:00
|
|
|
c := &dynamicClient{
|
|
|
|
pooladdr: uri,
|
|
|
|
certs: certs,
|
|
|
|
timeout: timeout,
|
2015-10-17 00:59:24 +02:00
|
|
|
}
|
2019-11-21 18:45:06 +01:00
|
|
|
c.commonClient = newCommonClient(invitations, c.serve, fmt.Sprintf("dynamicClient@%p", c))
|
2019-07-09 11:40:30 +02:00
|
|
|
return c
|
2015-10-17 00:59:24 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 08:41:15 +01:00
|
|
|
func (c *dynamicClient) serve(ctx context.Context) error {
|
2015-10-17 00:59:24 +02:00
|
|
|
uri := *c.pooladdr
|
|
|
|
|
|
|
|
// Trim off the `dynamic+` prefix
|
|
|
|
uri.Scheme = uri.Scheme[8:]
|
|
|
|
|
|
|
|
l.Debugln(c, "looking up dynamic relays")
|
|
|
|
|
2020-02-24 21:57:15 +01:00
|
|
|
req, err := http.NewRequest("GET", uri.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
l.Debugln(c, "failed to lookup dynamic relays", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Cancel = ctx.Done()
|
|
|
|
data, err := http.DefaultClient.Do(req)
|
2015-10-17 00:59:24 +02:00
|
|
|
if err != nil {
|
|
|
|
l.Debugln(c, "failed to lookup dynamic relays", err)
|
2019-07-09 11:40:30 +02:00
|
|
|
return err
|
2015-10-17 00:59:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var ann dynamicAnnouncement
|
|
|
|
err = json.NewDecoder(data.Body).Decode(&ann)
|
|
|
|
data.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
l.Debugln(c, "failed to lookup dynamic relays", err)
|
2019-07-09 11:40:30 +02:00
|
|
|
return err
|
2015-10-17 00:59:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var addrs []string
|
|
|
|
for _, relayAnn := range ann.Relays {
|
|
|
|
ruri, err := url.Parse(relayAnn.URL)
|
|
|
|
if err != nil {
|
|
|
|
l.Debugln(c, "failed to parse dynamic relay address", relayAnn.URL, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
l.Debugln(c, "found", ruri)
|
|
|
|
addrs = append(addrs, ruri.String())
|
|
|
|
}
|
|
|
|
|
2019-11-21 08:41:15 +01:00
|
|
|
for _, addr := range relayAddressesOrder(ctx, addrs) {
|
2015-10-17 00:59:24 +02:00
|
|
|
select {
|
2019-11-21 08:41:15 +01:00
|
|
|
case <-ctx.Done():
|
2015-10-17 00:59:24 +02:00
|
|
|
l.Debugln(c, "stopping")
|
2019-07-09 11:40:30 +02:00
|
|
|
return nil
|
2015-10-17 00:59:24 +02:00
|
|
|
default:
|
|
|
|
ruri, err := url.Parse(addr)
|
|
|
|
if err != nil {
|
|
|
|
l.Debugln(c, "skipping relay", addr, err)
|
|
|
|
continue
|
|
|
|
}
|
2019-07-09 11:40:30 +02:00
|
|
|
client := newStaticClient(ruri, c.certs, c.invitations, c.timeout)
|
2015-10-17 00:59:24 +02:00
|
|
|
c.mut.Lock()
|
|
|
|
c.client = client
|
|
|
|
c.mut.Unlock()
|
|
|
|
|
|
|
|
c.client.Serve()
|
|
|
|
|
|
|
|
c.mut.Lock()
|
|
|
|
c.client = nil
|
|
|
|
c.mut.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
l.Debugln(c, "could not find a connectable relay")
|
2020-03-03 22:40:00 +01:00
|
|
|
return errors.New("could not find a connectable relay")
|
2015-10-17 00:59:24 +02:00
|
|
|
}
|
|
|
|
|
2019-07-19 19:40:40 +02:00
|
|
|
func (c *dynamicClient) Stop() {
|
|
|
|
c.mut.RLock()
|
|
|
|
if c.client != nil {
|
|
|
|
c.client.Stop()
|
|
|
|
}
|
|
|
|
c.mut.RUnlock()
|
|
|
|
c.commonClient.Stop()
|
|
|
|
}
|
|
|
|
|
2016-05-04 21:38:12 +02:00
|
|
|
func (c *dynamicClient) Error() error {
|
2015-10-17 00:59:24 +02:00
|
|
|
c.mut.RLock()
|
|
|
|
defer c.mut.RUnlock()
|
|
|
|
if c.client == nil {
|
2019-07-09 23:29:19 +02:00
|
|
|
return c.commonClient.Error()
|
2015-10-17 00:59:24 +02:00
|
|
|
}
|
2016-05-04 21:38:12 +02:00
|
|
|
return c.client.Error()
|
2015-10-17 00:59:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *dynamicClient) Latency() time.Duration {
|
|
|
|
c.mut.RLock()
|
|
|
|
defer c.mut.RUnlock()
|
|
|
|
if c.client == nil {
|
|
|
|
return time.Hour
|
|
|
|
}
|
|
|
|
return c.client.Latency()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *dynamicClient) String() string {
|
|
|
|
return fmt.Sprintf("DynamicClient:%p:%s@%s", c, c.URI(), c.pooladdr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *dynamicClient) URI() *url.URL {
|
|
|
|
c.mut.RLock()
|
|
|
|
defer c.mut.RUnlock()
|
|
|
|
if c.client == nil {
|
2016-05-04 21:38:12 +02:00
|
|
|
return nil
|
2015-10-17 00:59:24 +02:00
|
|
|
}
|
|
|
|
return c.client.URI()
|
|
|
|
}
|
|
|
|
|
2016-05-08 12:54:22 +02:00
|
|
|
// This is the announcement received from the relay server;
|
2015-10-17 00:59:24 +02:00
|
|
|
// {"relays": [{"url": "relay://10.20.30.40:5060"}, ...]}
|
|
|
|
type dynamicAnnouncement struct {
|
|
|
|
Relays []struct {
|
|
|
|
URL string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-21 00:58:29 +01:00
|
|
|
// relayAddressesOrder checks the latency to each relay, rounds latency down to
|
|
|
|
// the closest 50ms, and puts them in buckets of 50ms latency ranges. Then
|
|
|
|
// shuffles each bucket, and returns all addresses starting with the ones from
|
|
|
|
// the lowest latency bucket, ending with the highest latency buceket.
|
2019-11-21 08:41:15 +01:00
|
|
|
func relayAddressesOrder(ctx context.Context, input []string) []string {
|
2015-11-21 00:58:29 +01:00
|
|
|
buckets := make(map[int][]string)
|
|
|
|
|
|
|
|
for _, relay := range input {
|
2019-11-26 08:39:51 +01:00
|
|
|
latency, err := osutil.GetLatencyForURL(ctx, relay)
|
2015-11-21 00:58:29 +01:00
|
|
|
if err != nil {
|
|
|
|
latency = time.Hour
|
2015-10-17 00:59:24 +02:00
|
|
|
}
|
|
|
|
|
2015-11-21 00:58:29 +01:00
|
|
|
id := int(latency/time.Millisecond) / 50
|
2015-10-17 00:59:24 +02:00
|
|
|
|
2015-11-21 00:58:29 +01:00
|
|
|
buckets[id] = append(buckets[id], relay)
|
2019-07-19 19:40:40 +02:00
|
|
|
|
|
|
|
select {
|
2019-11-21 08:41:15 +01:00
|
|
|
case <-ctx.Done():
|
2019-07-19 19:40:40 +02:00
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
}
|
2015-10-17 00:59:24 +02:00
|
|
|
}
|
|
|
|
|
2015-11-21 00:58:29 +01:00
|
|
|
var ids []int
|
|
|
|
for id, bucket := range buckets {
|
2019-05-29 09:56:40 +02:00
|
|
|
rand.Shuffle(bucket)
|
2015-11-21 00:58:29 +01:00
|
|
|
ids = append(ids, id)
|
|
|
|
}
|
2015-10-17 00:59:24 +02:00
|
|
|
|
2015-11-21 00:58:29 +01:00
|
|
|
sort.Ints(ids)
|
2015-10-17 00:59:24 +02:00
|
|
|
|
2019-07-17 10:55:28 +02:00
|
|
|
addresses := make([]string, 0, len(input))
|
2015-11-21 00:58:29 +01:00
|
|
|
for _, id := range ids {
|
|
|
|
addresses = append(addresses, buckets[id]...)
|
|
|
|
}
|
2015-10-17 00:59:24 +02:00
|
|
|
|
2015-11-21 00:58:29 +01:00
|
|
|
return addresses
|
2015-10-17 00:59:24 +02:00
|
|
|
}
|