mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-08-07 05:23:22 +08:00
55 lines
1017 B
Go
55 lines
1017 B
Go
package netutil
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"golang.org/x/net/proxy"
|
|
)
|
|
|
|
func NewProxyDialer(proxyUrl string) (proxy.Dialer, error) {
|
|
url, err := url.Parse(proxyUrl)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return proxy.FromURL(url, proxy.Direct)
|
|
}
|
|
|
|
func NewProxyHTTPClient(proxyUrl string) (*http.Client, error) {
|
|
if proxyUrl == "" {
|
|
return http.DefaultClient, nil
|
|
}
|
|
|
|
u, err := url.Parse(proxyUrl)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
switch u.Scheme {
|
|
case "http", "https":
|
|
return &http.Client{
|
|
Transport: &http.Transport{
|
|
Proxy: http.ProxyURL(u),
|
|
},
|
|
}, nil
|
|
case "socks5":
|
|
dialer, err := proxy.SOCKS5("tcp", u.Host, nil, proxy.Direct)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &http.Client{
|
|
Transport: &http.Transport{
|
|
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
return dialer.Dial(network, addr)
|
|
},
|
|
},
|
|
}, nil
|
|
default:
|
|
return nil, fmt.Errorf("unsupported proxy scheme: %s", u.Scheme)
|
|
}
|
|
}
|