github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/common/dialer/conntrack/track.go (about)

     1  package conntrack
     2  
     3  import (
     4  	"io"
     5  	"sync"
     6  
     7  	"github.com/sagernet/sing/common"
     8  	"github.com/sagernet/sing/common/x/list"
     9  )
    10  
    11  var (
    12  	connAccess     sync.RWMutex
    13  	openConnection list.List[io.Closer]
    14  )
    15  
    16  func Count() int {
    17  	if !Enabled {
    18  		return 0
    19  	}
    20  	return openConnection.Len()
    21  }
    22  
    23  func List() []io.Closer {
    24  	if !Enabled {
    25  		return nil
    26  	}
    27  	connAccess.RLock()
    28  	defer connAccess.RUnlock()
    29  	connList := make([]io.Closer, 0, openConnection.Len())
    30  	for element := openConnection.Front(); element != nil; element = element.Next() {
    31  		connList = append(connList, element.Value)
    32  	}
    33  	return connList
    34  }
    35  
    36  func Close() {
    37  	if !Enabled {
    38  		return
    39  	}
    40  	connAccess.Lock()
    41  	defer connAccess.Unlock()
    42  	for element := openConnection.Front(); element != nil; element = element.Next() {
    43  		common.Close(element.Value)
    44  		element.Value = nil
    45  	}
    46  	openConnection.Init()
    47  }