The connections service no longer depends directly on the syncthing model object, but on an interface instead. This makes it drastically easier to write clients that handle the model differently, but still want to benefit from existing and future connections changes in the core. This was motivated by burkemw3's interest in creating a FUSE client that can present a view of the global model, but not have all of the file data locally. The actual decoupling was done by adding a connections.Model interface. This interface is effectively an extension of the protocol.Model interface that also handles connections alongside the modified service.
106 lines
2.1 KiB
Go
106 lines
2.1 KiB
Go
// Copyright (C) 2015 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"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/syncthing/syncthing/lib/model"
|
|
"github.com/syncthing/syncthing/lib/osutil"
|
|
)
|
|
|
|
func init() {
|
|
dialers["tcp"] = tcpDialer
|
|
listeners["tcp"] = tcpListener
|
|
}
|
|
|
|
func tcpDialer(uri *url.URL, tlsCfg *tls.Config) (*tls.Conn, error) {
|
|
host, port, err := net.SplitHostPort(uri.Host)
|
|
if err != nil && strings.HasPrefix(err.Error(), "missing port") {
|
|
// addr is on the form "1.2.3.4"
|
|
uri.Host = net.JoinHostPort(uri.Host, "22000")
|
|
} else if err == nil && port == "" {
|
|
// addr is on the form "1.2.3.4:"
|
|
uri.Host = net.JoinHostPort(host, "22000")
|
|
}
|
|
|
|
raddr, err := net.ResolveTCPAddr("tcp", uri.Host)
|
|
if err != nil {
|
|
if debug {
|
|
l.Debugln(err)
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
conn, err := net.DialTCP("tcp", nil, raddr)
|
|
if err != nil {
|
|
if debug {
|
|
l.Debugln(err)
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
err = osutil.SetTCPOptions(conn)
|
|
if err != nil {
|
|
l.Infoln(err)
|
|
}
|
|
|
|
tc := tls.Client(conn, tlsCfg)
|
|
err = tc.Handshake()
|
|
if err != nil {
|
|
tc.Close()
|
|
return nil, err
|
|
}
|
|
|
|
return tc, nil
|
|
}
|
|
|
|
func tcpListener(uri *url.URL, tlsCfg *tls.Config, conns chan<- model.IntermediateConnection) {
|
|
tcaddr, err := net.ResolveTCPAddr("tcp", uri.Host)
|
|
if err != nil {
|
|
l.Fatalln("listen (BEP/tcp):", err)
|
|
return
|
|
}
|
|
listener, err := net.ListenTCP("tcp", tcaddr)
|
|
if err != nil {
|
|
l.Fatalln("listen (BEP/tcp):", err)
|
|
return
|
|
}
|
|
|
|
for {
|
|
conn, err := listener.Accept()
|
|
if err != nil {
|
|
l.Warnln("Accepting connection (BEP/tcp):", err)
|
|
continue
|
|
}
|
|
|
|
if debug {
|
|
l.Debugln("connect from", conn.RemoteAddr())
|
|
}
|
|
|
|
err = osutil.SetTCPOptions(conn.(*net.TCPConn))
|
|
if err != nil {
|
|
l.Infoln(err)
|
|
}
|
|
|
|
tc := tls.Server(conn, tlsCfg)
|
|
err = tc.Handshake()
|
|
if err != nil {
|
|
l.Infoln("TLS handshake (BEP/tcp):", err)
|
|
tc.Close()
|
|
continue
|
|
}
|
|
|
|
conns <- model.IntermediateConnection{
|
|
tc, model.ConnectionTypeDirectAccept,
|
|
}
|
|
}
|
|
}
|