2019-05-29 07:56:40 +00:00
|
|
|
// Copyright (C) 2019 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 https://mozilla.org/MPL/2.0/.
|
|
|
|
|
2021-08-19 19:05:28 +00:00
|
|
|
//go:build go1.15 && !noquic
|
|
|
|
// +build go1.15,!noquic
|
2019-05-29 07:56:40 +00:00
|
|
|
|
|
|
|
package connections
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
2022-08-16 08:01:49 +00:00
|
|
|
"fmt"
|
2019-05-29 07:56:40 +00:00
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
2023-02-02 21:00:50 +00:00
|
|
|
"github.com/quic-go/quic-go"
|
2019-05-29 07:56:40 +00:00
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
|
|
|
"github.com/syncthing/syncthing/lib/connections/registry"
|
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
|
|
)
|
|
|
|
|
2019-08-23 08:15:52 +00:00
|
|
|
const (
|
|
|
|
// The timeout for connecting, accepting and creating the various
|
|
|
|
// streams.
|
|
|
|
quicOperationTimeout = 10 * time.Second
|
|
|
|
)
|
2019-05-29 07:56:40 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
factory := &quicDialerFactory{}
|
|
|
|
for _, scheme := range []string{"quic", "quic4", "quic6"} {
|
|
|
|
dialers[scheme] = factory
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type quicDialer struct {
|
2019-11-26 07:36:58 +00:00
|
|
|
commonDialer
|
2022-04-09 14:04:56 +00:00
|
|
|
registry *registry.Registry
|
2019-05-29 07:56:40 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 07:39:51 +00:00
|
|
|
func (d *quicDialer) Dial(ctx context.Context, _ protocol.DeviceID, uri *url.URL) (internalConn, error) {
|
2019-05-29 07:56:40 +00:00
|
|
|
uri = fixupPort(uri, config.DefaultQUICPort)
|
|
|
|
|
2021-07-10 11:53:51 +00:00
|
|
|
network := quicNetwork(uri)
|
2021-07-05 11:19:56 +00:00
|
|
|
|
|
|
|
addr, err := net.ResolveUDPAddr(network, uri.Host)
|
2019-05-29 07:56:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return internalConn{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var conn net.PacketConn
|
2019-06-09 21:14:00 +00:00
|
|
|
// We need to track who created the conn.
|
|
|
|
// Given we always pass the connection to quic, it assumes it's a remote connection it never closes it,
|
|
|
|
// So our wrapper around it needs to close it, but it only needs to close it if it's not the listening connection.
|
|
|
|
var createdConn net.PacketConn
|
2022-04-09 14:04:56 +00:00
|
|
|
listenConn := d.registry.Get(uri.Scheme, packetConnUnspecified)
|
2021-10-06 08:52:51 +00:00
|
|
|
if listenConn != nil {
|
2019-05-29 07:56:40 +00:00
|
|
|
conn = listenConn.(net.PacketConn)
|
|
|
|
} else {
|
|
|
|
if packetConn, err := net.ListenPacket("udp", ":0"); err != nil {
|
|
|
|
return internalConn{}, err
|
|
|
|
} else {
|
|
|
|
conn = packetConn
|
2019-06-09 21:14:00 +00:00
|
|
|
createdConn = packetConn
|
2019-05-29 07:56:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 07:39:51 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, quicOperationTimeout)
|
2019-08-23 08:15:52 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2019-05-29 07:56:40 +00:00
|
|
|
session, err := quic.DialContext(ctx, conn, addr, uri.Host, d.tlsCfg, quicConfig)
|
|
|
|
if err != nil {
|
2019-06-09 21:14:00 +00:00
|
|
|
if createdConn != nil {
|
|
|
|
_ = createdConn.Close()
|
2019-05-29 07:56:40 +00:00
|
|
|
}
|
2022-08-16 08:01:49 +00:00
|
|
|
return internalConn{}, fmt.Errorf("dial: %w", err)
|
2019-05-29 07:56:40 +00:00
|
|
|
}
|
|
|
|
|
2019-08-23 08:15:52 +00:00
|
|
|
stream, err := session.OpenStreamSync(ctx)
|
2019-05-29 07:56:40 +00:00
|
|
|
if err != nil {
|
|
|
|
// It's ok to close these, this does not close the underlying packetConn.
|
2020-05-01 07:14:28 +00:00
|
|
|
_ = session.CloseWithError(1, err.Error())
|
2019-06-09 21:14:00 +00:00
|
|
|
if createdConn != nil {
|
|
|
|
_ = createdConn.Close()
|
2019-05-29 07:56:40 +00:00
|
|
|
}
|
2022-08-16 08:01:49 +00:00
|
|
|
return internalConn{}, fmt.Errorf("open stream: %w", err)
|
2019-05-29 07:56:40 +00:00
|
|
|
}
|
|
|
|
|
2023-04-16 12:54:28 +00:00
|
|
|
priority := d.wanPriority
|
|
|
|
isLocal := d.lanChecker.isLAN(session.RemoteAddr())
|
|
|
|
if isLocal {
|
|
|
|
priority = d.lanPriority
|
|
|
|
}
|
|
|
|
return newInternalConn(&quicTlsConn{session, stream, createdConn}, connTypeQUICClient, isLocal, priority), nil
|
2019-05-29 07:56:40 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 21:23:12 +00:00
|
|
|
type quicDialerFactory struct{}
|
2019-05-29 07:56:40 +00:00
|
|
|
|
2023-04-16 12:54:28 +00:00
|
|
|
func (quicDialerFactory) New(opts config.OptionsConfiguration, tlsCfg *tls.Config, registry *registry.Registry, lanChecker *lanChecker) genericDialer {
|
2021-05-14 13:26:02 +00:00
|
|
|
// So the idea is that we should probably try dialing every 20 seconds.
|
|
|
|
// However it would still be nice if this was adjustable/proportional to ReconnectIntervalS
|
|
|
|
// But prevent something silly like 1/3 = 0 etc.
|
|
|
|
quicInterval := opts.ReconnectIntervalS / 3
|
|
|
|
if quicInterval < 10 {
|
|
|
|
quicInterval = 10
|
|
|
|
}
|
2022-04-09 14:04:56 +00:00
|
|
|
return &quicDialer{
|
|
|
|
commonDialer: commonDialer{
|
|
|
|
reconnectInterval: time.Duration(quicInterval) * time.Second,
|
|
|
|
tlsCfg: tlsCfg,
|
2023-04-16 12:54:28 +00:00
|
|
|
lanPriority: opts.ConnectionPriorityQUICLAN,
|
|
|
|
wanPriority: opts.ConnectionPriorityQUICWAN,
|
|
|
|
lanChecker: lanChecker,
|
2022-04-09 14:04:56 +00:00
|
|
|
},
|
|
|
|
registry: registry,
|
|
|
|
}
|
2019-05-29 07:56:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (quicDialerFactory) AlwaysWAN() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (quicDialerFactory) Valid(_ config.Configuration) error {
|
|
|
|
// Always valid
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (quicDialerFactory) String() string {
|
|
|
|
return "QUIC Dialer"
|
|
|
|
}
|