feat: add foreground mode, pause/resume commands, API docs

This commit is contained in:
2026-03-25 00:12:14 +08:00
parent b00f95b7fb
commit 8ce1e6531a
8 changed files with 502 additions and 76 deletions

View File

@@ -43,7 +43,14 @@ func GetLogDir() string {
func LoadConfig() (*Config, error) {
configDir := GetConfigDir()
configPath := filepath.Join(configDir, "config.json")
return loadConfigFile(configPath)
}
func LoadConfigFrom(path string) (*Config, error) {
return loadConfigFile(path)
}
func loadConfigFile(configPath string) (*Config, error) {
data, err := os.ReadFile(configPath)
if err != nil {
if os.IsNotExist(err) {

52
pkg/config/pause.go Normal file
View File

@@ -0,0 +1,52 @@
package config
import (
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
// SetPause writes a timestamp into pause_until.txt
func SetPause(until time.Time) error {
dir := GetConfigDir()
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
path := filepath.Join(dir, "pause_until.txt")
ts := strconv.FormatInt(until.Unix(), 10)
return os.WriteFile(path, []byte(ts), 0644)
}
// ClearPause removes the pause_until.txt file
func ClearPause() error {
path := filepath.Join(GetConfigDir(), "pause_until.txt")
return os.Remove(path)
}
// IsPaused checks if the service is currently paused via IPC file
func IsPaused() (bool, int64) {
path := filepath.Join(GetConfigDir(), "pause_until.txt")
data, err := os.ReadFile(path)
if err != nil {
return false, 0
}
tsStr := strings.TrimSpace(string(data))
untilUnix, err := strconv.ParseInt(tsStr, 10, 64)
if err != nil {
// Invalid file, clean it up
os.Remove(path)
return false, 0
}
now := time.Now().Unix()
if now < untilUnix {
return true, untilUnix - now
}
// Expired
os.Remove(path)
return false, 0
}

View File

@@ -34,6 +34,24 @@ func InitLogger() error {
return nil
}
func SwitchToFrontLog() {
if fileLogger != nil {
fileLogger.Close()
}
logDir := config.GetLogDir()
if err := os.MkdirAll(logDir, 0755); err != nil {
logDir = "logs"
}
logFile := filepath.Join(logDir, "network_monitor_front.log")
fileLogger = &lumberjack.Logger{
Filename: logFile,
MaxSize: 10,
MaxBackups: 30,
MaxAge: 30,
Compress: false,
}
}
func EnableDebug() {
debugEnabled = true
}

View File

@@ -18,6 +18,7 @@ func Run(ctx context.Context, cfg *config.Config) {
var failureStartTime time.Time
var inFailureWindow bool = false
var wasPausedBefore bool = false
normalStatusCounter := 0
const normalStatusLogInterval = 24
@@ -30,6 +31,27 @@ func Run(ctx context.Context, cfg *config.Config) {
default:
}
isPaused, remainingSec := config.IsPaused()
if isPaused {
if !wasPausedBefore {
mins := remainingSec / 60
if mins < 1 {
mins = 1
}
logger.Info("进入管理员特设休眠挂起状态, 预计将在 %d 分钟后自动唤醒", mins)
wasPausedBefore = true
if inFailureWindow {
inFailureWindow = false
normalStatusCounter = 0
}
}
time.Sleep(15 * time.Second)
continue
} else if wasPausedBefore {
logger.Info("休眠指令逾期或已被撤消, 唤醒并恢复网络探测")
wasPausedBefore = false
}
isOnline := pinger.Ping(cfg.TargetIP, 3)
if isOnline {