sing-box/common/dialer/conntrack/conn.go

55 lines
837 B
Go
Raw Normal View History

2023-03-03 11:26:54 +00:00
package conntrack
import (
2023-03-16 02:55:06 +00:00
"io"
2023-03-03 11:26:54 +00:00
"net"
"github.com/sagernet/sing/common/x/list"
)
type Conn struct {
net.Conn
2023-03-16 02:55:06 +00:00
element *list.Element[io.Closer]
2023-03-03 11:26:54 +00:00
}
2023-03-16 02:55:06 +00:00
func NewConn(conn net.Conn) (*Conn, error) {
2023-03-03 11:26:54 +00:00
connAccess.Lock()
2023-03-16 02:55:06 +00:00
element := openConnection.PushBack(conn)
2023-03-03 11:26:54 +00:00
connAccess.Unlock()
2023-03-16 02:55:06 +00:00
if KillerEnabled {
err := killerCheck()
if err != nil {
conn.Close()
return nil, err
}
}
2023-03-03 11:26:54 +00:00
return &Conn{
Conn: conn,
element: element,
2023-03-16 02:55:06 +00:00
}, nil
2023-03-03 11:26:54 +00:00
}
func (c *Conn) Close() error {
if c.element.Value != nil {
connAccess.Lock()
if c.element.Value != nil {
openConnection.Remove(c.element)
c.element.Value = nil
}
connAccess.Unlock()
}
return c.Conn.Close()
}
func (c *Conn) Upstream() any {
return c.Conn
}
func (c *Conn) ReaderReplaceable() bool {
return true
}
func (c *Conn) WriterReplaceable() bool {
return true
}