style: using is struct to handle all judgements global envs

This commit is contained in:
2026-04-02 21:38:50 +08:00
parent aa22b04a1f
commit 78bd1cb3cc
5 changed files with 48 additions and 27 deletions

3
.vscode/launch.json vendored
View File

@@ -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}/../"
}
]
}

View File

@@ -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")
}

38
main.go
View File

@@ -22,23 +22,31 @@ type SoftwareInfo struct {
BuildType string
}
var softwareInfo SoftwareInfo
type StatusInfo struct {
Status string
}
var isDebug bool
var isOnline bool
func main() {
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
}
type Is struct {
debug bool
online bool
watchdogConnected bool
}
var is Is = Is {
debug: false,
online: false,
watchdogConnected: false,
}
func main() {
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)

View File

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

View File

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