feat: add initial project structure with logging and watchdog service
Implement basic watchdog service for Linux systemd with: - Configuration loading - Logging system with database support - WebSocket log broadcasting - TCP server for agent communication - Project setup with Go modules
This commit is contained in:
78
main.go
Normal file
78
main.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"Watchdog_Linux-systemd/postLog"
|
||||
"fmt"
|
||||
"net"
|
||||
)
|
||||
|
||||
const (
|
||||
listenAddr = "127.0.0.1"
|
||||
listenPort = "10080"
|
||||
Type = "tcp"
|
||||
)
|
||||
|
||||
type SoftwareInfo struct {
|
||||
Name string
|
||||
Version string
|
||||
Developer string
|
||||
BuildVer int16
|
||||
Description string
|
||||
BuildType string
|
||||
}
|
||||
|
||||
var softwareInfo SoftwareInfo = SoftwareInfo{
|
||||
Name: "Watchdog_Linux-systemd",
|
||||
Version: "0.0.1",
|
||||
Developer: "Super-frpc",
|
||||
BuildVer: 1,
|
||||
Description: "Super-frpc Watchdog service for Linux systemd",
|
||||
BuildType: "Debug",
|
||||
}
|
||||
|
||||
var isDebug bool
|
||||
|
||||
func main() {
|
||||
loadConfig()
|
||||
if isDebug == true {
|
||||
postLog.SetDebugMode(true)
|
||||
}
|
||||
postLog.Info(fmt.Sprintf("%s %s (Build %d.%s) by %s", softwareInfo.Name, softwareInfo.Version, softwareInfo.BuildVer, softwareInfo.BuildType, softwareInfo.Developer))
|
||||
listen, err := net.Listen(Type, fmt.Sprintf("%s:%s", listenAddr, listenPort))
|
||||
if err != nil {
|
||||
postLog.Fatal(fmt.Sprintf("Failed to listen: %v, err: %v, %v", listenAddr, listenPort, err))
|
||||
}
|
||||
defer listen.Close()
|
||||
postLog.Info(fmt.Sprintf("Server is running on %s:%s", listenAddr, listenPort))
|
||||
|
||||
for {
|
||||
conn, err := listen.Accept()
|
||||
if err != nil {
|
||||
postLog.Error(fmt.Sprintf("Failed to accept: %v, err: %v", conn, err))
|
||||
}
|
||||
go handleRequest(conn)
|
||||
}
|
||||
}
|
||||
|
||||
func handleRequest(conn net.Conn) {
|
||||
defer conn.Close()
|
||||
|
||||
buffer := make([]byte, 1024)
|
||||
n, err := conn.Read(buffer)
|
||||
if err != nil {
|
||||
postLog.Error(fmt.Sprintf("Failed to read: %v, err: %v", conn, err))
|
||||
return
|
||||
}
|
||||
|
||||
recvMsg := string(buffer[:n])
|
||||
postLog.Debug(fmt.Sprintf("Received: %v", recvMsg))
|
||||
responseMsg := ""
|
||||
if recvMsg == "watchdogAgentConnectionTest" {
|
||||
responseMsg = "success"
|
||||
} else {
|
||||
responseMsg = "error: unknown message"
|
||||
}
|
||||
if _, err := conn.Write([]byte(responseMsg)); err != nil {
|
||||
postLog.Error(fmt.Sprintf("Failed to write: %v, err: %v", conn, err))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user