Skip to content

Commit

Permalink
Fix missing nil check for URLTest
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Dec 26, 2023
1 parent 082e3fb commit 76a295a
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions outbound/urltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func NewURLTest(ctx context.Context, router adapter.Router, logger log.ContextLo
outbound := &URLTest{
myOutboundAdapter: myOutboundAdapter{
protocol: C.TypeURLTest,
network: []string{N.NetworkTCP, N.NetworkUDP},
router: router,
logger: logger,
tag: tag,
Expand All @@ -61,13 +62,6 @@ func NewURLTest(ctx context.Context, router adapter.Router, logger log.ContextLo
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 {
Expand All @@ -93,7 +87,12 @@ func (s *URLTest) Close() error {
}

func (s *URLTest) Now() string {
return s.group.Select(N.NetworkTCP).Tag()
if s.group.selectedOutboundTCP != nil {
return s.group.selectedOutboundTCP.Tag()
} else if s.group.selectedOutboundUDP != nil {
return s.group.selectedOutboundUDP.Tag()
}
return ""
}

func (s *URLTest) All() []string {
Expand All @@ -111,6 +110,9 @@ func (s *URLTest) CheckOutbounds() {
func (s *URLTest) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
s.group.Touch()
outbound := s.group.Select(network)
if outbound == nil {
return nil, E.New("missing supported outbound")
}
conn, err := outbound.DialContext(ctx, network, destination)
if err == nil {
return s.group.interruptGroup.NewConn(conn, interrupt.IsExternalConnectionFromContext(ctx)), nil
Expand All @@ -123,6 +125,9 @@ func (s *URLTest) DialContext(ctx context.Context, network string, destination M
func (s *URLTest) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
s.group.Touch()
outbound := s.group.Select(N.NetworkUDP)
if outbound == nil {
return nil, E.New("missing supported outbound")
}
conn, err := outbound.ListenPacket(ctx, destination)
if err == nil {
return s.group.interruptGroup.NewPacketConn(conn, interrupt.IsExternalConnectionFromContext(ctx)), nil
Expand Down Expand Up @@ -346,12 +351,12 @@ func (g *URLTestGroup) urlTest(ctx context.Context, force bool) (map[string]uint
func (g *URLTestGroup) performUpdateCheck() {
outbound := g.Select(N.NetworkTCP)
var updated bool
if outbound != g.selectedOutboundTCP {
if outbound != nil && outbound != g.selectedOutboundTCP {
g.selectedOutboundTCP = outbound
updated = true
}
outbound = g.Select(N.NetworkUDP)
if outbound != g.selectedOutboundUDP {
if outbound != nil && outbound != g.selectedOutboundUDP {
g.selectedOutboundUDP = outbound
updated = true
}
Expand Down

0 comments on commit 76a295a

Please sign in to comment.