mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-06-13 21:54:13 +08:00
fix may pick nil outbound if fallback not set
This commit is contained in:
parent
9a9840a41e
commit
24298883d9
@ -90,6 +90,16 @@ func (s *Balancer) NewPacketConnection(ctx context.Context, conn N.PacketConn, m
|
|||||||
|
|
||||||
// initialize inits the balancer
|
// initialize inits the balancer
|
||||||
func (s *Balancer) initialize() error {
|
func (s *Balancer) initialize() error {
|
||||||
|
// the fallback is required, in case that all outbounds are not available,
|
||||||
|
// we can pick it instead of returning nil to avoid panic.
|
||||||
|
if s.fallbackTag == "" {
|
||||||
|
return E.New("fallback not set")
|
||||||
|
}
|
||||||
|
outbound, loaded := s.router.Outbound(s.fallbackTag)
|
||||||
|
if !loaded {
|
||||||
|
return E.New("fallback outbound not found: ", s.fallbackTag)
|
||||||
|
}
|
||||||
|
s.fallback = outbound
|
||||||
for i, tag := range s.tags {
|
for i, tag := range s.tags {
|
||||||
outbound, loaded := s.router.Outbound(tag)
|
outbound, loaded := s.router.Outbound(tag)
|
||||||
if !loaded {
|
if !loaded {
|
||||||
@ -97,13 +107,6 @@ func (s *Balancer) initialize() error {
|
|||||||
}
|
}
|
||||||
s.nodes = append(s.nodes, balancer.NewNode(outbound))
|
s.nodes = append(s.nodes, balancer.NewNode(outbound))
|
||||||
}
|
}
|
||||||
if s.fallbackTag != "" {
|
|
||||||
outbound, loaded := s.router.Outbound(s.fallbackTag)
|
|
||||||
if !loaded {
|
|
||||||
return E.New("fallback outbound not found: ", s.fallbackTag)
|
|
||||||
}
|
|
||||||
s.fallback = outbound
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,7 +132,7 @@ func (s *Balancer) pick() adapter.Outbound {
|
|||||||
// not started
|
// not started
|
||||||
count := len(s.nodes)
|
count := len(s.nodes)
|
||||||
if count == 0 {
|
if count == 0 {
|
||||||
// goes to fallbackTag
|
// goes to fallback
|
||||||
return s.fallback
|
return s.fallback
|
||||||
}
|
}
|
||||||
picked := s.nodes[rand.Intn(count)]
|
picked := s.nodes[rand.Intn(count)]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user