This commit is contained in:
liwei
2024-05-01 18:35:32 +08:00
parent 070db64ca9
commit f8ea009fe9
6 changed files with 27 additions and 12 deletions
+5
View File
@@ -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 = ""
+5 -4
View File
@@ -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()
+2 -1
View File
@@ -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"`
+12
View File
@@ -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)
}