chore: decrease direct depend on the sing package

This commit is contained in:
wwqgtxx
2023-01-16 10:50:31 +08:00
parent 643fdd0bce
commit 50832aab47
10 changed files with 108 additions and 73 deletions

View File

@ -7,17 +7,16 @@ import (
"io"
"net"
"github.com/Dreamacro/clash/common/buf"
N "github.com/Dreamacro/clash/common/net"
"github.com/gofrs/uuid"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
"github.com/sagernet/sing/common/network"
xtls "github.com/xtls/go"
"google.golang.org/protobuf/proto"
)
type Conn struct {
network.ExtendedConn
N.ExtendedConn
dst *DstAddr
id *uuid.UUID
addons *Addons
@ -67,30 +66,30 @@ func (vc *Conn) sendRequest() (err error) {
requestLen += len(vc.dst.Addr)
}
_buffer := buf.StackNewSize(requestLen)
defer common.KeepAlive(_buffer)
buffer := common.Dup(_buffer)
defer buf.KeepAlive(_buffer)
buffer := buf.Dup(_buffer)
defer buffer.Release()
common.Must(
buffer.WriteByte(Version), // protocol version
common.Error(buffer.Write(vc.id.Bytes())), // 16 bytes of uuid
buf.Must(
buffer.WriteByte(Version), // protocol version
buf.Error(buffer.Write(vc.id.Bytes())), // 16 bytes of uuid
buffer.WriteByte(byte(len(addonsBytes))),
common.Error(buffer.Write(addonsBytes)),
buf.Error(buffer.Write(addonsBytes)),
)
if vc.dst.Mux {
common.Must(buffer.WriteByte(CommandMux))
buf.Must(buffer.WriteByte(CommandMux))
} else {
if vc.dst.UDP {
common.Must(buffer.WriteByte(CommandUDP))
buf.Must(buffer.WriteByte(CommandUDP))
} else {
common.Must(buffer.WriteByte(CommandTCP))
buf.Must(buffer.WriteByte(CommandTCP))
}
binary.BigEndian.PutUint16(buffer.Extend(2), vc.dst.Port)
common.Must(
buf.Must(
buffer.WriteByte(vc.dst.AddrType),
common.Error(buffer.Write(vc.dst.Addr)),
buf.Error(buffer.Write(vc.dst.Addr)),
)
}
@ -124,7 +123,7 @@ func (vc *Conn) recvResponse() error {
}
func (vc *Conn) Upstream() any {
if wrapper, ok := vc.ExtendedConn.(*bufio.ExtendedConnWrapper); ok {
if wrapper, ok := vc.ExtendedConn.(*N.ExtendedConnWrapper); ok {
return wrapper.Conn
}
return vc.ExtendedConn
@ -133,7 +132,7 @@ func (vc *Conn) Upstream() any {
// newConn return a Conn instance
func newConn(conn net.Conn, client *Client, dst *DstAddr) (*Conn, error) {
c := &Conn{
ExtendedConn: bufio.NewExtendedConn(conn),
ExtendedConn: N.NewExtendedConn(conn),
id: client.uuid,
dst: dst,
}

View File

@ -19,10 +19,10 @@ import (
"time"
_ "unsafe"
"github.com/Dreamacro/clash/common/buf"
N "github.com/Dreamacro/clash/common/net"
"github.com/gorilla/websocket"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
"github.com/sagernet/sing/common/network"
)
//go:linkname maskBytes github.com/gorilla/websocket.maskBytes
@ -33,7 +33,7 @@ type websocketConn struct {
reader io.Reader
remoteAddr net.Addr
rawWriter network.ExtendedWriter
rawWriter N.ExtendedWriter
// https://godoc.org/github.com/gorilla/websocket#hdr-Concurrency
rMux sync.Mutex
@ -42,7 +42,7 @@ type websocketConn struct {
type websocketWithEarlyDataConn struct {
net.Conn
wsWriter network.ExtendedWriter
wsWriter N.ExtendedWriter
underlay net.Conn
closed bool
dialed chan bool
@ -209,7 +209,7 @@ func (wsedc *websocketWithEarlyDataConn) Dial(earlyData []byte) error {
}
wsedc.dialed <- true
wsedc.wsWriter = bufio.NewExtendedWriter(wsedc.Conn)
wsedc.wsWriter = N.NewExtendedWriter(wsedc.Conn)
if earlyDataBuf.Len() != 0 {
_, err = wsedc.Conn.Write(earlyDataBuf.Bytes())
}
@ -373,7 +373,7 @@ func streamWebsocketConn(conn net.Conn, c *WebsocketConfig, earlyData *bytes.Buf
return &websocketConn{
conn: wsConn,
rawWriter: bufio.NewExtendedWriter(wsConn.UnderlyingConn()),
rawWriter: N.NewExtendedWriter(wsConn.UnderlyingConn()),
remoteAddr: conn.RemoteAddr(),
}, nil
}