Clash.Meta/proxy/redir/utils.go

41 lines
722 B
Go
Raw Normal View History

package redir
import (
"net"
"github.com/Dreamacro/clash/common/pool"
)
type fakeConn struct {
net.PacketConn
2020-03-10 20:36:24 +08:00
lAddr *net.UDPAddr
buf []byte
}
func (c *fakeConn) Data() []byte {
return c.buf
}
2020-03-10 20:36:24 +08:00
// 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) {
2020-03-10 20:36:24 +08:00
tc, err := dialUDP("udp", addr.(*net.UDPAddr), c.lAddr)
if err != nil {
n = 0
return
}
n, err = tc.Write(b)
tc.Close()
return
}
// LocalAddr returns the source IP/Port of UDP Packet
func (c *fakeConn) LocalAddr() net.Addr {
2020-03-10 20:36:24 +08:00
return c.lAddr
}
func (c *fakeConn) Close() error {
err := c.PacketConn.Close()
pool.BufPool.Put(c.buf[:cap(c.buf)])
return err
}