2019-07-25 09:47:39 +00:00
|
|
|
package socks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
2019-10-11 12:11:18 +00:00
|
|
|
"github.com/Dreamacro/clash/common/pool"
|
2019-07-25 09:47:39 +00:00
|
|
|
"github.com/Dreamacro/clash/component/socks5"
|
|
|
|
)
|
|
|
|
|
2020-04-16 10:19:36 +00:00
|
|
|
type packet struct {
|
|
|
|
pc net.PacketConn
|
2020-01-31 06:43:54 +00:00
|
|
|
rAddr net.Addr
|
|
|
|
payload []byte
|
|
|
|
bufRef []byte
|
2019-07-25 09:47:39 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 10:19:36 +00:00
|
|
|
func (c *packet) Data() []byte {
|
2019-12-28 10:44:01 +00:00
|
|
|
return c.payload
|
2019-07-25 09:47:39 +00:00
|
|
|
}
|
|
|
|
|
2019-12-28 10:44:01 +00:00
|
|
|
// WriteBack wirtes UDP packet with source(ip, port) = `addr`
|
2020-04-16 10:19:36 +00:00
|
|
|
func (c *packet) WriteBack(b []byte, addr net.Addr) (n int, err error) {
|
2020-01-31 06:43:54 +00:00
|
|
|
packet, err := socks5.EncodeUDPPacket(socks5.ParseAddrToSocksAddr(addr), b)
|
2019-07-25 09:47:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-04-16 10:19:36 +00:00
|
|
|
return c.pc.WriteTo(packet, c.rAddr)
|
2019-07-25 09:47:39 +00:00
|
|
|
}
|
|
|
|
|
2019-12-28 10:44:01 +00:00
|
|
|
// LocalAddr returns the source IP/Port of UDP Packet
|
2020-04-16 10:19:36 +00:00
|
|
|
func (c *packet) LocalAddr() net.Addr {
|
2020-02-20 03:29:16 +00:00
|
|
|
return c.rAddr
|
2019-07-25 09:47:39 +00:00
|
|
|
}
|
2019-10-11 12:11:18 +00:00
|
|
|
|
2020-04-16 10:19:36 +00:00
|
|
|
func (c *packet) Drop() {
|
2019-10-11 12:11:18 +00:00
|
|
|
pool.BufPool.Put(c.bufRef[:cap(c.bufRef)])
|
2020-04-16 10:19:36 +00:00
|
|
|
return
|
2019-10-11 12:11:18 +00:00
|
|
|
}
|