2016-05-04 12:38:12 -07: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,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
package connections
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
|
|
|
"github.com/syncthing/syncthing/lib/dialer"
|
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
const tcpPriority = 10
|
|
|
|
|
|
|
|
func init() {
|
2016-05-16 17:05:38 -07:00
|
|
|
factory := &tcpDialerFactory{}
|
2016-05-04 12:38:12 -07:00
|
|
|
for _, scheme := range []string{"tcp", "tcp4", "tcp6"} {
|
2016-05-16 17:05:38 -07:00
|
|
|
dialers[scheme] = factory
|
2016-05-04 12:38:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type tcpDialer struct {
|
|
|
|
cfg *config.Wrapper
|
|
|
|
tlsCfg *tls.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *tcpDialer) Dial(id protocol.DeviceID, uri *url.URL) (IntermediateConnection, error) {
|
|
|
|
uri = fixupPort(uri)
|
|
|
|
|
2016-05-20 18:31:23 -07:00
|
|
|
conn, err := dialer.DialTimeout(uri.Scheme, uri.Host, 10*time.Second)
|
2016-05-04 12:38:12 -07:00
|
|
|
if err != nil {
|
|
|
|
l.Debugln(err)
|
|
|
|
return IntermediateConnection{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tc := tls.Client(conn, d.tlsCfg)
|
|
|
|
err = tc.Handshake()
|
|
|
|
if err != nil {
|
|
|
|
tc.Close()
|
|
|
|
return IntermediateConnection{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return IntermediateConnection{tc, "TCP (Client)", tcpPriority}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *tcpDialer) RedialFrequency() time.Duration {
|
|
|
|
return time.Duration(d.cfg.Options().ReconnectIntervalS) * time.Second
|
|
|
|
}
|
|
|
|
|
2016-05-09 08:33:25 -07:00
|
|
|
type tcpDialerFactory struct{}
|
|
|
|
|
|
|
|
func (tcpDialerFactory) New(cfg *config.Wrapper, tlsCfg *tls.Config) genericDialer {
|
2016-05-04 12:38:12 -07:00
|
|
|
return &tcpDialer{
|
|
|
|
cfg: cfg,
|
|
|
|
tlsCfg: tlsCfg,
|
|
|
|
}
|
|
|
|
}
|
2016-05-09 08:33:25 -07:00
|
|
|
|
|
|
|
func (tcpDialerFactory) Priority() int {
|
|
|
|
return tcpPriority
|
|
|
|
}
|
2016-05-16 17:05:38 -07:00
|
|
|
|
|
|
|
func (tcpDialerFactory) Enabled(cfg config.Configuration) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tcpDialerFactory) String() string {
|
|
|
|
return "TCP Dialer"
|
|
|
|
}
|