summaryrefslogtreecommitdiff
path: root/bilibili
diff options
context:
space:
mode:
authorKeuin <[email protected]>2022-09-12 04:42:22 +0800
committerKeuin <[email protected]>2022-09-12 04:42:22 +0800
commit2b1e0dbeab6227f67e2ced780f325938202d51c3 (patch)
treeecc51f22f47ae7e75f4c75206a173ff22c21305e /bilibili
parent25fc31b21eae2ae117180b72363f240d51008bee (diff)
Improve config `allowed_network_types`.v0.3.0
- Rename values to "ipv4", "ipv6" and "any". - Validate them when parsing.
Diffstat (limited to 'bilibili')
-rw-r--r--bilibili/netprobe.go26
1 files changed, 22 insertions, 4 deletions
diff --git a/bilibili/netprobe.go b/bilibili/netprobe.go
index ac11a77..419cc20 100644
--- a/bilibili/netprobe.go
+++ b/bilibili/netprobe.go
@@ -2,17 +2,35 @@ package bilibili
import (
"context"
+ "fmt"
"net"
)
type IpNetType string
var (
- IPv6Net IpNetType = "tcp6"
- IPv4Net IpNetType = "tcp4"
- IP64 IpNetType = "tcp"
+ IPv6Net IpNetType = "ipv6"
+ IPv4Net IpNetType = "ipv4"
+ IP64 IpNetType = "any"
)
+// GetDialNetString returns the string accepted by net.Dialer::DialContext
+func (t IpNetType) GetDialNetString() string {
+ switch t {
+ case IPv4Net:
+ return "tcp4"
+ case IPv6Net:
+ return "tcp6"
+ case IP64:
+ return "tcp"
+ }
+ return ""
+}
+
+func (t IpNetType) String() string {
+ return fmt.Sprintf("%s(%s)", string(t), t.GetDialNetString())
+}
+
type netContext = func(context.Context, string, string) (net.Conn, error)
type netProbe struct {
@@ -36,6 +54,6 @@ func (p *netProbe) NextNetworkType(dialer net.Dialer) (netContext, IpNetType) {
network := p.list[p.i]
p.i++
return func(ctx context.Context, _, addr string) (net.Conn, error) {
- return dialer.DialContext(ctx, string(network), addr)
+ return dialer.DialContext(ctx, network.GetDialNetString(), addr)
}, network
}