agent enable ipv6 connections stat

This commit is contained in:
naiba
2022-10-18 15:40:01 +00:00
parent b6a70cf32a
commit 2dfa6b2b86
6 changed files with 61 additions and 52 deletions

View File

@@ -19,7 +19,7 @@ import (
"github.com/blang/semver"
"github.com/go-ping/ping"
"github.com/gorilla/websocket"
"github.com/naiba/go-github-selfupdate/selfupdate"
"github.com/nezhahq/go-github-selfupdate/selfupdate"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/host"
psnet "github.com/shirou/gopsutil/v3/net"

View File

@@ -9,7 +9,7 @@ import (
"syscall"
"time"
"github.com/Erope/goss"
"github.com/nezhahq/goss"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/host"
@@ -80,7 +80,7 @@ func GetHost(agentConfig *model.AgentConfig) *model.Host {
Arch: hi.KernelArch,
Virtualization: hi.VirtualizationSystem,
BootTime: hi.BootTime,
IP: cachedIP,
IP: CachedIP,
CountryCode: strings.ToLower(cachedCountry),
Version: Version,
}
@@ -116,13 +116,22 @@ func GetState(agentConfig *model.AgentConfig, skipConnectionCount bool, skipProc
if !skipConnectionCount {
ss_err := true
if runtime.GOOS == "linux" {
tcpStat, err_tcp := goss.ConnectionsWithProtocol(syscall.IPPROTO_TCP)
udpStat, err_udp := goss.ConnectionsWithProtocol(syscall.IPPROTO_UDP)
tcpStat, err_tcp := goss.ConnectionsWithProtocol(goss.AF_INET, syscall.IPPROTO_TCP)
udpStat, err_udp := goss.ConnectionsWithProtocol(goss.AF_INET, syscall.IPPROTO_UDP)
if err_tcp == nil && err_udp == nil {
ss_err = false
tcpConnCount = uint64(len(tcpStat))
udpConnCount = uint64(len(udpStat))
}
if strings.Contains(CachedIP, ":") {
tcpStat6, err_tcp := goss.ConnectionsWithProtocol(goss.AF_INET6, syscall.IPPROTO_TCP)
udpStat6, err_udp := goss.ConnectionsWithProtocol(goss.AF_INET6, syscall.IPPROTO_UDP)
if err_tcp == nil && err_udp == nil {
ss_err = false
tcpConnCount = uint64(len(tcpStat6))
udpConnCount = uint64(len(udpStat6))
}
}
}
if ss_err {
conns, _ := net.Connections("all")

View File

@@ -2,7 +2,7 @@ package monitor
import (
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"
"time"
@@ -46,7 +46,7 @@ var (
// "https://ip.seeip.org/geoip", // 不精确
// "https://freegeoip.app/json/", // 需要 Key
}
cachedIP, cachedCountry string
CachedIP, cachedCountry string
httpClientV4 = utils.NewSingleStackHTTPClient(time.Second*20, time.Second*5, time.Second*10, false)
httpClientV6 = utils.NewSingleStackHTTPClient(time.Second*20, time.Second*5, time.Second*10, true)
)
@@ -61,9 +61,9 @@ func UpdateIP() {
continue
}
if ipv4.IP == "" || ipv6.IP == "" {
cachedIP = fmt.Sprintf("%s%s", ipv4.IP, ipv6.IP)
CachedIP = fmt.Sprintf("%s%s", ipv4.IP, ipv6.IP)
} else {
cachedIP = fmt.Sprintf("%s/%s", ipv4.IP, ipv6.IP)
CachedIP = fmt.Sprintf("%s/%s", ipv4.IP, ipv6.IP)
}
if ipv4.CountryCode != "" {
cachedCountry = ipv4.CountryCode
@@ -86,7 +86,7 @@ func fetchGeoIP(servers []string, isV6 bool) geoIP {
resp, err = httpClientV4.Get(servers[i])
}
if err == nil {
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
continue
}

View File

@@ -1,7 +1,7 @@
package monitor
import (
"io/ioutil"
"io"
"testing"
"github.com/stretchr/testify/assert"
@@ -13,7 +13,7 @@ func TestGeoIPApi(t *testing.T) {
for i := 0; i < len(geoIPApiList); i++ {
resp, err := httpGetWithUA(httpClientV4, geoIPApiList[i])
assert.Nil(t, err)
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
assert.Nil(t, err)
resp.Body.Close()
var ip geoIP