diff --git a/common/utils/netutil/proxy.go b/common/utils/netutil/proxy.go index 038702f..367af89 100644 --- a/common/utils/netutil/proxy.go +++ b/common/utils/netutil/proxy.go @@ -6,7 +6,10 @@ import ( "net" "net/http" "net/url" + "sync" + "github.com/charmbracelet/log" + "github.com/krau/SaveAny-Bot/config" "golang.org/x/net/proxy" ) @@ -20,7 +23,11 @@ func NewProxyDialer(proxyUrl string) (proxy.Dialer, error) { func NewProxyHTTPClient(proxyUrl string) (*http.Client, error) { if proxyUrl == "" { - return http.DefaultClient, nil + return &http.Client{ + Transport: &http.Transport{ + Proxy: http.ProxyFromEnvironment, + }, + }, nil } u, err := url.Parse(proxyUrl) @@ -52,3 +59,21 @@ func NewProxyHTTPClient(proxyUrl string) (*http.Client, error) { return nil, fmt.Errorf("unsupported proxy scheme: %s", u.Scheme) } } + +var ( + defaultProxyHttpClient *http.Client + onceLoadDefaultProxyHttpClient sync.Once +) + +func DefaultParserHTTPClient() *http.Client { + onceLoadDefaultProxyHttpClient.Do(func() { + client, err := NewProxyHTTPClient(config.C().Parser.Proxy) + if err != nil { + log.Warn("Failed to create default proxy HTTP client, using http.DefaultClient", "error", err) + defaultProxyHttpClient = http.DefaultClient + } else { + defaultProxyHttpClient = client + } + }) + return defaultProxyHttpClient +} diff --git a/config/parser.go b/config/parser.go index cce3ed1..b792095 100644 --- a/config/parser.go +++ b/config/parser.go @@ -1,10 +1,10 @@ package config type parserConfig struct { - PluginEnable bool `toml:"plugin_enable" mapstructure:"plugin_enable" json:"plugin_enable"` - PluginDirs []string `toml:"plugin_dirs" mapstructure:"plugin_dirs" json:"plugin_dirs"` - - ParserCfgs map[string]map[string]any `mapstructure:",remain"` + PluginEnable bool `toml:"plugin_enable" mapstructure:"plugin_enable" json:"plugin_enable"` + PluginDirs []string `toml:"plugin_dirs" mapstructure:"plugin_dirs" json:"plugin_dirs"` + Proxy string `toml:"proxy" mapstructure:"proxy" json:"proxy"` + ParserCfgs map[string]map[string]any `mapstructure:",remain"` } func (c Config) GetParserConfigByName(name string) map[string]any {