mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-24 17:41:29 +00:00
Fix grpc lite transport encoding
This commit is contained in:
parent
87c6fd4c0f
commit
c506255e0f
|
@ -2,19 +2,16 @@ package v2raygrpclite
|
||||||
|
|
||||||
import (
|
import (
|
||||||
std_bufio "bufio"
|
std_bufio "bufio"
|
||||||
"bytes"
|
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/sagernet/sing/common"
|
"github.com/sagernet/sing/common"
|
||||||
"github.com/sagernet/sing/common/baderror"
|
"github.com/sagernet/sing/common/baderror"
|
||||||
"github.com/sagernet/sing/common/buf"
|
"github.com/sagernet/sing/common/buf"
|
||||||
"github.com/sagernet/sing/common/bufio"
|
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
"github.com/sagernet/sing/common/rw"
|
"github.com/sagernet/sing/common/rw"
|
||||||
)
|
)
|
||||||
|
@ -30,7 +27,6 @@ type GunConn struct {
|
||||||
create chan struct{}
|
create chan struct{}
|
||||||
err error
|
err error
|
||||||
readRemaining int
|
readRemaining int
|
||||||
writeAccess sync.Mutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newGunConn(reader io.Reader, writer io.Writer, flusher http.Flusher) *GunConn {
|
func newGunConn(reader io.Reader, writer io.Writer, flusher http.Flusher) *GunConn {
|
||||||
|
@ -100,19 +96,22 @@ func (c *GunConn) read(b []byte) (n int, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *GunConn) Write(b []byte) (n int, err error) {
|
func (c *GunConn) Write(b []byte) (n int, err error) {
|
||||||
protobufHeader := [1 + binary.MaxVarintLen64]byte{0x0A}
|
varLen := rw.UVariantLen(uint64(len(b)))
|
||||||
varuintLen := binary.PutUvarint(protobufHeader[1:], uint64(len(b)))
|
buffer := buf.NewSize(6 + varLen + len(b))
|
||||||
grpcHeader := buf.Get(5)
|
header := buffer.Extend(6 + varLen)
|
||||||
grpcPayloadLen := uint32(1 + varuintLen + len(b))
|
header[0] = 0x00
|
||||||
binary.BigEndian.PutUint32(grpcHeader[1:5], grpcPayloadLen)
|
binary.BigEndian.PutUint32(header[1:5], uint32(1+varLen+len(b)))
|
||||||
c.writeAccess.Lock()
|
header[5] = 0x0A
|
||||||
_, err = bufio.Copy(c.writer, io.MultiReader(bytes.NewReader(grpcHeader), bytes.NewReader(protobufHeader[:varuintLen+1]), bytes.NewReader(b)))
|
binary.PutUvarint(header[6:], uint64(len(b)))
|
||||||
c.writeAccess.Unlock()
|
common.Must1(buffer.Write(b))
|
||||||
buf.Put(grpcHeader)
|
_, err = c.writer.Write(buffer.Bytes())
|
||||||
if err == nil && c.flusher != nil {
|
if err != nil {
|
||||||
|
return 0, baderror.WrapH2(err)
|
||||||
|
}
|
||||||
|
if c.flusher != nil {
|
||||||
c.flusher.Flush()
|
c.flusher.Flush()
|
||||||
}
|
}
|
||||||
return len(b), baderror.WrapH2(err)
|
return len(b), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *GunConn) WriteBuffer(buffer *buf.Buffer) error {
|
func (c *GunConn) WriteBuffer(buffer *buf.Buffer) error {
|
||||||
|
@ -120,16 +119,18 @@ func (c *GunConn) WriteBuffer(buffer *buf.Buffer) error {
|
||||||
dataLen := buffer.Len()
|
dataLen := buffer.Len()
|
||||||
varLen := rw.UVariantLen(uint64(dataLen))
|
varLen := rw.UVariantLen(uint64(dataLen))
|
||||||
header := buffer.ExtendHeader(6 + varLen)
|
header := buffer.ExtendHeader(6 + varLen)
|
||||||
_ = header[6]
|
|
||||||
header[0] = 0x00
|
header[0] = 0x00
|
||||||
binary.BigEndian.PutUint32(header[1:5], uint32(1+varLen+dataLen))
|
binary.BigEndian.PutUint32(header[1:5], uint32(1+varLen+dataLen))
|
||||||
header[5] = 0x0A
|
header[5] = 0x0A
|
||||||
binary.PutUvarint(header[6:], uint64(dataLen))
|
binary.PutUvarint(header[6:], uint64(dataLen))
|
||||||
err := rw.WriteBytes(c.writer, buffer.Bytes())
|
err := rw.WriteBytes(c.writer, buffer.Bytes())
|
||||||
if err == nil && c.flusher != nil {
|
if err != nil {
|
||||||
|
return baderror.WrapH2(err)
|
||||||
|
}
|
||||||
|
if c.flusher != nil {
|
||||||
c.flusher.Flush()
|
c.flusher.Flush()
|
||||||
}
|
}
|
||||||
return baderror.WrapH2(err)
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *GunConn) FrontHeadroom() int {
|
func (c *GunConn) FrontHeadroom() int {
|
||||||
|
|
Loading…
Reference in a new issue