Add support for HTTP/HTTPS proxies

Fixes #638
This commit is contained in:
Frank Denis 2018-11-15 18:47:33 +01:00
parent e48779c2eb
commit 2e147364e9
3 changed files with 20 additions and 2 deletions

View File

@ -6,6 +6,7 @@ import (
"flag"
"fmt"
"net"
"net/http"
"net/url"
"os"
"path"
@ -71,6 +72,7 @@ type Config struct {
NetprobeAddress string `toml:"netprobe_address"`
NetprobeTimeout int `toml:"netprobe_timeout"`
OfflineMode bool `toml:"offline_mode"`
HTTPProxyURL string `toml:"http_proxy"`
}
func newConfig() Config {
@ -254,11 +256,18 @@ func ConfigLoad(proxy *Proxy, svcFlag *string) error {
proxy.xTransport.useIPv4 = config.SourceIPv4
proxy.xTransport.useIPv6 = config.SourceIPv6
proxy.xTransport.keepAlive = time.Duration(config.KeepAlive) * time.Second
if len(config.HTTPProxyURL) > 0 {
httpProxyURL, err := url.Parse(config.HTTPProxyURL)
if err != nil {
dlog.Fatalf("Unable to parse the HTTP proxy URL [%v]", config.HTTPProxyURL)
}
proxy.xTransport.httpProxyFunction = http.ProxyURL(httpProxyURL)
}
if len(config.Proxy) > 0 {
proxyDialerURL, err := url.Parse(config.Proxy)
if err != nil {
dlog.Fatalf("Unable to parse proxy url [%v]", config.Proxy)
dlog.Fatalf("Unable to parse the proxy URL [%v]", config.Proxy)
}
proxyDialer, err := netproxy.FromURL(proxyDialerURL, netproxy.Direct)
if err != nil {

View File

@ -85,13 +85,18 @@ require_nofilter = true
force_tcp = false
## HTTP / SOCKS proxy
## SOCKS proxy
## Uncomment the following line to route all TCP connections to a local Tor node
## Tor doesn't support UDP, so set `force_tcp` to `true` as well.
# proxy = "socks5://127.0.0.1:9050"
## HTTP/HTTPS proxy
## Only for DoH servers
# http_proxy = "http://127.0.0.1:8888"
## How long a DNS query will wait for a response, in milliseconds

View File

@ -43,6 +43,7 @@ type XTransport struct {
tlsDisableSessionTickets bool
tlsCipherSuite []uint16
proxyDialer *netproxy.Dialer
httpProxyFunction func(*http.Request) (*url.URL, error)
}
var DefaultKeepAlive = 5 * time.Second
@ -104,6 +105,9 @@ func (xTransport *XTransport) rebuildTransport() {
}
},
}
if xTransport.httpProxyFunction != nil {
transport.Proxy = xTransport.httpProxyFunction
}
if xTransport.tlsDisableSessionTickets || xTransport.tlsCipherSuite != nil {
tlsClientConfig := tls.Config{
SessionTicketsDisabled: xTransport.tlsDisableSessionTickets,