2020-02-09 17:02:48 +08:00
|
|
|
package dialer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-02-15 21:42:46 +08:00
|
|
|
"errors"
|
2022-04-23 00:30:25 +08:00
|
|
|
"fmt"
|
2020-02-09 17:02:48 +08:00
|
|
|
"net"
|
2022-04-20 01:52:51 +08:00
|
|
|
"net/netip"
|
2022-12-13 11:18:32 +08:00
|
|
|
"runtime"
|
2022-08-28 13:41:19 +08:00
|
|
|
"strings"
|
2022-04-27 21:37:20 +08:00
|
|
|
"sync"
|
2022-12-07 20:01:44 +08:00
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/component/resolver"
|
|
|
|
|
|
|
|
"go.uber.org/atomic"
|
2020-02-09 17:02:48 +08:00
|
|
|
)
|
|
|
|
|
2022-04-27 21:37:20 +08:00
|
|
|
var (
|
|
|
|
dialMux sync.Mutex
|
|
|
|
actualSingleDialContext = singleDialContext
|
|
|
|
actualDualStackDialContext = dualStackDialContext
|
2022-05-26 19:49:12 +08:00
|
|
|
tcpConcurrent = false
|
2022-04-27 21:37:20 +08:00
|
|
|
DisableIPv6 = false
|
2022-08-28 13:41:19 +08:00
|
|
|
ErrorInvalidedNetworkStack = errors.New("invalided network stack")
|
|
|
|
ErrorDisableIPv6 = errors.New("IPv6 is disabled, dialer cancel")
|
2022-04-27 21:37:20 +08:00
|
|
|
)
|
2022-04-23 00:27:22 +08:00
|
|
|
|
2022-12-13 11:18:32 +08:00
|
|
|
func ParseNetwork(network string, addr netip.Addr) string {
|
|
|
|
if runtime.GOOS == "windows" { // fix bindIfaceToListenConfig() in windows force bind to an ipv4 address
|
|
|
|
if !strings.HasSuffix(network, "4") &&
|
|
|
|
!strings.HasSuffix(network, "6") &&
|
|
|
|
addr.Unmap().Is6() {
|
|
|
|
network += "6"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return network
|
|
|
|
}
|
|
|
|
|
2022-11-25 08:08:14 +08:00
|
|
|
func ApplyOptions(options ...Option) *option {
|
2022-02-23 02:38:50 +08:00
|
|
|
opt := &option{
|
|
|
|
interfaceName: DefaultInterface.Load(),
|
|
|
|
routingMark: int(DefaultRoutingMark.Load()),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range DefaultOptions {
|
|
|
|
o(opt)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range options {
|
|
|
|
o(opt)
|
|
|
|
}
|
|
|
|
|
2022-11-25 08:08:14 +08:00
|
|
|
return opt
|
|
|
|
}
|
|
|
|
|
|
|
|
func DialContext(ctx context.Context, network, address string, options ...Option) (net.Conn, error) {
|
|
|
|
opt := ApplyOptions(options...)
|
|
|
|
|
2022-08-28 13:41:19 +08:00
|
|
|
if opt.network == 4 || opt.network == 6 {
|
|
|
|
if strings.Contains(network, "tcp") {
|
|
|
|
network = "tcp"
|
|
|
|
} else {
|
|
|
|
network = "udp"
|
|
|
|
}
|
|
|
|
|
|
|
|
network = fmt.Sprintf("%s%d", network, opt.network)
|
|
|
|
}
|
|
|
|
|
2020-02-15 21:42:46 +08:00
|
|
|
switch network {
|
|
|
|
case "tcp4", "tcp6", "udp4", "udp6":
|
2022-04-27 21:37:20 +08:00
|
|
|
return actualSingleDialContext(ctx, network, address, opt)
|
2020-02-15 21:42:46 +08:00
|
|
|
case "tcp", "udp":
|
2022-04-27 21:37:20 +08:00
|
|
|
return actualDualStackDialContext(ctx, network, address, opt)
|
2020-02-15 21:42:46 +08:00
|
|
|
default:
|
2022-08-28 13:41:19 +08:00
|
|
|
return nil, ErrorInvalidedNetworkStack
|
2020-02-15 21:42:46 +08:00
|
|
|
}
|
2020-02-09 17:02:48 +08:00
|
|
|
}
|
|
|
|
|
2021-09-06 23:07:34 +08:00
|
|
|
func ListenPacket(ctx context.Context, network, address string, options ...Option) (net.PacketConn, error) {
|
2021-11-07 16:48:51 +08:00
|
|
|
cfg := &option{
|
|
|
|
interfaceName: DefaultInterface.Load(),
|
2022-02-17 14:23:47 +08:00
|
|
|
routingMark: int(DefaultRoutingMark.Load()),
|
2021-11-07 16:48:51 +08:00
|
|
|
}
|
2021-09-06 23:07:34 +08:00
|
|
|
|
2021-11-07 16:48:51 +08:00
|
|
|
for _, o := range DefaultOptions {
|
|
|
|
o(cfg)
|
2021-09-06 23:07:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range options {
|
|
|
|
o(cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
lc := &net.ListenConfig{}
|
|
|
|
if cfg.interfaceName != "" {
|
|
|
|
addr, err := bindIfaceToListenConfig(cfg.interfaceName, lc, network, address)
|
2020-04-24 23:48:55 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-02-15 21:42:46 +08:00
|
|
|
}
|
2021-09-06 23:07:34 +08:00
|
|
|
address = addr
|
|
|
|
}
|
|
|
|
if cfg.addrReuse {
|
|
|
|
addrReuseToListenConfig(lc)
|
2020-02-15 21:42:46 +08:00
|
|
|
}
|
2021-11-08 16:59:48 +08:00
|
|
|
if cfg.routingMark != 0 {
|
|
|
|
bindMarkToListenConfig(cfg.routingMark, lc, network, address)
|
|
|
|
}
|
2020-10-22 00:11:49 +08:00
|
|
|
|
2021-09-06 23:07:34 +08:00
|
|
|
return lc.ListenPacket(ctx, network, address)
|
2020-02-09 17:02:48 +08:00
|
|
|
}
|
2020-02-15 21:42:46 +08:00
|
|
|
|
2022-04-27 21:37:20 +08:00
|
|
|
func SetDial(concurrent bool) {
|
|
|
|
dialMux.Lock()
|
2022-05-26 19:49:12 +08:00
|
|
|
tcpConcurrent = concurrent
|
2022-04-27 21:37:20 +08:00
|
|
|
if concurrent {
|
|
|
|
actualSingleDialContext = concurrentSingleDialContext
|
|
|
|
actualDualStackDialContext = concurrentDualStackDialContext
|
|
|
|
} else {
|
|
|
|
actualSingleDialContext = singleDialContext
|
2022-05-27 20:43:39 +08:00
|
|
|
actualDualStackDialContext = dualStackDialContext
|
2022-04-27 21:37:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
dialMux.Unlock()
|
|
|
|
}
|
|
|
|
|
2022-05-26 19:49:12 +08:00
|
|
|
func GetDial() bool {
|
|
|
|
return tcpConcurrent
|
|
|
|
}
|
|
|
|
|
2022-04-20 01:52:51 +08:00
|
|
|
func dialContext(ctx context.Context, network string, destination netip.Addr, port string, opt *option) (net.Conn, error) {
|
2021-09-06 23:07:34 +08:00
|
|
|
dialer := &net.Dialer{}
|
|
|
|
if opt.interfaceName != "" {
|
|
|
|
if err := bindIfaceToDialer(opt.interfaceName, dialer, network, destination); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2021-11-08 16:59:48 +08:00
|
|
|
if opt.routingMark != 0 {
|
|
|
|
bindMarkToDialer(opt.routingMark, dialer, network, destination)
|
|
|
|
}
|
2021-09-06 23:07:34 +08:00
|
|
|
|
2022-04-23 00:30:25 +08:00
|
|
|
if DisableIPv6 && destination.Is6() {
|
2022-08-28 13:41:19 +08:00
|
|
|
return nil, ErrorDisableIPv6
|
2022-04-23 00:30:25 +08:00
|
|
|
}
|
|
|
|
|
2021-09-06 23:07:34 +08:00
|
|
|
return dialer.DialContext(ctx, network, net.JoinHostPort(destination.String(), port))
|
|
|
|
}
|
|
|
|
|
2022-02-23 02:38:50 +08:00
|
|
|
func dualStackDialContext(ctx context.Context, network, address string, opt *option) (net.Conn, error) {
|
2020-02-15 21:42:46 +08:00
|
|
|
host, port, err := net.SplitHostPort(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
returned := make(chan struct{})
|
|
|
|
defer close(returned)
|
|
|
|
|
|
|
|
type dialResult struct {
|
|
|
|
net.Conn
|
|
|
|
error
|
|
|
|
resolved bool
|
|
|
|
ipv6 bool
|
|
|
|
done bool
|
|
|
|
}
|
|
|
|
results := make(chan dialResult)
|
|
|
|
var primary, fallback dialResult
|
|
|
|
|
2022-12-07 20:01:44 +08:00
|
|
|
startRacer := func(ctx context.Context, network, host string, r resolver.Resolver, ipv6 bool) {
|
2020-02-15 21:42:46 +08:00
|
|
|
result := dialResult{ipv6: ipv6, done: true}
|
|
|
|
defer func() {
|
|
|
|
select {
|
|
|
|
case results <- result:
|
|
|
|
case <-returned:
|
|
|
|
if result.Conn != nil {
|
2022-04-20 01:52:51 +08:00
|
|
|
_ = result.Conn.Close()
|
2020-02-15 21:42:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-04-20 01:52:51 +08:00
|
|
|
var ip netip.Addr
|
2020-02-15 21:42:46 +08:00
|
|
|
if ipv6 {
|
2022-12-07 20:01:44 +08:00
|
|
|
if r == nil {
|
2022-11-12 13:18:36 +08:00
|
|
|
ip, result.error = resolver.ResolveIPv6ProxyServerHost(ctx, host)
|
2022-02-23 02:38:50 +08:00
|
|
|
} else {
|
2022-12-07 20:01:44 +08:00
|
|
|
ip, result.error = resolver.ResolveIPv6WithResolver(ctx, host, r)
|
2022-02-23 02:38:50 +08:00
|
|
|
}
|
2020-02-15 21:42:46 +08:00
|
|
|
} else {
|
2022-12-07 20:01:44 +08:00
|
|
|
if r == nil {
|
2022-11-12 13:18:36 +08:00
|
|
|
ip, result.error = resolver.ResolveIPv4ProxyServerHost(ctx, host)
|
2022-02-23 02:38:50 +08:00
|
|
|
} else {
|
2022-12-07 20:01:44 +08:00
|
|
|
ip, result.error = resolver.ResolveIPv4WithResolver(ctx, host, r)
|
2022-02-23 02:38:50 +08:00
|
|
|
}
|
2020-02-15 21:42:46 +08:00
|
|
|
}
|
|
|
|
if result.error != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
result.resolved = true
|
|
|
|
|
2022-02-23 02:38:50 +08:00
|
|
|
result.Conn, result.error = dialContext(ctx, network, ip, port, opt)
|
2020-02-15 21:42:46 +08:00
|
|
|
}
|
|
|
|
|
2022-12-07 20:01:44 +08:00
|
|
|
go startRacer(ctx, network+"4", host, opt.resolver, false)
|
|
|
|
go startRacer(ctx, network+"6", host, opt.resolver, true)
|
2020-02-15 21:42:46 +08:00
|
|
|
|
2022-06-25 09:00:35 +08:00
|
|
|
count := 2
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
select {
|
|
|
|
case res := <-results:
|
|
|
|
if res.error == nil {
|
|
|
|
return res.Conn, nil
|
|
|
|
}
|
2020-02-15 21:42:46 +08:00
|
|
|
|
2022-06-25 09:00:35 +08:00
|
|
|
if !res.ipv6 {
|
|
|
|
primary = res
|
2020-08-25 22:19:59 +08:00
|
|
|
} else {
|
2022-06-25 09:00:35 +08:00
|
|
|
fallback = res
|
2020-02-15 21:42:46 +08:00
|
|
|
}
|
2022-06-25 09:00:35 +08:00
|
|
|
|
|
|
|
if primary.done && fallback.done {
|
|
|
|
if primary.resolved {
|
|
|
|
return nil, primary.error
|
|
|
|
} else if fallback.resolved {
|
|
|
|
return nil, fallback.error
|
|
|
|
} else {
|
|
|
|
return nil, primary.error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
2022-11-25 08:08:14 +08:00
|
|
|
err = ctx.Err()
|
2022-06-25 09:00:35 +08:00
|
|
|
break
|
2020-02-15 21:42:46 +08:00
|
|
|
}
|
|
|
|
}
|
2020-08-25 22:19:59 +08:00
|
|
|
|
2022-11-25 08:08:14 +08:00
|
|
|
if err == nil {
|
|
|
|
err = fmt.Errorf("dual stack dial failed")
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("dual stack dial failed:%w", err)
|
2022-11-19 10:57:33 +08:00
|
|
|
}
|
|
|
|
return nil, err
|
2020-02-15 21:42:46 +08:00
|
|
|
}
|
2022-04-23 00:27:22 +08:00
|
|
|
|
2022-04-27 21:37:20 +08:00
|
|
|
func concurrentDualStackDialContext(ctx context.Context, network, address string, opt *option) (net.Conn, error) {
|
2022-04-23 00:27:22 +08:00
|
|
|
host, port, err := net.SplitHostPort(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-27 21:37:20 +08:00
|
|
|
var ips []netip.Addr
|
2022-12-07 20:01:44 +08:00
|
|
|
if opt.resolver != nil {
|
|
|
|
ips, err = resolver.LookupIPWithResolver(ctx, host, opt.resolver)
|
2022-04-27 21:37:20 +08:00
|
|
|
} else {
|
2022-11-12 13:18:36 +08:00
|
|
|
ips, err = resolver.LookupIPProxyServerHost(ctx, host)
|
2022-04-27 21:37:20 +08:00
|
|
|
}
|
|
|
|
|
2022-08-13 16:47:24 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-27 21:37:20 +08:00
|
|
|
return concurrentDialContext(ctx, network, ips, port, opt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func concurrentDialContext(ctx context.Context, network string, ips []netip.Addr, port string, opt *option) (net.Conn, error) {
|
2022-04-23 00:27:22 +08:00
|
|
|
returned := make(chan struct{})
|
|
|
|
defer close(returned)
|
|
|
|
|
|
|
|
type dialResult struct {
|
|
|
|
ip netip.Addr
|
|
|
|
net.Conn
|
|
|
|
error
|
2022-08-28 13:41:19 +08:00
|
|
|
isPrimary bool
|
|
|
|
done bool
|
2022-04-23 00:27:22 +08:00
|
|
|
}
|
|
|
|
|
2022-08-28 13:41:19 +08:00
|
|
|
preferCount := atomic.NewInt32(0)
|
2022-04-23 00:27:22 +08:00
|
|
|
results := make(chan dialResult)
|
|
|
|
tcpRacer := func(ctx context.Context, ip netip.Addr) {
|
2022-08-28 13:41:19 +08:00
|
|
|
result := dialResult{ip: ip, done: true}
|
2022-04-23 00:27:22 +08:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
select {
|
|
|
|
case results <- result:
|
|
|
|
case <-returned:
|
|
|
|
if result.Conn != nil {
|
2022-08-28 13:41:19 +08:00
|
|
|
_ = result.Conn.Close()
|
2022-04-23 00:27:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2022-08-28 13:41:19 +08:00
|
|
|
if strings.Contains(network, "tcp") {
|
|
|
|
network = "tcp"
|
|
|
|
} else {
|
|
|
|
network = "udp"
|
|
|
|
}
|
2022-04-23 00:27:22 +08:00
|
|
|
|
|
|
|
if ip.Is6() {
|
2022-08-28 13:41:19 +08:00
|
|
|
network += "6"
|
|
|
|
if opt.prefer != 4 {
|
|
|
|
result.isPrimary = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ip.Is4() {
|
|
|
|
network += "4"
|
|
|
|
if opt.prefer != 6 {
|
|
|
|
result.isPrimary = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.isPrimary {
|
|
|
|
preferCount.Add(1)
|
2022-04-23 00:27:22 +08:00
|
|
|
}
|
2022-04-27 21:37:20 +08:00
|
|
|
|
2022-08-28 13:41:19 +08:00
|
|
|
result.Conn, result.error = dialContext(ctx, network, ip, port, opt)
|
2022-04-23 00:27:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, ip := range ips {
|
|
|
|
go tcpRacer(ctx, ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
connCount := len(ips)
|
2022-08-28 13:41:19 +08:00
|
|
|
var fallback dialResult
|
2022-10-16 13:12:49 +08:00
|
|
|
var primaryError error
|
2022-11-19 10:57:33 +08:00
|
|
|
var finalError error
|
2022-06-25 09:00:35 +08:00
|
|
|
for i := 0; i < connCount; i++ {
|
|
|
|
select {
|
|
|
|
case res := <-results:
|
|
|
|
if res.error == nil {
|
2022-08-28 13:41:19 +08:00
|
|
|
if res.isPrimary {
|
|
|
|
return res.Conn, nil
|
|
|
|
} else {
|
2022-08-28 20:26:13 +08:00
|
|
|
if !fallback.done || fallback.error != nil {
|
|
|
|
fallback = res
|
|
|
|
}
|
2022-08-28 13:41:19 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if res.isPrimary {
|
2022-10-16 13:12:49 +08:00
|
|
|
primaryError = res.error
|
2022-08-28 13:41:19 +08:00
|
|
|
preferCount.Add(-1)
|
2022-08-28 20:26:13 +08:00
|
|
|
if preferCount.Load() == 0 && fallback.done && fallback.error == nil {
|
2022-08-28 13:41:19 +08:00
|
|
|
return fallback.Conn, nil
|
|
|
|
}
|
|
|
|
}
|
2022-06-25 09:00:35 +08:00
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
2022-08-28 20:26:13 +08:00
|
|
|
if fallback.done && fallback.error == nil {
|
2022-08-28 13:41:19 +08:00
|
|
|
return fallback.Conn, nil
|
|
|
|
}
|
2022-11-25 08:08:14 +08:00
|
|
|
finalError = ctx.Err()
|
2022-04-23 00:27:22 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-04-27 21:37:20 +08:00
|
|
|
|
2022-08-28 20:26:13 +08:00
|
|
|
if fallback.done && fallback.error == nil {
|
|
|
|
return fallback.Conn, nil
|
|
|
|
}
|
|
|
|
|
2022-10-16 13:12:49 +08:00
|
|
|
if primaryError != nil {
|
|
|
|
return nil, primaryError
|
|
|
|
}
|
|
|
|
|
|
|
|
if fallback.error != nil {
|
|
|
|
return nil, fallback.error
|
|
|
|
}
|
|
|
|
|
2022-11-25 08:08:14 +08:00
|
|
|
if finalError == nil {
|
|
|
|
finalError = fmt.Errorf("all ips %v tcp shake hands failed", ips)
|
|
|
|
} else {
|
|
|
|
finalError = fmt.Errorf("concurrent dial failed:%w", finalError)
|
2022-11-19 10:50:13 +08:00
|
|
|
}
|
|
|
|
|
2022-11-19 10:57:33 +08:00
|
|
|
return nil, finalError
|
2022-04-27 21:37:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func singleDialContext(ctx context.Context, network string, address string, opt *option) (net.Conn, error) {
|
|
|
|
host, port, err := net.SplitHostPort(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ip netip.Addr
|
|
|
|
switch network {
|
|
|
|
case "tcp4", "udp4":
|
2022-12-07 20:01:44 +08:00
|
|
|
if opt.resolver == nil {
|
2022-11-12 13:18:36 +08:00
|
|
|
ip, err = resolver.ResolveIPv4ProxyServerHost(ctx, host)
|
2022-04-27 21:37:20 +08:00
|
|
|
} else {
|
2022-12-07 20:01:44 +08:00
|
|
|
ip, err = resolver.ResolveIPv4WithResolver(ctx, host, opt.resolver)
|
2022-04-27 21:37:20 +08:00
|
|
|
}
|
|
|
|
default:
|
2022-12-07 20:01:44 +08:00
|
|
|
if opt.resolver == nil {
|
2022-11-12 13:18:36 +08:00
|
|
|
ip, err = resolver.ResolveIPv6ProxyServerHost(ctx, host)
|
2022-04-27 21:37:20 +08:00
|
|
|
} else {
|
2022-12-07 20:01:44 +08:00
|
|
|
ip, err = resolver.ResolveIPv6WithResolver(ctx, host, opt.resolver)
|
2022-04-27 21:37:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dialContext(ctx, network, ip, port, opt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func concurrentSingleDialContext(ctx context.Context, network string, address string, opt *option) (net.Conn, error) {
|
2022-08-28 13:41:19 +08:00
|
|
|
switch network {
|
|
|
|
case "tcp4", "udp4":
|
|
|
|
return concurrentIPv4DialContext(ctx, network, address, opt)
|
|
|
|
default:
|
|
|
|
return concurrentIPv6DialContext(ctx, network, address, opt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func concurrentIPv4DialContext(ctx context.Context, network, address string, opt *option) (net.Conn, error) {
|
2022-04-27 21:37:20 +08:00
|
|
|
host, port, err := net.SplitHostPort(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ips []netip.Addr
|
2022-12-07 20:01:44 +08:00
|
|
|
if opt.resolver == nil {
|
2022-11-12 13:18:36 +08:00
|
|
|
ips, err = resolver.LookupIPv4ProxyServerHost(ctx, host)
|
2022-08-28 13:41:19 +08:00
|
|
|
} else {
|
2022-12-07 20:01:44 +08:00
|
|
|
ips, err = resolver.LookupIPv4WithResolver(ctx, host, opt.resolver)
|
2022-08-28 13:41:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return concurrentDialContext(ctx, network, ips, port, opt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func concurrentIPv6DialContext(ctx context.Context, network, address string, opt *option) (net.Conn, error) {
|
|
|
|
host, port, err := net.SplitHostPort(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ips []netip.Addr
|
2022-12-07 20:01:44 +08:00
|
|
|
if opt.resolver == nil {
|
2022-11-12 13:18:36 +08:00
|
|
|
ips, err = resolver.LookupIPv6ProxyServerHost(ctx, host)
|
2022-08-28 13:41:19 +08:00
|
|
|
} else {
|
2022-12-07 20:01:44 +08:00
|
|
|
ips, err = resolver.LookupIPv6WithResolver(ctx, host, opt.resolver)
|
2022-04-27 21:37:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return concurrentDialContext(ctx, network, ips, port, opt)
|
2022-04-23 00:27:22 +08:00
|
|
|
}
|
2022-12-19 21:34:07 +08:00
|
|
|
|
2022-12-20 00:11:02 +08:00
|
|
|
type dialer struct {
|
|
|
|
opt option
|
2022-12-19 21:34:07 +08:00
|
|
|
}
|
|
|
|
|
2022-12-20 00:11:02 +08:00
|
|
|
func (d dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
|
|
|
return DialContext(ctx, network, address, withOption(d.opt))
|
2022-12-19 21:34:07 +08:00
|
|
|
}
|
|
|
|
|
2022-12-20 00:11:02 +08:00
|
|
|
func (d dialer) ListenPacket(ctx context.Context, network, address string, rAddrPort netip.AddrPort) (net.PacketConn, error) {
|
|
|
|
return ListenPacket(ctx, ParseNetwork(network, rAddrPort.Addr()), address, withOption(d.opt))
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDialer(options ...Option) dialer {
|
|
|
|
opt := ApplyOptions(options...)
|
|
|
|
return dialer{opt: *opt}
|
2022-12-19 21:34:07 +08:00
|
|
|
}
|