1
0
Эх сурвалжийг харах

Fix hysteria stream error

世界 3 жил өмнө
parent
commit
4b61d6e875

+ 51 - 1
common/baderror/baderror.go

@@ -1,6 +1,13 @@
 package baderror
 
-import "strings"
+import (
+	"context"
+	"io"
+	"net"
+	"strings"
+
+	E "github.com/sagernet/sing/common/exceptions"
+)
 
 func Contains(err error, msgList ...string) bool {
 	for _, msg := range msgList {
@@ -10,3 +17,46 @@ func Contains(err error, msgList ...string) bool {
 	}
 	return false
 }
+
+func WrapH2(err error) error {
+	if err == nil {
+		return nil
+	}
+	err = E.Unwrap(err)
+	if err == io.ErrUnexpectedEOF {
+		return io.EOF
+	}
+	if Contains(err, "client disconnected", "body closed by handler") {
+		return net.ErrClosed
+	}
+	return err
+}
+
+func WrapGRPC(err error) error {
+	// grpc uses stupid internal error types
+	if err == nil {
+		return nil
+	}
+	if Contains(err, "EOF") {
+		return io.EOF
+	}
+	if Contains(err, "Canceled") {
+		return context.Canceled
+	}
+	if Contains(err,
+		"the client connection is closing",
+		"server closed the stream without sending trailers") {
+		return net.ErrClosed
+	}
+	return err
+}
+
+func WrapQUIC(err error) error {
+	if err == nil {
+		return nil
+	}
+	if Contains(err, "canceled with error code 0") {
+		return net.ErrClosed
+	}
+	return err
+}

+ 0 - 26
common/baderror/grpc.go

@@ -1,26 +0,0 @@
-package baderror
-
-import (
-	"context"
-	"io"
-	"net"
-)
-
-func WrapGRPC(err error) error {
-	// grpc uses stupid internal error types
-	if err == nil {
-		return nil
-	}
-	if Contains(err, "EOF") {
-		return io.EOF
-	}
-	if Contains(err, "Canceled") {
-		return context.Canceled
-	}
-	if Contains(err,
-		"the client connection is closing",
-		"server closed the stream without sending trailers") {
-		return net.ErrClosed
-	}
-	return err
-}

+ 0 - 22
common/baderror/h2.go

@@ -1,22 +0,0 @@
-package baderror
-
-import (
-	"io"
-	"net"
-
-	E "github.com/sagernet/sing/common/exceptions"
-)
-
-func WrapH2(err error) error {
-	if err == nil {
-		return nil
-	}
-	err = E.Unwrap(err)
-	if err == io.ErrUnexpectedEOF {
-		return io.EOF
-	}
-	if Contains(err, "client disconnected", "body closed by handler") {
-		return net.ErrClosed
-	}
-	return err
-}

+ 11 - 4
transport/hysteria/wrap.go

@@ -6,6 +6,7 @@ import (
 	"syscall"
 
 	"github.com/sagernet/quic-go"
+	"github.com/sagernet/sing-box/common/baderror"
 	"github.com/sagernet/sing/common"
 )
 
@@ -38,6 +39,16 @@ type StreamWrapper struct {
 	quic.Stream
 }
 
+func (s *StreamWrapper) Read(p []byte) (n int, err error) {
+	n, err = s.Stream.Read(p)
+	return n, baderror.WrapQUIC(err)
+}
+
+func (s *StreamWrapper) Write(p []byte) (n int, err error) {
+	n, err = s.Stream.Write(p)
+	return n, baderror.WrapQUIC(err)
+}
+
 func (s *StreamWrapper) LocalAddr() net.Addr {
 	return s.Conn.LocalAddr()
 }
@@ -50,10 +61,6 @@ func (s *StreamWrapper) Upstream() any {
 	return s.Stream
 }
 
-func (s *StreamWrapper) ReaderReplaceable() bool {
-	return true
-}
-
 func (s *StreamWrapper) WriterReplaceable() bool {
 	return true
 }