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

     1  package bufio
     2  
     3  import (
     4  	"io"
     5  	"net"
     6  
     7  	M "github.com/sagernet/sing/common/metadata"
     8  )
     9  
    10  type AddrConn struct {
    11  	net.Conn
    12  	M.Metadata
    13  }
    14  
    15  func (c *AddrConn) LocalAddr() net.Addr {
    16  	if c.Metadata.Destination.IsValid() {
    17  		return c.Metadata.Destination.TCPAddr()
    18  	}
    19  	return c.Conn.LocalAddr()
    20  }
    21  
    22  func (c *AddrConn) RemoteAddr() net.Addr {
    23  	if c.Metadata.Source.IsValid() {
    24  		return c.Metadata.Source.TCPAddr()
    25  	}
    26  	return c.Conn.RemoteAddr()
    27  }
    28  
    29  func (c *AddrConn) ReadFrom(r io.Reader) (n int64, err error) {
    30  	return Copy(c.Conn, r)
    31  }
    32  
    33  func (c *AddrConn) WriteTo(w io.Writer) (n int64, err error) {
    34  	return Copy(w, c.Conn)
    35  }
    36  
    37  func (c *AddrConn) ReaderReplaceable() bool {
    38  	return true
    39  }
    40  
    41  func (c *AddrConn) WriterReplaceable() bool {
    42  	return true
    43  }
    44  
    45  func (c *AddrConn) Upstream() any {
    46  	return c.Conn
    47  }