2021-09-06 23:07:34 +08:00
|
|
|
package dialer
|
|
|
|
|
2022-08-28 13:41:19 +08:00
|
|
|
import (
|
2023-03-07 09:30:51 +08:00
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
|
2023-04-22 15:37:57 +08:00
|
|
|
"github.com/Dreamacro/clash/common/atomic"
|
2022-12-07 20:01:44 +08:00
|
|
|
"github.com/Dreamacro/clash/component/resolver"
|
2022-08-28 13:41:19 +08:00
|
|
|
)
|
2021-09-06 23:07:34 +08:00
|
|
|
|
2021-11-07 16:48:51 +08:00
|
|
|
var (
|
2022-02-17 14:23:47 +08:00
|
|
|
DefaultOptions []Option
|
2023-04-22 15:37:57 +08:00
|
|
|
DefaultInterface = atomic.NewTypedValue[string]("")
|
2022-02-17 14:23:47 +08:00
|
|
|
DefaultRoutingMark = atomic.NewInt32(0)
|
2021-11-07 16:48:51 +08:00
|
|
|
)
|
|
|
|
|
2023-03-07 09:30:51 +08:00
|
|
|
type NetDialer interface {
|
|
|
|
DialContext(ctx context.Context, network, address string) (net.Conn, error)
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:48:51 +08:00
|
|
|
type option struct {
|
2021-09-06 23:07:34 +08:00
|
|
|
interfaceName string
|
2023-10-26 10:27:38 +08:00
|
|
|
fallbackBind bool
|
2021-09-06 23:07:34 +08:00
|
|
|
addrReuse bool
|
2021-11-08 16:59:48 +08:00
|
|
|
routingMark int
|
2022-08-28 13:41:19 +08:00
|
|
|
network int
|
|
|
|
prefer int
|
2023-02-24 13:53:44 +08:00
|
|
|
tfo bool
|
2023-08-09 16:57:39 +08:00
|
|
|
mpTcp bool
|
2022-12-07 20:01:44 +08:00
|
|
|
resolver resolver.Resolver
|
2023-03-07 09:30:51 +08:00
|
|
|
netDialer NetDialer
|
2021-09-06 23:07:34 +08:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:48:51 +08:00
|
|
|
type Option func(opt *option)
|
2021-09-06 23:07:34 +08:00
|
|
|
|
|
|
|
func WithInterface(name string) Option {
|
2021-11-07 16:48:51 +08:00
|
|
|
return func(opt *option) {
|
2021-09-06 23:07:34 +08:00
|
|
|
opt.interfaceName = name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 10:27:38 +08:00
|
|
|
func WithFallbackBind(fallback bool) Option {
|
|
|
|
return func(opt *option) {
|
|
|
|
opt.fallbackBind = fallback
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-06 23:07:34 +08:00
|
|
|
func WithAddrReuse(reuse bool) Option {
|
2021-11-07 16:48:51 +08:00
|
|
|
return func(opt *option) {
|
2021-09-06 23:07:34 +08:00
|
|
|
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
|
|
|
|
2022-12-07 20:01:44 +08:00
|
|
|
func WithResolver(r resolver.Resolver) Option {
|
2022-03-28 00:44:13 +08:00
|
|
|
return func(opt *option) {
|
2022-12-07 20:01:44 +08:00
|
|
|
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
|
|
|
|
2023-02-24 13:53:44 +08:00
|
|
|
func WithTFO(tfo bool) Option {
|
|
|
|
return func(opt *option) {
|
|
|
|
opt.tfo = tfo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-09 16:57:39 +08:00
|
|
|
func WithMPTCP(mpTcp bool) Option {
|
|
|
|
return func(opt *option) {
|
|
|
|
opt.mpTcp = mpTcp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-07 09:30:51 +08:00
|
|
|
func WithNetDialer(netDialer NetDialer) Option {
|
|
|
|
return func(opt *option) {
|
|
|
|
opt.netDialer = netDialer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|