From d88f0e8f3cef7c194728d439de1ac20744946b84 Mon Sep 17 00:00:00 2001 From: jenfonro <799170122@qq.com> Date: Tue, 4 Nov 2025 09:01:35 +0800 Subject: [PATCH] feat(net): support proxy configuration via config file (#1359) * support proxy * debug * debug2 * del debug * add proxy configuration with env var fallback * comments to en * refactor(env): fallback env --------- Co-authored-by: jyxjjj <773933146@qq.com> --- drivers/base/client.go | 3 +++ internal/bootstrap/config.go | 15 +++++++++++++++ internal/conf/config.go | 2 ++ internal/net/serve.go | 14 +++++++++----- internal/net/util.go | 23 +++++++++++++++++++++++ 5 files changed, 52 insertions(+), 5 deletions(-) diff --git a/drivers/base/client.go b/drivers/base/client.go index a91d6254..819d1d78 100644 --- a/drivers/base/client.go +++ b/drivers/base/client.go @@ -25,6 +25,7 @@ func InitClient() { }), ).SetTLSClientConfig(&tls.Config{InsecureSkipVerify: conf.Conf.TlsInsecureSkipVerify}) NoRedirectClient.SetHeader("user-agent", UserAgent) + net.SetRestyProxyIfConfigured(NoRedirectClient) RestyClient = NewRestyClient() HttpClient = net.NewHttpClient() @@ -37,5 +38,7 @@ func NewRestyClient() *resty.Client { SetRetryResetReaders(true). SetTimeout(DefaultTimeout). SetTLSClientConfig(&tls.Config{InsecureSkipVerify: conf.Conf.TlsInsecureSkipVerify}) + + net.SetRestyProxyIfConfigured(client) return client } diff --git a/internal/bootstrap/config.go b/internal/bootstrap/config.go index bb381c95..116b4cd3 100644 --- a/internal/bootstrap/config.go +++ b/internal/bootstrap/config.go @@ -140,6 +140,10 @@ func InitConfig() { log.Fatalf("create temp dir error: %+v", err) } log.Debugf("config: %+v", conf.Conf) + + // Validate and display proxy configuration status + validateProxyConfig() + base.InitClient() initURL() } @@ -179,3 +183,14 @@ func CleanTempDir() { } } } + +// validateProxyConfig validates proxy configuration and displays status at startup +func validateProxyConfig() { + if conf.Conf.ProxyAddress != "" { + if _, err := url.Parse(conf.Conf.ProxyAddress); err == nil { + log.Infof("Proxy enabled: %s", conf.Conf.ProxyAddress) + } else { + log.Errorf("Invalid proxy address format: %s, error: %v", conf.Conf.ProxyAddress, err) + } + } +} diff --git a/internal/conf/config.go b/internal/conf/config.go index 4f239a11..c5ace800 100644 --- a/internal/conf/config.go +++ b/internal/conf/config.go @@ -131,6 +131,7 @@ type Config struct { FTP FTP `json:"ftp" envPrefix:"FTP_"` SFTP SFTP `json:"sftp" envPrefix:"SFTP_"` LastLaunchedVersion string `json:"last_launched_version"` + ProxyAddress string `json:"proxy_address" env:"PROXY_ADDRESS"` } func DefaultConfig(dataDir string) *Config { @@ -244,5 +245,6 @@ func DefaultConfig(dataDir string) *Config { Listen: ":5222", }, LastLaunchedVersion: "", + ProxyAddress: "", } } diff --git a/internal/net/serve.go b/internal/net/serve.go index f8d2e3ca..6a20460b 100644 --- a/internal/net/serve.go +++ b/internal/net/serve.go @@ -283,11 +283,15 @@ func HttpClient() *http.Client { } func NewHttpClient() *http.Client { + transport := &http.Transport{ + Proxy: http.ProxyFromEnvironment, + TLSClientConfig: &tls.Config{InsecureSkipVerify: conf.Conf.TlsInsecureSkipVerify}, + } + + SetProxyIfConfigured(transport) + return &http.Client{ - Timeout: time.Hour * 48, - Transport: &http.Transport{ - Proxy: http.ProxyFromEnvironment, - TLSClientConfig: &tls.Config{InsecureSkipVerify: conf.Conf.TlsInsecureSkipVerify}, - }, + Timeout: time.Hour * 48, + Transport: transport, } } diff --git a/internal/net/util.go b/internal/net/util.go index 40b5e145..7b799c13 100644 --- a/internal/net/util.go +++ b/internal/net/util.go @@ -7,12 +7,15 @@ import ( "mime/multipart" "net/http" "net/textproto" + "net/url" "strings" "time" + "github.com/OpenListTeam/OpenList/v4/internal/conf" "github.com/OpenListTeam/OpenList/v4/pkg/utils" "github.com/OpenListTeam/OpenList/v4/pkg/http_range" + "github.com/go-resty/resty/v2" log "github.com/sirupsen/logrus" ) @@ -350,3 +353,23 @@ func GetRangedHttpReader(readCloser io.ReadCloser, offset, length int64) (io.Rea // return an io.ReadCloser that is limited to `length` bytes. return &LimitedReadCloser{readCloser, length_int}, nil } + +// SetProxyIfConfigured sets proxy for HTTP Transport if configured +func SetProxyIfConfigured(transport *http.Transport) { + // If proxy address is configured, override environment variable settings + if conf.Conf.ProxyAddress != "" { + if proxyURL, err := url.Parse(conf.Conf.ProxyAddress); err == nil { + transport.Proxy = http.ProxyURL(proxyURL) + } + } +} + +// SetRestyProxyIfConfigured sets proxy for Resty client if configured +func SetRestyProxyIfConfigured(client *resty.Client) { + // If proxy address is configured, override environment variable settings + if conf.Conf.ProxyAddress != "" { + if proxyURL, err := url.Parse(conf.Conf.ProxyAddress); err == nil { + client.SetProxy(proxyURL.String()) + } + } +}