mihomo/common/net/bind.go

46 lines
987 B
Go
Raw Normal View History

2022-12-19 21:34:07 +08:00
package net
import "net"
type bindPacketConn struct {
EnhancePacketConn
2022-12-19 21:34:07 +08:00
rAddr net.Addr
}
func (c *bindPacketConn) Read(b []byte) (n int, err error) {
n, _, err = c.EnhancePacketConn.ReadFrom(b)
2022-12-19 21:34:07 +08:00
return n, err
}
func (c *bindPacketConn) WaitRead() (data []byte, put func(), err error) {
data, put, _, err = c.EnhancePacketConn.WaitReadFrom()
return
2022-12-19 21:34:07 +08:00
}
func (c *bindPacketConn) Write(b []byte) (n int, err error) {
return c.EnhancePacketConn.WriteTo(b, c.rAddr)
2022-12-19 21:34:07 +08:00
}
func (c *bindPacketConn) RemoteAddr() net.Addr {
return c.rAddr
}
func (c *bindPacketConn) LocalAddr() net.Addr {
if c.EnhancePacketConn.LocalAddr() == nil {
2022-12-19 21:34:07 +08:00
return &net.UDPAddr{IP: net.IPv4zero, Port: 0}
} else {
return c.EnhancePacketConn.LocalAddr()
2022-12-19 21:34:07 +08:00
}
}
func (c *bindPacketConn) Upstream() any {
return c.EnhancePacketConn
}
2023-05-12 09:14:27 +08:00
func NewBindPacketConn(pc net.PacketConn, rAddr net.Addr) net.Conn {
2022-12-19 21:34:07 +08:00
return &bindPacketConn{
2023-05-12 09:14:27 +08:00
EnhancePacketConn: NewEnhancePacketConn(pc),
rAddr: rAddr,
2022-12-19 21:34:07 +08:00
}
}