|
@@ -26,6 +26,7 @@ var (
|
|
|
|
|
|
type URLTest struct {
|
|
|
myOutboundAdapter
|
|
|
+ ctx context.Context
|
|
|
tags []string
|
|
|
link string
|
|
|
interval time.Duration
|
|
@@ -33,7 +34,7 @@ type URLTest struct {
|
|
|
group *URLTestGroup
|
|
|
}
|
|
|
|
|
|
-func NewURLTest(router adapter.Router, logger log.ContextLogger, tag string, options option.URLTestOutboundOptions) (*URLTest, error) {
|
|
|
+func NewURLTest(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.URLTestOutboundOptions) (*URLTest, error) {
|
|
|
outbound := &URLTest{
|
|
|
myOutboundAdapter: myOutboundAdapter{
|
|
|
protocol: C.TypeURLTest,
|
|
@@ -41,6 +42,7 @@ func NewURLTest(router adapter.Router, logger log.ContextLogger, tag string, opt
|
|
|
logger: logger,
|
|
|
tag: tag,
|
|
|
},
|
|
|
+ ctx: ctx,
|
|
|
tags: options.Outbounds,
|
|
|
link: options.URL,
|
|
|
interval: time.Duration(options.Interval),
|
|
@@ -68,7 +70,7 @@ func (s *URLTest) Start() error {
|
|
|
}
|
|
|
outbounds = append(outbounds, detour)
|
|
|
}
|
|
|
- s.group = NewURLTestGroup(s.router, s.logger, outbounds, s.link, s.interval, s.tolerance)
|
|
|
+ s.group = NewURLTestGroup(s.ctx, s.router, s.logger, outbounds, s.link, s.interval, s.tolerance)
|
|
|
return s.group.Start()
|
|
|
}
|
|
|
|
|
@@ -97,14 +99,7 @@ func (s *URLTest) DialContext(ctx context.Context, network string, destination M
|
|
|
return conn, nil
|
|
|
}
|
|
|
s.logger.ErrorContext(ctx, err)
|
|
|
- go s.group.checkOutbounds()
|
|
|
- outbounds := s.group.Fallback(outbound)
|
|
|
- for _, fallback := range outbounds {
|
|
|
- conn, err = fallback.DialContext(ctx, network, destination)
|
|
|
- if err == nil {
|
|
|
- return conn, nil
|
|
|
- }
|
|
|
- }
|
|
|
+ s.group.history.DeleteURLTestHistory(outbound.Tag())
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
@@ -115,14 +110,7 @@ func (s *URLTest) ListenPacket(ctx context.Context, destination M.Socksaddr) (ne
|
|
|
return conn, nil
|
|
|
}
|
|
|
s.logger.ErrorContext(ctx, err)
|
|
|
- go s.group.checkOutbounds()
|
|
|
- outbounds := s.group.Fallback(outbound)
|
|
|
- for _, fallback := range outbounds {
|
|
|
- conn, err = fallback.ListenPacket(ctx, destination)
|
|
|
- if err == nil {
|
|
|
- return conn, nil
|
|
|
- }
|
|
|
- }
|
|
|
+ s.group.history.DeleteURLTestHistory(outbound.Tag())
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
@@ -135,6 +123,7 @@ func (s *URLTest) NewPacketConnection(ctx context.Context, conn N.PacketConn, me
|
|
|
}
|
|
|
|
|
|
type URLTestGroup struct {
|
|
|
+ ctx context.Context
|
|
|
router adapter.Router
|
|
|
logger log.Logger
|
|
|
outbounds []adapter.Outbound
|
|
@@ -147,11 +136,7 @@ type URLTestGroup struct {
|
|
|
close chan struct{}
|
|
|
}
|
|
|
|
|
|
-func NewURLTestGroup(router adapter.Router, logger log.Logger, outbounds []adapter.Outbound, link string, interval time.Duration, tolerance uint16) *URLTestGroup {
|
|
|
- if link == "" {
|
|
|
- //goland:noinspection HttpUrlsUsage
|
|
|
- link = "http://www.gstatic.com/generate_204"
|
|
|
- }
|
|
|
+func NewURLTestGroup(ctx context.Context, router adapter.Router, logger log.Logger, outbounds []adapter.Outbound, link string, interval time.Duration, tolerance uint16) *URLTestGroup {
|
|
|
if interval == 0 {
|
|
|
interval = C.DefaultURLTestInterval
|
|
|
}
|
|
@@ -165,6 +150,7 @@ func NewURLTestGroup(router adapter.Router, logger log.Logger, outbounds []adapt
|
|
|
history = urltest.NewHistoryStorage()
|
|
|
}
|
|
|
return &URLTestGroup{
|
|
|
+ ctx: ctx,
|
|
|
router: router,
|
|
|
logger: logger,
|
|
|
outbounds: outbounds,
|
|
@@ -254,7 +240,7 @@ func (g *URLTestGroup) loopCheck() {
|
|
|
}
|
|
|
|
|
|
func (g *URLTestGroup) checkOutbounds() {
|
|
|
- _, _ = g.URLTest(context.Background(), g.link)
|
|
|
+ _, _ = g.URLTest(g.ctx, g.link)
|
|
|
}
|
|
|
|
|
|
func (g *URLTestGroup) URLTest(ctx context.Context, link string) (map[string]uint16, error) {
|