2022-09-15 07:22:08 +00:00
|
|
|
package outbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"sort"
|
2023-04-11 08:43:45 +00:00
|
|
|
"sync"
|
2022-09-15 07:22:08 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
|
|
"github.com/sagernet/sing-box/common/urltest"
|
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
"github.com/sagernet/sing-box/log"
|
|
|
|
"github.com/sagernet/sing-box/option"
|
|
|
|
"github.com/sagernet/sing/common"
|
2023-04-13 08:11:46 +00:00
|
|
|
"github.com/sagernet/sing/common/atomic"
|
2022-09-15 07:22:08 +00:00
|
|
|
"github.com/sagernet/sing/common/batch"
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
N "github.com/sagernet/sing/common/network"
|
2023-07-02 08:45:30 +00:00
|
|
|
"github.com/sagernet/sing/service"
|
2022-09-15 07:22:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-04-13 08:11:46 +00:00
|
|
|
_ adapter.Outbound = (*URLTest)(nil)
|
|
|
|
_ adapter.OutboundGroup = (*URLTest)(nil)
|
|
|
|
_ adapter.InterfaceUpdateListener = (*URLTest)(nil)
|
2022-09-15 07:22:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type URLTest struct {
|
|
|
|
myOutboundAdapter
|
2023-04-13 01:03:08 +00:00
|
|
|
ctx context.Context
|
2022-09-15 07:22:08 +00:00
|
|
|
tags []string
|
|
|
|
link string
|
|
|
|
interval time.Duration
|
|
|
|
tolerance uint16
|
|
|
|
group *URLTestGroup
|
|
|
|
}
|
|
|
|
|
2023-04-13 01:03:08 +00:00
|
|
|
func NewURLTest(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.URLTestOutboundOptions) (*URLTest, error) {
|
2022-09-15 07:22:08 +00:00
|
|
|
outbound := &URLTest{
|
|
|
|
myOutboundAdapter: myOutboundAdapter{
|
2023-06-13 14:38:05 +00:00
|
|
|
protocol: C.TypeURLTest,
|
|
|
|
router: router,
|
|
|
|
logger: logger,
|
|
|
|
tag: tag,
|
|
|
|
dependencies: options.Outbounds,
|
2022-09-15 07:22:08 +00:00
|
|
|
},
|
2023-04-13 01:03:08 +00:00
|
|
|
ctx: ctx,
|
2022-09-15 07:22:08 +00:00
|
|
|
tags: options.Outbounds,
|
|
|
|
link: options.URL,
|
|
|
|
interval: time.Duration(options.Interval),
|
|
|
|
tolerance: options.Tolerance,
|
|
|
|
}
|
|
|
|
if len(outbound.tags) == 0 {
|
|
|
|
return nil, E.New("missing tags")
|
|
|
|
}
|
|
|
|
return outbound, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *URLTest) Network() []string {
|
|
|
|
if s.group == nil {
|
|
|
|
return []string{N.NetworkTCP, N.NetworkUDP}
|
|
|
|
}
|
|
|
|
return s.group.Select(N.NetworkTCP).Network()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *URLTest) Start() error {
|
|
|
|
outbounds := make([]adapter.Outbound, 0, len(s.tags))
|
|
|
|
for i, tag := range s.tags {
|
|
|
|
detour, loaded := s.router.Outbound(tag)
|
|
|
|
if !loaded {
|
|
|
|
return E.New("outbound ", i, " not found: ", tag)
|
|
|
|
}
|
|
|
|
outbounds = append(outbounds, detour)
|
|
|
|
}
|
2023-04-13 01:03:08 +00:00
|
|
|
s.group = NewURLTestGroup(s.ctx, s.router, s.logger, outbounds, s.link, s.interval, s.tolerance)
|
2023-07-02 08:45:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *URLTest) PostStart() error {
|
|
|
|
go s.CheckOutbounds()
|
2023-04-13 08:11:46 +00:00
|
|
|
return nil
|
2022-09-15 07:22:08 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 08:43:45 +00:00
|
|
|
func (s *URLTest) Close() error {
|
2022-09-15 07:22:08 +00:00
|
|
|
return common.Close(
|
|
|
|
common.PtrOrNil(s.group),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *URLTest) Now() string {
|
|
|
|
return s.group.Select(N.NetworkTCP).Tag()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *URLTest) All() []string {
|
|
|
|
return s.tags
|
|
|
|
}
|
|
|
|
|
2023-04-11 08:43:45 +00:00
|
|
|
func (s *URLTest) URLTest(ctx context.Context, link string) (map[string]uint16, error) {
|
|
|
|
return s.group.URLTest(ctx, link)
|
|
|
|
}
|
|
|
|
|
2023-07-02 08:45:30 +00:00
|
|
|
func (s *URLTest) CheckOutbounds() {
|
|
|
|
s.group.CheckOutbounds(true)
|
|
|
|
}
|
|
|
|
|
2022-09-15 07:22:08 +00:00
|
|
|
func (s *URLTest) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
2023-04-13 08:11:46 +00:00
|
|
|
s.group.Start()
|
2022-09-15 07:22:08 +00:00
|
|
|
outbound := s.group.Select(network)
|
|
|
|
conn, err := outbound.DialContext(ctx, network, destination)
|
|
|
|
if err == nil {
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
s.logger.ErrorContext(ctx, err)
|
2023-04-13 01:03:08 +00:00
|
|
|
s.group.history.DeleteURLTestHistory(outbound.Tag())
|
2022-09-15 07:22:08 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *URLTest) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
2023-04-13 08:11:46 +00:00
|
|
|
s.group.Start()
|
2022-09-15 07:22:08 +00:00
|
|
|
outbound := s.group.Select(N.NetworkUDP)
|
|
|
|
conn, err := outbound.ListenPacket(ctx, destination)
|
|
|
|
if err == nil {
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
s.logger.ErrorContext(ctx, err)
|
2023-04-13 01:03:08 +00:00
|
|
|
s.group.history.DeleteURLTestHistory(outbound.Tag())
|
2022-09-15 07:22:08 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *URLTest) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
|
|
|
|
return NewConnection(ctx, s, conn, metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *URLTest) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
|
|
|
|
return NewPacketConnection(ctx, s, conn, metadata)
|
|
|
|
}
|
|
|
|
|
2023-04-13 08:11:46 +00:00
|
|
|
func (s *URLTest) InterfaceUpdated() error {
|
|
|
|
go s.group.CheckOutbounds(true)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-15 07:22:08 +00:00
|
|
|
type URLTestGroup struct {
|
2023-04-13 01:03:08 +00:00
|
|
|
ctx context.Context
|
2022-09-15 07:22:08 +00:00
|
|
|
router adapter.Router
|
|
|
|
logger log.Logger
|
|
|
|
outbounds []adapter.Outbound
|
|
|
|
link string
|
|
|
|
interval time.Duration
|
|
|
|
tolerance uint16
|
|
|
|
history *urltest.HistoryStorage
|
2023-04-13 08:11:46 +00:00
|
|
|
checking atomic.Bool
|
2022-09-15 07:22:08 +00:00
|
|
|
|
2023-04-13 08:11:46 +00:00
|
|
|
access sync.Mutex
|
2022-09-15 07:22:08 +00:00
|
|
|
ticker *time.Ticker
|
|
|
|
close chan struct{}
|
|
|
|
}
|
|
|
|
|
2023-04-13 01:03:08 +00:00
|
|
|
func NewURLTestGroup(ctx context.Context, router adapter.Router, logger log.Logger, outbounds []adapter.Outbound, link string, interval time.Duration, tolerance uint16) *URLTestGroup {
|
2022-09-15 07:22:08 +00:00
|
|
|
if interval == 0 {
|
2022-09-17 12:52:16 +00:00
|
|
|
interval = C.DefaultURLTestInterval
|
2022-09-15 07:22:08 +00:00
|
|
|
}
|
|
|
|
if tolerance == 0 {
|
|
|
|
tolerance = 50
|
|
|
|
}
|
|
|
|
var history *urltest.HistoryStorage
|
2023-07-02 08:45:30 +00:00
|
|
|
if history = service.PtrFromContext[urltest.HistoryStorage](ctx); history != nil {
|
|
|
|
} else if clashServer := router.ClashServer(); clashServer != nil {
|
2022-09-15 07:22:08 +00:00
|
|
|
history = clashServer.HistoryStorage()
|
|
|
|
} else {
|
|
|
|
history = urltest.NewHistoryStorage()
|
|
|
|
}
|
|
|
|
return &URLTestGroup{
|
2023-04-13 01:03:08 +00:00
|
|
|
ctx: ctx,
|
2022-09-15 07:22:08 +00:00
|
|
|
router: router,
|
|
|
|
logger: logger,
|
|
|
|
outbounds: outbounds,
|
|
|
|
link: link,
|
|
|
|
interval: interval,
|
|
|
|
tolerance: tolerance,
|
|
|
|
history: history,
|
|
|
|
close: make(chan struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 08:11:46 +00:00
|
|
|
func (g *URLTestGroup) Start() {
|
|
|
|
if g.ticker != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
g.access.Lock()
|
|
|
|
defer g.access.Unlock()
|
|
|
|
if g.ticker != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-09-15 07:22:08 +00:00
|
|
|
g.ticker = time.NewTicker(g.interval)
|
|
|
|
go g.loopCheck()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *URLTestGroup) Close() error {
|
2023-04-13 08:11:46 +00:00
|
|
|
if g.ticker == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-15 07:22:08 +00:00
|
|
|
g.ticker.Stop()
|
|
|
|
close(g.close)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *URLTestGroup) Select(network string) adapter.Outbound {
|
|
|
|
var minDelay uint16
|
|
|
|
var minTime time.Time
|
|
|
|
var minOutbound adapter.Outbound
|
|
|
|
for _, detour := range g.outbounds {
|
|
|
|
if !common.Contains(detour.Network(), network) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
history := g.history.LoadURLTestHistory(RealTag(detour))
|
|
|
|
if history == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if minDelay == 0 || minDelay > history.Delay+g.tolerance || minDelay > history.Delay-g.tolerance && minTime.Before(history.Time) {
|
|
|
|
minDelay = history.Delay
|
|
|
|
minTime = history.Time
|
|
|
|
minOutbound = detour
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if minOutbound == nil {
|
|
|
|
for _, detour := range g.outbounds {
|
|
|
|
if !common.Contains(detour.Network(), network) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
minOutbound = detour
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return minOutbound
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *URLTestGroup) Fallback(used adapter.Outbound) []adapter.Outbound {
|
|
|
|
outbounds := make([]adapter.Outbound, 0, len(g.outbounds)-1)
|
|
|
|
for _, detour := range g.outbounds {
|
|
|
|
if detour != used {
|
|
|
|
outbounds = append(outbounds, detour)
|
|
|
|
}
|
|
|
|
}
|
2023-04-30 08:58:07 +00:00
|
|
|
sort.SliceStable(outbounds, func(i, j int) bool {
|
2022-09-15 07:22:08 +00:00
|
|
|
oi := outbounds[i]
|
|
|
|
oj := outbounds[j]
|
|
|
|
hi := g.history.LoadURLTestHistory(RealTag(oi))
|
|
|
|
if hi == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
hj := g.history.LoadURLTestHistory(RealTag(oj))
|
|
|
|
if hj == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return hi.Delay < hj.Delay
|
|
|
|
})
|
|
|
|
return outbounds
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *URLTestGroup) loopCheck() {
|
2023-04-13 08:11:46 +00:00
|
|
|
go g.CheckOutbounds(true)
|
2022-09-15 07:22:08 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-g.close:
|
|
|
|
return
|
|
|
|
case <-g.ticker.C:
|
2023-04-13 08:11:46 +00:00
|
|
|
g.CheckOutbounds(false)
|
2022-09-15 07:22:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 08:11:46 +00:00
|
|
|
func (g *URLTestGroup) CheckOutbounds(force bool) {
|
|
|
|
_, _ = g.urlTest(g.ctx, g.link, force)
|
2023-04-11 08:43:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *URLTestGroup) URLTest(ctx context.Context, link string) (map[string]uint16, error) {
|
2023-04-13 08:11:46 +00:00
|
|
|
return g.urlTest(ctx, link, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *URLTestGroup) urlTest(ctx context.Context, link string, force bool) (map[string]uint16, error) {
|
|
|
|
result := make(map[string]uint16)
|
|
|
|
if g.checking.Swap(true) {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
defer g.checking.Store(false)
|
2023-04-11 08:43:45 +00:00
|
|
|
b, _ := batch.New(ctx, batch.WithConcurrencyNum[any](10))
|
2022-09-15 07:22:08 +00:00
|
|
|
checked := make(map[string]bool)
|
2023-04-11 08:43:45 +00:00
|
|
|
var resultAccess sync.Mutex
|
2022-09-15 07:22:08 +00:00
|
|
|
for _, detour := range g.outbounds {
|
|
|
|
tag := detour.Tag()
|
|
|
|
realTag := RealTag(detour)
|
|
|
|
if checked[realTag] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
history := g.history.LoadURLTestHistory(realTag)
|
2023-04-13 08:11:46 +00:00
|
|
|
if !force && history != nil && time.Now().Sub(history.Time) < g.interval {
|
2022-09-15 07:22:08 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
checked[realTag] = true
|
|
|
|
p, loaded := g.router.Outbound(realTag)
|
|
|
|
if !loaded {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
b.Go(realTag, func() (any, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), C.TCPTimeout)
|
|
|
|
defer cancel()
|
2023-04-11 08:43:45 +00:00
|
|
|
t, err := urltest.URLTest(ctx, link, p)
|
2022-09-15 07:22:08 +00:00
|
|
|
if err != nil {
|
|
|
|
g.logger.Debug("outbound ", tag, " unavailable: ", err)
|
|
|
|
g.history.DeleteURLTestHistory(realTag)
|
|
|
|
} else {
|
|
|
|
g.logger.Debug("outbound ", tag, " available: ", t, "ms")
|
|
|
|
g.history.StoreURLTestHistory(realTag, &urltest.History{
|
|
|
|
Time: time.Now(),
|
|
|
|
Delay: t,
|
|
|
|
})
|
2023-04-11 08:43:45 +00:00
|
|
|
resultAccess.Lock()
|
|
|
|
result[tag] = t
|
|
|
|
resultAccess.Unlock()
|
2022-09-15 07:22:08 +00:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
b.Wait()
|
2023-04-11 08:43:45 +00:00
|
|
|
return result, nil
|
2022-09-15 07:22:08 +00:00
|
|
|
}
|