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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2016-05-17 00:05:38 +00:00
|
|
|
factory := &tcpDialerFactory{}
|
2016-05-04 19:38:12 +00:00
|
|
|
for _, scheme := range []string{"tcp", "tcp4", "tcp6"} {
|
2016-05-17 00:05:38 +00:00
|
|
|
dialers[scheme] = factory
|
2016-05-04 19:38:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type tcpDialer struct {
|
2019-11-26 07:36:58 +00:00
|
|
|
commonDialer
|
2022-04-09 14:04:56 +00:00
|
|
|
registry *registry.Registry
|
2016-05-04 19:38:12 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 07:39:51 +00:00
|
|
|
func (d *tcpDialer) Dial(ctx context.Context, _ protocol.DeviceID, uri *url.URL) (internalConn, error) {
|
2017-03-07 12:44:16 +00:00
|
|
|
uri = fixupPort(uri, config.DefaultTCPPort)
|
2016-05-04 19:38:12 +00:00
|
|
|
|
2019-11-26 07:39:51 +00:00
|
|
|
timeoutCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
|
|
defer cancel()
|
2022-04-09 14:04:56 +00:00
|
|
|
conn, err := dialer.DialContextReusePortFunc(d.registry)(timeoutCtx, uri.Scheme, uri.Host)
|
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-12-20 20:14:52 +00:00
|
|
|
err = dialer.SetTCPOptions(conn)
|
|
|
|
if err != nil {
|
2017-11-14 21:49:36 +00:00
|
|
|
l.Debugln("Dial (BEP/tcp): setting tcp options:", err)
|
2016-12-20 20:14:52 +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/tcp): setting traffic class:", err)
|
2016-12-20 20:14:52 +00:00
|
|
|
}
|
|
|
|
|
2016-05-04 19:38:12 +00:00
|
|
|
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
|
|
|
priority := d.wanPriority
|
|
|
|
isLocal := d.lanChecker.isLAN(conn.RemoteAddr())
|
|
|
|
if isLocal {
|
|
|
|
priority = d.lanPriority
|
|
|
|
}
|
|
|
|
return newInternalConn(tc, connTypeTCPClient, isLocal, priority), nil
|
2016-05-04 19:38:12 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 15:33:25 +00:00
|
|
|
type tcpDialerFactory struct{}
|
|
|
|
|
2023-04-16 12:54:28 +00:00
|
|
|
func (tcpDialerFactory) New(opts config.OptionsConfiguration, tlsCfg *tls.Config, registry *registry.Registry, lanChecker *lanChecker) genericDialer {
|
2022-04-09 14:04:56 +00:00
|
|
|
return &tcpDialer{
|
|
|
|
commonDialer: commonDialer{
|
|
|
|
trafficClass: opts.TrafficClass,
|
|
|
|
reconnectInterval: time.Duration(opts.ReconnectIntervalS) * time.Second,
|
|
|
|
tlsCfg: tlsCfg,
|
2023-04-16 12:54:28 +00:00
|
|
|
lanPriority: opts.ConnectionPriorityTCPLAN,
|
|
|
|
wanPriority: opts.ConnectionPriorityTCPWAN,
|
|
|
|
lanChecker: lanChecker,
|
2022-04-09 14:04:56 +00:00
|
|
|
},
|
|
|
|
registry: registry,
|
|
|
|
}
|
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 (tcpDialerFactory) AlwaysWAN() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-02-09 10:40:57 +00:00
|
|
|
func (tcpDialerFactory) Valid(_ config.Configuration) error {
|
|
|
|
// Always valid
|
|
|
|
return nil
|
2016-05-17 00:05:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tcpDialerFactory) String() string {
|
|
|
|
return "TCP Dialer"
|
|
|
|
}
|