mirror of
https://github.com/fankes/komari-agent.git
synced 2025-10-18 18:49:23 +08:00
feat: 自动发布realease
This commit is contained in:
80
monitoring/ip.go
Normal file
80
monitoring/ip.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package monitoring
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
var userAgent = "curl/8.0.1"
|
||||
|
||||
func GetIPv4Address() (string, error) {
|
||||
webAPIs := []string{"https://api.bilibili.com/x/web-interface/zone", "https://ip.sb", "https://api.ipify.org?format=json"}
|
||||
|
||||
for _, api := range webAPIs {
|
||||
// get ipv4
|
||||
req, err := http.NewRequest("GET", api, nil)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
req.Header.Set("User-Agent", userAgent)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
// 使用正则表达式从响应体中提取IPv4地址
|
||||
re := regexp.MustCompile(`\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}`)
|
||||
ipv4 := re.FindString(string(body))
|
||||
if ipv4 != "" {
|
||||
return ipv4, nil
|
||||
}
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func GetIPv6Address() (string, error) {
|
||||
webAPIs := []string{"https://api6.ipify.org?format=json", "https://ipv6.icanhazip.com"}
|
||||
|
||||
for _, api := range webAPIs {
|
||||
// get ipv6
|
||||
req, err := http.NewRequest("GET", api, nil)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
req.Header.Set("User-Agent", userAgent)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
// 使用正则表达式从响应体中提取IPv6地址
|
||||
re := regexp.MustCompile(`([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])`)
|
||||
ipv6 := re.FindString(string(body))
|
||||
if ipv6 != "" {
|
||||
return ipv6, nil
|
||||
}
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func GetIPAddress() (ipv4, ipv6 string, err error) {
|
||||
ipv4, err = GetIPv4Address()
|
||||
if err != nil {
|
||||
ipv4 = ""
|
||||
}
|
||||
ipv6, err = GetIPv6Address()
|
||||
if err != nil {
|
||||
ipv6 = ""
|
||||
}
|
||||
|
||||
return ipv4, ipv6, nil
|
||||
}
|
@@ -1,49 +0,0 @@
|
||||
package monitoring
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/sys/windows/registry"
|
||||
)
|
||||
|
||||
func OSName() string {
|
||||
if runtime.GOOS == "windows" {
|
||||
key, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
|
||||
if err != nil {
|
||||
return "Microsoft Windows"
|
||||
}
|
||||
defer key.Close()
|
||||
|
||||
productName, _, err := key.GetStringValue("ProductName")
|
||||
if err != nil {
|
||||
return "Microsoft Windows"
|
||||
}
|
||||
|
||||
return productName
|
||||
} else if runtime.GOOS == "linux" {
|
||||
file, err := os.Open("/etc/os-release")
|
||||
if err != nil {
|
||||
return "Linux"
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if strings.HasPrefix(line, "PRETTY_NAME=") {
|
||||
return strings.Trim(line[len("PRETTY_NAME="):], `"`)
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return "Linux"
|
||||
}
|
||||
|
||||
return "Linux"
|
||||
}
|
||||
|
||||
return "Unknown"
|
||||
}
|
32
monitoring/os_linux.go
Normal file
32
monitoring/os_linux.go
Normal file
@@ -0,0 +1,32 @@
|
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package monitoring
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func OSName() string {
|
||||
file, err := os.Open("/etc/os-release")
|
||||
if err != nil {
|
||||
return "Linux"
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if strings.HasPrefix(line, "PRETTY_NAME=") {
|
||||
return strings.Trim(line[len("PRETTY_NAME="):], `"`)
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return "Linux"
|
||||
}
|
||||
|
||||
return "Linux"
|
||||
}
|
23
monitoring/os_windows.go
Normal file
23
monitoring/os_windows.go
Normal file
@@ -0,0 +1,23 @@
|
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package monitoring
|
||||
|
||||
import (
|
||||
"golang.org/x/sys/windows/registry"
|
||||
)
|
||||
|
||||
func OSName() string {
|
||||
key, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
|
||||
if err != nil {
|
||||
return "Microsoft Windows"
|
||||
}
|
||||
defer key.Close()
|
||||
|
||||
productName, _, err := key.GetStringValue("ProductName")
|
||||
if err != nil {
|
||||
return "Microsoft Windows"
|
||||
}
|
||||
|
||||
return productName
|
||||
}
|
33
monitoring/process_linux.go
Normal file
33
monitoring/process_linux.go
Normal file
@@ -0,0 +1,33 @@
|
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package monitoring
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// ProcessCount returns the number of running processes
|
||||
func ProcessCount() (count int) {
|
||||
return processCountLinux()
|
||||
}
|
||||
|
||||
// processCountLinux counts processes by reading /proc directory
|
||||
func processCountLinux() (count int) {
|
||||
procDir := "/proc"
|
||||
|
||||
entries, err := os.ReadDir(procDir)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
for _, entry := range entries {
|
||||
if _, err := strconv.ParseInt(entry.Name(), 10, 64); err == nil {
|
||||
//if _, err := filepath.ParseInt(entry.Name(), 10, 64); err == nil {
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
@@ -1,38 +1,17 @@
|
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package monitoring
|
||||
|
||||
import (
|
||||
"os"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// ProcessCount returns the number of running processes
|
||||
func ProcessCount() (count int) {
|
||||
if runtime.GOOS == "windows" {
|
||||
return processCountWindows()
|
||||
}
|
||||
return processCountLinux()
|
||||
}
|
||||
return processCountWindows()
|
||||
|
||||
// processCountLinux counts processes by reading /proc directory
|
||||
func processCountLinux() (count int) {
|
||||
procDir := "/proc"
|
||||
|
||||
entries, err := os.ReadDir(procDir)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
for _, entry := range entries {
|
||||
if _, err := strconv.ParseInt(entry.Name(), 10, 64); err == nil {
|
||||
//if _, err := filepath.ParseInt(entry.Name(), 10, 64); err == nil {
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// processCountWindows counts processes using Windows API
|
Reference in New Issue
Block a user