mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-14 01:04:14 +00:00
1bae4b7f50
* all: Use context in lib/dialer * a bit slimmer * https://github.com/syncthing/syncthing/pull/5753 * bot * missed adding debug.go * errors.Cause * simultaneous dialing * anti-leak
61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
// 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,
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
package dialer
|
|
|
|
import (
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"time"
|
|
|
|
"golang.org/x/net/proxy"
|
|
)
|
|
|
|
var (
|
|
noFallback = os.Getenv("ALL_PROXY_NO_FALLBACK") != ""
|
|
)
|
|
|
|
func init() {
|
|
proxy.RegisterDialerType("socks", socksDialerFunction)
|
|
|
|
if proxyDialer := proxy.FromEnvironment(); proxyDialer != proxy.Direct {
|
|
http.DefaultTransport = &http.Transport{
|
|
DialContext: DialContext,
|
|
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")
|
|
if noFallback {
|
|
l.Infoln("Proxy fallback disabled")
|
|
}
|
|
}()
|
|
} else {
|
|
go func() {
|
|
time.Sleep(500 * time.Millisecond)
|
|
l.Debugln("Dialer logging disabled, as no proxy was detected")
|
|
}()
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
}
|