clash/proxy/redir/utils.go

38 lines
627 B
Go
Raw Normal View History

package redir
import (
"net"
"github.com/Dreamacro/clash/common/pool"
)
type packet struct {
2020-03-10 12:36:24 +00:00
lAddr *net.UDPAddr
buf []byte
}
func (c *packet) Data() []byte {
return c.buf
}
2020-10-14 11:56:02 +00:00
// WriteBack opens a new socket binding `addr` to write UDP packet back
func (c *packet) WriteBack(b []byte, addr net.Addr) (n int, err error) {
2020-03-10 12:36:24 +00: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 *packet) LocalAddr() net.Addr {
2020-03-10 12:36:24 +00:00
return c.lAddr
}
func (c *packet) Drop() {
2020-04-24 16:30:40 +00:00
pool.Put(c.buf)
}