github.com/metacubex/mihomo@v1.18.5/component/dialer/options.go (about)

     1  package dialer
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  
     7  	"github.com/metacubex/mihomo/common/atomic"
     8  	"github.com/metacubex/mihomo/component/resolver"
     9  )
    10  
    11  var (
    12  	DefaultOptions     []Option
    13  	DefaultInterface   = atomic.NewTypedValue[string]("")
    14  	DefaultRoutingMark = atomic.NewInt32(0)
    15  )
    16  
    17  type NetDialer interface {
    18  	DialContext(ctx context.Context, network, address string) (net.Conn, error)
    19  }
    20  
    21  type option struct {
    22  	interfaceName string
    23  	fallbackBind  bool
    24  	addrReuse     bool
    25  	routingMark   int
    26  	network       int
    27  	prefer        int
    28  	tfo           bool
    29  	mpTcp         bool
    30  	resolver      resolver.Resolver
    31  	netDialer     NetDialer
    32  }
    33  
    34  type Option func(opt *option)
    35  
    36  func WithInterface(name string) Option {
    37  	return func(opt *option) {
    38  		opt.interfaceName = name
    39  	}
    40  }
    41  
    42  func WithFallbackBind(fallback bool) Option {
    43  	return func(opt *option) {
    44  		opt.fallbackBind = fallback
    45  	}
    46  }
    47  
    48  func WithAddrReuse(reuse bool) Option {
    49  	return func(opt *option) {
    50  		opt.addrReuse = reuse
    51  	}
    52  }
    53  
    54  func WithRoutingMark(mark int) Option {
    55  	return func(opt *option) {
    56  		opt.routingMark = mark
    57  	}
    58  }
    59  
    60  func WithResolver(r resolver.Resolver) Option {
    61  	return func(opt *option) {
    62  		opt.resolver = r
    63  	}
    64  }
    65  
    66  func WithPreferIPv4() Option {
    67  	return func(opt *option) {
    68  		opt.prefer = 4
    69  	}
    70  }
    71  
    72  func WithPreferIPv6() Option {
    73  	return func(opt *option) {
    74  		opt.prefer = 6
    75  	}
    76  }
    77  
    78  func WithOnlySingleStack(isIPv4 bool) Option {
    79  	return func(opt *option) {
    80  		if isIPv4 {
    81  			opt.network = 4
    82  		} else {
    83  			opt.network = 6
    84  		}
    85  	}
    86  }
    87  
    88  func WithTFO(tfo bool) Option {
    89  	return func(opt *option) {
    90  		opt.tfo = tfo
    91  	}
    92  }
    93  
    94  func WithMPTCP(mpTcp bool) Option {
    95  	return func(opt *option) {
    96  		opt.mpTcp = mpTcp
    97  	}
    98  }
    99  
   100  func WithNetDialer(netDialer NetDialer) Option {
   101  	return func(opt *option) {
   102  		opt.netDialer = netDialer
   103  	}
   104  }
   105  
   106  func WithOption(o option) Option {
   107  	return func(opt *option) {
   108  		*opt = o
   109  	}
   110  }