Skip to content

Commit

Permalink
读取tcp和udp数量换成net库
Browse files Browse the repository at this point in the history
  • Loading branch information
hopetree committed Apr 26, 2024
1 parent db25c08 commit 1c8655d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 26 deletions.
2 changes: 1 addition & 1 deletion app/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package app

const RuntimeVersion = "0.1.3"
const RuntimeVersion = "0.1.4"
29 changes: 4 additions & 25 deletions host/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package host
import (
"fmt"
"github.com/shirou/gopsutil/net"
"os/exec"
"strconv"
"strings"
"time"
)

Expand Down Expand Up @@ -54,33 +51,15 @@ func getNetworkSpeedStr() (uploadSpeedStr, downloadSpeedStr string, err error) {

func getConnections() (int, int, error) {
var tcp, udp int
cmd := exec.Command("sh", "-c", "expr $(ss -t | wc -l) - 1")

// 获取命令输出
tcpString, err := cmd.Output()
tcpConnCount, err := net.Connections("tcp")
if err == nil {
tcpNum, err := strconv.Atoi(strings.TrimSpace(string(tcpString)))
if err == nil {
tcp = tcpNum
if tcp < 0 {
tcp = 0
}
}
tcp = len(tcpConnCount)
}

cmd = exec.Command("sh", "-c", "expr $(ss -u | wc -l) - 1")

// 获取命令输出
udpString, err := cmd.Output()
udpConnCount, err := net.Connections("udp")
if err == nil {
udpNum, err := strconv.Atoi(strings.TrimSpace(string(udpString)))
if err == nil {
udp = udpNum
if udp < 0 {
udp = 0
}
}
udp = len(udpConnCount)
}

return tcp, udp, nil
}

0 comments on commit 1c8655d

Please sign in to comment.