Skip to content

Commit d88f0e8

Browse files
jenfonrojyxjjj
andauthored
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>
1 parent 0857478 commit d88f0e8

File tree

5 files changed

+52
-5
lines changed

5 files changed

+52
-5
lines changed

drivers/base/client.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func InitClient() {
2525
}),
2626
).SetTLSClientConfig(&tls.Config{InsecureSkipVerify: conf.Conf.TlsInsecureSkipVerify})
2727
NoRedirectClient.SetHeader("user-agent", UserAgent)
28+
net.SetRestyProxyIfConfigured(NoRedirectClient)
2829

2930
RestyClient = NewRestyClient()
3031
HttpClient = net.NewHttpClient()
@@ -37,5 +38,7 @@ func NewRestyClient() *resty.Client {
3738
SetRetryResetReaders(true).
3839
SetTimeout(DefaultTimeout).
3940
SetTLSClientConfig(&tls.Config{InsecureSkipVerify: conf.Conf.TlsInsecureSkipVerify})
41+
42+
net.SetRestyProxyIfConfigured(client)
4043
return client
4144
}

internal/bootstrap/config.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ func InitConfig() {
140140
log.Fatalf("create temp dir error: %+v", err)
141141
}
142142
log.Debugf("config: %+v", conf.Conf)
143+
144+
// Validate and display proxy configuration status
145+
validateProxyConfig()
146+
143147
base.InitClient()
144148
initURL()
145149
}
@@ -179,3 +183,14 @@ func CleanTempDir() {
179183
}
180184
}
181185
}
186+
187+
// validateProxyConfig validates proxy configuration and displays status at startup
188+
func validateProxyConfig() {
189+
if conf.Conf.ProxyAddress != "" {
190+
if _, err := url.Parse(conf.Conf.ProxyAddress); err == nil {
191+
log.Infof("Proxy enabled: %s", conf.Conf.ProxyAddress)
192+
} else {
193+
log.Errorf("Invalid proxy address format: %s, error: %v", conf.Conf.ProxyAddress, err)
194+
}
195+
}
196+
}

internal/conf/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ type Config struct {
131131
FTP FTP `json:"ftp" envPrefix:"FTP_"`
132132
SFTP SFTP `json:"sftp" envPrefix:"SFTP_"`
133133
LastLaunchedVersion string `json:"last_launched_version"`
134+
ProxyAddress string `json:"proxy_address" env:"PROXY_ADDRESS"`
134135
}
135136

136137
func DefaultConfig(dataDir string) *Config {
@@ -244,5 +245,6 @@ func DefaultConfig(dataDir string) *Config {
244245
Listen: ":5222",
245246
},
246247
LastLaunchedVersion: "",
248+
ProxyAddress: "",
247249
}
248250
}

internal/net/serve.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,15 @@ func HttpClient() *http.Client {
283283
}
284284

285285
func NewHttpClient() *http.Client {
286+
transport := &http.Transport{
287+
Proxy: http.ProxyFromEnvironment,
288+
TLSClientConfig: &tls.Config{InsecureSkipVerify: conf.Conf.TlsInsecureSkipVerify},
289+
}
290+
291+
SetProxyIfConfigured(transport)
292+
286293
return &http.Client{
287-
Timeout: time.Hour * 48,
288-
Transport: &http.Transport{
289-
Proxy: http.ProxyFromEnvironment,
290-
TLSClientConfig: &tls.Config{InsecureSkipVerify: conf.Conf.TlsInsecureSkipVerify},
291-
},
294+
Timeout: time.Hour * 48,
295+
Transport: transport,
292296
}
293297
}

internal/net/util.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ import (
77
"mime/multipart"
88
"net/http"
99
"net/textproto"
10+
"net/url"
1011
"strings"
1112
"time"
1213

14+
"github.com/OpenListTeam/OpenList/v4/internal/conf"
1315
"github.com/OpenListTeam/OpenList/v4/pkg/utils"
1416

1517
"github.com/OpenListTeam/OpenList/v4/pkg/http_range"
18+
"github.com/go-resty/resty/v2"
1619
log "github.com/sirupsen/logrus"
1720
)
1821

@@ -350,3 +353,23 @@ func GetRangedHttpReader(readCloser io.ReadCloser, offset, length int64) (io.Rea
350353
// return an io.ReadCloser that is limited to `length` bytes.
351354
return &LimitedReadCloser{readCloser, length_int}, nil
352355
}
356+
357+
// SetProxyIfConfigured sets proxy for HTTP Transport if configured
358+
func SetProxyIfConfigured(transport *http.Transport) {
359+
// If proxy address is configured, override environment variable settings
360+
if conf.Conf.ProxyAddress != "" {
361+
if proxyURL, err := url.Parse(conf.Conf.ProxyAddress); err == nil {
362+
transport.Proxy = http.ProxyURL(proxyURL)
363+
}
364+
}
365+
}
366+
367+
// SetRestyProxyIfConfigured sets proxy for Resty client if configured
368+
func SetRestyProxyIfConfigured(client *resty.Client) {
369+
// If proxy address is configured, override environment variable settings
370+
if conf.Conf.ProxyAddress != "" {
371+
if proxyURL, err := url.Parse(conf.Conf.ProxyAddress); err == nil {
372+
client.SetProxy(proxyURL.String())
373+
}
374+
}
375+
}

0 commit comments

Comments
 (0)