mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-11-16 11:42:43 +08:00
44 lines
832 B
Go
44 lines
832 B
Go
package tproxy
|
|
|
|
import (
|
|
"net"
|
|
"net/netip"
|
|
|
|
"github.com/Dreamacro/clash/common/pool"
|
|
)
|
|
|
|
type packet struct {
|
|
pc net.PacketConn
|
|
lAddr netip.AddrPort
|
|
buf []byte
|
|
}
|
|
|
|
func (c *packet) Data() []byte {
|
|
return c.buf
|
|
}
|
|
|
|
// 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) {
|
|
tc, err := dialUDP("udp", addr.(*net.UDPAddr).AddrPort(), 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 {
|
|
return &net.UDPAddr{IP: c.lAddr.Addr().AsSlice(), Port: int(c.lAddr.Port()), Zone: c.lAddr.Addr().Zone()}
|
|
}
|
|
|
|
func (c *packet) Drop() {
|
|
pool.Put(c.buf)
|
|
}
|
|
|
|
func (c *packet) InAddr() net.Addr {
|
|
return c.pc.LocalAddr()
|
|
}
|