diff options
author | Keuin <[email protected]> | 2022-09-08 13:27:37 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2022-09-08 13:27:37 +0800 |
commit | 177ecb3de2f9ca8e23c3eeb19b6875a0c6593355 (patch) | |
tree | 6a8d82c994317420f2294ac3678c497d596cc1fb /bilibili/client.go | |
parent | ea58d5d3c9f0d4534a9ffd028b9461a75922d54f (diff) |
Support specify IP network type (ipv4, ipv6, in arbitrary combination and priority)
Diffstat (limited to 'bilibili/client.go')
-rw-r--r-- | bilibili/client.go | 28 |
1 files changed, 24 insertions, 4 deletions
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) } |