mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-11-16 11:42:43 +08:00
chore: decrease direct depend on the sing package
This commit is contained in:
parent
643fdd0bce
commit
50832aab47
|
@ -7,12 +7,11 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
|
||||||
"github.com/gofrs/uuid"
|
"github.com/gofrs/uuid"
|
||||||
"github.com/sagernet/sing/common/bufio"
|
|
||||||
"github.com/sagernet/sing/common/network"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Base struct {
|
type Base struct {
|
||||||
|
@ -169,7 +168,7 @@ func NewBase(opt BaseOption) *Base {
|
||||||
}
|
}
|
||||||
|
|
||||||
type conn struct {
|
type conn struct {
|
||||||
network.ExtendedConn
|
N.ExtendedConn
|
||||||
chain C.Chain
|
chain C.Chain
|
||||||
actualRemoteDestination string
|
actualRemoteDestination string
|
||||||
}
|
}
|
||||||
|
@ -189,14 +188,14 @@ func (c *conn) AppendToChains(a C.ProxyAdapter) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *conn) Upstream() any {
|
func (c *conn) Upstream() any {
|
||||||
if wrapper, ok := c.ExtendedConn.(*bufio.ExtendedConnWrapper); ok {
|
if wrapper, ok := c.ExtendedConn.(*N.ExtendedConnWrapper); ok {
|
||||||
return wrapper.Conn
|
return wrapper.Conn
|
||||||
}
|
}
|
||||||
return c.ExtendedConn
|
return c.ExtendedConn
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConn(c net.Conn, a C.ProxyAdapter) C.Conn {
|
func NewConn(c net.Conn, a C.ProxyAdapter) C.Conn {
|
||||||
return &conn{bufio.NewExtendedConn(c), []string{a.Name()}, parseRemoteDestination(a.Addr())}
|
return &conn{N.NewExtendedConn(c), []string{a.Name()}, parseRemoteDestination(a.Addr())}
|
||||||
}
|
}
|
||||||
|
|
||||||
type packetConn struct {
|
type packetConn struct {
|
||||||
|
|
|
@ -8,14 +8,13 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
tlsC "github.com/Dreamacro/clash/component/tls"
|
tlsC "github.com/Dreamacro/clash/component/tls"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/Dreamacro/clash/transport/gun"
|
"github.com/Dreamacro/clash/transport/gun"
|
||||||
"github.com/Dreamacro/clash/transport/trojan"
|
"github.com/Dreamacro/clash/transport/trojan"
|
||||||
"github.com/Dreamacro/clash/transport/vless"
|
"github.com/Dreamacro/clash/transport/vless"
|
||||||
|
|
||||||
"github.com/sagernet/sing/common/bufio"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Trojan struct {
|
type Trojan struct {
|
||||||
|
@ -97,7 +96,7 @@ func (t *Trojan) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error)
|
||||||
return c, err
|
return c, err
|
||||||
}
|
}
|
||||||
err = t.instance.WriteHeader(c, trojan.CommandTCP, serializesSocksAddr(metadata))
|
err = t.instance.WriteHeader(c, trojan.CommandTCP, serializesSocksAddr(metadata))
|
||||||
return bufio.NewExtendedConn(c), err
|
return N.NewExtendedConn(c), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
|
|
19
common/buf/sing.go
Normal file
19
common/buf/sing.go
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
package buf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/sagernet/sing/common"
|
||||||
|
"github.com/sagernet/sing/common/buf"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Buffer = buf.Buffer
|
||||||
|
|
||||||
|
var StackNewSize = buf.StackNewSize
|
||||||
|
var KeepAlive = common.KeepAlive
|
||||||
|
|
||||||
|
//go:norace
|
||||||
|
func Dup[T any](obj T) T {
|
||||||
|
return common.Dup(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
var Must = common.Must
|
||||||
|
var Error = common.Error
|
|
@ -4,23 +4,21 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/sagernet/sing/common/buf"
|
"github.com/Dreamacro/clash/common/buf"
|
||||||
sing_bufio "github.com/sagernet/sing/common/bufio"
|
|
||||||
"github.com/sagernet/sing/common/network"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ network.ExtendedConn = (*BufferedConn)(nil)
|
var _ ExtendedConn = (*BufferedConn)(nil)
|
||||||
|
|
||||||
type BufferedConn struct {
|
type BufferedConn struct {
|
||||||
r *bufio.Reader
|
r *bufio.Reader
|
||||||
network.ExtendedConn
|
ExtendedConn
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBufferedConn(c net.Conn) *BufferedConn {
|
func NewBufferedConn(c net.Conn) *BufferedConn {
|
||||||
if bc, ok := c.(*BufferedConn); ok {
|
if bc, ok := c.(*BufferedConn); ok {
|
||||||
return bc
|
return bc
|
||||||
}
|
}
|
||||||
return &BufferedConn{bufio.NewReader(c), sing_bufio.NewExtendedConn(c)}
|
return &BufferedConn{bufio.NewReader(c), NewExtendedConn(c)}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reader returns the internal bufio.Reader.
|
// Reader returns the internal bufio.Reader.
|
||||||
|
@ -58,7 +56,7 @@ func (c *BufferedConn) ReadBuffer(buffer *buf.Buffer) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *BufferedConn) Upstream() any {
|
func (c *BufferedConn) Upstream() any {
|
||||||
if wrapper, ok := c.ExtendedConn.(*sing_bufio.ExtendedConnWrapper); ok {
|
if wrapper, ok := c.ExtendedConn.(*ExtendedConnWrapper); ok {
|
||||||
return wrapper.Conn
|
return wrapper.Conn
|
||||||
}
|
}
|
||||||
return c.ExtendedConn
|
return c.ExtendedConn
|
||||||
|
|
|
@ -1,24 +1,24 @@
|
||||||
package net
|
package net
|
||||||
|
|
||||||
import (
|
//import (
|
||||||
"io"
|
// "io"
|
||||||
"net"
|
// "net"
|
||||||
"time"
|
// "time"
|
||||||
)
|
//)
|
||||||
|
//
|
||||||
// Relay copies between left and right bidirectionally.
|
//// Relay copies between left and right bidirectionally.
|
||||||
func Relay(leftConn, rightConn net.Conn) {
|
//func Relay(leftConn, rightConn net.Conn) {
|
||||||
ch := make(chan error)
|
// ch := make(chan error)
|
||||||
|
//
|
||||||
go func() {
|
// go func() {
|
||||||
// Wrapping to avoid using *net.TCPConn.(ReadFrom)
|
// // Wrapping to avoid using *net.TCPConn.(ReadFrom)
|
||||||
// See also https://github.com/Dreamacro/clash/pull/1209
|
// // See also https://github.com/Dreamacro/clash/pull/1209
|
||||||
_, err := io.Copy(WriteOnlyWriter{Writer: leftConn}, ReadOnlyReader{Reader: rightConn})
|
// _, err := io.Copy(WriteOnlyWriter{Writer: leftConn}, ReadOnlyReader{Reader: rightConn})
|
||||||
leftConn.SetReadDeadline(time.Now())
|
// leftConn.SetReadDeadline(time.Now())
|
||||||
ch <- err
|
// ch <- err
|
||||||
}()
|
// }()
|
||||||
|
//
|
||||||
_, _ = io.Copy(WriteOnlyWriter{Writer: rightConn}, ReadOnlyReader{Reader: leftConn})
|
// _, _ = io.Copy(WriteOnlyWriter{Writer: rightConn}, ReadOnlyReader{Reader: leftConn})
|
||||||
rightConn.SetReadDeadline(time.Now())
|
// rightConn.SetReadDeadline(time.Now())
|
||||||
<-ch
|
// <-ch
|
||||||
}
|
//}
|
||||||
|
|
24
common/net/sing.go
Normal file
24
common/net/sing.go
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
package net
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/sagernet/sing/common/bufio"
|
||||||
|
"github.com/sagernet/sing/common/network"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ExtendedConnWrapper = bufio.ExtendedConnWrapper
|
||||||
|
|
||||||
|
var NewExtendedConn = bufio.NewExtendedConn
|
||||||
|
var NewExtendedWriter = bufio.NewExtendedWriter
|
||||||
|
var NewExtendedReader = bufio.NewExtendedReader
|
||||||
|
|
||||||
|
type ExtendedConn = network.ExtendedConn
|
||||||
|
type ExtendedWriter = network.ExtendedWriter
|
||||||
|
type ExtendedReader = network.ExtendedReader
|
||||||
|
|
||||||
|
// Relay copies between left and right bidirectionally.
|
||||||
|
func Relay(leftConn, rightConn net.Conn) {
|
||||||
|
_ = bufio.CopyConn(context.TODO(), leftConn, rightConn)
|
||||||
|
}
|
|
@ -7,17 +7,16 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
|
"github.com/Dreamacro/clash/common/buf"
|
||||||
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
|
|
||||||
"github.com/gofrs/uuid"
|
"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"
|
xtls "github.com/xtls/go"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Conn struct {
|
type Conn struct {
|
||||||
network.ExtendedConn
|
N.ExtendedConn
|
||||||
dst *DstAddr
|
dst *DstAddr
|
||||||
id *uuid.UUID
|
id *uuid.UUID
|
||||||
addons *Addons
|
addons *Addons
|
||||||
|
@ -67,30 +66,30 @@ func (vc *Conn) sendRequest() (err error) {
|
||||||
requestLen += len(vc.dst.Addr)
|
requestLen += len(vc.dst.Addr)
|
||||||
}
|
}
|
||||||
_buffer := buf.StackNewSize(requestLen)
|
_buffer := buf.StackNewSize(requestLen)
|
||||||
defer common.KeepAlive(_buffer)
|
defer buf.KeepAlive(_buffer)
|
||||||
buffer := common.Dup(_buffer)
|
buffer := buf.Dup(_buffer)
|
||||||
defer buffer.Release()
|
defer buffer.Release()
|
||||||
|
|
||||||
common.Must(
|
buf.Must(
|
||||||
buffer.WriteByte(Version), // protocol version
|
buffer.WriteByte(Version), // protocol version
|
||||||
common.Error(buffer.Write(vc.id.Bytes())), // 16 bytes of uuid
|
buf.Error(buffer.Write(vc.id.Bytes())), // 16 bytes of uuid
|
||||||
buffer.WriteByte(byte(len(addonsBytes))),
|
buffer.WriteByte(byte(len(addonsBytes))),
|
||||||
common.Error(buffer.Write(addonsBytes)),
|
buf.Error(buffer.Write(addonsBytes)),
|
||||||
)
|
)
|
||||||
|
|
||||||
if vc.dst.Mux {
|
if vc.dst.Mux {
|
||||||
common.Must(buffer.WriteByte(CommandMux))
|
buf.Must(buffer.WriteByte(CommandMux))
|
||||||
} else {
|
} else {
|
||||||
if vc.dst.UDP {
|
if vc.dst.UDP {
|
||||||
common.Must(buffer.WriteByte(CommandUDP))
|
buf.Must(buffer.WriteByte(CommandUDP))
|
||||||
} else {
|
} else {
|
||||||
common.Must(buffer.WriteByte(CommandTCP))
|
buf.Must(buffer.WriteByte(CommandTCP))
|
||||||
}
|
}
|
||||||
|
|
||||||
binary.BigEndian.PutUint16(buffer.Extend(2), vc.dst.Port)
|
binary.BigEndian.PutUint16(buffer.Extend(2), vc.dst.Port)
|
||||||
common.Must(
|
buf.Must(
|
||||||
buffer.WriteByte(vc.dst.AddrType),
|
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 {
|
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 wrapper.Conn
|
||||||
}
|
}
|
||||||
return vc.ExtendedConn
|
return vc.ExtendedConn
|
||||||
|
@ -133,7 +132,7 @@ func (vc *Conn) Upstream() any {
|
||||||
// newConn return a Conn instance
|
// newConn return a Conn instance
|
||||||
func newConn(conn net.Conn, client *Client, dst *DstAddr) (*Conn, error) {
|
func newConn(conn net.Conn, client *Client, dst *DstAddr) (*Conn, error) {
|
||||||
c := &Conn{
|
c := &Conn{
|
||||||
ExtendedConn: bufio.NewExtendedConn(conn),
|
ExtendedConn: N.NewExtendedConn(conn),
|
||||||
id: client.uuid,
|
id: client.uuid,
|
||||||
dst: dst,
|
dst: dst,
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,10 +19,10 @@ import (
|
||||||
"time"
|
"time"
|
||||||
_ "unsafe"
|
_ "unsafe"
|
||||||
|
|
||||||
|
"github.com/Dreamacro/clash/common/buf"
|
||||||
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"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
|
//go:linkname maskBytes github.com/gorilla/websocket.maskBytes
|
||||||
|
@ -33,7 +33,7 @@ type websocketConn struct {
|
||||||
reader io.Reader
|
reader io.Reader
|
||||||
remoteAddr net.Addr
|
remoteAddr net.Addr
|
||||||
|
|
||||||
rawWriter network.ExtendedWriter
|
rawWriter N.ExtendedWriter
|
||||||
|
|
||||||
// https://godoc.org/github.com/gorilla/websocket#hdr-Concurrency
|
// https://godoc.org/github.com/gorilla/websocket#hdr-Concurrency
|
||||||
rMux sync.Mutex
|
rMux sync.Mutex
|
||||||
|
@ -42,7 +42,7 @@ type websocketConn struct {
|
||||||
|
|
||||||
type websocketWithEarlyDataConn struct {
|
type websocketWithEarlyDataConn struct {
|
||||||
net.Conn
|
net.Conn
|
||||||
wsWriter network.ExtendedWriter
|
wsWriter N.ExtendedWriter
|
||||||
underlay net.Conn
|
underlay net.Conn
|
||||||
closed bool
|
closed bool
|
||||||
dialed chan bool
|
dialed chan bool
|
||||||
|
@ -209,7 +209,7 @@ func (wsedc *websocketWithEarlyDataConn) Dial(earlyData []byte) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
wsedc.dialed <- true
|
wsedc.dialed <- true
|
||||||
wsedc.wsWriter = bufio.NewExtendedWriter(wsedc.Conn)
|
wsedc.wsWriter = N.NewExtendedWriter(wsedc.Conn)
|
||||||
if earlyDataBuf.Len() != 0 {
|
if earlyDataBuf.Len() != 0 {
|
||||||
_, err = wsedc.Conn.Write(earlyDataBuf.Bytes())
|
_, err = wsedc.Conn.Write(earlyDataBuf.Bytes())
|
||||||
}
|
}
|
||||||
|
@ -373,7 +373,7 @@ func streamWebsocketConn(conn net.Conn, c *WebsocketConfig, earlyData *bytes.Buf
|
||||||
|
|
||||||
return &websocketConn{
|
return &websocketConn{
|
||||||
conn: wsConn,
|
conn: wsConn,
|
||||||
rawWriter: bufio.NewExtendedWriter(wsConn.UnderlyingConn()),
|
rawWriter: N.NewExtendedWriter(wsConn.UnderlyingConn()),
|
||||||
remoteAddr: conn.RemoteAddr(),
|
remoteAddr: conn.RemoteAddr(),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,14 @@
|
||||||
package tunnel
|
package tunnel
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"errors"
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/Dreamacro/clash/common/pool"
|
"github.com/Dreamacro/clash/common/pool"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
|
||||||
"github.com/sagernet/sing/common/bufio"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func handleUDPToRemote(packet C.UDPPacket, pc C.PacketConn, metadata *C.Metadata) error {
|
func handleUDPToRemote(packet C.UDPPacket, pc C.PacketConn, metadata *C.Metadata) error {
|
||||||
|
@ -62,5 +60,5 @@ func handleUDPToLocal(packet C.UDPPacket, pc net.PacketConn, key string, oAddr,
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleSocket(ctx C.ConnContext, outbound net.Conn) {
|
func handleSocket(ctx C.ConnContext, outbound net.Conn) {
|
||||||
bufio.CopyConn(context.TODO(), ctx.Conn(), outbound)
|
N.Relay(ctx.Conn(), outbound)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,12 +4,11 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/Dreamacro/clash/common/buf"
|
||||||
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
|
||||||
"github.com/gofrs/uuid"
|
"github.com/gofrs/uuid"
|
||||||
"github.com/sagernet/sing/common/buf"
|
|
||||||
"github.com/sagernet/sing/common/bufio"
|
|
||||||
"github.com/sagernet/sing/common/network"
|
|
||||||
"go.uber.org/atomic"
|
"go.uber.org/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -33,8 +32,8 @@ type tcpTracker struct {
|
||||||
C.Conn `json:"-"`
|
C.Conn `json:"-"`
|
||||||
*trackerInfo
|
*trackerInfo
|
||||||
manager *Manager
|
manager *Manager
|
||||||
extendedReader network.ExtendedReader
|
extendedReader N.ExtendedReader
|
||||||
extendedWriter network.ExtendedWriter
|
extendedWriter N.ExtendedWriter
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tt *tcpTracker) ID() string {
|
func (tt *tcpTracker) ID() string {
|
||||||
|
@ -107,8 +106,8 @@ func NewTCPTracker(conn C.Conn, manager *Manager, metadata *C.Metadata, rule C.R
|
||||||
UploadTotal: atomic.NewInt64(0),
|
UploadTotal: atomic.NewInt64(0),
|
||||||
DownloadTotal: atomic.NewInt64(0),
|
DownloadTotal: atomic.NewInt64(0),
|
||||||
},
|
},
|
||||||
extendedReader: bufio.NewExtendedReader(conn),
|
extendedReader: N.NewExtendedReader(conn),
|
||||||
extendedWriter: bufio.NewExtendedWriter(conn),
|
extendedWriter: N.NewExtendedWriter(conn),
|
||||||
}
|
}
|
||||||
|
|
||||||
if rule != nil {
|
if rule != nil {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user