release: v0.2.0
Some checks failed
Release / Build & Release (push) Has been cancelled
Some checks failed
Release / Build & Release (push) Has been cancelled
Comprehensive release containing structural, UX, and behavioral upgrades since v0.1.0: 1. Namespace Transition: Renamed core executable and project namespaces from 'smart-monitor' to 'smart-shutdown'. 2. Objective Vocabulary Refactoring: Normalized output strings and logging descriptors system-wide to a strict, professional tone. 3. Advanced Status Query: 'status' subcommand now retrieves the parsed configuration, log locations/sizes, and tails the last 10 lines of the local log file. 4. Runtime Configuration Setter: Introduced 'config set' subcommand to modify the configuration file with strict type validations. 5. Auto-System Deployment: Remapped 'install' to clone the executable into system domains and register global PATH variables. 6. Cleaner Removal: 'uninstall' purges binary clones and clears environmental variables, leaving zero traces. 7. Documentation Rewrite: Generated an objective README file featuring copy-paste ready Markdown blocks.
This commit is contained in:
@@ -7,35 +7,28 @@ import (
|
||||
"smart-shutdown/pkg/logger"
|
||||
)
|
||||
|
||||
// Ping 连通性测试,向指定的 targetIP 发送 ICMP 请求。
|
||||
// 如果规定时间内可达返回 true,超时或者全部丢包返回 false。
|
||||
func Ping(targetIP string, timeoutSec int) bool {
|
||||
pinger, err := probing.NewPinger(targetIP)
|
||||
if err != nil {
|
||||
logger.Warn("解析目标 IP 失败 [%s]: %v", targetIP, err)
|
||||
logger.Warn("目标 IP 解析异常 [%s]: %v", targetIP, err)
|
||||
return false
|
||||
}
|
||||
|
||||
// 大部分操作系统为了安全,普通程序发 ICMP 包会被拦截。开启 Privileged 会尝试使用 raw sockets 发行
|
||||
// 本程序由于本身作为系统服务(System 或 Root 权限)运行,故直接开启特权模式以保障发包成功率。
|
||||
pinger.SetPrivileged(true)
|
||||
|
||||
// 只发送 1 个探测包
|
||||
pinger.Count = 1
|
||||
pinger.Timeout = time.Duration(timeoutSec) * time.Second
|
||||
|
||||
err = pinger.Run()
|
||||
if err != nil {
|
||||
logger.Fail("Ping 测试运行时遇到异常: %v", err)
|
||||
logger.Fail("探针执行失败: %v", err)
|
||||
return false
|
||||
}
|
||||
|
||||
stats := pinger.Statistics()
|
||||
// 只要成功收到至少 1 个回包,认为网络通畅
|
||||
if stats.PacketsRecv > 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
// 丢包或者无回音
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user