clash/proxy/socks/utils.go

40 lines
801 B
Go
Raw Normal View History

package socks
import (
"net"
2019-10-11 20:11:18 +08:00
"github.com/Dreamacro/clash/common/pool"
"github.com/Dreamacro/clash/component/socks5"
)
type fakeConn struct {
net.PacketConn
2020-01-31 14:43:54 +08:00
rAddr net.Addr
payload []byte
bufRef []byte
}
func (c *fakeConn) Data() []byte {
return c.payload
}
// WriteBack wirtes UDP packet with source(ip, port) = `addr`
func (c *fakeConn) WriteBack(b []byte, addr net.Addr) (n int, err error) {
2020-01-31 14:43:54 +08:00
packet, err := socks5.EncodeUDPPacket(socks5.ParseAddrToSocksAddr(addr), b)
if err != nil {
return
}
2020-01-31 14:43:54 +08:00
return c.PacketConn.WriteTo(packet, c.rAddr)
}
// LocalAddr returns the source IP/Port of UDP Packet
func (c *fakeConn) LocalAddr() net.Addr {
return c.rAddr
}
2019-10-11 20:11:18 +08:00
func (c *fakeConn) Close() error {
2020-01-31 15:03:59 +08:00
err := c.PacketConn.Close()
2019-10-11 20:11:18 +08:00
pool.BufPool.Put(c.bufRef[:cap(c.bufRef)])
2020-01-31 15:03:59 +08:00
return err
2019-10-11 20:11:18 +08:00
}