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"
|
|
|
|
"github.com/syncthing/syncthing/lib/dialer"
|
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
|
|
)
|
|
|
|
|
2019-05-29 07:56:40 +00:00
|
|
|
const tcpPriority = 10
|
|
|
|
|
2016-05-04 19:38:12 +00:00
|
|
|
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
|
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()
|
2020-06-16 07:17:07 +00:00
|
|
|
conn, err := dialer.DialContextReusePort(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
|
|
|
}
|
|
|
|
|
2016-11-30 07:54:20 +00:00
|
|
|
return internalConn{tc, connTypeTCPClient, tcpPriority}, nil
|
2016-05-04 19:38:12 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 15:33:25 +00:00
|
|
|
type tcpDialerFactory struct{}
|
|
|
|
|
2019-11-26 07:36:58 +00:00
|
|
|
func (tcpDialerFactory) New(opts config.OptionsConfiguration, tlsCfg *tls.Config) genericDialer {
|
|
|
|
return &tcpDialer{commonDialer{
|
|
|
|
trafficClass: opts.TrafficClass,
|
|
|
|
reconnectInterval: time.Duration(opts.ReconnectIntervalS) * time.Second,
|
|
|
|
tlsCfg: tlsCfg,
|
|
|
|
}}
|
2016-05-04 19:38:12 +00:00
|
|
|
}
|
2016-05-09 15:33:25 +00:00
|
|
|
|
|
|
|
func (tcpDialerFactory) Priority() int {
|
|
|
|
return tcpPriority
|
|
|
|
}
|
2016-05-17 00:05:38 +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"
|
|
|
|
}
|