2020-02-09 17:02:48 +08:00
|
|
|
package dialer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-02 09:59:18 +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"
|
2023-02-26 13:05:55 +08:00
|
|
|
"os"
|
2022-08-28 13:41:19 +08:00
|
|
|
"strings"
|
2022-04-27 21:37:20 +08:00
|
|
|
"sync"
|
2023-02-26 11:24:49 +08:00
|
|
|
"time"
|
2022-12-07 20:01:44 +08:00
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/component/resolver"
|
2023-11-17 13:19:24 +08:00
|
|
|
"github.com/metacubex/mihomo/constant/features"
|
|
|
|
"golang.org/x/exp/slices"
|
2020-02-09 17:02:48 +08:00
|
|
|
)
|
|
|
|
|
2023-03-06 23:23:05 +08:00
|
|
|
type dialFunc func(ctx context.Context, network string, ips []netip.Addr, port string, opt *option) (net.Conn, error)
|
|
|
|
|
2022-04-27 21:37:20 +08:00
|
|
|
var (
|
2023-02-26 11:24:49 +08:00
|
|
|
dialMux sync.Mutex
|
|
|
|
actualSingleStackDialContext = serialSingleStackDialContext
|
|
|
|
actualDualStackDialContext = serialDualStackDialContext
|
|
|
|
tcpConcurrent = false
|
|
|
|
fallbackTimeout = 300 * time.Millisecond
|
2022-04-27 21:37:20 +08:00
|
|
|
)
|
2022-04-23 00:27:22 +08:00
|
|
|
|
2022-12-22 09:53:11 +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) {
|
2022-12-22 09:53:11 +08:00
|
|
|
opt := applyOptions(options...)
|
2022-11-25 08:08:14 +08:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-03-06 23:23:05 +08:00
|
|
|
ips, port, err := parseAddr(ctx, network, address, opt.resolver)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-15 21:42:46 +08:00
|
|
|
switch network {
|
|
|
|
case "tcp4", "tcp6", "udp4", "udp6":
|
2023-03-06 23:23:05 +08:00
|
|
|
return actualSingleStackDialContext(ctx, network, ips, port, opt)
|
2020-02-15 21:42:46 +08:00
|
|
|
case "tcp", "udp":
|
2023-03-06 23:23:05 +08:00
|
|
|
return actualDualStackDialContext(ctx, network, ips, port, 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) {
|
2023-11-17 13:19:24 +08:00
|
|
|
if slices.Contains(features.TAGS, "cmfa") {
|
|
|
|
return listenPacketHooked(ctx, network, address)
|
|
|
|
}
|
|
|
|
|
2023-02-22 19:14:11 +08:00
|
|
|
cfg := applyOptions(options...)
|
2021-09-06 23:07:34 +08:00
|
|
|
|
|
|
|
lc := &net.ListenConfig{}
|
|
|
|
if cfg.interfaceName != "" {
|
2023-10-26 10:27:38 +08:00
|
|
|
bind := bindIfaceToListenConfig
|
|
|
|
if cfg.fallbackBind {
|
|
|
|
bind = fallbackBindIfaceToListenConfig
|
|
|
|
}
|
|
|
|
addr, err := bind(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
|
|
|
|
2023-03-06 23:23:05 +08:00
|
|
|
func SetTcpConcurrent(concurrent bool) {
|
2022-04-27 21:37:20 +08:00
|
|
|
dialMux.Lock()
|
2023-03-06 23:23:05 +08:00
|
|
|
defer dialMux.Unlock()
|
2022-05-26 19:49:12 +08:00
|
|
|
tcpConcurrent = concurrent
|
2022-04-27 21:37:20 +08:00
|
|
|
if concurrent {
|
2023-02-26 11:24:49 +08:00
|
|
|
actualSingleStackDialContext = concurrentSingleStackDialContext
|
2022-04-27 21:37:20 +08:00
|
|
|
actualDualStackDialContext = concurrentDualStackDialContext
|
|
|
|
} else {
|
2023-02-26 11:24:49 +08:00
|
|
|
actualSingleStackDialContext = serialSingleStackDialContext
|
|
|
|
actualDualStackDialContext = serialDualStackDialContext
|
2022-04-27 21:37:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 23:23:05 +08:00
|
|
|
func GetTcpConcurrent() bool {
|
|
|
|
dialMux.Lock()
|
|
|
|
defer dialMux.Unlock()
|
2022-05-26 19:49:12 +08:00
|
|
|
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) {
|
2023-11-17 13:19:24 +08:00
|
|
|
if slices.Contains(features.TAGS, "cmfa") {
|
|
|
|
return dialContextHooked(ctx, network, destination, port)
|
|
|
|
}
|
|
|
|
|
2023-03-07 09:30:51 +08:00
|
|
|
address := net.JoinHostPort(destination.String(), port)
|
|
|
|
|
|
|
|
netDialer := opt.netDialer
|
|
|
|
switch netDialer.(type) {
|
|
|
|
case nil:
|
|
|
|
netDialer = &net.Dialer{}
|
|
|
|
case *net.Dialer:
|
2023-03-10 16:17:43 +08:00
|
|
|
_netDialer := *netDialer.(*net.Dialer)
|
|
|
|
netDialer = &_netDialer // make a copy
|
2023-03-07 09:30:51 +08:00
|
|
|
default:
|
|
|
|
return netDialer.DialContext(ctx, network, address)
|
|
|
|
}
|
|
|
|
|
|
|
|
dialer := netDialer.(*net.Dialer)
|
2021-09-06 23:07:34 +08:00
|
|
|
if opt.interfaceName != "" {
|
2023-10-26 10:27:38 +08:00
|
|
|
bind := bindIfaceToDialer
|
|
|
|
if opt.fallbackBind {
|
|
|
|
bind = fallbackBindIfaceToDialer
|
|
|
|
}
|
|
|
|
if err := bind(opt.interfaceName, dialer, network, destination); err != nil {
|
2021-09-06 23:07:34 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2021-11-08 16:59:48 +08:00
|
|
|
if opt.routingMark != 0 {
|
|
|
|
bindMarkToDialer(opt.routingMark, dialer, network, destination)
|
|
|
|
}
|
2023-08-09 16:57:39 +08:00
|
|
|
if opt.mpTcp {
|
|
|
|
setMultiPathTCP(dialer)
|
|
|
|
}
|
2023-02-24 13:53:44 +08:00
|
|
|
if opt.tfo {
|
|
|
|
return dialTFO(ctx, *dialer, network, address)
|
|
|
|
}
|
|
|
|
return dialer.DialContext(ctx, network, address)
|
2021-09-06 23:07:34 +08:00
|
|
|
}
|
|
|
|
|
2023-03-06 23:23:05 +08:00
|
|
|
func serialSingleStackDialContext(ctx context.Context, network string, ips []netip.Addr, port string, opt *option) (net.Conn, error) {
|
2023-02-26 11:24:49 +08:00
|
|
|
return serialDialContext(ctx, network, ips, port, opt)
|
|
|
|
}
|
2023-02-22 19:14:11 +08:00
|
|
|
|
2023-03-06 23:23:05 +08:00
|
|
|
func serialDualStackDialContext(ctx context.Context, network string, ips []netip.Addr, port string, opt *option) (net.Conn, error) {
|
|
|
|
return dualStackDialContext(ctx, serialDialContext, network, ips, port, opt)
|
2023-02-26 11:24:49 +08:00
|
|
|
}
|
|
|
|
|
2023-03-06 23:23:05 +08:00
|
|
|
func concurrentSingleStackDialContext(ctx context.Context, network string, ips []netip.Addr, port string, opt *option) (net.Conn, error) {
|
|
|
|
return parallelDialContext(ctx, network, ips, port, opt)
|
2023-02-22 19:14:11 +08:00
|
|
|
}
|
|
|
|
|
2023-03-06 23:23:05 +08:00
|
|
|
func concurrentDualStackDialContext(ctx context.Context, network string, ips []netip.Addr, port string, opt *option) (net.Conn, error) {
|
2023-02-26 11:24:49 +08:00
|
|
|
if opt.prefer != 4 && opt.prefer != 6 {
|
|
|
|
return parallelDialContext(ctx, network, ips, port, opt)
|
|
|
|
}
|
2023-03-06 23:23:05 +08:00
|
|
|
return dualStackDialContext(ctx, parallelDialContext, network, ips, port, opt)
|
2023-02-26 11:24:49 +08:00
|
|
|
}
|
2020-02-15 21:42:46 +08:00
|
|
|
|
2023-03-06 23:23:05 +08:00
|
|
|
func dualStackDialContext(ctx context.Context, dialFn dialFunc, network string, ips []netip.Addr, port string, opt *option) (net.Conn, error) {
|
2023-04-12 22:06:21 +08:00
|
|
|
ipv4s, ipv6s := resolver.SortationAddr(ips)
|
2023-08-29 20:23:16 +08:00
|
|
|
if len(ipv4s) == 0 && len(ipv6s) == 0 {
|
|
|
|
return nil, ErrorNoIpAddress
|
|
|
|
}
|
2023-03-06 23:23:05 +08:00
|
|
|
|
2023-08-29 20:23:16 +08:00
|
|
|
preferIPVersion := opt.prefer
|
2023-02-26 11:24:49 +08:00
|
|
|
fallbackTicker := time.NewTicker(fallbackTimeout)
|
|
|
|
defer fallbackTicker.Stop()
|
2023-08-30 17:07:49 +08:00
|
|
|
|
2023-02-26 11:24:49 +08:00
|
|
|
results := make(chan dialResult)
|
2020-02-15 21:42:46 +08:00
|
|
|
returned := make(chan struct{})
|
|
|
|
defer close(returned)
|
2023-08-30 17:07:49 +08:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2023-03-06 23:23:05 +08:00
|
|
|
racer := func(ips []netip.Addr, isPrimary bool) {
|
2023-08-30 17:07:49 +08:00
|
|
|
defer wg.Done()
|
2023-02-26 11:24:49 +08:00
|
|
|
result := dialResult{isPrimary: isPrimary}
|
2020-02-15 21:42:46 +08:00
|
|
|
defer func() {
|
|
|
|
select {
|
|
|
|
case results <- result:
|
|
|
|
case <-returned:
|
2023-03-10 12:06:40 +08:00
|
|
|
if result.Conn != nil && result.error == nil {
|
2022-04-20 01:52:51 +08:00
|
|
|
_ = result.Conn.Close()
|
2020-02-15 21:42:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2023-03-06 23:23:05 +08:00
|
|
|
result.Conn, result.error = dialFn(ctx, network, ips, port, opt)
|
2020-02-15 21:42:46 +08:00
|
|
|
}
|
2023-08-29 20:23:16 +08:00
|
|
|
|
|
|
|
if len(ipv4s) != 0 {
|
2023-08-30 17:07:49 +08:00
|
|
|
wg.Add(1)
|
2023-08-29 20:23:16 +08:00
|
|
|
go racer(ipv4s, preferIPVersion != 6)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ipv6s) != 0 {
|
2023-08-30 17:07:49 +08:00
|
|
|
wg.Add(1)
|
2023-08-29 20:23:16 +08:00
|
|
|
go racer(ipv6s, preferIPVersion != 4)
|
|
|
|
}
|
|
|
|
|
2023-08-30 17:07:49 +08:00
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(results)
|
|
|
|
}()
|
|
|
|
|
2023-02-26 11:24:49 +08:00
|
|
|
var fallback dialResult
|
2023-03-10 14:12:18 +08:00
|
|
|
var errs []error
|
2023-08-30 17:07:49 +08:00
|
|
|
|
|
|
|
loop:
|
|
|
|
for {
|
2022-06-25 09:00:35 +08:00
|
|
|
select {
|
2023-02-26 11:24:49 +08:00
|
|
|
case <-fallbackTicker.C:
|
|
|
|
if fallback.error == nil && fallback.Conn != nil {
|
|
|
|
return fallback.Conn, nil
|
2020-02-15 21:42:46 +08:00
|
|
|
}
|
2023-08-30 17:07:49 +08:00
|
|
|
case res, ok := <-results:
|
|
|
|
if !ok {
|
|
|
|
break loop
|
|
|
|
}
|
2023-02-26 11:24:49 +08:00
|
|
|
if res.error == nil {
|
|
|
|
if res.isPrimary {
|
|
|
|
return res.Conn, nil
|
2022-06-25 09:00:35 +08:00
|
|
|
}
|
2023-02-26 11:24:49 +08:00
|
|
|
fallback = res
|
2023-02-26 21:01:44 +08:00
|
|
|
} else {
|
2023-03-10 14:12:18 +08:00
|
|
|
if res.isPrimary {
|
|
|
|
errs = append([]error{fmt.Errorf("connect failed: %w", res.error)}, errs...)
|
|
|
|
} else {
|
|
|
|
errs = append(errs, fmt.Errorf("connect failed: %w", res.error))
|
|
|
|
}
|
2022-06-25 09:00:35 +08:00
|
|
|
}
|
2020-02-15 21:42:46 +08:00
|
|
|
}
|
|
|
|
}
|
2023-08-30 17:07:49 +08:00
|
|
|
|
2023-03-10 20:16:14 +08:00
|
|
|
if fallback.error == nil && fallback.Conn != nil {
|
|
|
|
return fallback.Conn, nil
|
|
|
|
}
|
2023-07-02 09:59:18 +08:00
|
|
|
return nil, errors.Join(errs...)
|
2020-02-15 21:42:46 +08:00
|
|
|
}
|
2022-04-23 00:27:22 +08:00
|
|
|
|
2023-02-26 11:24:49 +08:00
|
|
|
func parallelDialContext(ctx context.Context, network string, ips []netip.Addr, port string, opt *option) (net.Conn, error) {
|
2023-02-26 21:01:44 +08:00
|
|
|
if len(ips) == 0 {
|
2023-02-26 22:20:25 +08:00
|
|
|
return nil, ErrorNoIpAddress
|
2023-02-26 21:01:44 +08:00
|
|
|
}
|
2023-02-26 11:24:49 +08:00
|
|
|
results := make(chan dialResult)
|
2022-04-23 00:27:22 +08:00
|
|
|
returned := make(chan struct{})
|
|
|
|
defer close(returned)
|
2023-03-06 23:23:05 +08:00
|
|
|
racer := func(ctx context.Context, ip netip.Addr) {
|
2023-03-10 22:08:01 +08:00
|
|
|
result := dialResult{isPrimary: true, ip: ip}
|
2022-04-23 00:27:22 +08:00
|
|
|
defer func() {
|
|
|
|
select {
|
|
|
|
case results <- result:
|
|
|
|
case <-returned:
|
2023-03-10 12:06:40 +08:00
|
|
|
if result.Conn != nil && result.error == 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
|
|
|
result.Conn, result.error = dialContext(ctx, network, ip, port, opt)
|
2022-04-23 00:27:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, ip := range ips {
|
2023-03-06 23:23:05 +08:00
|
|
|
go racer(ctx, ip)
|
2022-04-23 00:27:22 +08:00
|
|
|
}
|
2023-03-10 14:12:18 +08:00
|
|
|
var errs []error
|
2023-03-10 22:08:01 +08:00
|
|
|
for i := 0; i < len(ips); i++ {
|
|
|
|
res := <-results
|
|
|
|
if res.error == nil {
|
|
|
|
return res.Conn, nil
|
2022-04-23 00:27:22 +08:00
|
|
|
}
|
2023-03-10 22:08:01 +08:00
|
|
|
errs = append(errs, res.error)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
2023-07-02 09:59:18 +08:00
|
|
|
return nil, errors.Join(errs...)
|
2022-04-23 00:27:22 +08:00
|
|
|
}
|
2023-03-10 22:08:01 +08:00
|
|
|
return nil, os.ErrDeadlineExceeded
|
2023-02-26 11:24:49 +08:00
|
|
|
}
|
2022-04-27 21:37:20 +08:00
|
|
|
|
2023-02-26 11:24:49 +08:00
|
|
|
func serialDialContext(ctx context.Context, network string, ips []netip.Addr, port string, opt *option) (net.Conn, error) {
|
2023-02-26 21:01:44 +08:00
|
|
|
if len(ips) == 0 {
|
2023-02-26 22:20:25 +08:00
|
|
|
return nil, ErrorNoIpAddress
|
2023-02-26 21:01:44 +08:00
|
|
|
}
|
2023-03-06 23:23:05 +08:00
|
|
|
var errs []error
|
2023-02-26 11:24:49 +08:00
|
|
|
for _, ip := range ips {
|
2023-03-06 23:23:05 +08:00
|
|
|
if conn, err := dialContext(ctx, network, ip, port, opt); err == nil {
|
2023-02-26 11:24:49 +08:00
|
|
|
return conn, nil
|
|
|
|
} else {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
2022-11-19 10:50:13 +08:00
|
|
|
}
|
2023-07-02 09:59:18 +08:00
|
|
|
return nil, errors.Join(errs...)
|
2023-02-26 11:24:49 +08:00
|
|
|
}
|
2022-11-19 10:50:13 +08:00
|
|
|
|
2023-02-26 11:24:49 +08:00
|
|
|
type dialResult struct {
|
|
|
|
ip netip.Addr
|
|
|
|
net.Conn
|
|
|
|
error
|
|
|
|
isPrimary bool
|
2022-04-27 21:37:20 +08:00
|
|
|
}
|
|
|
|
|
2023-02-26 11:24:49 +08:00
|
|
|
func parseAddr(ctx context.Context, network, address string, preferResolver resolver.Resolver) ([]netip.Addr, string, error) {
|
2022-04-27 21:37:20 +08:00
|
|
|
host, port, err := net.SplitHostPort(address)
|
|
|
|
if err != nil {
|
2023-02-26 11:24:49 +08:00
|
|
|
return nil, "-1", err
|
2022-04-27 21:37:20 +08:00
|
|
|
}
|
|
|
|
|
2023-02-22 19:14:11 +08:00
|
|
|
var ips []netip.Addr
|
2022-04-27 21:37:20 +08:00
|
|
|
switch network {
|
|
|
|
case "tcp4", "udp4":
|
2023-02-26 11:24:49 +08:00
|
|
|
if preferResolver == nil {
|
2023-02-22 19:14:11 +08:00
|
|
|
ips, err = resolver.LookupIPv4ProxyServerHost(ctx, host)
|
2022-04-27 21:37:20 +08:00
|
|
|
} else {
|
2023-02-26 11:24:49 +08:00
|
|
|
ips, err = resolver.LookupIPv4WithResolver(ctx, host, preferResolver)
|
2022-04-27 21:37:20 +08:00
|
|
|
}
|
2023-02-26 11:24:49 +08:00
|
|
|
case "tcp6", "udp6":
|
|
|
|
if preferResolver == nil {
|
2023-02-22 19:14:11 +08:00
|
|
|
ips, err = resolver.LookupIPv6ProxyServerHost(ctx, host)
|
2022-04-27 21:37:20 +08:00
|
|
|
} else {
|
2023-02-26 11:24:49 +08:00
|
|
|
ips, err = resolver.LookupIPv6WithResolver(ctx, host, preferResolver)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if preferResolver == nil {
|
2023-02-26 13:52:10 +08:00
|
|
|
ips, err = resolver.LookupIPProxyServerHost(ctx, host)
|
2023-02-26 11:24:49 +08:00
|
|
|
} else {
|
|
|
|
ips, err = resolver.LookupIPWithResolver(ctx, host, preferResolver)
|
2022-04-27 21:37:20 +08:00
|
|
|
}
|
|
|
|
}
|
2022-08-28 13:41:19 +08:00
|
|
|
if err != nil {
|
2023-02-26 11:24:49 +08:00
|
|
|
return nil, "-1", fmt.Errorf("dns resolve failed: %w", err)
|
2022-08-28 13:41:19 +08:00
|
|
|
}
|
2023-03-10 00:25:22 +08:00
|
|
|
for i, ip := range ips {
|
|
|
|
if ip.Is4In6() {
|
|
|
|
ips[i] = ip.Unmap()
|
|
|
|
}
|
|
|
|
}
|
2023-02-26 11:24:49 +08:00
|
|
|
return ips, port, nil
|
2022-08-28 13:41:19 +08:00
|
|
|
}
|
|
|
|
|
2022-12-22 09:53:11 +08:00
|
|
|
type Dialer struct {
|
2023-04-11 12:51:24 +08:00
|
|
|
Opt option
|
2022-12-19 21:34:07 +08:00
|
|
|
}
|
|
|
|
|
2022-12-22 09:53:11 +08:00
|
|
|
func (d Dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
2023-04-11 12:51:24 +08:00
|
|
|
return DialContext(ctx, network, address, WithOption(d.Opt))
|
2022-12-19 21:34:07 +08:00
|
|
|
}
|
|
|
|
|
2022-12-22 09:53:11 +08:00
|
|
|
func (d Dialer) ListenPacket(ctx context.Context, network, address string, rAddrPort netip.AddrPort) (net.PacketConn, error) {
|
2023-04-11 12:51:24 +08:00
|
|
|
opt := WithOption(d.Opt)
|
2023-02-28 15:53:23 +08:00
|
|
|
if rAddrPort.Addr().Unmap().IsLoopback() {
|
|
|
|
// avoid "The requested address is not valid in its context."
|
|
|
|
opt = WithInterface("")
|
|
|
|
}
|
|
|
|
return ListenPacket(ctx, ParseNetwork(network, rAddrPort.Addr()), address, opt)
|
2022-12-20 00:11:02 +08:00
|
|
|
}
|
|
|
|
|
2022-12-22 09:53:11 +08:00
|
|
|
func NewDialer(options ...Option) Dialer {
|
|
|
|
opt := applyOptions(options...)
|
2023-04-11 12:51:24 +08:00
|
|
|
return Dialer{Opt: *opt}
|
2022-12-19 21:34:07 +08:00
|
|
|
}
|