2017-03-07 05:44:16 -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/protocol"
|
2017-12-27 04:33:12 -07:00
|
|
|
"github.com/xtaci/kcp-go"
|
2017-03-09 06:01:07 -07:00
|
|
|
"github.com/xtaci/smux"
|
2017-03-07 05:44:16 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
factory := &kcpDialerFactory{}
|
|
|
|
for _, scheme := range []string{"kcp", "kcp4", "kcp6"} {
|
|
|
|
dialers[scheme] = factory
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type kcpDialer struct {
|
|
|
|
cfg *config.Wrapper
|
|
|
|
tlsCfg *tls.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *kcpDialer) Dial(id protocol.DeviceID, uri *url.URL) (internalConn, error) {
|
|
|
|
uri = fixupPort(uri, config.DefaultKCPPort)
|
|
|
|
|
|
|
|
var conn *kcp.UDPSession
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Try to dial via an existing listening connection
|
|
|
|
// giving better changes punching through NAT.
|
|
|
|
if f := getDialingFilter(); f != nil {
|
2017-09-02 07:11:48 -07:00
|
|
|
conn, err = kcp.NewConn(uri.Host, nil, 0, 0, f.NewConn(kcpConversationFilterPriority, &kcpConversationFilter{}))
|
2017-03-07 05:44:16 -07:00
|
|
|
l.Debugf("dial %s using existing conn on %s", uri.String(), conn.LocalAddr())
|
|
|
|
} else {
|
|
|
|
conn, err = kcp.DialWithOptions(uri.Host, nil, 0, 0)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return internalConn{}, err
|
|
|
|
}
|
|
|
|
|
2017-03-07 05:55:50 -07:00
|
|
|
opts := d.cfg.Options()
|
|
|
|
|
2017-03-07 05:44:16 -07:00
|
|
|
conn.SetStreamMode(true)
|
|
|
|
conn.SetACKNoDelay(false)
|
2017-03-07 05:55:50 -07:00
|
|
|
conn.SetWindowSize(opts.KCPSendWindowSize, opts.KCPReceiveWindowSize)
|
|
|
|
conn.SetNoDelay(boolInt(opts.KCPNoDelay), opts.KCPUpdateIntervalMs, boolInt(opts.KCPFastResend), boolInt(!opts.KCPCongestionControl))
|
2017-03-07 05:44:16 -07:00
|
|
|
|
2017-03-09 06:01:07 -07:00
|
|
|
ses, err := smux.Client(conn, smuxConfig)
|
2017-03-07 05:44:16 -07:00
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return internalConn{}, err
|
|
|
|
}
|
2017-05-21 11:48:34 -07:00
|
|
|
|
|
|
|
ses.SetDeadline(time.Now().Add(10 * time.Second))
|
2017-03-07 05:44:16 -07:00
|
|
|
stream, err := ses.OpenStream()
|
|
|
|
if err != nil {
|
|
|
|
ses.Close()
|
|
|
|
return internalConn{}, err
|
|
|
|
}
|
2017-05-21 11:48:34 -07:00
|
|
|
ses.SetDeadline(time.Time{})
|
2017-03-07 05:44:16 -07:00
|
|
|
|
|
|
|
tc := tls.Client(&sessionClosingStream{stream, ses}, d.tlsCfg)
|
|
|
|
tc.SetDeadline(time.Now().Add(time.Second * 10))
|
|
|
|
err = tc.Handshake()
|
|
|
|
if err != nil {
|
|
|
|
tc.Close()
|
|
|
|
return internalConn{}, err
|
|
|
|
}
|
|
|
|
tc.SetDeadline(time.Time{})
|
|
|
|
|
|
|
|
return internalConn{tc, connTypeKCPClient, kcpPriority}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *kcpDialer) RedialFrequency() time.Duration {
|
|
|
|
// For restricted NATs, the UDP mapping will potentially only be open for 20-30 seconds
|
|
|
|
// hence try dialing just as often.
|
|
|
|
return time.Duration(d.cfg.Options().StunKeepaliveS) * time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
type kcpDialerFactory struct{}
|
|
|
|
|
|
|
|
func (kcpDialerFactory) New(cfg *config.Wrapper, tlsCfg *tls.Config) genericDialer {
|
|
|
|
return &kcpDialer{
|
|
|
|
cfg: cfg,
|
|
|
|
tlsCfg: tlsCfg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (kcpDialerFactory) Priority() int {
|
|
|
|
return kcpPriority
|
|
|
|
}
|
|
|
|
|
2017-11-22 00:05:49 -07:00
|
|
|
func (kcpDialerFactory) AlwaysWAN() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-03-07 05:44:16 -07:00
|
|
|
func (kcpDialerFactory) Enabled(cfg config.Configuration) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (kcpDialerFactory) String() string {
|
|
|
|
return "KCP Dialer"
|
|
|
|
}
|