2023-03-03 11:26:54 +00: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 02:55:06 +00:00
|
|
|
openConnection list.List[io.Closer]
|
2023-03-03 11:26:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Count() int {
|
|
|
|
return openConnection.Len()
|
|
|
|
}
|
|
|
|
|
2023-03-16 02:55:06 +00:00
|
|
|
func List() []io.Closer {
|
2023-03-03 11:26:54 +00:00
|
|
|
connAccess.RLock()
|
|
|
|
defer connAccess.RUnlock()
|
2023-03-16 02:55:06 +00:00
|
|
|
connList := make([]io.Closer, 0, openConnection.Len())
|
2023-03-03 11:26:54 +00: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 02:55:06 +00:00
|
|
|
common.Close(element.Value)
|
2023-03-03 11:26:54 +00:00
|
|
|
element.Value = nil
|
|
|
|
}
|
2023-03-16 02:55:06 +00:00
|
|
|
openConnection.Init()
|
2023-03-03 11:26:54 +00:00
|
|
|
}
|