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