2015-11-26 16:31:37 -07:00
|
|
|
// Copyright (C) 2015 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-08 23:52:18 -07:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2015-11-26 16:31:37 -07:00
|
|
|
|
|
|
|
package dialer
|
|
|
|
|
|
|
|
import (
|
2019-11-26 00:39:51 -07:00
|
|
|
"context"
|
|
|
|
"errors"
|
2016-05-31 01:11:57 -07:00
|
|
|
"fmt"
|
2015-11-26 16:31:37 -07:00
|
|
|
"net"
|
|
|
|
"time"
|
2016-12-20 13:14:52 -07:00
|
|
|
|
|
|
|
"golang.org/x/net/ipv4"
|
|
|
|
"golang.org/x/net/ipv6"
|
2019-11-26 00:39:51 -07:00
|
|
|
"golang.org/x/net/proxy"
|
2015-11-26 16:31:37 -07:00
|
|
|
)
|
|
|
|
|
2019-11-26 00:39:51 -07:00
|
|
|
var errUnexpectedInterfaceType = errors.New("unexpected interface type")
|
2016-01-29 18:52:32 -07:00
|
|
|
|
2016-05-31 01:11:57 -07:00
|
|
|
// SetTCPOptions sets our default TCP options on a TCP connection, possibly
|
|
|
|
// digging through dialerConn to extract the *net.TCPConn
|
|
|
|
func SetTCPOptions(conn net.Conn) error {
|
|
|
|
switch conn := conn.(type) {
|
2020-02-26 04:37:23 -07:00
|
|
|
case dialerConn:
|
|
|
|
return SetTCPOptions(conn.Conn)
|
2016-05-31 01:11:57 -07:00
|
|
|
case *net.TCPConn:
|
|
|
|
var err error
|
|
|
|
if err = conn.SetLinger(0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = conn.SetNoDelay(false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = conn.SetKeepAlivePeriod(60 * time.Second); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = conn.SetKeepAlive(true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown connection type %T", conn)
|
2016-01-29 18:52:32 -07:00
|
|
|
}
|
|
|
|
}
|
2016-12-20 13:14:52 -07:00
|
|
|
|
|
|
|
func SetTrafficClass(conn net.Conn, class int) error {
|
|
|
|
switch conn := conn.(type) {
|
2020-02-26 04:37:23 -07:00
|
|
|
case dialerConn:
|
|
|
|
return SetTrafficClass(conn.Conn, class)
|
2016-12-20 13:14:52 -07:00
|
|
|
case *net.TCPConn:
|
|
|
|
e1 := ipv4.NewConn(conn).SetTOS(class)
|
|
|
|
e2 := ipv6.NewConn(conn).SetTrafficClass(class)
|
|
|
|
|
|
|
|
if e1 != nil {
|
|
|
|
return e1
|
|
|
|
}
|
|
|
|
return e2
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown connection type %T", conn)
|
|
|
|
}
|
|
|
|
}
|
2019-11-26 00:39:51 -07:00
|
|
|
|
|
|
|
func dialContextWithFallback(ctx context.Context, fallback proxy.ContextDialer, network, addr string) (net.Conn, error) {
|
|
|
|
dialer, ok := proxy.FromEnvironment().(proxy.ContextDialer)
|
|
|
|
if !ok {
|
|
|
|
return nil, errUnexpectedInterfaceType
|
|
|
|
}
|
|
|
|
if dialer == proxy.Direct {
|
2020-01-23 14:37:35 -07:00
|
|
|
conn, err := fallback.DialContext(ctx, network, addr)
|
|
|
|
l.Debugf("Dialing direct result %s %s: %v %v", network, addr, conn, err)
|
|
|
|
return conn, err
|
2019-11-26 00:39:51 -07:00
|
|
|
}
|
|
|
|
if noFallback {
|
2020-01-23 14:37:35 -07:00
|
|
|
conn, err := dialer.DialContext(ctx, network, addr)
|
|
|
|
l.Debugf("Dialing no fallback result %s %s: %v %v", network, addr, conn, err)
|
2020-02-26 05:16:18 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-02-26 04:37:23 -07:00
|
|
|
}
|
2020-02-26 05:16:18 -07:00
|
|
|
return dialerConn{conn, newDialerAddr(network, addr)}, nil
|
2019-11-26 00:39:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
var proxyConn, fallbackConn net.Conn
|
|
|
|
var proxyErr, fallbackErr error
|
|
|
|
proxyDone := make(chan struct{})
|
|
|
|
fallbackDone := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
proxyConn, proxyErr = dialer.DialContext(ctx, network, addr)
|
2020-01-23 14:37:35 -07:00
|
|
|
l.Debugf("Dialing proxy result %s %s: %v %v", network, addr, proxyConn, proxyErr)
|
2020-02-26 05:16:18 -07:00
|
|
|
if proxyErr == nil {
|
|
|
|
proxyConn = dialerConn{proxyConn, newDialerAddr(network, addr)}
|
2020-02-26 04:37:23 -07:00
|
|
|
}
|
2019-11-26 00:39:51 -07:00
|
|
|
close(proxyDone)
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
fallbackConn, fallbackErr = fallback.DialContext(ctx, network, addr)
|
2020-01-23 14:37:35 -07:00
|
|
|
l.Debugf("Dialing fallback result %s %s: %v %v", network, addr, fallbackConn, fallbackErr)
|
2019-11-26 00:39:51 -07:00
|
|
|
close(fallbackDone)
|
|
|
|
}()
|
|
|
|
<-proxyDone
|
|
|
|
if proxyErr == nil {
|
|
|
|
go func() {
|
|
|
|
<-fallbackDone
|
|
|
|
if fallbackErr == nil {
|
2020-02-26 04:37:23 -07:00
|
|
|
_ = fallbackConn.Close()
|
2019-11-26 00:39:51 -07:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
return proxyConn, nil
|
|
|
|
}
|
|
|
|
<-fallbackDone
|
|
|
|
return fallbackConn, fallbackErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// DialContext dials via context and/or directly, depending on how it is configured.
|
|
|
|
// If dialing via proxy and allowing fallback, dialing for both happens simultaneously
|
|
|
|
// and the proxy connection is returned if successful.
|
|
|
|
func DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
|
|
return dialContextWithFallback(ctx, proxy.Direct, network, addr)
|
|
|
|
}
|