feat: 自动发布realease

This commit is contained in:
Akizon77
2025-04-29 20:57:23 +08:00
parent f21001c71e
commit 79d1760163
13 changed files with 324 additions and 238 deletions

80
monitoring/ip.go Normal file
View 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
}

View File

@@ -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
View 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
View 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
}

View 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
}

View File

@@ -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