Clash.Meta/component/dialer/options.go

77 lines
1.2 KiB
Go
Raw Normal View History

package dialer
2022-08-28 13:41:19 +08:00
import (
"github.com/Dreamacro/clash/component/resolver"
2022-08-28 13:41:19 +08:00
"go.uber.org/atomic"
)
var (
DefaultOptions []Option
DefaultInterface = atomic.NewString("")
DefaultRoutingMark = atomic.NewInt32(0)
)
type option struct {
interfaceName string
addrReuse bool
2021-11-08 16:59:48 +08:00
routingMark int
2022-08-28 13:41:19 +08:00
network int
prefer int
resolver resolver.Resolver
}
type Option func(opt *option)
func WithInterface(name string) Option {
return func(opt *option) {
opt.interfaceName = name
}
}
func WithAddrReuse(reuse bool) Option {
return func(opt *option) {
opt.addrReuse = reuse
}
}
2021-11-08 16:59:48 +08:00
func WithRoutingMark(mark int) Option {
return func(opt *option) {
opt.routingMark = mark
}
}
2022-03-28 00:44:13 +08:00
func WithResolver(r resolver.Resolver) Option {
2022-03-28 00:44:13 +08:00
return func(opt *option) {
opt.resolver = r
2022-03-28 00:44:13 +08:00
}
}
2022-08-28 13:41:19 +08:00
func WithPreferIPv4() Option {
return func(opt *option) {
opt.prefer = 4
}
}
func WithPreferIPv6() Option {
return func(opt *option) {
opt.prefer = 6
}
}
func WithOnlySingleStack(isIPv4 bool) Option {
return func(opt *option) {
if isIPv4 {
opt.network = 4
} else {
opt.network = 6
}
}
}
2022-12-20 00:11:02 +08:00
2022-12-22 09:53:11 +08:00
func WithOption(o option) Option {
2022-12-20 00:11:02 +08:00
return func(opt *option) {
*opt = o
}
}