mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-06-07 08:31:02 +08:00
feat: proxy client for parser
This commit is contained in:
@@ -6,7 +6,10 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/charmbracelet/log"
|
||||||
|
"github.com/krau/SaveAny-Bot/config"
|
||||||
"golang.org/x/net/proxy"
|
"golang.org/x/net/proxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -20,7 +23,11 @@ func NewProxyDialer(proxyUrl string) (proxy.Dialer, error) {
|
|||||||
|
|
||||||
func NewProxyHTTPClient(proxyUrl string) (*http.Client, error) {
|
func NewProxyHTTPClient(proxyUrl string) (*http.Client, error) {
|
||||||
if proxyUrl == "" {
|
if proxyUrl == "" {
|
||||||
return http.DefaultClient, nil
|
return &http.Client{
|
||||||
|
Transport: &http.Transport{
|
||||||
|
Proxy: http.ProxyFromEnvironment,
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
u, err := url.Parse(proxyUrl)
|
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)
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
type parserConfig struct {
|
type parserConfig struct {
|
||||||
PluginEnable bool `toml:"plugin_enable" mapstructure:"plugin_enable" json:"plugin_enable"`
|
PluginEnable bool `toml:"plugin_enable" mapstructure:"plugin_enable" json:"plugin_enable"`
|
||||||
PluginDirs []string `toml:"plugin_dirs" mapstructure:"plugin_dirs" json:"plugin_dirs"`
|
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"`
|
ParserCfgs map[string]map[string]any `mapstructure:",remain"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Config) GetParserConfigByName(name string) map[string]any {
|
func (c Config) GetParserConfigByName(name string) map[string]any {
|
||||||
|
|||||||
Reference in New Issue
Block a user