This commit is contained in:
liwei committed 2024-05-01 18:35:32 +08:00
1 parent 070db64ca9
commit f8ea009fe9
6 files changed
+27 -12

No files matched your search

+1 -4
View File
@@ -10,7 +10,6 @@ import (
"github.com/GopeedLab/gopeed/pkg/util" "github.com/GopeedLab/gopeed/pkg/util"
"github.com/anacrolix/torrent" "github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo" "github.com/anacrolix/torrent/metainfo"
"net/http"
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
@@ -69,9 +68,7 @@ func (f *Fetcher) initClient() (err error) {
cfg.Bep20 = fmt.Sprintf("-GP%s-", parseBep20()) cfg.Bep20 = fmt.Sprintf("-GP%s-", parseBep20())
cfg.ExtendedHandshakeClientVersion = fmt.Sprintf("Gopeed %s", base.Version) cfg.ExtendedHandshakeClientVersion = fmt.Sprintf("Gopeed %s", base.Version)
cfg.ListenPort = f.config.ListenPort cfg.ListenPort = f.config.ListenPort
if f.ctl.ProxyUrl != nil { cfg.HTTPProxy = util.ProxyUrlToHandler(f.ctl.ProxyUrl)
cfg.HTTPProxy = http.ProxyURL(f.ctl.ProxyUrl)
}
cfg.DefaultStorage = newFileOpts(newFileClientOpts{ cfg.DefaultStorage = newFileOpts(newFileClientOpts{
ClientBaseDir: cfg.DataDir, ClientBaseDir: cfg.DataDir,
HandleFileTorrent: func(infoHash metainfo.Hash, ft *fileTorrentImpl) { HandleFileTorrent: func(infoHash metainfo.Hash, ft *fileTorrentImpl) {
+2 -3
View File
@@ -9,6 +9,7 @@ import (
"github.com/GopeedLab/gopeed/internal/fetcher" "github.com/GopeedLab/gopeed/internal/fetcher"
"github.com/GopeedLab/gopeed/pkg/base" "github.com/GopeedLab/gopeed/pkg/base"
fhttp "github.com/GopeedLab/gopeed/pkg/protocol/http" fhttp "github.com/GopeedLab/gopeed/pkg/protocol/http"
"github.com/GopeedLab/gopeed/pkg/util"
"github.com/xiaoqidun/setft" "github.com/xiaoqidun/setft"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
"io" "io"
@@ -454,9 +455,7 @@ func (f *Fetcher) buildClient() *http.Client {
transport := &http.Transport{} transport := &http.Transport{}
// Cookie handle // Cookie handle
jar, _ := cookiejar.New(nil) jar, _ := cookiejar.New(nil)
if f.ctl.ProxyUrl != nil { transport.Proxy = util.ProxyUrlToHandler(f.ctl.ProxyUrl)
transport.Proxy = http.ProxyURL(f.ctl.ProxyUrl)
}
return &http.Client{ return &http.Client{
Transport: transport, Transport: transport,
Jar: jar, Jar: jar,
+5
View File
@@ -93,6 +93,11 @@ func TestDownloader_CreateWithProxy(t *testing.T) {
proxyCfg.Enable = false proxyCfg.Enable = false
return proxyCfg return proxyCfg
}) })
// System proxy
doTestDownloaderCreateWithProxy(t, false, func(proxyCfg *DownloaderProxyConfig) *DownloaderProxyConfig {
proxyCfg.Scheme = "system"
return proxyCfg
})
// Invalid proxy scheme // Invalid proxy scheme
doTestDownloaderCreateWithProxy(t, false, func(proxyCfg *DownloaderProxyConfig) *DownloaderProxyConfig { doTestDownloaderCreateWithProxy(t, false, func(proxyCfg *DownloaderProxyConfig) *DownloaderProxyConfig {
proxyCfg.Scheme = "" proxyCfg.Scheme = ""
+5 -4
View File
@@ -2,9 +2,11 @@ package xhr
import ( import (
"bytes" "bytes"
"errors"
"github.com/GopeedLab/gopeed/pkg/download/engine/inject/file" "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/inject/formdata"
"github.com/GopeedLab/gopeed/pkg/download/engine/util" "github.com/GopeedLab/gopeed/pkg/download/engine/util"
urlUtil "github.com/GopeedLab/gopeed/pkg/util"
"github.com/dop251/goja" "github.com/dop251/goja"
"io" "io"
"mime/multipart" "mime/multipart"
@@ -203,9 +205,7 @@ func (xhr *XMLHttpRequest) Send(data goja.Value) {
req.Header.Set(k, v) req.Header.Set(k, v)
} }
transport := &http.Transport{} transport := &http.Transport{}
if xhr.proxyUrl != nil { transport.Proxy = urlUtil.ProxyUrlToHandler(xhr.proxyUrl)
transport.Proxy = http.ProxyURL(xhr.proxyUrl)
}
client := &http.Client{ client := &http.Client{
Transport: transport, Transport: transport,
Timeout: time.Duration(xhr.Timeout) * time.Millisecond, Timeout: time.Duration(xhr.Timeout) * time.Millisecond,
@@ -213,7 +213,8 @@ func (xhr *XMLHttpRequest) Send(data goja.Value) {
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
// handle timeout error // 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 { if xhr.Timeout > 0 {
xhr.Upload.callOntimeout() xhr.Upload.callOntimeout()
xhr.callOntimeout() xhr.callOntimeout()
+2 -1
View File
@@ -141,7 +141,8 @@ func (cfg *DownloaderStoreConfig) ProxyUrl() *url.URL {
} }
type DownloaderProxyConfig struct { 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"` Scheme string `json:"scheme"`
Host string `json:"host"` Host string `json:"host"`
Usr string `json:"usr"` Usr string `json:"usr"`
+12
View File
@@ -2,6 +2,7 @@ package util
import ( import (
"encoding/base64" "encoding/base64"
"net/http"
"net/url" "net/url"
"regexp" "regexp"
"strings" "strings"
@@ -51,3 +52,14 @@ func BuildProxyUrl(scheme, host, usr, pwd string) *url.URL {
Host: host, 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)
}