@@ -13,6 +13,7 @@ import (
|
||||
"net"
|
||||
|
||||
"github.com/lucas-clemente/quic-go"
|
||||
"github.com/syncthing/syncthing/lib/util"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -65,21 +66,5 @@ func (q *quicTlsConn) ConnectionState() tls.ConnectionState {
|
||||
|
||||
// Sort available packet connections by ip address, preferring unspecified local address.
|
||||
func packetConnLess(i interface{}, j interface{}) bool {
|
||||
iIsUnspecified := false
|
||||
jIsUnspecified := false
|
||||
iLocalAddr := i.(net.PacketConn).LocalAddr()
|
||||
jLocalAddr := j.(net.PacketConn).LocalAddr()
|
||||
|
||||
if host, _, err := net.SplitHostPort(iLocalAddr.String()); err == nil {
|
||||
iIsUnspecified = host == "" || net.ParseIP(host).IsUnspecified()
|
||||
}
|
||||
if host, _, err := net.SplitHostPort(jLocalAddr.String()); err == nil {
|
||||
jIsUnspecified = host == "" || net.ParseIP(host).IsUnspecified()
|
||||
}
|
||||
|
||||
if jIsUnspecified == iIsUnspecified {
|
||||
return len(iLocalAddr.Network()) < len(jLocalAddr.Network())
|
||||
}
|
||||
|
||||
return iIsUnspecified
|
||||
return util.AddressUnspecifiedLess(i.(net.PacketConn).LocalAddr(), j.(net.PacketConn).LocalAddr())
|
||||
}
|
||||
|
||||
@@ -1,92 +0,0 @@
|
||||
// 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 http://mozilla.org/MPL/2.0/.
|
||||
|
||||
// +build go1.12
|
||||
|
||||
package connections
|
||||
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type mockPacketConn struct {
|
||||
addr mockedAddr
|
||||
}
|
||||
|
||||
func (mockPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (mockPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (mockPacketConn) Close() error {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (c *mockPacketConn) LocalAddr() net.Addr {
|
||||
return c.addr
|
||||
}
|
||||
|
||||
func (mockPacketConn) SetDeadline(t time.Time) error {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (mockPacketConn) SetReadDeadline(t time.Time) error {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (mockPacketConn) SetWriteDeadline(t time.Time) error {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
type mockedAddr struct {
|
||||
network string
|
||||
addr string
|
||||
}
|
||||
|
||||
func (a mockedAddr) Network() string {
|
||||
return a.network
|
||||
}
|
||||
|
||||
func (a mockedAddr) String() string {
|
||||
return a.addr
|
||||
}
|
||||
|
||||
func TestPacketConnLess(t *testing.T) {
|
||||
cases := []struct {
|
||||
netA string
|
||||
addrA string
|
||||
netB string
|
||||
addrB string
|
||||
}{
|
||||
// B is assumed the winner.
|
||||
{"tcp", "127.0.0.1:1234", "tcp", ":1235"},
|
||||
{"tcp", "127.0.0.1:1234", "tcp", "0.0.0.0:1235"},
|
||||
{"tcp4", "0.0.0.0:1234", "tcp", "0.0.0.0:1235"}, // tcp4 on the first one
|
||||
}
|
||||
|
||||
for i, testCase := range cases {
|
||||
|
||||
conns := []*mockPacketConn{
|
||||
{mockedAddr{testCase.netA, testCase.addrA}},
|
||||
{mockedAddr{testCase.netB, testCase.addrB}},
|
||||
}
|
||||
|
||||
if packetConnLess(conns[0], conns[1]) {
|
||||
t.Error(i, "unexpected")
|
||||
}
|
||||
if !packetConnLess(conns[1], conns[0]) {
|
||||
t.Error(i, "unexpected")
|
||||
}
|
||||
if packetConnLess(conns[0], conns[0]) || packetConnLess(conns[1], conns[1]) {
|
||||
t.Error(i, "unexpected")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,7 @@ func (d *tcpDialer) Dial(ctx context.Context, _ protocol.DeviceID, uri *url.URL)
|
||||
|
||||
timeoutCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
defer cancel()
|
||||
conn, err := dialer.DialContext(timeoutCtx, uri.Scheme, uri.Host)
|
||||
conn, err := dialer.DialContextReusePort(timeoutCtx, uri.Scheme, uri.Host)
|
||||
if err != nil {
|
||||
return internalConn{}, err
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/syncthing/syncthing/lib/config"
|
||||
"github.com/syncthing/syncthing/lib/connections/registry"
|
||||
"github.com/syncthing/syncthing/lib/dialer"
|
||||
"github.com/syncthing/syncthing/lib/nat"
|
||||
"github.com/syncthing/syncthing/lib/util"
|
||||
@@ -50,14 +51,21 @@ func (t *tcpListener) serve(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
listener, err := net.ListenTCP(t.uri.Scheme, tcaddr)
|
||||
lc := net.ListenConfig{
|
||||
Control: dialer.ReusePortControl,
|
||||
}
|
||||
|
||||
listener, err := lc.Listen(context.TODO(), t.uri.Scheme, tcaddr.String())
|
||||
if err != nil {
|
||||
l.Infoln("Listen (BEP/tcp):", err)
|
||||
return err
|
||||
}
|
||||
t.notifyAddressesChanged(t)
|
||||
registry.Register(t.uri.Scheme, tcaddr)
|
||||
|
||||
defer listener.Close()
|
||||
defer t.clearAddresses(t)
|
||||
defer registry.Unregister(t.uri.Scheme, tcaddr)
|
||||
|
||||
l.Infof("TCP listener (%v) starting", listener.Addr())
|
||||
defer l.Infof("TCP listener (%v) shutting down", listener.Addr())
|
||||
@@ -75,9 +83,12 @@ func (t *tcpListener) serve(ctx context.Context) error {
|
||||
acceptFailures := 0
|
||||
const maxAcceptFailures = 10
|
||||
|
||||
// :(, but what can you do.
|
||||
tcpListener := listener.(*net.TCPListener)
|
||||
|
||||
for {
|
||||
listener.SetDeadline(time.Now().Add(time.Second))
|
||||
conn, err := listener.Accept()
|
||||
_ = tcpListener.SetDeadline(time.Now().Add(time.Second))
|
||||
conn, err := tcpListener.Accept()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
if err == nil {
|
||||
@@ -156,6 +167,14 @@ func (t *tcpListener) WANAddresses() []*url.URL {
|
||||
}
|
||||
}
|
||||
t.mut.RUnlock()
|
||||
|
||||
// If we support ReusePort, add an unspecified zero port address, which will be resolved by the discovery server
|
||||
// in hopes that TCP punch through works.
|
||||
if dialer.SupportsReusePort {
|
||||
uri := *t.uri
|
||||
uri.Host = "0.0.0.0:0"
|
||||
uris = append([]*url.URL{&uri}, uris...)
|
||||
}
|
||||
return uris
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
// 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/.
|
||||
|
||||
// +build !solaris,!windows
|
||||
|
||||
package dialer
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
var SupportsReusePort = false
|
||||
|
||||
func init() {
|
||||
fd, err := unix.Socket(unix.AF_INET, unix.SOCK_STREAM, unix.IPPROTO_IP)
|
||||
if err != nil {
|
||||
l.Debugln("Failed to create a socket", err)
|
||||
return
|
||||
}
|
||||
defer func() { _ = unix.Close(fd) }()
|
||||
|
||||
err = unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
|
||||
switch {
|
||||
case err == unix.ENOPROTOOPT || err == unix.EINVAL:
|
||||
l.Debugln("SO_REUSEPORT not supported")
|
||||
case err != nil:
|
||||
l.Debugln("Unknown error when determining SO_REUSEPORT support", err)
|
||||
default:
|
||||
l.Debugln("SO_REUSEPORT supported")
|
||||
SupportsReusePort = true
|
||||
}
|
||||
}
|
||||
|
||||
func ReusePortControl(_, _ string, c syscall.RawConn) error {
|
||||
if !SupportsReusePort {
|
||||
return nil
|
||||
}
|
||||
var opErr error
|
||||
err := c.Control(func(fd uintptr) {
|
||||
opErr = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return opErr
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// 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/.
|
||||
|
||||
// +build solaris
|
||||
|
||||
package dialer
|
||||
|
||||
import "syscall"
|
||||
|
||||
var SupportsReusePort = false
|
||||
|
||||
func ReusePortControl(_, _ string, _ syscall.RawConn) error {
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// 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/.
|
||||
|
||||
// +build windows
|
||||
|
||||
package dialer
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
)
|
||||
|
||||
var SupportsReusePort = true
|
||||
|
||||
func ReusePortControl(_, _ string, c syscall.RawConn) error {
|
||||
var opErr error
|
||||
err := c.Control(func(fd uintptr) {
|
||||
// On Windows, SO_REUSEADDR is equivalent to SO_REUSEPORT on Linux.
|
||||
opErr = syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
|
||||
})
|
||||
if err != nil {
|
||||
l.Debugln("ReusePortControl", err)
|
||||
return err
|
||||
}
|
||||
if opErr != nil {
|
||||
l.Debugln("ReusePortControl", opErr)
|
||||
}
|
||||
return opErr
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/syncthing/syncthing/lib/util"
|
||||
"golang.org/x/net/proxy"
|
||||
)
|
||||
|
||||
@@ -60,6 +61,11 @@ func socksDialerFunction(u *url.URL, forward proxy.Dialer) (proxy.Dialer, error)
|
||||
return proxy.SOCKS5("tcp", u.Host, auth, forward)
|
||||
}
|
||||
|
||||
// Sort available addresses, preferring unspecified address.
|
||||
func tcpAddrLess(i interface{}, j interface{}) bool {
|
||||
return util.AddressUnspecifiedLess(i.(*net.TCPAddr), j.(*net.TCPAddr))
|
||||
}
|
||||
|
||||
// dialerConn is needed because proxy dialed connections have RemoteAddr() pointing at the proxy,
|
||||
// which then screws up various things such as IsLAN checks, and "let's populate the relay invitation address from
|
||||
// existing connection" shenanigans.
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/syncthing/syncthing/lib/connections/registry"
|
||||
"golang.org/x/net/ipv4"
|
||||
"golang.org/x/net/ipv6"
|
||||
"golang.org/x/net/proxy"
|
||||
@@ -121,3 +122,22 @@ func dialContextWithFallback(ctx context.Context, fallback proxy.ContextDialer,
|
||||
func DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
return dialContextWithFallback(ctx, proxy.Direct, network, addr)
|
||||
}
|
||||
|
||||
// DialContextReusePort tries dialing via proxy if a proxy is configured, and falls back to
|
||||
// a direct connection reusing the port from the connections registry, if no proxy is defined, or connecting via proxy
|
||||
// fails. If the context has a timeout, the timeout might be applied twice.
|
||||
func DialContextReusePort(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
dialer := &net.Dialer{
|
||||
Control: ReusePortControl,
|
||||
}
|
||||
localAddrInterface := registry.Get(network, tcpAddrLess)
|
||||
if localAddrInterface != nil {
|
||||
if addr, ok := localAddrInterface.(*net.TCPAddr); !ok {
|
||||
return nil, errUnexpectedInterfaceType
|
||||
} else {
|
||||
dialer.LocalAddr = addr
|
||||
}
|
||||
}
|
||||
|
||||
return dialContextWithFallback(ctx, dialer, network, addr)
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ func NewGlobal(server string, cert tls.Certificate, addrList AddressLister, evLo
|
||||
var announceClient httpClient = &contextClient{&http.Client{
|
||||
Timeout: requestTimeout,
|
||||
Transport: &http.Transport{
|
||||
DialContext: dialer.DialContext,
|
||||
DialContext: dialer.DialContextReusePort,
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: opts.insecure,
|
||||
|
||||
@@ -9,6 +9,7 @@ package util
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"strconv"
|
||||
@@ -176,6 +177,25 @@ func Address(network, host string) string {
|
||||
return u.String()
|
||||
}
|
||||
|
||||
// AddressUnspecifiedLess is a comparator function preferring least specific network address (most widely listening,
|
||||
// namely preferring 0.0.0.0 over some IP), if both IPs are equal, it prefers the less restrictive network (prefers tcp
|
||||
// over tcp4)
|
||||
func AddressUnspecifiedLess(a, b net.Addr) bool {
|
||||
aIsUnspecified := false
|
||||
bIsUnspecified := false
|
||||
if host, _, err := net.SplitHostPort(a.String()); err == nil {
|
||||
aIsUnspecified = host == "" || net.ParseIP(host).IsUnspecified()
|
||||
}
|
||||
if host, _, err := net.SplitHostPort(b.String()); err == nil {
|
||||
bIsUnspecified = host == "" || net.ParseIP(host).IsUnspecified()
|
||||
}
|
||||
|
||||
if aIsUnspecified == bIsUnspecified {
|
||||
return len(a.Network()) < len(b.Network())
|
||||
}
|
||||
return aIsUnspecified
|
||||
}
|
||||
|
||||
// AsService wraps the given function to implement suture.Service by calling
|
||||
// that function on serve and closing the passed channel when Stop is called.
|
||||
func AsService(fn func(ctx context.Context), creator string) suture.Service {
|
||||
|
||||
@@ -227,6 +227,50 @@ func TestCopyMatching(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
type mockedAddr struct {
|
||||
network string
|
||||
addr string
|
||||
}
|
||||
|
||||
func (a mockedAddr) Network() string {
|
||||
return a.network
|
||||
}
|
||||
|
||||
func (a mockedAddr) String() string {
|
||||
return a.addr
|
||||
}
|
||||
|
||||
func TestInspecifiedAddressLess(t *testing.T) {
|
||||
cases := []struct {
|
||||
netA string
|
||||
addrA string
|
||||
netB string
|
||||
addrB string
|
||||
}{
|
||||
// B is assumed the winner.
|
||||
{"tcp", "127.0.0.1:1234", "tcp", ":1235"},
|
||||
{"tcp", "127.0.0.1:1234", "tcp", "0.0.0.0:1235"},
|
||||
{"tcp4", "0.0.0.0:1234", "tcp", "0.0.0.0:1235"}, // tcp4 on the first one
|
||||
}
|
||||
|
||||
for i, testCase := range cases {
|
||||
addrs := []mockedAddr{
|
||||
{testCase.netA, testCase.addrA},
|
||||
{testCase.netB, testCase.addrB},
|
||||
}
|
||||
|
||||
if AddressUnspecifiedLess(addrs[0], addrs[1]) {
|
||||
t.Error(i, "unexpected")
|
||||
}
|
||||
if !AddressUnspecifiedLess(addrs[1], addrs[0]) {
|
||||
t.Error(i, "unexpected")
|
||||
}
|
||||
if AddressUnspecifiedLess(addrs[0], addrs[0]) || AddressUnspecifiedLess(addrs[1], addrs[1]) {
|
||||
t.Error(i, "unexpected")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUtilStopTwicePanic(t *testing.T) {
|
||||
name := "foo"
|
||||
s := AsService(func(ctx context.Context) {
|
||||
|
||||
Reference in New Issue
Block a user