mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-11-16 11:42:43 +08:00
51 lines
965 B
Go
51 lines
965 B
Go
package socks
|
|
|
|
import (
|
|
"net"
|
|
|
|
"github.com/Dreamacro/clash/common/pool"
|
|
C "github.com/Dreamacro/clash/constant"
|
|
"github.com/Dreamacro/clash/transport/socks5"
|
|
)
|
|
|
|
type packet struct {
|
|
pc net.PacketConn
|
|
rAddr net.Addr
|
|
payload []byte
|
|
bufRef []byte
|
|
}
|
|
|
|
func (c *packet) Data() []byte {
|
|
return c.payload
|
|
}
|
|
|
|
// WriteBack write UDP packet with source(ip, port) = `addr`
|
|
func (c *packet) WriteBack(b []byte, addr net.Addr) (n int, err error) {
|
|
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
|
|
}
|
|
|
|
func (c *packet) Drop() {
|
|
pool.Put(c.bufRef)
|
|
}
|
|
|
|
func (c *packet) InAddr() net.Addr {
|
|
return c.pc.LocalAddr()
|
|
}
|
|
|
|
func (c *packet) SetNatTable(natTable C.NatTable) {
|
|
// no need
|
|
}
|
|
|
|
func (c *packet) SetUdpInChan(in chan<- C.PacketAdapter) {
|
|
// no need
|
|
}
|