Skip to content

Commit 4b61d6e

Browse files
committed
Fix hysteria stream error
1 parent 7d83e35 commit 4b61d6e

File tree

4 files changed

+62
-53
lines changed

4 files changed

+62
-53
lines changed

common/baderror/baderror.go

+51-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package baderror
22

3-
import "strings"
3+
import (
4+
"context"
5+
"io"
6+
"net"
7+
"strings"
8+
9+
E "github.com/sagernet/sing/common/exceptions"
10+
)
411

512
func Contains(err error, msgList ...string) bool {
613
for _, msg := range msgList {
@@ -10,3 +17,46 @@ func Contains(err error, msgList ...string) bool {
1017
}
1118
return false
1219
}
20+
21+
func WrapH2(err error) error {
22+
if err == nil {
23+
return nil
24+
}
25+
err = E.Unwrap(err)
26+
if err == io.ErrUnexpectedEOF {
27+
return io.EOF
28+
}
29+
if Contains(err, "client disconnected", "body closed by handler") {
30+
return net.ErrClosed
31+
}
32+
return err
33+
}
34+
35+
func WrapGRPC(err error) error {
36+
// grpc uses stupid internal error types
37+
if err == nil {
38+
return nil
39+
}
40+
if Contains(err, "EOF") {
41+
return io.EOF
42+
}
43+
if Contains(err, "Canceled") {
44+
return context.Canceled
45+
}
46+
if Contains(err,
47+
"the client connection is closing",
48+
"server closed the stream without sending trailers") {
49+
return net.ErrClosed
50+
}
51+
return err
52+
}
53+
54+
func WrapQUIC(err error) error {
55+
if err == nil {
56+
return nil
57+
}
58+
if Contains(err, "canceled with error code 0") {
59+
return net.ErrClosed
60+
}
61+
return err
62+
}

common/baderror/grpc.go

-26
This file was deleted.

common/baderror/h2.go

-22
This file was deleted.

transport/hysteria/wrap.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"syscall"
77

88
"github.com/sagernet/quic-go"
9+
"github.com/sagernet/sing-box/common/baderror"
910
"github.com/sagernet/sing/common"
1011
)
1112

@@ -38,6 +39,16 @@ type StreamWrapper struct {
3839
quic.Stream
3940
}
4041

42+
func (s *StreamWrapper) Read(p []byte) (n int, err error) {
43+
n, err = s.Stream.Read(p)
44+
return n, baderror.WrapQUIC(err)
45+
}
46+
47+
func (s *StreamWrapper) Write(p []byte) (n int, err error) {
48+
n, err = s.Stream.Write(p)
49+
return n, baderror.WrapQUIC(err)
50+
}
51+
4152
func (s *StreamWrapper) LocalAddr() net.Addr {
4253
return s.Conn.LocalAddr()
4354
}
@@ -50,10 +61,6 @@ func (s *StreamWrapper) Upstream() any {
5061
return s.Stream
5162
}
5263

53-
func (s *StreamWrapper) ReaderReplaceable() bool {
54-
return true
55-
}
56-
5764
func (s *StreamWrapper) WriterReplaceable() bool {
5865
return true
5966
}

0 commit comments

Comments
 (0)