2016-05-04 19:38:12 +00:00
|
|
|
// Copyright (C) 2016 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,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2016-05-04 19:38:12 +00:00
|
|
|
|
|
|
|
package connections
|
|
|
|
|
|
|
|
import (
|
2019-11-26 07:39:51 +00:00
|
|
|
"context"
|
2016-05-04 19:38:12 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
2022-04-09 14:04:56 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/connections/registry"
|
2016-05-04 19:38:12 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/dialer"
|
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
|
|
"github.com/syncthing/syncthing/lib/relay/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2016-05-09 15:33:25 +00:00
|
|
|
dialers["relay"] = relayDialerFactory{}
|
2016-05-04 19:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type relayDialer struct {
|
2019-11-26 07:36:58 +00:00
|
|
|
commonDialer
|
2016-05-04 19:38:12 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 07:39:51 +00:00
|
|
|
func (d *relayDialer) Dial(ctx context.Context, id protocol.DeviceID, uri *url.URL) (internalConn, error) {
|
|
|
|
inv, err := client.GetInvitationFromRelay(ctx, uri, id, d.tlsCfg.Certificates, 10*time.Second)
|
2016-05-04 19:38:12 +00:00
|
|
|
if err != nil {
|
2016-11-30 07:54:20 +00:00
|
|
|
return internalConn{}, err
|
2016-05-04 19:38:12 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 07:39:51 +00:00
|
|
|
conn, err := client.JoinSession(ctx, inv)
|
2016-05-04 19:38:12 +00:00
|
|
|
if err != nil {
|
2016-11-30 07:54:20 +00:00
|
|
|
return internalConn{}, err
|
2016-05-04 19:38:12 +00:00
|
|
|
}
|
|
|
|
|
2016-05-31 08:11:57 +00:00
|
|
|
err = dialer.SetTCPOptions(conn)
|
2016-05-04 19:38:12 +00:00
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
2016-11-30 07:54:20 +00:00
|
|
|
return internalConn{}, err
|
2016-05-04 19:38:12 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 07:36:58 +00:00
|
|
|
err = dialer.SetTrafficClass(conn, d.trafficClass)
|
2016-12-20 20:14:52 +00:00
|
|
|
if err != nil {
|
2017-11-14 21:49:36 +00:00
|
|
|
l.Debugln("Dial (BEP/relay): setting traffic class:", err)
|
2016-12-20 20:14:52 +00:00
|
|
|
}
|
|
|
|
|
2016-05-04 19:38:12 +00:00
|
|
|
var tc *tls.Conn
|
|
|
|
if inv.ServerSocket {
|
|
|
|
tc = tls.Server(conn, d.tlsCfg)
|
|
|
|
} else {
|
|
|
|
tc = tls.Client(conn, d.tlsCfg)
|
|
|
|
}
|
|
|
|
|
2016-07-02 20:33:31 +00:00
|
|
|
err = tlsTimedHandshake(tc)
|
2016-05-04 19:38:12 +00:00
|
|
|
if err != nil {
|
|
|
|
tc.Close()
|
2016-11-30 07:54:20 +00:00
|
|
|
return internalConn{}, err
|
2016-05-04 19:38:12 +00:00
|
|
|
}
|
|
|
|
|
2023-04-16 12:54:28 +00:00
|
|
|
return newInternalConn(tc, connTypeRelayClient, false, d.wanPriority), nil
|
2016-05-04 19:38:12 +00:00
|
|
|
}
|
|
|
|
|
2023-04-19 08:40:29 +00:00
|
|
|
func (d *relayDialer) Priority(host string) int {
|
|
|
|
return d.wanPriority
|
|
|
|
}
|
|
|
|
|
2016-05-09 15:33:25 +00:00
|
|
|
type relayDialerFactory struct{}
|
|
|
|
|
2023-04-16 12:54:28 +00:00
|
|
|
func (relayDialerFactory) New(opts config.OptionsConfiguration, tlsCfg *tls.Config, _ *registry.Registry, _ *lanChecker) genericDialer {
|
2019-11-26 07:36:58 +00:00
|
|
|
return &relayDialer{commonDialer{
|
|
|
|
trafficClass: opts.TrafficClass,
|
|
|
|
reconnectInterval: time.Duration(opts.RelayReconnectIntervalM) * time.Minute,
|
|
|
|
tlsCfg: tlsCfg,
|
2023-04-16 12:54:28 +00:00
|
|
|
wanPriority: opts.ConnectionPriorityRelay,
|
|
|
|
lanPriority: opts.ConnectionPriorityRelay,
|
2019-11-26 07:36:58 +00:00
|
|
|
}}
|
2016-05-04 19:38:12 +00:00
|
|
|
}
|
2016-05-09 15:33:25 +00:00
|
|
|
|
2017-11-22 07:05:49 +00:00
|
|
|
func (relayDialerFactory) AlwaysWAN() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-02-09 10:40:57 +00:00
|
|
|
func (relayDialerFactory) Valid(cfg config.Configuration) error {
|
|
|
|
if !cfg.Options.RelaysEnabled {
|
|
|
|
return errDisabled
|
|
|
|
}
|
|
|
|
return nil
|
2016-05-17 00:05:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (relayDialerFactory) String() string {
|
|
|
|
return "Relay Dialer"
|
|
|
|
}
|