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