mihomo/listener/socks/utils.go

45 lines
784 B
Go
Raw Permalink Normal View History

package socks
import (
"net"
2021-05-13 22:18:49 +08:00
"github.com/Dreamacro/clash/transport/socks5"
)
type packet struct {
pc net.PacketConn
2020-01-31 14:43:54 +08:00
rAddr net.Addr
payload []byte
put func()
}
func (c *packet) Data() []byte {
return c.payload
}
2020-10-14 19:56:02 +08:00
// WriteBack write UDP packet with source(ip, port) = `addr`
func (c *packet) 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
}
return c.pc.WriteTo(packet, c.rAddr)
}
// LocalAddr returns the source IP/Port of UDP Packet
func (c *packet) LocalAddr() net.Addr {
return c.rAddr
}
2019-10-11 20:11:18 +08:00
func (c *packet) Drop() {
if c.put != nil {
c.put()
c.put = nil
}
c.payload = nil
2019-10-11 20:11:18 +08:00
}
2022-11-11 23:36:06 +08:00
func (c *packet) InAddr() net.Addr {
return c.pc.LocalAddr()
}