2022-12-19 21:34:07 +08:00
|
|
|
package net
|
|
|
|
|
|
|
|
import "net"
|
|
|
|
|
|
|
|
type bindPacketConn struct {
|
2023-05-11 19:01:41 +08:00
|
|
|
EnhancePacketConn
|
2022-12-19 21:34:07 +08:00
|
|
|
rAddr net.Addr
|
|
|
|
}
|
|
|
|
|
2023-05-11 19:01:41 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-05-11 19:01:41 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-05-11 19:01:41 +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
|
|
|
}
|
|
|
|
|
2023-05-11 19:01:41 +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 {
|
2023-05-11 19:01:41 +08:00
|
|
|
return c.EnhancePacketConn.LocalAddr()
|
2022-12-19 21:34:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-11 19:01:41 +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),
|
2023-05-11 19:01:41 +08:00
|
|
|
rAddr: rAddr,
|
2022-12-19 21:34:07 +08:00
|
|
|
}
|
|
|
|
}
|