github.com/sagernet/sing@v0.4.0-beta.19.0.20240518125136-f67a0988a636/common/bufio/bind.go (about)

     1  package bufio
     2  
     3  import (
     4  	"net"
     5  
     6  	"github.com/sagernet/sing/common/buf"
     7  	M "github.com/sagernet/sing/common/metadata"
     8  	N "github.com/sagernet/sing/common/network"
     9  )
    10  
    11  type BindPacketConn interface {
    12  	N.NetPacketConn
    13  	net.Conn
    14  }
    15  
    16  type bindPacketConn struct {
    17  	N.NetPacketConn
    18  	addr net.Addr
    19  }
    20  
    21  func NewBindPacketConn(conn net.PacketConn, addr net.Addr) BindPacketConn {
    22  	return &bindPacketConn{
    23  		NewPacketConn(conn),
    24  		addr,
    25  	}
    26  }
    27  
    28  func (c *bindPacketConn) Read(b []byte) (n int, err error) {
    29  	n, _, err = c.ReadFrom(b)
    30  	return
    31  }
    32  
    33  func (c *bindPacketConn) Write(b []byte) (n int, err error) {
    34  	return c.WriteTo(b, c.addr)
    35  }
    36  
    37  func (c *bindPacketConn) CreateReadWaiter() (N.ReadWaiter, bool) {
    38  	readWaiter, isReadWaiter := CreatePacketReadWaiter(c.NetPacketConn)
    39  	if !isReadWaiter {
    40  		return nil, false
    41  	}
    42  	return &BindPacketReadWaiter{readWaiter}, true
    43  }
    44  
    45  func (c *bindPacketConn) RemoteAddr() net.Addr {
    46  	return c.addr
    47  }
    48  
    49  func (c *bindPacketConn) Upstream() any {
    50  	return c.NetPacketConn
    51  }
    52  
    53  var (
    54  	_ N.NetPacketConn         = (*UnbindPacketConn)(nil)
    55  	_ N.PacketReadWaitCreator = (*UnbindPacketConn)(nil)
    56  )
    57  
    58  type UnbindPacketConn struct {
    59  	N.ExtendedConn
    60  	addr M.Socksaddr
    61  }
    62  
    63  func NewUnbindPacketConn(conn net.Conn) N.NetPacketConn {
    64  	return &UnbindPacketConn{
    65  		NewExtendedConn(conn),
    66  		M.SocksaddrFromNet(conn.RemoteAddr()),
    67  	}
    68  }
    69  
    70  func NewUnbindPacketConnWithAddr(conn net.Conn, addr M.Socksaddr) N.NetPacketConn {
    71  	return &UnbindPacketConn{
    72  		NewExtendedConn(conn),
    73  		addr,
    74  	}
    75  }
    76  
    77  func (c *UnbindPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
    78  	n, err = c.ExtendedConn.Read(p)
    79  	if err == nil {
    80  		addr = c.addr.UDPAddr()
    81  	}
    82  	return
    83  }
    84  
    85  func (c *UnbindPacketConn) WriteTo(p []byte, _ net.Addr) (n int, err error) {
    86  	return c.ExtendedConn.Write(p)
    87  }
    88  
    89  func (c *UnbindPacketConn) ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, err error) {
    90  	err = c.ExtendedConn.ReadBuffer(buffer)
    91  	if err != nil {
    92  		return
    93  	}
    94  	destination = c.addr
    95  	return
    96  }
    97  
    98  func (c *UnbindPacketConn) WritePacket(buffer *buf.Buffer, _ M.Socksaddr) error {
    99  	return c.ExtendedConn.WriteBuffer(buffer)
   100  }
   101  
   102  func (c *UnbindPacketConn) CreateReadWaiter() (N.PacketReadWaiter, bool) {
   103  	readWaiter, isReadWaiter := CreateReadWaiter(c.ExtendedConn)
   104  	if !isReadWaiter {
   105  		return nil, false
   106  	}
   107  	return &UnbindPacketReadWaiter{readWaiter, c.addr}, true
   108  }
   109  
   110  func (c *UnbindPacketConn) Upstream() any {
   111  	return c.ExtendedConn
   112  }