sing-box/common/dialer/dialer.go

52 lines
1.3 KiB
Go
Raw Permalink Normal View History

package dialer
import (
"context"
2022-07-08 04:58:43 +00:00
"time"
2022-07-06 07:01:09 +00:00
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/option"
2022-07-11 10:44:59 +00:00
"github.com/sagernet/sing-dns"
E "github.com/sagernet/sing/common/exceptions"
2022-07-08 15:03:57 +00:00
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/service"
)
func New(ctx context.Context, options option.DialerOptions) (N.Dialer, error) {
2024-11-10 04:11:21 +00:00
networkManager := service.FromContext[adapter.NetworkManager](ctx)
if options.IsWireGuardListener {
2024-11-10 04:11:21 +00:00
return NewDefault(networkManager, options)
}
2023-08-08 08:14:03 +00:00
var (
dialer N.Dialer
err error
)
if options.Detour == "" {
2024-11-10 04:11:21 +00:00
dialer, err = NewDefault(networkManager, options)
2023-08-08 08:14:03 +00:00
if err != nil {
return nil, err
}
} else {
outboundManager := service.FromContext[adapter.OutboundManager](ctx)
if outboundManager == nil {
return nil, E.New("missing outbound manager")
}
dialer = NewDetour(outboundManager, options.Detour)
}
2024-11-10 04:11:21 +00:00
if networkManager == nil {
return NewDefault(networkManager, options)
2022-07-07 15:36:32 +00:00
}
2024-08-20 10:56:50 +00:00
if options.Detour == "" {
2024-11-10 04:11:21 +00:00
router := service.FromContext[adapter.Router](ctx)
if router != nil {
dialer = NewResolveDialer(
router,
dialer,
options.Detour == "" && !options.TCPFastOpen,
dns.DomainStrategy(options.DomainStrategy),
time.Duration(options.FallbackDelay))
}
}
2023-08-08 08:14:03 +00:00
return dialer, nil
}