2022-07-03 20:59:25 +08:00
|
|
|
package dialer
|
|
|
|
|
|
|
|
import (
|
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"
|
2023-08-08 16:14:03 +08:00
|
|
|
"github.com/sagernet/sing/common"
|
2022-07-08 23:03:57 +08:00
|
|
|
N "github.com/sagernet/sing/common/network"
|
2022-07-03 20:59:25 +08:00
|
|
|
)
|
|
|
|
|
2023-08-08 16:14:03 +08:00
|
|
|
func MustNew(router adapter.Router, options option.DialerOptions) N.Dialer {
|
|
|
|
return common.Must1(New(router, options))
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(router adapter.Router, options option.DialerOptions) (N.Dialer, error) {
|
|
|
|
var (
|
|
|
|
dialer N.Dialer
|
|
|
|
err error
|
|
|
|
)
|
2022-07-03 20:59:25 +08:00
|
|
|
if options.Detour == "" {
|
2023-08-08 16:14:03 +08:00
|
|
|
dialer, err = NewDefault(router, options)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-03 20:59:25 +08:00
|
|
|
} else {
|
2022-09-03 12:55:10 +08:00
|
|
|
dialer = NewDetour(router, options.Detour)
|
2022-07-07 23:36:32 +08:00
|
|
|
}
|
2022-07-11 18:44:59 +08:00
|
|
|
domainStrategy := dns.DomainStrategy(options.DomainStrategy)
|
|
|
|
if domainStrategy != dns.DomainStrategyAsIS || options.Detour == "" {
|
2023-09-27 13:17:16 +08:00
|
|
|
dialer = NewResolveDialer(
|
|
|
|
router,
|
|
|
|
dialer,
|
|
|
|
options.Detour == "" && !options.TCPFastOpen,
|
|
|
|
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
|
|
|
}
|