Add early conn interface

This commit is contained in:
世界
2023-02-26 23:08:20 +08:00
parent 22bf7a9509
commit 5ce3ddee9b
9 changed files with 46 additions and 51 deletions

View File

@@ -26,6 +26,8 @@ const (
var CRLF = []byte{'\r', '\n'}
var _ N.EarlyConn = (*ClientConn)(nil)
type ClientConn struct {
N.ExtendedConn
key [KeyLength]byte
@@ -41,6 +43,10 @@ func NewClientConn(conn net.Conn, key [KeyLength]byte, destination M.Socksaddr)
}
}
func (c *ClientConn) NeedHandshake() bool {
return !c.headerWritten
}
func (c *ClientConn) Write(p []byte) (n int, err error) {
if c.headerWritten {
return c.ExtendedConn.Write(p)
@@ -101,6 +107,10 @@ func NewClientPacketConn(conn net.Conn, key [KeyLength]byte) *ClientPacketConn {
}
}
func (c *ClientPacketConn) NeedHandshake() bool {
return !c.headerWritten
}
func (c *ClientPacketConn) ReadPacket(buffer *buf.Buffer) (M.Socksaddr, error) {
return ReadPacket(c.Conn, buffer)
}

View File

@@ -10,6 +10,7 @@ import (
"github.com/sagernet/sing/common/buf"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/gofrs/uuid"
)
@@ -82,6 +83,8 @@ func (c *Client) DialEarlyXUDPPacketConn(conn net.Conn, destination M.Socksaddr)
return vmess.NewXUDPConn(&Conn{Conn: conn, protocolConn: conn, key: c.key, command: vmess.CommandMux, destination: destination, flow: c.flow}, destination), nil
}
var _ N.EarlyConn = (*Conn)(nil)
type Conn struct {
net.Conn
protocolConn net.Conn
@@ -93,6 +96,10 @@ type Conn struct {
responseRead bool
}
func (c *Conn) NeedHandshake() bool {
return !c.requestWritten
}
func (c *Conn) Read(b []byte) (n int, err error) {
if !c.responseRead {
err = ReadResponse(c.Conn)