Add dns client

This commit is contained in:
世界
2022-07-06 23:11:48 +08:00
parent 651c4b539a
commit 8a761d7e3b
23 changed files with 582 additions and 145 deletions

View File

@@ -20,7 +20,7 @@ type defaultDialer struct {
net.ListenConfig
}
func newDefault(options option.DialerOptions) N.Dialer {
func NewDefault(options option.DialerOptions) N.Dialer {
var dialer net.Dialer
var listener net.ListenConfig
if options.BindInterface != "" {

View File

@@ -10,27 +10,31 @@ import (
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/option"
)
type detourDialer struct {
router adapter.Router
options option.DialerOptions
detour string
dialer N.Dialer
initOnce sync.Once
initErr error
}
func newDetour(router adapter.Router, options option.DialerOptions) N.Dialer {
return &detourDialer{router: router, options: options}
func NewDetour(router adapter.Router, detour string) N.Dialer {
return &detourDialer{router: router, detour: detour}
}
func (d *detourDialer) Start() error {
_, err := d.Dialer()
return err
}
func (d *detourDialer) Dialer() (N.Dialer, error) {
d.initOnce.Do(func() {
var loaded bool
d.dialer, loaded = d.router.Outbound(d.options.Detour)
d.dialer, loaded = d.router.Outbound(d.detour)
if !loaded {
d.initErr = E.New("outbound detour not found: ", d.options.Detour)
d.initErr = E.New("outbound detour not found: ", d.detour)
}
})
return d.dialer, d.initErr

View File

@@ -11,12 +11,12 @@ import (
func New(router adapter.Router, options option.DialerOptions) N.Dialer {
var dialer N.Dialer
if options.Detour == "" {
dialer = newDefault(options)
dialer = NewDefault(options)
} else {
dialer = newDetour(router, options)
dialer = NewDetour(router, options.Detour)
}
if options.OverrideOptions.IsValid() {
dialer = newOverride(dialer, common.PtrValueOrDefault(options.OverrideOptions))
dialer = NewOverride(dialer, common.PtrValueOrDefault(options.OverrideOptions))
}
return dialer
}

View File

@@ -22,7 +22,7 @@ type overrideDialer struct {
uotEnabled bool
}
func newOverride(upstream N.Dialer, options option.OverrideStreamOptions) N.Dialer {
func NewOverride(upstream N.Dialer, options option.OverrideStreamOptions) N.Dialer {
return &overrideDialer{
upstream,
options.TLS,

View File

@@ -5,7 +5,6 @@ package dialer
import (
"syscall"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/control"
E "github.com/sagernet/sing/common/exceptions"
)
@@ -42,6 +41,6 @@ func ProtectPath(protectPath string) control.Func {
err := conn.Control(func(fd uintptr) {
innerErr = sendAncillaryFileDescriptors(protectPath, []int{int(fd)})
})
return common.AnyError(innerErr, err)
return E.Errors(innerErr, err)
}
}