Skip to content

Commit

Permalink
Fix hysteria stream error
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Sep 7, 2022
1 parent 7d83e35 commit 4b61d6e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 53 deletions.
52 changes: 51 additions & 1 deletion common/baderror/baderror.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}
26 changes: 0 additions & 26 deletions common/baderror/grpc.go

This file was deleted.

22 changes: 0 additions & 22 deletions common/baderror/h2.go

This file was deleted.

15 changes: 11 additions & 4 deletions transport/hysteria/wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"syscall"

"github.com/sagernet/quic-go"
"github.com/sagernet/sing-box/common/baderror"
"github.com/sagernet/sing/common"
)

Expand Down Expand Up @@ -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()
}
Expand All @@ -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
}
Expand Down

0 comments on commit 4b61d6e

Please sign in to comment.