From 177ecb3de2f9ca8e23c3eeb19b6875a0c6593355 Mon Sep 17 00:00:00 2001 From: Keuin Date: Thu, 8 Sep 2022 13:27:37 +0800 Subject: Support specify IP network type (ipv4, ipv6, in arbitrary combination and priority) --- bilibili/client.go | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'bilibili/client.go') diff --git a/bilibili/client.go b/bilibili/client.go index fcea395..8097115 100644 --- a/bilibili/client.go +++ b/bilibili/client.go @@ -7,6 +7,7 @@ package bilibili import ( "context" "log" + "net" "net/http" "os" ) @@ -21,25 +22,44 @@ type Bilibili struct { userAgent string http *http.Client loggerCommon - ctx context.Context + ctx context.Context + netTypes []IpNetType } -func NewBilibiliWithContext(ctx context.Context) Bilibili { +func NewBilibiliWithContext(ctx context.Context, netTypes []IpNetType) Bilibili { logger := loggerCommon{ debug: log.New(os.Stderr, "DEBUG: ", log.Ldate|log.Ltime|log.Lshortfile), info: log.New(os.Stderr, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile), warn: log.New(os.Stderr, "WARNING: ", log.Ldate|log.Ltime|log.Lshortfile), error: log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile), } + + var nets []IpNetType + nets = append(nets, netTypes...) + if len(nets) == 0 { + nets = append(nets, IP64) + } + + var dialer net.Dialer + np := newNetProbe(nets) + transport := http.DefaultTransport.(*http.Transport).Clone() + transport.DialTLSContext = nil + transport.DialContext, _ = np.NextNetworkType(dialer) + return Bilibili{ loggerCommon: logger, userAgent: kUserAgent, http: http.DefaultClient, ctx: ctx, + netTypes: nets, } } -func NewBilibili() Bilibili { +func NewBilibiliWithNetType(netTypes []IpNetType) Bilibili { ctx := context.Background() - return NewBilibiliWithContext(ctx) + return NewBilibiliWithContext(ctx, netTypes) +} + +func NewBilibili() Bilibili { + return NewBilibiliWithNetType(nil) } -- cgit v1.2.3