@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user