mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-11-16 19:56:51 +08:00
chore: add WaitReadFrom support in ssr
This commit is contained in:
parent
66ca57a787
commit
755f7c27b8
|
@ -19,7 +19,6 @@ import (
|
||||||
"github.com/metacubex/quic-go/congestion"
|
"github.com/metacubex/quic-go/congestion"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
|
|
||||||
N "github.com/Dreamacro/clash/common/net"
|
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
"github.com/Dreamacro/clash/component/proxydialer"
|
"github.com/Dreamacro/clash/component/proxydialer"
|
||||||
tlsC "github.com/Dreamacro/clash/component/tls"
|
tlsC "github.com/Dreamacro/clash/component/tls"
|
||||||
|
@ -325,7 +324,6 @@ func (c *hyPacketConn) WaitReadFrom() (data []byte, put func(), addr net.Addr, e
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
data = b
|
data = b
|
||||||
put = N.NilPut
|
|
||||||
addr = M.ParseSocksaddr(addrStr).UDPAddr()
|
addr = M.ParseSocksaddr(addrStr).UDPAddr()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,6 @@ import (
|
||||||
"github.com/Dreamacro/clash/transport/restls"
|
"github.com/Dreamacro/clash/transport/restls"
|
||||||
obfs "github.com/Dreamacro/clash/transport/simple-obfs"
|
obfs "github.com/Dreamacro/clash/transport/simple-obfs"
|
||||||
shadowtls "github.com/Dreamacro/clash/transport/sing-shadowtls"
|
shadowtls "github.com/Dreamacro/clash/transport/sing-shadowtls"
|
||||||
"github.com/Dreamacro/clash/transport/socks5"
|
|
||||||
v2rayObfs "github.com/Dreamacro/clash/transport/v2ray-plugin"
|
v2rayObfs "github.com/Dreamacro/clash/transport/v2ray-plugin"
|
||||||
|
|
||||||
restlsC "github.com/3andne/restls-client-go"
|
restlsC "github.com/3andne/restls-client-go"
|
||||||
|
@ -330,36 +329,3 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
||||||
restlsConfig: restlsConfig,
|
restlsConfig: restlsConfig,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type ssPacketConn struct {
|
|
||||||
net.PacketConn
|
|
||||||
rAddr net.Addr
|
|
||||||
}
|
|
||||||
|
|
||||||
func (spc *ssPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
|
|
||||||
packet, err := socks5.EncodeUDPPacket(socks5.ParseAddrToSocksAddr(addr), b)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return spc.PacketConn.WriteTo(packet[3:], spc.rAddr)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (spc *ssPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
|
||||||
n, _, e := spc.PacketConn.ReadFrom(b)
|
|
||||||
if e != nil {
|
|
||||||
return 0, nil, e
|
|
||||||
}
|
|
||||||
|
|
||||||
addr := socks5.SplitAddr(b[:n])
|
|
||||||
if addr == nil {
|
|
||||||
return 0, nil, errors.New("parse addr error")
|
|
||||||
}
|
|
||||||
|
|
||||||
udpAddr := addr.UDPAddr()
|
|
||||||
if udpAddr == nil {
|
|
||||||
return 0, nil, errors.New("parse addr error")
|
|
||||||
}
|
|
||||||
|
|
||||||
copy(b, b[len(addr):])
|
|
||||||
return n - len(addr), udpAddr, e
|
|
||||||
}
|
|
||||||
|
|
|
@ -2,16 +2,19 @@ package outbound
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
"github.com/Dreamacro/clash/component/proxydialer"
|
"github.com/Dreamacro/clash/component/proxydialer"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/Dreamacro/clash/transport/shadowsocks/core"
|
"github.com/Dreamacro/clash/transport/shadowsocks/core"
|
||||||
"github.com/Dreamacro/clash/transport/shadowsocks/shadowaead"
|
"github.com/Dreamacro/clash/transport/shadowsocks/shadowaead"
|
||||||
"github.com/Dreamacro/clash/transport/shadowsocks/shadowstream"
|
"github.com/Dreamacro/clash/transport/shadowsocks/shadowstream"
|
||||||
|
"github.com/Dreamacro/clash/transport/socks5"
|
||||||
"github.com/Dreamacro/clash/transport/ssr/obfs"
|
"github.com/Dreamacro/clash/transport/ssr/obfs"
|
||||||
"github.com/Dreamacro/clash/transport/ssr/protocol"
|
"github.com/Dreamacro/clash/transport/ssr/protocol"
|
||||||
)
|
)
|
||||||
|
@ -110,9 +113,9 @@ func (ssr *ShadowSocksR) ListenPacketWithDialer(ctx context.Context, dialer C.Di
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
pc = ssr.cipher.PacketConn(pc)
|
epc := ssr.cipher.PacketConn(N.NewEnhancePacketConn(pc))
|
||||||
pc = ssr.protocol.PacketConn(pc)
|
epc = ssr.protocol.PacketConn(epc)
|
||||||
return newPacketConn(&ssPacketConn{PacketConn: pc, rAddr: addr}, ssr), nil
|
return newPacketConn(&ssrPacketConn{EnhancePacketConn: epc, rAddr: addr}, ssr), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SupportWithDialer implements C.ProxyAdapter
|
// SupportWithDialer implements C.ProxyAdapter
|
||||||
|
@ -188,3 +191,62 @@ func NewShadowSocksR(option ShadowSocksROption) (*ShadowSocksR, error) {
|
||||||
protocol: protocol,
|
protocol: protocol,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ssrPacketConn struct {
|
||||||
|
N.EnhancePacketConn
|
||||||
|
rAddr net.Addr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (spc *ssrPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
|
||||||
|
packet, err := socks5.EncodeUDPPacket(socks5.ParseAddrToSocksAddr(addr), b)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return spc.EnhancePacketConn.WriteTo(packet[3:], spc.rAddr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (spc *ssrPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
||||||
|
n, _, e := spc.EnhancePacketConn.ReadFrom(b)
|
||||||
|
if e != nil {
|
||||||
|
return 0, nil, e
|
||||||
|
}
|
||||||
|
|
||||||
|
addr := socks5.SplitAddr(b[:n])
|
||||||
|
if addr == nil {
|
||||||
|
return 0, nil, errors.New("parse addr error")
|
||||||
|
}
|
||||||
|
|
||||||
|
udpAddr := addr.UDPAddr()
|
||||||
|
if udpAddr == nil {
|
||||||
|
return 0, nil, errors.New("parse addr error")
|
||||||
|
}
|
||||||
|
|
||||||
|
copy(b, b[len(addr):])
|
||||||
|
return n - len(addr), udpAddr, e
|
||||||
|
}
|
||||||
|
|
||||||
|
func (spc *ssrPacketConn) WaitReadFrom() (data []byte, put func(), addr net.Addr, err error) {
|
||||||
|
data, put, _, err = spc.EnhancePacketConn.WaitReadFrom()
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
_addr := socks5.SplitAddr(data)
|
||||||
|
if _addr == nil {
|
||||||
|
if put != nil {
|
||||||
|
put()
|
||||||
|
}
|
||||||
|
return nil, nil, nil, errors.New("parse addr error")
|
||||||
|
}
|
||||||
|
|
||||||
|
addr = _addr.UDPAddr()
|
||||||
|
if addr == nil {
|
||||||
|
if put != nil {
|
||||||
|
put()
|
||||||
|
}
|
||||||
|
return nil, nil, nil, errors.New("parse addr error")
|
||||||
|
}
|
||||||
|
|
||||||
|
data = data[len(_addr):]
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
type EnhancePacketConn = packet.EnhancePacketConn
|
type EnhancePacketConn = packet.EnhancePacketConn
|
||||||
type WaitReadFrom = packet.WaitReadFrom
|
type WaitReadFrom = packet.WaitReadFrom
|
||||||
|
|
||||||
var NilPut = packet.NilPut
|
|
||||||
var NewEnhancePacketConn = packet.NewEnhancePacketConn
|
var NewEnhancePacketConn = packet.NewEnhancePacketConn
|
||||||
var NewThreadSafePacketConn = packet.NewThreadSafePacketConn
|
var NewThreadSafePacketConn = packet.NewThreadSafePacketConn
|
||||||
var NewRefPacketConn = packet.NewRefPacketConn
|
var NewRefPacketConn = packet.NewRefPacketConn
|
||||||
|
|
|
@ -10,8 +10,6 @@ type WaitReadFrom interface {
|
||||||
WaitReadFrom() (data []byte, put func(), addr net.Addr, err error)
|
WaitReadFrom() (data []byte, put func(), addr net.Addr, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NilPut() {}
|
|
||||||
|
|
||||||
type EnhancePacketConn interface {
|
type EnhancePacketConn interface {
|
||||||
net.PacketConn
|
net.PacketConn
|
||||||
WaitReadFrom
|
WaitReadFrom
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -22,7 +22,7 @@ require (
|
||||||
github.com/mdlayher/netlink v1.7.2
|
github.com/mdlayher/netlink v1.7.2
|
||||||
github.com/metacubex/quic-go v0.33.3-0.20230510010206-687b537b6a58
|
github.com/metacubex/quic-go v0.33.3-0.20230510010206-687b537b6a58
|
||||||
github.com/metacubex/sing-shadowsocks v0.2.2-0.20230509230448-a5157cc00a1c
|
github.com/metacubex/sing-shadowsocks v0.2.2-0.20230509230448-a5157cc00a1c
|
||||||
github.com/metacubex/sing-shadowsocks2 v0.0.0-20230519030442-556ef530768f
|
github.com/metacubex/sing-shadowsocks2 v0.0.0-20230528144023-05418c94ed2d
|
||||||
github.com/metacubex/sing-tun v0.1.5-0.20230509224930-30065d4b6376
|
github.com/metacubex/sing-tun v0.1.5-0.20230509224930-30065d4b6376
|
||||||
github.com/metacubex/sing-wireguard v0.0.0-20230426030325-41db09ae771a
|
github.com/metacubex/sing-wireguard v0.0.0-20230426030325-41db09ae771a
|
||||||
github.com/miekg/dns v1.1.54
|
github.com/miekg/dns v1.1.54
|
||||||
|
|
4
go.sum
4
go.sum
|
@ -100,8 +100,8 @@ github.com/metacubex/sing v0.0.0-20230526162852-6afe73474070 h1:AT/Qfe9MvCxyrI9u
|
||||||
github.com/metacubex/sing v0.0.0-20230526162852-6afe73474070/go.mod h1:Ta8nHnDLAwqySzKhGoKk4ZIB+vJ3GTKj7UPrWYvM+4w=
|
github.com/metacubex/sing v0.0.0-20230526162852-6afe73474070/go.mod h1:Ta8nHnDLAwqySzKhGoKk4ZIB+vJ3GTKj7UPrWYvM+4w=
|
||||||
github.com/metacubex/sing-shadowsocks v0.2.2-0.20230509230448-a5157cc00a1c h1:LpVNvlW/xE+mR8z76xJeYZlYznZXEmU4TeWeuygYdJg=
|
github.com/metacubex/sing-shadowsocks v0.2.2-0.20230509230448-a5157cc00a1c h1:LpVNvlW/xE+mR8z76xJeYZlYznZXEmU4TeWeuygYdJg=
|
||||||
github.com/metacubex/sing-shadowsocks v0.2.2-0.20230509230448-a5157cc00a1c/go.mod h1:4uQQReKMTU7KTfOykVBe/oGJ00pl38d+BYJ99+mx26s=
|
github.com/metacubex/sing-shadowsocks v0.2.2-0.20230509230448-a5157cc00a1c/go.mod h1:4uQQReKMTU7KTfOykVBe/oGJ00pl38d+BYJ99+mx26s=
|
||||||
github.com/metacubex/sing-shadowsocks2 v0.0.0-20230519030442-556ef530768f h1:aWgVMoAm5V2Ur9key6L//mUSBrVMl/zw/4GDG4ZjyZI=
|
github.com/metacubex/sing-shadowsocks2 v0.0.0-20230528144023-05418c94ed2d h1:lWbWl3pZA1x8TgYDw07jo1u5RtbBRIlxuJDV4FW0WeQ=
|
||||||
github.com/metacubex/sing-shadowsocks2 v0.0.0-20230519030442-556ef530768f/go.mod h1:jVDD4N22bDPPKA73NvB7aqdlLWiAwv8D+jx7HwhcWak=
|
github.com/metacubex/sing-shadowsocks2 v0.0.0-20230528144023-05418c94ed2d/go.mod h1:jVDD4N22bDPPKA73NvB7aqdlLWiAwv8D+jx7HwhcWak=
|
||||||
github.com/metacubex/sing-tun v0.1.5-0.20230509224930-30065d4b6376 h1:zKNsbFQyleMFAP7NJYRew9sEMJuniuODH3V0FdWnEtk=
|
github.com/metacubex/sing-tun v0.1.5-0.20230509224930-30065d4b6376 h1:zKNsbFQyleMFAP7NJYRew9sEMJuniuODH3V0FdWnEtk=
|
||||||
github.com/metacubex/sing-tun v0.1.5-0.20230509224930-30065d4b6376/go.mod h1:BMfG00enVf90/CzcdX9PK3Dymgl7BZqHXJfexEyB7Cc=
|
github.com/metacubex/sing-tun v0.1.5-0.20230509224930-30065d4b6376/go.mod h1:BMfG00enVf90/CzcdX9PK3Dymgl7BZqHXJfexEyB7Cc=
|
||||||
github.com/metacubex/sing-vmess v0.1.5-0.20230520082358-78b126617899 h1:iRfcuztp7REfmOyasSlCL/pqNWfUDMTJ2CwbGpxpeks=
|
github.com/metacubex/sing-vmess v0.1.5-0.20230520082358-78b126617899 h1:iRfcuztp7REfmOyasSlCL/pqNWfUDMTJ2CwbGpxpeks=
|
||||||
|
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/adapter/inbound"
|
"github.com/Dreamacro/clash/adapter/inbound"
|
||||||
"github.com/Dreamacro/clash/common/pool"
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/Dreamacro/clash/common/sockopt"
|
"github.com/Dreamacro/clash/common/sockopt"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/Dreamacro/clash/log"
|
"github.com/Dreamacro/clash/log"
|
||||||
|
@ -29,19 +29,20 @@ func NewUDP(addr string, pickCipher core.Cipher, in chan<- C.PacketAdapter) (*UD
|
||||||
}
|
}
|
||||||
|
|
||||||
sl := &UDPListener{l, false}
|
sl := &UDPListener{l, false}
|
||||||
conn := pickCipher.PacketConn(l)
|
conn := pickCipher.PacketConn(N.NewEnhancePacketConn(l))
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
buf := pool.Get(pool.UDPBufferSize)
|
data, put, remoteAddr, err := conn.WaitReadFrom()
|
||||||
n, remoteAddr, err := conn.ReadFrom(buf)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pool.Put(buf)
|
if put != nil {
|
||||||
|
put()
|
||||||
|
}
|
||||||
if sl.closed {
|
if sl.closed {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
handleSocksUDP(conn, in, buf[:n], remoteAddr)
|
handleSocksUDP(conn, in, data, put, remoteAddr)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -57,11 +58,13 @@ func (l *UDPListener) LocalAddr() net.Addr {
|
||||||
return l.packetConn.LocalAddr()
|
return l.packetConn.LocalAddr()
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleSocksUDP(pc net.PacketConn, in chan<- C.PacketAdapter, buf []byte, addr net.Addr) {
|
func handleSocksUDP(pc net.PacketConn, in chan<- C.PacketAdapter, buf []byte, put func(), addr net.Addr) {
|
||||||
tgtAddr := socks5.SplitAddr(buf)
|
tgtAddr := socks5.SplitAddr(buf)
|
||||||
if tgtAddr == nil {
|
if tgtAddr == nil {
|
||||||
// Unresolved UDP packet, return buffer to the pool
|
// Unresolved UDP packet, return buffer to the pool
|
||||||
pool.Put(buf)
|
if put != nil {
|
||||||
|
put()
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
target := socks5.ParseAddr(tgtAddr.String())
|
target := socks5.ParseAddr(tgtAddr.String())
|
||||||
|
@ -71,7 +74,7 @@ func handleSocksUDP(pc net.PacketConn, in chan<- C.PacketAdapter, buf []byte, ad
|
||||||
pc: pc,
|
pc: pc,
|
||||||
rAddr: addr,
|
rAddr: addr,
|
||||||
payload: payload,
|
payload: payload,
|
||||||
bufRef: buf,
|
put: put,
|
||||||
}
|
}
|
||||||
select {
|
select {
|
||||||
case in <- inbound.NewPacket(target, packet, C.SHADOWSOCKS):
|
case in <- inbound.NewPacket(target, packet, C.SHADOWSOCKS):
|
||||||
|
|
|
@ -6,7 +6,6 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/pool"
|
|
||||||
"github.com/Dreamacro/clash/transport/socks5"
|
"github.com/Dreamacro/clash/transport/socks5"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -14,7 +13,7 @@ type packet struct {
|
||||||
pc net.PacketConn
|
pc net.PacketConn
|
||||||
rAddr net.Addr
|
rAddr net.Addr
|
||||||
payload []byte
|
payload []byte
|
||||||
bufRef []byte
|
put func()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *packet) Data() []byte {
|
func (c *packet) Data() []byte {
|
||||||
|
@ -37,7 +36,9 @@ func (c *packet) LocalAddr() net.Addr {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *packet) Drop() {
|
func (c *packet) Drop() {
|
||||||
pool.Put(c.bufRef)
|
if c.put != nil {
|
||||||
|
c.put()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *packet) InAddr() net.Addr {
|
func (c *packet) InAddr() net.Addr {
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/Dreamacro/clash/transport/shadowsocks/shadowaead"
|
"github.com/Dreamacro/clash/transport/shadowsocks/shadowaead"
|
||||||
"github.com/Dreamacro/clash/transport/shadowsocks/shadowstream"
|
"github.com/Dreamacro/clash/transport/shadowsocks/shadowstream"
|
||||||
)
|
)
|
||||||
|
@ -21,7 +22,7 @@ type StreamConnCipher interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type PacketConnCipher interface {
|
type PacketConnCipher interface {
|
||||||
PacketConn(net.PacketConn) net.PacketConn
|
PacketConn(N.EnhancePacketConn) N.EnhancePacketConn
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrCipherNotSupported occurs when a cipher is not supported (likely because of security concerns).
|
// ErrCipherNotSupported occurs when a cipher is not supported (likely because of security concerns).
|
||||||
|
@ -128,7 +129,7 @@ type AeadCipher struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (aead *AeadCipher) StreamConn(c net.Conn) net.Conn { return shadowaead.NewConn(c, aead) }
|
func (aead *AeadCipher) StreamConn(c net.Conn) net.Conn { return shadowaead.NewConn(c, aead) }
|
||||||
func (aead *AeadCipher) PacketConn(c net.PacketConn) net.PacketConn {
|
func (aead *AeadCipher) PacketConn(c N.EnhancePacketConn) N.EnhancePacketConn {
|
||||||
return shadowaead.NewPacketConn(c, aead)
|
return shadowaead.NewPacketConn(c, aead)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,7 +140,7 @@ type StreamCipher struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ciph *StreamCipher) StreamConn(c net.Conn) net.Conn { return shadowstream.NewConn(c, ciph) }
|
func (ciph *StreamCipher) StreamConn(c net.Conn) net.Conn { return shadowstream.NewConn(c, ciph) }
|
||||||
func (ciph *StreamCipher) PacketConn(c net.PacketConn) net.PacketConn {
|
func (ciph *StreamCipher) PacketConn(c N.EnhancePacketConn) N.EnhancePacketConn {
|
||||||
return shadowstream.NewPacketConn(c, ciph)
|
return shadowstream.NewPacketConn(c, ciph)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,8 +148,8 @@ func (ciph *StreamCipher) PacketConn(c net.PacketConn) net.PacketConn {
|
||||||
|
|
||||||
type dummy struct{}
|
type dummy struct{}
|
||||||
|
|
||||||
func (dummy) StreamConn(c net.Conn) net.Conn { return c }
|
func (dummy) StreamConn(c net.Conn) net.Conn { return c }
|
||||||
func (dummy) PacketConn(c net.PacketConn) net.PacketConn { return c }
|
func (dummy) PacketConn(c N.EnhancePacketConn) N.EnhancePacketConn { return c }
|
||||||
|
|
||||||
// key-derivation function from original Shadowsocks
|
// key-derivation function from original Shadowsocks
|
||||||
func Kdf(password string, keyLen int) []byte {
|
func Kdf(password string, keyLen int) []byte {
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/Dreamacro/clash/common/pool"
|
"github.com/Dreamacro/clash/common/pool"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -57,15 +58,15 @@ func Unpack(dst, pkt []byte, ciph Cipher) ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
type PacketConn struct {
|
type PacketConn struct {
|
||||||
net.PacketConn
|
N.EnhancePacketConn
|
||||||
Cipher
|
Cipher
|
||||||
}
|
}
|
||||||
|
|
||||||
const maxPacketSize = 64 * 1024
|
const maxPacketSize = 64 * 1024
|
||||||
|
|
||||||
// NewPacketConn wraps a net.PacketConn with cipher
|
// NewPacketConn wraps an N.EnhancePacketConn with cipher
|
||||||
func NewPacketConn(c net.PacketConn, ciph Cipher) *PacketConn {
|
func NewPacketConn(c N.EnhancePacketConn, ciph Cipher) *PacketConn {
|
||||||
return &PacketConn{PacketConn: c, Cipher: ciph}
|
return &PacketConn{EnhancePacketConn: c, Cipher: ciph}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteTo encrypts b and write to addr using the embedded PacketConn.
|
// WriteTo encrypts b and write to addr using the embedded PacketConn.
|
||||||
|
@ -76,13 +77,13 @@ func (c *PacketConn) WriteTo(b []byte, addr net.Addr) (int, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
_, err = c.PacketConn.WriteTo(buf, addr)
|
_, err = c.EnhancePacketConn.WriteTo(buf, addr)
|
||||||
return len(b), err
|
return len(b), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadFrom reads from the embedded PacketConn and decrypts into b.
|
// ReadFrom reads from the embedded PacketConn and decrypts into b.
|
||||||
func (c *PacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
func (c *PacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
||||||
n, addr, err := c.PacketConn.ReadFrom(b)
|
n, addr, err := c.EnhancePacketConn.ReadFrom(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return n, addr, err
|
return n, addr, err
|
||||||
}
|
}
|
||||||
|
@ -93,3 +94,20 @@ func (c *PacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
||||||
copy(b, bb)
|
copy(b, bb)
|
||||||
return len(bb), addr, err
|
return len(bb), addr, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *PacketConn) WaitReadFrom() (data []byte, put func(), addr net.Addr, err error) {
|
||||||
|
data, put, addr, err = c.EnhancePacketConn.WaitReadFrom()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
data, err = Unpack(data[c.Cipher.SaltSize():], data, c)
|
||||||
|
if err != nil {
|
||||||
|
if put != nil {
|
||||||
|
put()
|
||||||
|
}
|
||||||
|
data = nil
|
||||||
|
put = nil
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/Dreamacro/clash/common/pool"
|
"github.com/Dreamacro/clash/common/pool"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -43,13 +44,13 @@ func Unpack(dst, pkt []byte, s Cipher) ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
type PacketConn struct {
|
type PacketConn struct {
|
||||||
net.PacketConn
|
N.EnhancePacketConn
|
||||||
Cipher
|
Cipher
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPacketConn wraps a net.PacketConn with stream cipher encryption/decryption.
|
// NewPacketConn wraps an N.EnhancePacketConn with stream cipher encryption/decryption.
|
||||||
func NewPacketConn(c net.PacketConn, ciph Cipher) *PacketConn {
|
func NewPacketConn(c N.EnhancePacketConn, ciph Cipher) *PacketConn {
|
||||||
return &PacketConn{PacketConn: c, Cipher: ciph}
|
return &PacketConn{EnhancePacketConn: c, Cipher: ciph}
|
||||||
}
|
}
|
||||||
|
|
||||||
const maxPacketSize = 64 * 1024
|
const maxPacketSize = 64 * 1024
|
||||||
|
@ -61,12 +62,12 @@ func (c *PacketConn) WriteTo(b []byte, addr net.Addr) (int, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
_, err = c.PacketConn.WriteTo(buf, addr)
|
_, err = c.EnhancePacketConn.WriteTo(buf, addr)
|
||||||
return len(b), err
|
return len(b), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *PacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
func (c *PacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
||||||
n, addr, err := c.PacketConn.ReadFrom(b)
|
n, addr, err := c.EnhancePacketConn.ReadFrom(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return n, addr, err
|
return n, addr, err
|
||||||
}
|
}
|
||||||
|
@ -77,3 +78,20 @@ func (c *PacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
||||||
copy(b, bb)
|
copy(b, bb)
|
||||||
return len(bb), addr, err
|
return len(bb), addr, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *PacketConn) WaitReadFrom() (data []byte, put func(), addr net.Addr, err error) {
|
||||||
|
data, put, addr, err = c.EnhancePacketConn.WaitReadFrom()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
data, err = Unpack(data[c.IVSize():], data, c)
|
||||||
|
if err != nil {
|
||||||
|
if put != nil {
|
||||||
|
put()
|
||||||
|
}
|
||||||
|
data = nil
|
||||||
|
put = nil
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/Dreamacro/clash/common/pool"
|
"github.com/Dreamacro/clash/common/pool"
|
||||||
"github.com/Dreamacro/clash/log"
|
"github.com/Dreamacro/clash/log"
|
||||||
"github.com/Dreamacro/clash/transport/ssr/tools"
|
"github.com/Dreamacro/clash/transport/ssr/tools"
|
||||||
|
@ -82,13 +83,13 @@ func (a *authAES128) StreamConn(c net.Conn, iv []byte) net.Conn {
|
||||||
return &Conn{Conn: c, Protocol: p}
|
return &Conn{Conn: c, Protocol: p}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *authAES128) PacketConn(c net.PacketConn) net.PacketConn {
|
func (a *authAES128) PacketConn(c N.EnhancePacketConn) N.EnhancePacketConn {
|
||||||
p := &authAES128{
|
p := &authAES128{
|
||||||
Base: a.Base,
|
Base: a.Base,
|
||||||
authAES128Function: a.authAES128Function,
|
authAES128Function: a.authAES128Function,
|
||||||
userData: a.userData,
|
userData: a.userData,
|
||||||
}
|
}
|
||||||
return &PacketConn{PacketConn: c, Protocol: p}
|
return &PacketConn{EnhancePacketConn: c, Protocol: p}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *authAES128) Decode(dst, src *bytes.Buffer) error {
|
func (a *authAES128) Decode(dst, src *bytes.Buffer) error {
|
||||||
|
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/Dreamacro/clash/common/pool"
|
"github.com/Dreamacro/clash/common/pool"
|
||||||
"github.com/Dreamacro/clash/log"
|
"github.com/Dreamacro/clash/log"
|
||||||
"github.com/Dreamacro/clash/transport/shadowsocks/core"
|
"github.com/Dreamacro/clash/transport/shadowsocks/core"
|
||||||
|
@ -83,13 +84,13 @@ func (a *authChainA) StreamConn(c net.Conn, iv []byte) net.Conn {
|
||||||
return &Conn{Conn: c, Protocol: p}
|
return &Conn{Conn: c, Protocol: p}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *authChainA) PacketConn(c net.PacketConn) net.PacketConn {
|
func (a *authChainA) PacketConn(c N.EnhancePacketConn) N.EnhancePacketConn {
|
||||||
p := &authChainA{
|
p := &authChainA{
|
||||||
Base: a.Base,
|
Base: a.Base,
|
||||||
salt: a.salt,
|
salt: a.salt,
|
||||||
userData: a.userData,
|
userData: a.userData,
|
||||||
}
|
}
|
||||||
return &PacketConn{PacketConn: c, Protocol: p}
|
return &PacketConn{EnhancePacketConn: c, Protocol: p}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *authChainA) Decode(dst, src *bytes.Buffer) error {
|
func (a *authChainA) Decode(dst, src *bytes.Buffer) error {
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"hash/crc32"
|
"hash/crc32"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/Dreamacro/clash/common/pool"
|
"github.com/Dreamacro/clash/common/pool"
|
||||||
"github.com/Dreamacro/clash/transport/ssr/tools"
|
"github.com/Dreamacro/clash/transport/ssr/tools"
|
||||||
|
|
||||||
|
@ -35,7 +36,7 @@ func (a *authSHA1V4) StreamConn(c net.Conn, iv []byte) net.Conn {
|
||||||
return &Conn{Conn: c, Protocol: p}
|
return &Conn{Conn: c, Protocol: p}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *authSHA1V4) PacketConn(c net.PacketConn) net.PacketConn {
|
func (a *authSHA1V4) PacketConn(c N.EnhancePacketConn) N.EnhancePacketConn {
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,8 @@ package protocol
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
)
|
)
|
||||||
|
|
||||||
type origin struct{}
|
type origin struct{}
|
||||||
|
@ -13,7 +15,7 @@ func newOrigin(b *Base) Protocol { return &origin{} }
|
||||||
|
|
||||||
func (o *origin) StreamConn(c net.Conn, iv []byte) net.Conn { return c }
|
func (o *origin) StreamConn(c net.Conn, iv []byte) net.Conn { return c }
|
||||||
|
|
||||||
func (o *origin) PacketConn(c net.PacketConn) net.PacketConn { return c }
|
func (o *origin) PacketConn(c N.EnhancePacketConn) N.EnhancePacketConn { return c }
|
||||||
|
|
||||||
func (o *origin) Decode(dst, src *bytes.Buffer) error {
|
func (o *origin) Decode(dst, src *bytes.Buffer) error {
|
||||||
dst.ReadFrom(src)
|
dst.ReadFrom(src)
|
||||||
|
|
|
@ -3,11 +3,12 @@ package protocol
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/Dreamacro/clash/common/pool"
|
"github.com/Dreamacro/clash/common/pool"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PacketConn struct {
|
type PacketConn struct {
|
||||||
net.PacketConn
|
N.EnhancePacketConn
|
||||||
Protocol
|
Protocol
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,12 +19,12 @@ func (c *PacketConn) WriteTo(b []byte, addr net.Addr) (int, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
_, err = c.PacketConn.WriteTo(buf.Bytes(), addr)
|
_, err = c.EnhancePacketConn.WriteTo(buf.Bytes(), addr)
|
||||||
return len(b), err
|
return len(b), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *PacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
func (c *PacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
||||||
n, addr, err := c.PacketConn.ReadFrom(b)
|
n, addr, err := c.EnhancePacketConn.ReadFrom(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return n, addr, err
|
return n, addr, err
|
||||||
}
|
}
|
||||||
|
@ -34,3 +35,20 @@ func (c *PacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
||||||
copy(b, decoded)
|
copy(b, decoded)
|
||||||
return len(decoded), addr, nil
|
return len(decoded), addr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *PacketConn) WaitReadFrom() (data []byte, put func(), addr net.Addr, err error) {
|
||||||
|
data, put, addr, err = c.EnhancePacketConn.WaitReadFrom()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
data, err = c.DecodePacket(data)
|
||||||
|
if err != nil {
|
||||||
|
if put != nil {
|
||||||
|
put()
|
||||||
|
}
|
||||||
|
data = nil
|
||||||
|
put = nil
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
@ -6,6 +6,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
|
|
||||||
"github.com/zhangyunhao116/fastrand"
|
"github.com/zhangyunhao116/fastrand"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -22,7 +24,7 @@ var (
|
||||||
|
|
||||||
type Protocol interface {
|
type Protocol interface {
|
||||||
StreamConn(net.Conn, []byte) net.Conn
|
StreamConn(net.Conn, []byte) net.Conn
|
||||||
PacketConn(net.PacketConn) net.PacketConn
|
PacketConn(N.EnhancePacketConn) N.EnhancePacketConn
|
||||||
Decode(dst, src *bytes.Buffer) error
|
Decode(dst, src *bytes.Buffer) error
|
||||||
Encode(buf *bytes.Buffer, b []byte) error
|
Encode(buf *bytes.Buffer, b []byte) error
|
||||||
DecodePacket([]byte) ([]byte, error)
|
DecodePacket([]byte) ([]byte, error)
|
||||||
|
|
|
@ -370,7 +370,9 @@ func (pc *PacketConn) WaitReadFrom() (data []byte, put func(), addr net.Addr, er
|
||||||
|
|
||||||
_, err = io.ReadFull(pc.Conn, data[:2+2]) // u16be length + CR LF
|
_, err = io.ReadFull(pc.Conn, data[:2+2]) // u16be length + CR LF
|
||||||
if err != nil {
|
if err != nil {
|
||||||
put()
|
if put != nil {
|
||||||
|
put()
|
||||||
|
}
|
||||||
return nil, nil, nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
length := binary.BigEndian.Uint16(data)
|
length := binary.BigEndian.Uint16(data)
|
||||||
|
@ -379,11 +381,15 @@ func (pc *PacketConn) WaitReadFrom() (data []byte, put func(), addr net.Addr, er
|
||||||
data = data[:length]
|
data = data[:length]
|
||||||
_, err = io.ReadFull(pc.Conn, data)
|
_, err = io.ReadFull(pc.Conn, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
put()
|
if put != nil {
|
||||||
|
put()
|
||||||
|
}
|
||||||
return nil, nil, nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
put()
|
if put != nil {
|
||||||
|
put()
|
||||||
|
}
|
||||||
return nil, nil, addr, nil
|
return nil, nil, addr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -205,7 +205,6 @@ func (q *quicStreamPacketConn) WaitReadFrom() (data []byte, put func(), addr net
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
data = packet.DATA
|
data = packet.DATA
|
||||||
put = N.NilPut
|
|
||||||
addr = packet.ADDR.UDPAddr()
|
addr = packet.ADDR.UDPAddr()
|
||||||
} else {
|
} else {
|
||||||
err = net.ErrClosed
|
err = net.ErrClosed
|
||||||
|
|
Loading…
Reference in New Issue
Block a user