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 (
|
2020-02-26 04:37:23 -07:00
|
|
|
"net"
|
2015-11-26 16:31:37 -07:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2020-06-16 00:17:07 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/util"
|
2015-11-26 16:31:37 -07:00
|
|
|
"golang.org/x/net/proxy"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-11-26 00:39:51 -07:00
|
|
|
noFallback = os.Getenv("ALL_PROXY_NO_FALLBACK") != ""
|
2015-11-26 16:31:37 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-12-08 05:04:43 -07:00
|
|
|
proxy.RegisterDialerType("socks", socksDialerFunction)
|
|
|
|
|
2019-11-26 00:39:51 -07:00
|
|
|
if proxyDialer := proxy.FromEnvironment(); proxyDialer != proxy.Direct {
|
2015-11-26 16:31:37 -07:00
|
|
|
http.DefaultTransport = &http.Transport{
|
2019-11-26 00:39:51 -07:00
|
|
|
DialContext: DialContext,
|
2015-11-26 16:31:37 -07:00
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Defer this, so that logging gets setup.
|
|
|
|
go func() {
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
l.Infoln("Proxy settings detected")
|
2016-04-24 09:30:20 -07:00
|
|
|
if noFallback {
|
|
|
|
l.Infoln("Proxy fallback disabled")
|
|
|
|
}
|
2015-11-26 16:31:37 -07:00
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
go func() {
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
l.Debugln("Dialer logging disabled, as no proxy was detected")
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-08 05:04:43 -07:00
|
|
|
// This is a rip off of proxy.FromURL for "socks" URL scheme
|
|
|
|
func socksDialerFunction(u *url.URL, forward proxy.Dialer) (proxy.Dialer, error) {
|
|
|
|
var auth *proxy.Auth
|
|
|
|
if u.User != nil {
|
|
|
|
auth = new(proxy.Auth)
|
|
|
|
auth.User = u.User.Username()
|
|
|
|
if p, ok := u.User.Password(); ok {
|
|
|
|
auth.Password = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return proxy.SOCKS5("tcp", u.Host, auth, forward)
|
|
|
|
}
|
2020-02-26 04:37:23 -07:00
|
|
|
|
2020-06-16 00:17:07 -07:00
|
|
|
// Sort available addresses, preferring unspecified address.
|
|
|
|
func tcpAddrLess(i interface{}, j interface{}) bool {
|
|
|
|
return util.AddressUnspecifiedLess(i.(*net.TCPAddr), j.(*net.TCPAddr))
|
|
|
|
}
|
|
|
|
|
2020-02-26 04:37:23 -07:00
|
|
|
// 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.
|
|
|
|
type dialerConn struct {
|
|
|
|
net.Conn
|
|
|
|
addr net.Addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c dialerConn) RemoteAddr() net.Addr {
|
|
|
|
return c.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDialerAddr(network, addr string) net.Addr {
|
|
|
|
netAddr, err := net.ResolveIPAddr(network, addr)
|
|
|
|
if err == nil {
|
|
|
|
return netAddr
|
|
|
|
}
|
|
|
|
return fallbackAddr{network, addr}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fallbackAddr struct {
|
|
|
|
network string
|
|
|
|
addr string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a fallbackAddr) Network() string {
|
|
|
|
return a.network
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a fallbackAddr) String() string {
|
|
|
|
return a.addr
|
|
|
|
}
|