diff --git a/docs/changelog.md b/docs/changelog.md index e51e6ed94b..71362bf5e8 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -2,6 +2,10 @@ icon: material/alert-decagram --- +#### 1.14.0-alpha.20 + +** Fixes and improvements + #### 1.14.0-alpha.19 * Preserve comments between formatting diff --git a/protocol/group/selector.go b/protocol/group/selector.go index 85bea2b964..1da8466b4e 100644 --- a/protocol/group/selector.go +++ b/protocol/group/selector.go @@ -141,7 +141,11 @@ func (s *Selector) SelectOutbound(tag string) bool { } func (s *Selector) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { - conn, err := s.selected.Load().DialContext(ctx, network, destination) + selected := s.selected.Load() + if selected == nil { + return nil, E.New("selector ", s.Tag(), " has no selected outbound") + } + conn, err := selected.DialContext(ctx, network, destination) if err != nil { return nil, err } @@ -149,7 +153,11 @@ func (s *Selector) DialContext(ctx context.Context, network string, destination } func (s *Selector) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { - conn, err := s.selected.Load().ListenPacket(ctx, destination) + selected := s.selected.Load() + if selected == nil { + return nil, E.New("selector ", s.Tag(), " has no selected outbound") + } + conn, err := selected.ListenPacket(ctx, destination) if err != nil { return nil, err } @@ -159,6 +167,10 @@ func (s *Selector) ListenPacket(ctx context.Context, destination M.Socksaddr) (n func (s *Selector) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) { ctx = interrupt.ContextWithIsExternalConnection(ctx) selected := s.selected.Load() + if selected == nil { + N.CloseOnHandshakeFailure(conn, onClose, E.New("selector ", s.Tag(), " has no selected outbound")) + return + } if outboundHandler, isHandler := selected.(adapter.ConnectionHandler); isHandler { outboundHandler.NewConnection(ctx, conn, metadata, onClose) } else { @@ -169,6 +181,10 @@ func (s *Selector) NewConnection(ctx context.Context, conn net.Conn, metadata ad func (s *Selector) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) { ctx = interrupt.ContextWithIsExternalConnection(ctx) selected := s.selected.Load() + if selected == nil { + N.CloseOnHandshakeFailure(conn, onClose, E.New("selector ", s.Tag(), " has no selected outbound")) + return + } if outboundHandler, isHandler := selected.(adapter.PacketConnectionHandler); isHandler { outboundHandler.NewPacketConnection(ctx, conn, metadata, onClose) } else { @@ -178,6 +194,9 @@ func (s *Selector) NewPacketConnection(ctx context.Context, conn N.PacketConn, m func (s *Selector) NewDirectRouteConnection(metadata adapter.InboundContext, routeContext tun.DirectRouteContext, timeout time.Duration) (tun.DirectRouteDestination, error) { selected := s.selected.Load() + if selected == nil { + return nil, E.New("selector ", s.Tag(), " has no selected outbound") + } if !common.Contains(selected.Network(), metadata.Network) { return nil, E.New(metadata.Network, " is not supported by outbound: ", selected.Tag()) }