Improve: recycle buffer after packet used
This commit is contained in:
parent
af00355b28
commit
dc9ae1edd0
6 changed files with 27 additions and 29 deletions
|
@ -135,8 +135,8 @@ type UDPPacket interface {
|
||||||
// this is important when using Fake-IP.
|
// this is important when using Fake-IP.
|
||||||
WriteBack(b []byte, addr net.Addr) (n int, err error)
|
WriteBack(b []byte, addr net.Addr) (n int, err error)
|
||||||
|
|
||||||
// Close closes the underlaying connection.
|
// Drop call after packet is used, could recycle buffer in this function.
|
||||||
Close() error
|
Drop()
|
||||||
|
|
||||||
// LocalAddr returns the source IP/Port of packet
|
// LocalAddr returns the source IP/Port of packet
|
||||||
LocalAddr() net.Addr
|
LocalAddr() net.Addr
|
||||||
|
|
|
@ -66,10 +66,9 @@ func (l *RedirUDPListener) Address() string {
|
||||||
|
|
||||||
func handleRedirUDP(pc net.PacketConn, buf []byte, lAddr *net.UDPAddr, rAddr *net.UDPAddr) {
|
func handleRedirUDP(pc net.PacketConn, buf []byte, lAddr *net.UDPAddr, rAddr *net.UDPAddr) {
|
||||||
target := socks5.ParseAddrToSocksAddr(rAddr)
|
target := socks5.ParseAddrToSocksAddr(rAddr)
|
||||||
packet := &fakeConn{
|
pkt := &packet{
|
||||||
PacketConn: pc,
|
lAddr: lAddr,
|
||||||
lAddr: lAddr,
|
buf: buf,
|
||||||
buf: buf,
|
|
||||||
}
|
}
|
||||||
tunnel.AddPacket(adapters.NewPacket(target, packet, C.REDIR))
|
tunnel.AddPacket(adapters.NewPacket(target, pkt, C.REDIR))
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,18 +6,17 @@ import (
|
||||||
"github.com/Dreamacro/clash/common/pool"
|
"github.com/Dreamacro/clash/common/pool"
|
||||||
)
|
)
|
||||||
|
|
||||||
type fakeConn struct {
|
type packet struct {
|
||||||
net.PacketConn
|
|
||||||
lAddr *net.UDPAddr
|
lAddr *net.UDPAddr
|
||||||
buf []byte
|
buf []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *fakeConn) Data() []byte {
|
func (c *packet) Data() []byte {
|
||||||
return c.buf
|
return c.buf
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteBack opens a new socket binding `addr` to wirte UDP packet back
|
// WriteBack opens a new socket binding `addr` to wirte UDP packet back
|
||||||
func (c *fakeConn) WriteBack(b []byte, addr net.Addr) (n int, err error) {
|
func (c *packet) WriteBack(b []byte, addr net.Addr) (n int, err error) {
|
||||||
tc, err := dialUDP("udp", addr.(*net.UDPAddr), c.lAddr)
|
tc, err := dialUDP("udp", addr.(*net.UDPAddr), c.lAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
n = 0
|
n = 0
|
||||||
|
@ -29,12 +28,11 @@ func (c *fakeConn) WriteBack(b []byte, addr net.Addr) (n int, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// LocalAddr returns the source IP/Port of UDP Packet
|
// LocalAddr returns the source IP/Port of UDP Packet
|
||||||
func (c *fakeConn) LocalAddr() net.Addr {
|
func (c *packet) LocalAddr() net.Addr {
|
||||||
return c.lAddr
|
return c.lAddr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *fakeConn) Close() error {
|
func (c *packet) Drop() {
|
||||||
err := c.PacketConn.Close()
|
|
||||||
pool.BufPool.Put(c.buf[:cap(c.buf)])
|
pool.BufPool.Put(c.buf[:cap(c.buf)])
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,11 +63,11 @@ func handleSocksUDP(pc net.PacketConn, buf []byte, addr net.Addr) {
|
||||||
pool.BufPool.Put(buf[:cap(buf)])
|
pool.BufPool.Put(buf[:cap(buf)])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
packet := &fakeConn{
|
packet := &packet{
|
||||||
PacketConn: pc,
|
pc: pc,
|
||||||
rAddr: addr,
|
rAddr: addr,
|
||||||
payload: payload,
|
payload: payload,
|
||||||
bufRef: buf,
|
bufRef: buf,
|
||||||
}
|
}
|
||||||
tunnel.AddPacket(adapters.NewPacket(target, packet, C.SOCKS))
|
tunnel.AddPacket(adapters.NewPacket(target, packet, C.SOCKS))
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,33 +7,32 @@ import (
|
||||||
"github.com/Dreamacro/clash/component/socks5"
|
"github.com/Dreamacro/clash/component/socks5"
|
||||||
)
|
)
|
||||||
|
|
||||||
type fakeConn struct {
|
type packet struct {
|
||||||
net.PacketConn
|
pc net.PacketConn
|
||||||
rAddr net.Addr
|
rAddr net.Addr
|
||||||
payload []byte
|
payload []byte
|
||||||
bufRef []byte
|
bufRef []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *fakeConn) Data() []byte {
|
func (c *packet) Data() []byte {
|
||||||
return c.payload
|
return c.payload
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteBack wirtes UDP packet with source(ip, port) = `addr`
|
// WriteBack wirtes UDP packet with source(ip, port) = `addr`
|
||||||
func (c *fakeConn) WriteBack(b []byte, addr net.Addr) (n int, err error) {
|
func (c *packet) WriteBack(b []byte, addr net.Addr) (n int, err error) {
|
||||||
packet, err := socks5.EncodeUDPPacket(socks5.ParseAddrToSocksAddr(addr), b)
|
packet, err := socks5.EncodeUDPPacket(socks5.ParseAddrToSocksAddr(addr), b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return c.PacketConn.WriteTo(packet, c.rAddr)
|
return c.pc.WriteTo(packet, c.rAddr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// LocalAddr returns the source IP/Port of UDP Packet
|
// LocalAddr returns the source IP/Port of UDP Packet
|
||||||
func (c *fakeConn) LocalAddr() net.Addr {
|
func (c *packet) LocalAddr() net.Addr {
|
||||||
return c.rAddr
|
return c.rAddr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *fakeConn) Close() error {
|
func (c *packet) Drop() {
|
||||||
err := c.PacketConn.Close()
|
|
||||||
pool.BufPool.Put(c.bufRef[:cap(c.bufRef)])
|
pool.BufPool.Put(c.bufRef[:cap(c.bufRef)])
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,6 +82,8 @@ func handleHTTP(request *adapters.HTTPAdapter, outbound net.Conn) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleUDPToRemote(packet C.UDPPacket, pc C.PacketConn, metadata *C.Metadata) {
|
func handleUDPToRemote(packet C.UDPPacket, pc C.PacketConn, metadata *C.Metadata) {
|
||||||
|
defer packet.Drop()
|
||||||
|
|
||||||
if _, err := pc.WriteWithMetadata(packet.Data(), metadata); err != nil {
|
if _, err := pc.WriteWithMetadata(packet.Data(), metadata); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue