github.com/sagernet/sing-box@v1.9.0-rc.20/common/conntrack/conn.go (about)

     1  package conntrack
     2  
     3  import (
     4  	"io"
     5  	"net"
     6  
     7  	"github.com/sagernet/sing/common/x/list"
     8  )
     9  
    10  type Conn struct {
    11  	net.Conn
    12  	element *list.Element[io.Closer]
    13  }
    14  
    15  func NewConn(conn net.Conn) (net.Conn, error) {
    16  	connAccess.Lock()
    17  	element := openConnection.PushBack(conn)
    18  	connAccess.Unlock()
    19  	if KillerEnabled {
    20  		err := KillerCheck()
    21  		if err != nil {
    22  			conn.Close()
    23  			return nil, err
    24  		}
    25  	}
    26  	return &Conn{
    27  		Conn:    conn,
    28  		element: element,
    29  	}, nil
    30  }
    31  
    32  func (c *Conn) Close() error {
    33  	if c.element.Value != nil {
    34  		connAccess.Lock()
    35  		if c.element.Value != nil {
    36  			openConnection.Remove(c.element)
    37  			c.element.Value = nil
    38  		}
    39  		connAccess.Unlock()
    40  	}
    41  	return c.Conn.Close()
    42  }
    43  
    44  func (c *Conn) Upstream() any {
    45  	return c.Conn
    46  }
    47  
    48  func (c *Conn) ReaderReplaceable() bool {
    49  	return true
    50  }
    51  
    52  func (c *Conn) WriterReplaceable() bool {
    53  	return true
    54  }