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 (
|
|
|
|
"crypto/tls"
|
2016-08-10 09:37:32 +00:00
|
|
|
"fmt"
|
2016-05-04 19:38:12 +00:00
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
|
|
|
"github.com/syncthing/syncthing/lib/nat"
|
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
|
|
)
|
|
|
|
|
2016-11-30 07:54:20 +00:00
|
|
|
// Connection is what we expose to the outside. It is a protocol.Connection
|
|
|
|
// that can be closed and has some metadata.
|
|
|
|
type Connection interface {
|
|
|
|
protocol.Connection
|
|
|
|
Type() string
|
2017-10-12 06:16:46 +00:00
|
|
|
Transport() string
|
2016-11-30 07:54:20 +00:00
|
|
|
RemoteAddr() net.Addr
|
2017-11-21 07:25:38 +00:00
|
|
|
Priority() int
|
2018-01-12 11:27:55 +00:00
|
|
|
String() string
|
2019-02-26 10:49:02 +00:00
|
|
|
Crypto() string
|
2016-05-04 19:38:12 +00:00
|
|
|
}
|
|
|
|
|
2016-11-30 07:54:20 +00:00
|
|
|
// completeConn is the aggregation of an internalConn and the
|
|
|
|
// protocol.Connection running on top of it. It implements the Connection
|
|
|
|
// interface.
|
|
|
|
type completeConn struct {
|
|
|
|
internalConn
|
2016-05-04 19:38:12 +00:00
|
|
|
protocol.Connection
|
|
|
|
}
|
|
|
|
|
2019-01-09 16:31:09 +00:00
|
|
|
func (c completeConn) Close(err error) {
|
|
|
|
c.Connection.Close(err)
|
|
|
|
c.internalConn.Close()
|
|
|
|
}
|
|
|
|
|
2016-11-30 07:54:20 +00:00
|
|
|
// internalConn is the raw TLS connection plus some metadata on where it
|
|
|
|
// came from (type, priority).
|
|
|
|
type internalConn struct {
|
|
|
|
*tls.Conn
|
|
|
|
connType connType
|
|
|
|
priority int
|
|
|
|
}
|
|
|
|
|
|
|
|
type connType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
connTypeRelayClient connType = iota
|
|
|
|
connTypeRelayServer
|
|
|
|
connTypeTCPClient
|
|
|
|
connTypeTCPServer
|
|
|
|
)
|
|
|
|
|
|
|
|
func (t connType) String() string {
|
|
|
|
switch t {
|
|
|
|
case connTypeRelayClient:
|
|
|
|
return "relay-client"
|
|
|
|
case connTypeRelayServer:
|
|
|
|
return "relay-server"
|
|
|
|
case connTypeTCPClient:
|
|
|
|
return "tcp-client"
|
|
|
|
case connTypeTCPServer:
|
|
|
|
return "tcp-server"
|
|
|
|
default:
|
|
|
|
return "unknown-type"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-12 06:16:46 +00:00
|
|
|
func (t connType) Transport() string {
|
|
|
|
switch t {
|
|
|
|
case connTypeRelayClient, connTypeRelayServer:
|
|
|
|
return "relay"
|
|
|
|
case connTypeTCPClient, connTypeTCPServer:
|
|
|
|
return "tcp"
|
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-09 16:31:09 +00:00
|
|
|
func (c internalConn) Close() {
|
|
|
|
// *tls.Conn.Close() does more than it says on the tin. Specifically, it
|
|
|
|
// sends a TLS alert message, which might block forever if the
|
|
|
|
// connection is dead and we don't have a deadline set.
|
2019-02-02 11:16:27 +00:00
|
|
|
c.SetWriteDeadline(time.Now().Add(250 * time.Millisecond))
|
|
|
|
c.Conn.Close()
|
2019-01-09 16:31:09 +00:00
|
|
|
}
|
|
|
|
|
2016-11-30 07:54:20 +00:00
|
|
|
func (c internalConn) Type() string {
|
|
|
|
return c.connType.String()
|
|
|
|
}
|
|
|
|
|
2017-11-21 07:25:38 +00:00
|
|
|
func (c internalConn) Priority() int {
|
|
|
|
return c.priority
|
|
|
|
}
|
|
|
|
|
2019-02-26 10:49:02 +00:00
|
|
|
func (c internalConn) Crypto() string {
|
|
|
|
cs := c.ConnectionState()
|
|
|
|
return fmt.Sprintf("%s-%s", tlsVersionNames[cs.Version], tlsCipherSuiteNames[cs.CipherSuite])
|
|
|
|
}
|
|
|
|
|
2017-10-12 06:16:46 +00:00
|
|
|
func (c internalConn) Transport() string {
|
2017-11-07 07:25:05 +00:00
|
|
|
transport := c.connType.Transport()
|
|
|
|
host, _, err := net.SplitHostPort(c.LocalAddr().String())
|
|
|
|
if err != nil {
|
|
|
|
return transport
|
|
|
|
}
|
|
|
|
ip := net.ParseIP(host)
|
|
|
|
if ip == nil {
|
|
|
|
return transport
|
|
|
|
}
|
|
|
|
if ip.To4() != nil {
|
|
|
|
return transport + "4"
|
|
|
|
}
|
|
|
|
return transport + "6"
|
2017-10-12 06:16:46 +00:00
|
|
|
}
|
|
|
|
|
2016-11-30 07:54:20 +00:00
|
|
|
func (c internalConn) String() string {
|
2019-02-26 10:49:02 +00:00
|
|
|
return fmt.Sprintf("%s-%s/%s/%s", c.LocalAddr(), c.RemoteAddr(), c.Type(), c.Crypto())
|
2016-08-10 09:37:32 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 15:33:25 +00:00
|
|
|
type dialerFactory interface {
|
2019-02-26 08:09:25 +00:00
|
|
|
New(config.Wrapper, *tls.Config) genericDialer
|
2016-05-09 15:33:25 +00:00
|
|
|
Priority() int
|
2017-11-22 07:05:49 +00:00
|
|
|
AlwaysWAN() bool
|
2018-02-09 10:40:57 +00:00
|
|
|
Valid(config.Configuration) error
|
2016-05-17 00:05:38 +00:00
|
|
|
String() string
|
2016-05-09 15:33:25 +00:00
|
|
|
}
|
2016-05-04 19:38:12 +00:00
|
|
|
|
|
|
|
type genericDialer interface {
|
2016-11-30 07:54:20 +00:00
|
|
|
Dial(protocol.DeviceID, *url.URL) (internalConn, error)
|
2016-05-04 19:38:12 +00:00
|
|
|
RedialFrequency() time.Duration
|
|
|
|
}
|
|
|
|
|
2016-05-17 00:05:38 +00:00
|
|
|
type listenerFactory interface {
|
2019-02-26 08:09:25 +00:00
|
|
|
New(*url.URL, config.Wrapper, *tls.Config, chan internalConn, *nat.Service) genericListener
|
2018-02-09 10:40:57 +00:00
|
|
|
Valid(config.Configuration) error
|
2016-05-17 00:05:38 +00:00
|
|
|
}
|
2016-05-04 19:38:12 +00:00
|
|
|
|
|
|
|
type genericListener interface {
|
|
|
|
Serve()
|
|
|
|
Stop()
|
|
|
|
URI() *url.URL
|
|
|
|
// A given address can potentially be mutated by the listener.
|
|
|
|
// For example we bind to tcp://0.0.0.0, but that for example might return
|
|
|
|
// tcp://gateway1.ip and tcp://gateway2.ip as WAN addresses due to there
|
|
|
|
// being multiple gateways, and us managing to get a UPnP mapping on both
|
|
|
|
// and tcp://192.168.0.1 and tcp://10.0.0.1 due to there being multiple
|
|
|
|
// network interfaces. (The later case for LAN addresses is made up just
|
|
|
|
// to provide an example)
|
|
|
|
WANAddresses() []*url.URL
|
|
|
|
LANAddresses() []*url.URL
|
|
|
|
Error() error
|
|
|
|
OnAddressesChanged(func(genericListener))
|
|
|
|
String() string
|
2016-05-17 00:05:38 +00:00
|
|
|
Factory() listenerFactory
|
2017-10-12 06:16:46 +00:00
|
|
|
NATType() string
|
2016-05-04 19:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Model interface {
|
|
|
|
protocol.Model
|
2016-06-09 10:50:14 +00:00
|
|
|
AddConnection(conn Connection, hello protocol.HelloResult)
|
2017-11-21 07:25:38 +00:00
|
|
|
Connection(remoteID protocol.DeviceID) (Connection, bool)
|
2016-08-05 09:29:49 +00:00
|
|
|
OnHello(protocol.DeviceID, net.Addr, protocol.HelloResult) error
|
2016-07-04 10:40:29 +00:00
|
|
|
GetHello(protocol.DeviceID) protocol.HelloIntf
|
2016-05-04 19:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// serviceFunc wraps a function to create a suture.Service without stop
|
|
|
|
// functionality.
|
|
|
|
type serviceFunc func()
|
|
|
|
|
|
|
|
func (f serviceFunc) Serve() { f() }
|
|
|
|
func (f serviceFunc) Stop() {}
|
|
|
|
|
|
|
|
type onAddressesChangedNotifier struct {
|
|
|
|
callbacks []func(genericListener)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *onAddressesChangedNotifier) OnAddressesChanged(callback func(genericListener)) {
|
|
|
|
o.callbacks = append(o.callbacks, callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *onAddressesChangedNotifier) notifyAddressesChanged(l genericListener) {
|
|
|
|
for _, callback := range o.callbacks {
|
|
|
|
callback(l)
|
|
|
|
}
|
|
|
|
}
|
2017-11-15 09:36:33 +00:00
|
|
|
|
|
|
|
type dialTarget struct {
|
2019-05-16 21:11:46 +00:00
|
|
|
addr string
|
2017-11-15 09:36:33 +00:00
|
|
|
dialer genericDialer
|
|
|
|
priority int
|
|
|
|
uri *url.URL
|
|
|
|
deviceID protocol.DeviceID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t dialTarget) Dial() (internalConn, error) {
|
|
|
|
l.Debugln("dialing", t.deviceID, t.uri, "prio", t.priority)
|
|
|
|
conn, err := t.dialer.Dial(t.deviceID, t.uri)
|
2017-11-19 17:38:13 +00:00
|
|
|
if err != nil {
|
|
|
|
l.Debugln("dialing", t.deviceID, t.uri, "error:", err)
|
|
|
|
} else {
|
|
|
|
l.Debugln("dialing", t.deviceID, t.uri, "success:", conn)
|
|
|
|
}
|
2017-11-15 09:36:33 +00:00
|
|
|
return conn, err
|
|
|
|
}
|