Supported both DialContext and proxy

This commit is contained in:
niuhuan 2022-03-19 13:54:16 +08:00
parent 68ac6eff64
commit bb1c7970bd
1 changed files with 47 additions and 12 deletions

View File

@ -7,6 +7,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
source "github.com/niuhuan/pica-go" source "github.com/niuhuan/pica-go"
"golang.org/x/net/proxy"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
@ -47,13 +48,6 @@ var switchAddresses = map[int]string{
var switchAddress = 1 var switchAddress = 1
var switchAddressPattern, _ = regexp.Compile("^.+picacomic\\.com:\\d+$") var switchAddressPattern, _ = regexp.Compile("^.+picacomic\\.com:\\d+$")
func switchAddressContext(ctx context.Context, network, addr string) (net.Conn, error) {
if sAddr, ok := switchAddresses[switchAddress]; ok {
addr = sAddr
}
return dialer.DialContext(ctx, network, addr)
}
func changeProxyUrl(urlStr string) bool { func changeProxyUrl(urlStr string) bool {
if urlStr == "" { if urlStr == "" {
client.Transport = &http.Transport{ client.Transport = &http.Transport{
@ -61,19 +55,60 @@ func changeProxyUrl(urlStr string) bool {
ExpectContinueTimeout: time.Second * 10, ExpectContinueTimeout: time.Second * 10,
ResponseHeaderTimeout: time.Second * 10, ResponseHeaderTimeout: time.Second * 10,
IdleConnTimeout: time.Second * 10, IdleConnTimeout: time.Second * 10,
DialContext: switchAddressContext, DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
if sAddr, ok := switchAddresses[switchAddress]; ok {
addr = sAddr
} }
return false return dialer.DialContext(ctx, network, addr)
}
client.Transport = &http.Transport{
Proxy: func(_ *http.Request) (*url.URL, error) {
return url.Parse(urlStr)
}, },
}
imageHttpClient.Transport = &http.Transport{
TLSHandshakeTimeout: time.Second * 10, TLSHandshakeTimeout: time.Second * 10,
ExpectContinueTimeout: time.Second * 10, ExpectContinueTimeout: time.Second * 10,
ResponseHeaderTimeout: time.Second * 10, ResponseHeaderTimeout: time.Second * 10,
IdleConnTimeout: time.Second * 10, IdleConnTimeout: time.Second * 10,
DialContext: switchAddressContext, DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialer.DialContext(ctx, network, addr)
},
}
return false
}
client.Transport = &http.Transport{
TLSHandshakeTimeout: time.Second * 10,
ExpectContinueTimeout: time.Second * 10,
ResponseHeaderTimeout: time.Second * 10,
IdleConnTimeout: time.Second * 10,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
proxyUrl, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
proxy, err := proxy.FromURL(proxyUrl, proxy.Direct)
if err != nil {
return nil, err
}
if sAddr, ok := switchAddresses[switchAddress]; ok {
addr = sAddr
}
return proxy.Dial(network, addr)
},
}
imageHttpClient.Transport = &http.Transport{
TLSHandshakeTimeout: time.Second * 10,
ExpectContinueTimeout: time.Second * 10,
ResponseHeaderTimeout: time.Second * 10,
IdleConnTimeout: time.Second * 10,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
proxyUrl, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
proxy, err := proxy.FromURL(proxyUrl, proxy.Direct)
if err != nil {
return nil, err
}
return proxy.Dial(network, addr)
},
} }
return true return true
} }