Fix gRPC transport half-close

This commit is contained in:
世界
2026-09-20 22:24:38 +08:00
parent 60f0a0369b
commit f5e8cdae19
+14 -11
View File
@@ -10,6 +10,8 @@ import (
"github.com/sagernet/sing/common/baderror"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"google.golang.org/grpc"
)
var _ net.Conn = (*GRPCConn)(nil)
@@ -21,15 +23,15 @@ type GRPCConn struct {
closeOnce sync.Once
}
func NewGRPCConn(service GunService, cancel context.CancelCauseFunc) *GRPCConn {
//nolint:staticcheck
if client, isClient := service.(GunService_TunClient); isClient {
service = &clientConnWrapper{client}
}
return &GRPCConn{
func NewGRPCConn(service GunService, cancel context.CancelCauseFunc) net.Conn {
conn := &GRPCConn{
GunService: service,
cancel: cancel,
}
if client, isClient := service.(grpc.ClientStream); isClient {
return &GRPCClientConn{GRPCConn: conn, client: client}
}
return conn
}
func (c *GRPCConn) Read(b []byte) (n int, err error) {
@@ -95,12 +97,13 @@ func (c *GRPCConn) Upstream() any {
return c.GunService
}
var _ N.WriteCloser = (*clientConnWrapper)(nil)
var _ N.WriteCloser = (*GRPCClientConn)(nil)
type clientConnWrapper struct {
GunService_TunClient
type GRPCClientConn struct {
*GRPCConn
client grpc.ClientStream
}
func (c *clientConnWrapper) CloseWrite() error {
return c.CloseSend()
func (c *GRPCClientConn) CloseWrite() error {
return c.client.CloseSend()
}