2019-07-25 17:47:39 +08:00
|
|
|
package socks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
2019-10-11 20:11:18 +08:00
|
|
|
"github.com/Dreamacro/clash/common/pool"
|
2019-07-25 17:47:39 +08:00
|
|
|
"github.com/Dreamacro/clash/component/socks5"
|
|
|
|
)
|
|
|
|
|
|
|
|
type fakeConn struct {
|
|
|
|
net.PacketConn
|
|
|
|
remoteAddr net.Addr
|
2019-10-11 20:11:18 +08:00
|
|
|
targetAddr socks5.Addr
|
2019-12-28 18:44:01 +08:00
|
|
|
payload []byte
|
2019-10-11 20:11:18 +08:00
|
|
|
bufRef []byte
|
2019-07-25 17:47:39 +08:00
|
|
|
}
|
|
|
|
|
2019-12-28 18:44:01 +08:00
|
|
|
func (c *fakeConn) Data() []byte {
|
|
|
|
return c.payload
|
2019-07-25 17:47:39 +08:00
|
|
|
}
|
|
|
|
|
2019-12-28 18:44:01 +08:00
|
|
|
// WriteBack wirtes UDP packet with source(ip, port) = `addr`
|
|
|
|
func (c *fakeConn) WriteBack(b []byte, addr net.Addr) (n int, err error) {
|
|
|
|
from := c.targetAddr
|
|
|
|
if addr != nil {
|
|
|
|
// if addr is provided, use the parsed addr
|
|
|
|
from = socks5.ParseAddrToSocksAddr(addr)
|
|
|
|
}
|
|
|
|
packet, err := socks5.EncodeUDPPacket(from, b)
|
2019-07-25 17:47:39 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return c.PacketConn.WriteTo(packet, c.remoteAddr)
|
|
|
|
}
|
|
|
|
|
2019-12-28 18:44:01 +08:00
|
|
|
// LocalAddr returns the source IP/Port of UDP Packet
|
|
|
|
func (c *fakeConn) LocalAddr() net.Addr {
|
2019-07-25 17:47:39 +08:00
|
|
|
return c.remoteAddr
|
|
|
|
}
|
2019-10-11 20:11:18 +08:00
|
|
|
|
|
|
|
func (c *fakeConn) Close() error {
|
|
|
|
err := c.PacketConn.Close()
|
|
|
|
pool.BufPool.Put(c.bufRef[:cap(c.bufRef)])
|
|
|
|
return err
|
|
|
|
}
|