sing-box/common/conntrack/track.go

48 lines
841 B
Go
Raw Permalink Normal View History

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