remove nodelay option for tunnel

This commit is contained in:
ginuerzh
2023-11-16 20:36:17 +08:00
parent 9584bdbf4c
commit f5a20fd0fc
5 changed files with 35 additions and 96 deletions

View File

@ -1,67 +1,24 @@
package tunnel
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"math"
"net"
"sync"
"github.com/go-gost/core/common/bufpool"
mdata "github.com/go-gost/core/metadata"
"github.com/go-gost/relay"
xrelay "github.com/go-gost/x/internal/util/relay"
)
type tcpConn struct {
net.Conn
wbuf *bytes.Buffer
once sync.Once
}
func (c *tcpConn) Read(b []byte) (n int, err error) {
c.once.Do(func() {
if c.wbuf != nil {
err = readResponse(c.Conn)
}
})
if err != nil {
return
}
return c.Conn.Read(b)
}
func (c *tcpConn) Write(b []byte) (n int, err error) {
n = len(b) // force byte length consistent
if c.wbuf != nil && c.wbuf.Len() > 0 {
c.wbuf.Write(b) // append the data to the cached header
_, err = c.Conn.Write(c.wbuf.Bytes())
c.wbuf.Reset()
return
}
_, err = c.Conn.Write(b)
return
}
type udpConn struct {
net.Conn
wbuf *bytes.Buffer
once sync.Once
}
func (c *udpConn) Read(b []byte) (n int, err error) {
c.once.Do(func() {
if c.wbuf != nil {
err = readResponse(c.Conn)
}
})
if err != nil {
return
}
var bb [2]byte
_, err = io.ReadFull(c.Conn, bb[:])
if err != nil {
@ -88,14 +45,6 @@ func (c *udpConn) Write(b []byte) (n int, err error) {
}
n = len(b)
if c.wbuf != nil && c.wbuf.Len() > 0 {
var bb [2]byte
binary.BigEndian.PutUint16(bb[:], uint16(len(b)))
c.wbuf.Write(bb[:])
c.wbuf.Write(b) // append the data to the cached header
_, err = c.wbuf.WriteTo(c.Conn)
return
}
var bb [2]byte
binary.BigEndian.PutUint16(bb[:], uint16(len(b)))
@ -119,7 +68,7 @@ func readResponse(r io.Reader) (err error) {
}
if resp.Status != relay.StatusOK {
err = fmt.Errorf("status %d", resp.Status)
err = fmt.Errorf("%d %s", resp.Status, xrelay.StatusText(resp.Status))
return
}
return nil

View File

@ -1,7 +1,6 @@
package tunnel
import (
"bytes"
"context"
"fmt"
"net"
@ -90,39 +89,20 @@ func (c *tunnelConnector) Connect(ctx context.Context, conn net.Conn, network, a
ID: c.md.tunnelID.ID(),
})
if c.md.noDelay {
if _, err := req.WriteTo(conn); err != nil {
return nil, err
}
// drain the response
if err := readResponse(conn); err != nil {
return nil, err
}
if _, err := req.WriteTo(conn); err != nil {
return nil, err
}
// drain the response
if err := readResponse(conn); err != nil {
return nil, err
}
switch network {
case "tcp", "tcp4", "tcp6":
if !c.md.noDelay {
cc := &tcpConn{
Conn: conn,
wbuf: &bytes.Buffer{},
}
if _, err := req.WriteTo(cc.wbuf); err != nil {
return nil, err
}
conn = cc
}
case "udp", "udp4", "udp6":
cc := &udpConn{
conn = &udpConn{
Conn: conn,
}
if !c.md.noDelay {
cc.wbuf = &bytes.Buffer{}
if _, err := req.WriteTo(cc.wbuf); err != nil {
return nil, err
}
}
conn = cc
default:
err := fmt.Errorf("network %s is unsupported", network)
log.Error(err)

View File

@ -18,13 +18,11 @@ var (
type metadata struct {
connectTimeout time.Duration
tunnelID relay.TunnelID
noDelay bool
muxCfg *mux.Config
}
func (c *tunnelConnector) parseMetadata(md mdata.Metadata) (err error) {
c.md.connectTimeout = mdutil.GetDuration(md, "connectTimeout")
c.md.noDelay = mdutil.GetBool(md, "nodelay")
if s := mdutil.GetString(md, "tunnelID", "tunnel.id"); s != "" {
uuid, err := uuid.Parse(s)