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 {
|
2023-04-05 04:38:56 +08:00
|
|
|
if !Enabled {
|
|
|
|
return 0
|
|
|
|
}
|
2023-03-03 19:26:54 +08:00
|
|
|
return openConnection.Len()
|
|
|
|
}
|
|
|
|
|
2023-03-16 10:55:06 +08:00
|
|
|
func List() []io.Closer {
|
2023-04-05 04:38:56 +08:00
|
|
|
if !Enabled {
|
|
|
|
return nil
|
|
|
|
}
|
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() {
|
2023-04-05 04:38:56 +08:00
|
|
|
if !Enabled {
|
|
|
|
return
|
|
|
|
}
|
2023-03-03 19:26:54 +08:00
|
|
|
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
|
|
|
}
|