From 78bd1cb3cc8fa9db1bc103c05c597018206ca7e9 Mon Sep 17 00:00:00 2001 From: NanamiAdmin Date: Thu, 2 Apr 2026 21:38:50 +0800 Subject: [PATCH] style: using is struct to handle all judgements global envs --- .vscode/launch.json | 3 ++- handlers.go | 2 +- main.go | 34 +++++++++++++++++++++------------- session.go | 2 +- watchdog/tcpClient.go | 34 +++++++++++++++++++++++----------- 5 files changed, 48 insertions(+), 27 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 608d3c6..944e897 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,12 +4,13 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { "name": "Launch Package", "type": "go", "request": "launch", "mode": "auto", - "program": "${fileDirname}" + "program": "${fileDirname}/../" } ] } \ No newline at end of file diff --git a/handlers.go b/handlers.go index cb4f3e9..f7229b5 100644 --- a/handlers.go +++ b/handlers.go @@ -54,7 +54,7 @@ func Auth(w http.ResponseWriter, r *http.Request, targetMethod string, allowedUs return 0, fmt.Errorf("Method not allowed: %s", targetMethod) } - if !isDebug && !ValidateTimeStamp(r.Header) { + if !is.debug && !ValidateTimeStamp(r.Header) { return 0, fmt.Errorf("Invalid or missing X-Timestamp in header") } diff --git a/main.go b/main.go index bd29aba..eed937a 100644 --- a/main.go +++ b/main.go @@ -22,23 +22,31 @@ type SoftwareInfo struct { BuildType string } -var softwareInfo SoftwareInfo +var softwareInfo SoftwareInfo = SoftwareInfo{ + Name: "Super-frpc", + Version: "0.0.1", + Developer: "Madobi Nanami", + BuildVer: 1, + BuildType: "debug", +} type StatusInfo struct { Status string } -var isDebug bool -var isOnline bool +type Is struct { + debug bool + online bool + watchdogConnected bool +} + +var is Is = Is { + debug: false, + online: false, + watchdogConnected: false, +} func main() { - softwareInfo = SoftwareInfo{ - Name: "Super-frpc", - Version: "0.0.1", - Developer: "Madobi Nanami", - BuildVer: 1, - BuildType: "debug", - } postLog.Info(fmt.Sprintf("%s %s (Build %d.%s) by %s", softwareInfo.Name, softwareInfo.Version, softwareInfo.BuildVer, softwareInfo.BuildType, softwareInfo.Developer)) configPath := flag.String("config", "./config.json", "path to config file") dbPath_data := flag.String("db", "./database.db", "path to database file") @@ -63,7 +71,7 @@ func main() { } postLog.SetDebugMode(config.Debug) - isDebug = config.Debug + is.debug = config.Debug if err := postLog.InitLogsDatabase(*dbPath_log); err != nil { postLog.Fatal(fmt.Sprintf("Failed to initialize logs database: %v", err)) @@ -112,7 +120,7 @@ func main() { go func() { postLog.Info(fmt.Sprintf("Server starting on %s", addr)) - isOnline = true + is.online = true if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { postLog.Fatal(fmt.Sprintf("Failed to start server: %v", err)) } @@ -152,7 +160,7 @@ func GetStatusHandler(w http.ResponseWriter, r *http.Request) { statusInfo := StatusInfo{ Status: "Online", } - if !isOnline { + if !is.online { statusInfo.Status = "Offline" } SendSuccessResponse(w, "getStatus", statusInfo) diff --git a/session.go b/session.go index 88d3818..2b2f159 100644 --- a/session.go +++ b/session.go @@ -221,7 +221,7 @@ func isValidPassword(password string) bool { // Validate password complexity and } func ValidateTimeStamp(header http.Header) bool { - if isDebug { + if is.debug { return true } diff --git a/watchdog/tcpClient.go b/watchdog/tcpClient.go index 811a7b2..ca1dc99 100644 --- a/watchdog/tcpClient.go +++ b/watchdog/tcpClient.go @@ -15,12 +15,9 @@ var ( isConnected bool recvChan chan string stopRecvChan chan struct{} + recvWg sync.WaitGroup ) -func notifyMessage(message string) { - postLog.Info(fmt.Sprintf("%s", message)) -} - func Init() error { tcpConnMutex.Lock() defer tcpConnMutex.Unlock() @@ -53,6 +50,7 @@ func tcpConnect(ipaddr string, port int) error { tcpConn = conn isConnected = true + recvWg.Add(1) go recvMsg() return nil @@ -66,7 +64,7 @@ func sendMsg(message string, target int) (string, error) { return "", fmt.Errorf("not connected") } - _, err := tcpConn.Write([]byte(message + "\n")) + _, err := tcpConn.Write([]byte(message)) if err != nil { tcpConnMutex.Unlock() return "", fmt.Errorf("failed to send message: %v", err) @@ -83,13 +81,23 @@ func sendMsg(message string, target int) (string, error) { } func recvMsg() { - reader := bufio.NewReader(tcpConn) + defer recvWg.Done() + for { + tcpConnMutex.Lock() + if tcpConn == nil { + tcpConnMutex.Unlock() + return + } + conn := tcpConn + tcpConnMutex.Unlock() + select { case <-stopRecvChan: return default: - tcpConn.SetReadDeadline(time.Now().Add(100 * time.Millisecond)) + conn.SetReadDeadline(time.Now().Add(100 * time.Millisecond)) + reader := bufio.NewReader(conn) line, err := reader.ReadString('\n') if err != nil { if netErr, ok := err.(net.Error); ok && netErr.Timeout() { @@ -118,7 +126,7 @@ func recvMsg() { } if len(line) > 0 && line != "success" && !isResponseMessage(line) { - notifyMessage(line) + postLog.Debug(fmt.Sprintf("[Watchdog] TCP Socket received message: %s", line)) } } } @@ -129,17 +137,21 @@ func isResponseMessage(msg string) bool { return msg == "success" || msg == "failed" } - - func Destroy() error { tcpConnMutex.Lock() - defer tcpConnMutex.Unlock() if stopRecvChan != nil { close(stopRecvChan) stopRecvChan = nil } + tcpConnMutex.Unlock() + + recvWg.Wait() + + tcpConnMutex.Lock() + defer tcpConnMutex.Unlock() + if tcpConn != nil { err := tcpConn.Close() tcpConn = nil