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

39 lines
754 B
Go
Raw Permalink Normal View History

2023-03-03 19:26:54 +08:00
package conntrack
import (
"io"
"sync"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/x/list"
)
var (
connAccess sync.RWMutex
2023-03-16 10:55:06 +08:00
openConnection list.List[io.Closer]
2023-03-03 19:26:54 +08:00
)
func Count() int {
return openConnection.Len()
}
2023-03-16 10:55:06 +08:00
func List() []io.Closer {
2023-03-03 19:26:54 +08:00
connAccess.RLock()
defer connAccess.RUnlock()
2023-03-16 10:55:06 +08:00
connList := make([]io.Closer, 0, openConnection.Len())
2023-03-03 19:26:54 +08:00
for element := openConnection.Front(); element != nil; element = element.Next() {
connList = append(connList, element.Value)
}
return connList
}
func Close() {
connAccess.Lock()
defer connAccess.Unlock()
for element := openConnection.Front(); element != nil; element = element.Next() {
2023-03-16 10:55:06 +08:00
common.Close(element.Value)
2023-03-03 19:26:54 +08:00
element.Value = nil
}
2023-03-16 10:55:06 +08:00
openConnection.Init()
2023-03-03 19:26:54 +08:00
}