mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-07-29 18:16:47 +03:00
Fix failover
This commit is contained in:
@@ -3,6 +3,7 @@ package group
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/sagernet/sing-box/adapter"
|
"github.com/sagernet/sing-box/adapter"
|
||||||
"github.com/sagernet/sing-box/adapter/outbound"
|
"github.com/sagernet/sing-box/adapter/outbound"
|
||||||
@@ -31,6 +32,9 @@ type Failover struct {
|
|||||||
logger logger.ContextLogger
|
logger logger.ContextLogger
|
||||||
tags []string
|
tags []string
|
||||||
outbounds map[string]adapter.Outbound
|
outbounds map[string]adapter.Outbound
|
||||||
|
lastUsedOutbound string
|
||||||
|
|
||||||
|
mtx sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFailover(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.FailoverOutboundOptions) (adapter.Outbound, error) {
|
func NewFailover(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.FailoverOutboundOptions) (adapter.Outbound, error) {
|
||||||
@@ -44,6 +48,7 @@ func NewFailover(ctx context.Context, router adapter.Router, logger log.ContextL
|
|||||||
logger: logger,
|
logger: logger,
|
||||||
tags: options.Outbounds,
|
tags: options.Outbounds,
|
||||||
outbounds: make(map[string]adapter.Outbound, len(options.Outbounds)),
|
outbounds: make(map[string]adapter.Outbound, len(options.Outbounds)),
|
||||||
|
lastUsedOutbound: options.Outbounds[0],
|
||||||
}
|
}
|
||||||
return outbound, nil
|
return outbound, nil
|
||||||
}
|
}
|
||||||
@@ -60,7 +65,9 @@ func (s *Failover) Start() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Failover) Now() string {
|
func (s *Failover) Now() string {
|
||||||
return s.tags[0]
|
s.mtx.Lock()
|
||||||
|
defer s.mtx.Unlock()
|
||||||
|
return s.lastUsedOutbound
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Failover) All() []string {
|
func (s *Failover) All() []string {
|
||||||
@@ -76,6 +83,9 @@ func (s *Failover) DialContext(ctx context.Context, network string, destination
|
|||||||
s.logger.ErrorContext(ctx, err)
|
s.logger.ErrorContext(ctx, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
s.mtx.Lock()
|
||||||
|
defer s.mtx.Unlock()
|
||||||
|
s.lastUsedOutbound = outbound.Tag()
|
||||||
return conn, nil
|
return conn, nil
|
||||||
}
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
Reference in New Issue
Block a user