mirror of
https://github.com/fankes/komari-agent.git
synced 2025-10-19 02:59:23 +08:00
Refactor monitoring package: remove platform-specific files and consolidate OS detection and process counting logic
- Deleted os_windows.go and process_windows.go, replacing them with platform-agnostic implementations in unit directory. - Removed Linux-specific process counting logic from process_linux.go and integrated it into unit. - Consolidated uptime and OS name retrieval into unit files for better organization. - Updated update mechanism to use global variables for current version and repository. - Introduced command-line flags for configuration, including disabling auto-update and web SSH. - Implemented WebSocket connection handling and terminal interaction for both Unix and Windows systems. - Added basic info upload functionality to server package, enhancing monitoring capabilities.
This commit is contained in:
80
monitoring/unit/ip.go
Normal file
80
monitoring/unit/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
|
||||
}
|
Reference in New Issue
Block a user