Add grpc compatibility test

This commit is contained in:
世界
2022-08-29 10:10:41 +08:00
parent 665c84ee42
commit d440a01792
20 changed files with 458 additions and 154 deletions

View File

@@ -2,12 +2,11 @@ package v2raygrpc
import (
"context"
"io"
"net"
"os"
"strings"
"time"
"github.com/sagernet/sing-box/common/baderror"
"github.com/sagernet/sing/common/rw"
)
@@ -36,7 +35,7 @@ func (c *GRPCConn) Read(b []byte) (n int, err error) {
return
}
hunk, err := c.Recv()
err = wrapError(err)
err = baderror.WrapGRPC(err)
if err != nil {
return
}
@@ -48,7 +47,7 @@ func (c *GRPCConn) Read(b []byte) (n int, err error) {
}
func (c *GRPCConn) Write(b []byte) (n int, err error) {
err = wrapError(c.Send(&Hunk{Data: b}))
err = baderror.WrapGRPC(c.Send(&Hunk{Data: b}))
if err != nil {
return
}
@@ -93,20 +92,3 @@ type clientConnWrapper struct {
func (c *clientConnWrapper) CloseWrite() error {
return c.CloseSend()
}
func wrapError(err error) error {
// grpc uses stupid internal error types
if err == nil {
return nil
}
if strings.Contains(err.Error(), "EOF") {
return io.EOF
}
if strings.Contains(err.Error(), "the client connection is closing") {
return net.ErrClosed
}
if strings.Contains(err.Error(), "server closed the stream without sending trailers") {
return net.ErrClosed
}
return err
}

View File

@@ -10,10 +10,10 @@ import (
"os"
"time"
"github.com/sagernet/sing-box/common/baderror"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/rw"
)
@@ -53,7 +53,7 @@ func (c *GunConn) setup(reader io.Reader, err error) {
func (c *GunConn) Read(b []byte) (n int, err error) {
n, err = c.read(b)
return n, wrapError(err)
return n, baderror.WrapH2(err)
}
func (c *GunConn) read(b []byte) (n int, err error) {
@@ -105,7 +105,7 @@ func (c *GunConn) Write(b []byte) (n int, err error) {
if c.flusher != nil {
c.flusher.Flush()
}
return len(b), wrapError(err)
return len(b), baderror.WrapH2(err)
}
func uLen(x uint64) int {
@@ -129,7 +129,7 @@ func (c *GunConn) WriteBuffer(buffer *buf.Buffer) error {
if c.flusher != nil {
c.flusher.Flush()
}
return wrapError(err)
return baderror.WrapH2(err)
}
func (c *GunConn) FrontHeadroom() int {
@@ -159,10 +159,3 @@ func (c *GunConn) SetReadDeadline(t time.Time) error {
func (c *GunConn) SetWriteDeadline(t time.Time) error {
return os.ErrInvalid
}
func wrapError(err error) error {
if E.IsMulti(err, io.ErrUnexpectedEOF) {
return io.EOF
}
return err
}

View File

@@ -7,8 +7,8 @@ import (
"os"
"time"
"github.com/sagernet/sing-box/common/baderror"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
)
type HTTPConn struct {
@@ -18,12 +18,12 @@ type HTTPConn struct {
func (c *HTTPConn) Read(b []byte) (n int, err error) {
n, err = c.reader.Read(b)
return n, wrapError(err)
return n, baderror.WrapH2(err)
}
func (c *HTTPConn) Write(b []byte) (n int, err error) {
n, err = c.writer.Write(b)
return n, wrapError(err)
return n, baderror.WrapH2(err)
}
func (c *HTTPConn) Close() error {
@@ -62,10 +62,3 @@ func (c *ServerHTTPConn) Write(b []byte) (n int, err error) {
}
return
}
func wrapError(err error) error {
if E.IsMulti(err, io.ErrUnexpectedEOF) {
return io.EOF
}
return err
}