Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/MHSanaei/3x-ui.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/util/sys
diff options
context:
space:
mode:
Diffstat (limited to 'util/sys')
-rw-r--r--util/sys/psutil.go2
-rw-r--r--util/sys/sys_linux.go4
-rw-r--r--util/sys/sys_windows.go5
3 files changed, 11 insertions, 0 deletions
diff --git a/util/sys/psutil.go b/util/sys/psutil.go
index 3d7cac80..98adf775 100644
--- a/util/sys/psutil.go
+++ b/util/sys/psutil.go
@@ -1,3 +1,5 @@
+// Package sys provides system utilities for monitoring network connections and CPU usage.
+// Platform-specific implementations are provided for Windows, Linux, and macOS.
package sys
import (
diff --git a/util/sys/sys_linux.go b/util/sys/sys_linux.go
index 8a494d62..23483b57 100644
--- a/util/sys/sys_linux.go
+++ b/util/sys/sys_linux.go
@@ -45,6 +45,8 @@ func getLinesNum(filename string) (int, error) {
return sum, nil
}
+// GetTCPCount returns the number of active TCP connections by reading
+// /proc/net/tcp and /proc/net/tcp6 when available.
func GetTCPCount() (int, error) {
root := HostProc()
@@ -75,6 +77,8 @@ func GetUDPCount() (int, error) {
return udp4 + udp6, nil
}
+// safeGetLinesNum returns 0 if the file does not exist, otherwise forwards
+// to getLinesNum to count the number of lines.
func safeGetLinesNum(path string) (int, error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
return 0, nil
diff --git a/util/sys/sys_windows.go b/util/sys/sys_windows.go
index f3eae076..186fa4bb 100644
--- a/util/sys/sys_windows.go
+++ b/util/sys/sys_windows.go
@@ -12,6 +12,7 @@ import (
"github.com/shirou/gopsutil/v4/net"
)
+// GetConnectionCount returns the number of active connections for the specified protocol ("tcp" or "udp").
func GetConnectionCount(proto string) (int, error) {
if proto != "tcp" && proto != "udp" {
return 0, errors.New("invalid protocol")
@@ -24,10 +25,12 @@ func GetConnectionCount(proto string) (int, error) {
return len(stats), nil
}
+// GetTCPCount returns the number of active TCP connections.
func GetTCPCount() (int, error) {
return GetConnectionCount("tcp")
}
+// GetUDPCount returns the number of active UDP connections.
func GetUDPCount() (int, error) {
return GetConnectionCount("udp")
}
@@ -50,6 +53,8 @@ type filetime struct {
HighDateTime uint32
}
+// ftToUint64 converts a Windows FILETIME-like struct to a uint64 for
+// arithmetic and delta calculations used by CPUPercentRaw.
func ftToUint64(ft filetime) uint64 {
return (uint64(ft.HighDateTime) << 32) | uint64(ft.LowDateTime)
}