diff --git a/internal/protocol/bt/fetcher.go b/internal/protocol/bt/fetcher.go index db1d8df4..c494a87e 100644 --- a/internal/protocol/bt/fetcher.go +++ b/internal/protocol/bt/fetcher.go @@ -10,7 +10,6 @@ import ( "github.com/GopeedLab/gopeed/pkg/util" "github.com/anacrolix/torrent" "github.com/anacrolix/torrent/metainfo" - "net/http" "path/filepath" "strings" "sync" @@ -69,9 +68,7 @@ func (f *Fetcher) initClient() (err error) { cfg.Bep20 = fmt.Sprintf("-GP%s-", parseBep20()) cfg.ExtendedHandshakeClientVersion = fmt.Sprintf("Gopeed %s", base.Version) cfg.ListenPort = f.config.ListenPort - if f.ctl.ProxyUrl != nil { - cfg.HTTPProxy = http.ProxyURL(f.ctl.ProxyUrl) - } + cfg.HTTPProxy = util.ProxyUrlToHandler(f.ctl.ProxyUrl) cfg.DefaultStorage = newFileOpts(newFileClientOpts{ ClientBaseDir: cfg.DataDir, HandleFileTorrent: func(infoHash metainfo.Hash, ft *fileTorrentImpl) { diff --git a/internal/protocol/http/fetcher.go b/internal/protocol/http/fetcher.go index e850c0df..46417b5d 100644 --- a/internal/protocol/http/fetcher.go +++ b/internal/protocol/http/fetcher.go @@ -9,6 +9,7 @@ import ( "github.com/GopeedLab/gopeed/internal/fetcher" "github.com/GopeedLab/gopeed/pkg/base" fhttp "github.com/GopeedLab/gopeed/pkg/protocol/http" + "github.com/GopeedLab/gopeed/pkg/util" "github.com/xiaoqidun/setft" "golang.org/x/sync/errgroup" "io" @@ -454,9 +455,7 @@ func (f *Fetcher) buildClient() *http.Client { transport := &http.Transport{} // Cookie handle jar, _ := cookiejar.New(nil) - if f.ctl.ProxyUrl != nil { - transport.Proxy = http.ProxyURL(f.ctl.ProxyUrl) - } + transport.Proxy = util.ProxyUrlToHandler(f.ctl.ProxyUrl) return &http.Client{ Transport: transport, Jar: jar, diff --git a/pkg/download/downloader_test.go b/pkg/download/downloader_test.go index 7836baf3..193da864 100644 --- a/pkg/download/downloader_test.go +++ b/pkg/download/downloader_test.go @@ -93,6 +93,11 @@ func TestDownloader_CreateWithProxy(t *testing.T) { proxyCfg.Enable = false return proxyCfg }) + // System proxy + doTestDownloaderCreateWithProxy(t, false, func(proxyCfg *DownloaderProxyConfig) *DownloaderProxyConfig { + proxyCfg.Scheme = "system" + return proxyCfg + }) // Invalid proxy scheme doTestDownloaderCreateWithProxy(t, false, func(proxyCfg *DownloaderProxyConfig) *DownloaderProxyConfig { proxyCfg.Scheme = "" diff --git a/pkg/download/engine/inject/xhr/module.go b/pkg/download/engine/inject/xhr/module.go index d91c56ac..0eedb3a7 100644 --- a/pkg/download/engine/inject/xhr/module.go +++ b/pkg/download/engine/inject/xhr/module.go @@ -2,9 +2,11 @@ package xhr import ( "bytes" + "errors" "github.com/GopeedLab/gopeed/pkg/download/engine/inject/file" "github.com/GopeedLab/gopeed/pkg/download/engine/inject/formdata" "github.com/GopeedLab/gopeed/pkg/download/engine/util" + urlUtil "github.com/GopeedLab/gopeed/pkg/util" "github.com/dop251/goja" "io" "mime/multipart" @@ -203,9 +205,7 @@ func (xhr *XMLHttpRequest) Send(data goja.Value) { req.Header.Set(k, v) } transport := &http.Transport{} - if xhr.proxyUrl != nil { - transport.Proxy = http.ProxyURL(xhr.proxyUrl) - } + transport.Proxy = urlUtil.ProxyUrlToHandler(xhr.proxyUrl) client := &http.Client{ Transport: transport, Timeout: time.Duration(xhr.Timeout) * time.Millisecond, @@ -213,7 +213,8 @@ func (xhr *XMLHttpRequest) Send(data goja.Value) { resp, err := client.Do(req) if err != nil { // handle timeout error - if err, ok := err.(net.Error); ok && err.Timeout() { + var ne net.Error + if errors.As(err, &ne) && ne.Timeout() { if xhr.Timeout > 0 { xhr.Upload.callOntimeout() xhr.callOntimeout() diff --git a/pkg/download/model.go b/pkg/download/model.go index c78521ce..d5e91c9c 100644 --- a/pkg/download/model.go +++ b/pkg/download/model.go @@ -141,7 +141,8 @@ func (cfg *DownloaderStoreConfig) ProxyUrl() *url.URL { } type DownloaderProxyConfig struct { - Enable bool `json:"enable"` + Enable bool `json:"enable"` + // Scheme is the proxy scheme, e.g. http, https, socks5, but there is a special scheme "system" that means use system proxy Scheme string `json:"scheme"` Host string `json:"host"` Usr string `json:"usr"` diff --git a/pkg/util/url.go b/pkg/util/url.go index 0a6ac190..a54c6780 100644 --- a/pkg/util/url.go +++ b/pkg/util/url.go @@ -2,6 +2,7 @@ package util import ( "encoding/base64" + "net/http" "net/url" "regexp" "strings" @@ -51,3 +52,14 @@ func BuildProxyUrl(scheme, host, usr, pwd string) *url.URL { Host: host, } } + +// ProxyUrlToHandler gets the proxy handler from the proxy url. +func ProxyUrlToHandler(proxyUrl *url.URL) func(*http.Request) (*url.URL, error) { + if proxyUrl == nil { + return nil + } + if proxyUrl.Scheme == "system" { + return http.ProxyFromEnvironment + } + return http.ProxyURL(proxyUrl) +}