summaryrefslogtreecommitdiff
path: root/bilibili/request.go
diff options
context:
space:
mode:
authorKeuin <[email protected]>2022-09-08 13:27:37 +0800
committerKeuin <[email protected]>2022-09-08 13:27:37 +0800
commit177ecb3de2f9ca8e23c3eeb19b6875a0c6593355 (patch)
tree6a8d82c994317420f2294ac3678c497d596cc1fb /bilibili/request.go
parentea58d5d3c9f0d4534a9ffd028b9461a75922d54f (diff)
Support specify IP network type (ipv4, ipv6, in arbitrary combination and priority)
Diffstat (limited to 'bilibili/request.go')
-rw-r--r--bilibili/request.go26
1 files changed, 25 insertions, 1 deletions
diff --git a/bilibili/request.go b/bilibili/request.go
index c31cf75..f9e2088 100644
--- a/bilibili/request.go
+++ b/bilibili/request.go
@@ -1,9 +1,11 @@
package bilibili
import (
+ "bilibili-livestream-archiver/common"
"encoding/json"
"io"
"log"
+ "net"
"net/http"
"strings"
)
@@ -37,7 +39,7 @@ func callGet[T BaseResponse[V], V any](b Bilibili, url string) (resp T, err erro
return
}
- r, err := b.http.Do(req)
+ r, err := b.Do(req)
if err != nil {
logger.Printf("ERROR: HTTP Request failed on API %v: %v", url, err)
return
@@ -66,3 +68,25 @@ func callGet[T BaseResponse[V], V any](b Bilibili, url string) (resp T, err erro
b.debug.Printf("HTTP %v, len: %v bytes, url: %v", r.StatusCode, len(data), url)
return
}
+
+func (b Bilibili) Do(req *http.Request) (resp *http.Response, err error) {
+ transport := http.DefaultTransport.(*http.Transport).Clone()
+ transport.DialTLSContext = nil
+
+ np := newNetProbe(b.netTypes)
+ var dialer net.Dialer
+ for netCtx, typeName := np.NextNetworkType(dialer); netCtx != nil; netCtx, typeName = np.NextNetworkType(dialer) {
+ transport.DialContext = netCtx
+ b.http.Transport = transport
+ resp, err = b.http.Do(req)
+
+ isOpErr := common.IsErrorOfType(err, &net.OpError{})
+ isAddrErr := common.IsErrorOfType(err, &net.AddrError{})
+ if err == nil || !isOpErr || !isAddrErr {
+ // return the first success request
+ b.loggerCommon.info.Printf("Request success with network %v.", typeName)
+ return
+ }
+ }
+ return
+}