2015-10-16 22:59:24 +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-10-16 22:59:24 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
2020-03-03 21:40:00 +00:00
|
|
|
"errors"
|
2015-10-16 22:59:24 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"sort"
|
2021-07-21 07:49:09 +00:00
|
|
|
"sync"
|
2015-10-16 22:59:24 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/osutil"
|
2019-05-29 07:56:40 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/rand"
|
2015-10-16 22:59:24 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/relay/protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
type dynamicClient struct {
|
2019-07-09 09:40:30 +00:00
|
|
|
commonClient
|
|
|
|
|
|
|
|
pooladdr *url.URL
|
|
|
|
certs []tls.Certificate
|
|
|
|
timeout time.Duration
|
|
|
|
|
2021-07-21 07:49:09 +00:00
|
|
|
mut sync.RWMutex // Protects client.
|
|
|
|
client *staticClient
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 07:49:09 +00:00
|
|
|
func newDynamicClient(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation, timeout time.Duration) *dynamicClient {
|
2019-07-09 09:40:30 +00:00
|
|
|
c := &dynamicClient{
|
|
|
|
pooladdr: uri,
|
|
|
|
certs: certs,
|
|
|
|
timeout: timeout,
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
2019-11-21 17:45:06 +00:00
|
|
|
c.commonClient = newCommonClient(invitations, c.serve, fmt.Sprintf("dynamicClient@%p", c))
|
2019-07-09 09:40:30 +00:00
|
|
|
return c
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 07:41:15 +00:00
|
|
|
func (c *dynamicClient) serve(ctx context.Context) error {
|
2015-10-16 22:59:24 +00:00
|
|
|
uri := *c.pooladdr
|
|
|
|
|
|
|
|
// Trim off the `dynamic+` prefix
|
|
|
|
uri.Scheme = uri.Scheme[8:]
|
|
|
|
|
|
|
|
l.Debugln(c, "looking up dynamic relays")
|
|
|
|
|
2022-07-28 15:14:49 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", uri.String(), nil)
|
2020-02-24 20:57:15 +00:00
|
|
|
if err != nil {
|
|
|
|
l.Debugln(c, "failed to lookup dynamic relays", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
data, err := http.DefaultClient.Do(req)
|
2015-10-16 22:59:24 +00:00
|
|
|
if err != nil {
|
|
|
|
l.Debugln(c, "failed to lookup dynamic relays", err)
|
2019-07-09 09:40:30 +00:00
|
|
|
return err
|
2015-10-16 22:59:24 +00: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 09:40:30 +00:00
|
|
|
return err
|
2015-10-16 22:59:24 +00: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 07:41:15 +00:00
|
|
|
for _, addr := range relayAddressesOrder(ctx, addrs) {
|
2015-10-16 22:59:24 +00:00
|
|
|
select {
|
2019-11-21 07:41:15 +00:00
|
|
|
case <-ctx.Done():
|
2015-10-16 22:59:24 +00:00
|
|
|
l.Debugln(c, "stopping")
|
2019-07-09 09:40:30 +00:00
|
|
|
return nil
|
2015-10-16 22:59:24 +00:00
|
|
|
default:
|
|
|
|
ruri, err := url.Parse(addr)
|
|
|
|
if err != nil {
|
|
|
|
l.Debugln(c, "skipping relay", addr, err)
|
|
|
|
continue
|
|
|
|
}
|
2019-07-09 09:40:30 +00:00
|
|
|
client := newStaticClient(ruri, c.certs, c.invitations, c.timeout)
|
2015-10-16 22:59:24 +00:00
|
|
|
c.mut.Lock()
|
|
|
|
c.client = client
|
|
|
|
c.mut.Unlock()
|
|
|
|
|
2021-05-10 20:26:25 +00:00
|
|
|
err = c.client.Serve(ctx)
|
|
|
|
l.Debugf("Disconnected from %s://%s: %v", c.client.URI().Scheme, c.client.URI().Host, err)
|
2015-10-16 22:59:24 +00:00
|
|
|
|
|
|
|
c.mut.Lock()
|
|
|
|
c.client = nil
|
|
|
|
c.mut.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
l.Debugln(c, "could not find a connectable relay")
|
2020-03-03 21:40:00 +00:00
|
|
|
return errors.New("could not find a connectable relay")
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
2016-05-04 19:38:12 +00:00
|
|
|
func (c *dynamicClient) Error() error {
|
2015-10-16 22:59:24 +00:00
|
|
|
c.mut.RLock()
|
|
|
|
defer c.mut.RUnlock()
|
|
|
|
if c.client == nil {
|
2019-07-09 21:29:19 +00:00
|
|
|
return c.commonClient.Error()
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
2016-05-04 19:38:12 +00:00
|
|
|
return c.client.Error()
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 19:38:12 +00:00
|
|
|
return nil
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
|
|
|
return c.client.URI()
|
|
|
|
}
|
|
|
|
|
2016-05-08 10:54:22 +00:00
|
|
|
// This is the announcement received from the relay server;
|
2015-10-16 22:59:24 +00:00
|
|
|
// {"relays": [{"url": "relay://10.20.30.40:5060"}, ...]}
|
|
|
|
type dynamicAnnouncement struct {
|
|
|
|
Relays []struct {
|
|
|
|
URL string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-20 23:58:29 +00: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 07:41:15 +00:00
|
|
|
func relayAddressesOrder(ctx context.Context, input []string) []string {
|
2015-11-20 23:58:29 +00:00
|
|
|
buckets := make(map[int][]string)
|
|
|
|
|
|
|
|
for _, relay := range input {
|
2019-11-26 07:39:51 +00:00
|
|
|
latency, err := osutil.GetLatencyForURL(ctx, relay)
|
2015-11-20 23:58:29 +00:00
|
|
|
if err != nil {
|
|
|
|
latency = time.Hour
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
2015-11-20 23:58:29 +00:00
|
|
|
id := int(latency/time.Millisecond) / 50
|
2015-10-16 22:59:24 +00:00
|
|
|
|
2015-11-20 23:58:29 +00:00
|
|
|
buckets[id] = append(buckets[id], relay)
|
2019-07-19 17:40:40 +00:00
|
|
|
|
|
|
|
select {
|
2019-11-21 07:41:15 +00:00
|
|
|
case <-ctx.Done():
|
2019-07-19 17:40:40 +00:00
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
}
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|
|
|
|
|
2015-11-20 23:58:29 +00:00
|
|
|
var ids []int
|
|
|
|
for id, bucket := range buckets {
|
2019-05-29 07:56:40 +00:00
|
|
|
rand.Shuffle(bucket)
|
2015-11-20 23:58:29 +00:00
|
|
|
ids = append(ids, id)
|
|
|
|
}
|
2015-10-16 22:59:24 +00:00
|
|
|
|
2015-11-20 23:58:29 +00:00
|
|
|
sort.Ints(ids)
|
2015-10-16 22:59:24 +00:00
|
|
|
|
2019-07-17 08:55:28 +00:00
|
|
|
addresses := make([]string, 0, len(input))
|
2015-11-20 23:58:29 +00:00
|
|
|
for _, id := range ids {
|
|
|
|
addresses = append(addresses, buckets[id]...)
|
|
|
|
}
|
2015-10-16 22:59:24 +00:00
|
|
|
|
2015-11-20 23:58:29 +00:00
|
|
|
return addresses
|
2015-10-16 22:59:24 +00:00
|
|
|
}
|