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/netprobe.go | |
parent | ea58d5d3c9f0d4534a9ffd028b9461a75922d54f (diff) |
Support specify IP network type (ipv4, ipv6, in arbitrary combination and priority)
Diffstat (limited to 'bilibili/netprobe.go')
-rw-r--r-- | bilibili/netprobe.go | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/bilibili/netprobe.go b/bilibili/netprobe.go new file mode 100644 index 0000000..ac11a77 --- /dev/null +++ b/bilibili/netprobe.go @@ -0,0 +1,41 @@ +package bilibili + +import ( + "context" + "net" +) + +type IpNetType string + +var ( + IPv6Net IpNetType = "tcp6" + IPv4Net IpNetType = "tcp4" + IP64 IpNetType = "tcp" +) + +type netContext = func(context.Context, string, string) (net.Conn, error) + +type netProbe struct { + list []IpNetType + i int +} + +func newNetProbe(protocols []IpNetType) netProbe { + var netList []IpNetType + netList = append(netList, protocols...) + return netProbe{ + list: netList, + i: 0, + } +} + +func (p *netProbe) NextNetworkType(dialer net.Dialer) (netContext, IpNetType) { + if p.i >= len(p.list) { + return nil, IP64 + } + network := p.list[p.i] + p.i++ + return func(ctx context.Context, _, addr string) (net.Conn, error) { + return dialer.DialContext(ctx, string(network), addr) + }, network +} |