refactor(socket): update socket handling to use a local socket and improve connection management
This commit is contained in:
17
config.go
17
config.go
@@ -1,6 +1,5 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"Watchdog_Linux-systemd/postLog"
|
"Watchdog_Linux-systemd/postLog"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -10,10 +9,6 @@ import (
|
|||||||
|
|
||||||
var Config struct {
|
var Config struct {
|
||||||
DebugMode bool `json:"debugMode"`
|
DebugMode bool `json:"debugMode"`
|
||||||
Debug struct {
|
|
||||||
ListenAddr string `json:"listenAddr"`
|
|
||||||
ListenPort int `json:"listenPort"`
|
|
||||||
} `json:"debug"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadConfig() {
|
func loadConfig() {
|
||||||
@@ -26,19 +21,9 @@ func loadConfig() {
|
|||||||
if err := decoder.Decode(&Config); err != nil {
|
if err := decoder.Decode(&Config); err != nil {
|
||||||
postLog.Fatal(fmt.Sprintf("Failed to decode config file: %v, err: %v", configFile, err))
|
postLog.Fatal(fmt.Sprintf("Failed to decode config file: %v, err: %v", configFile, err))
|
||||||
}
|
}
|
||||||
|
|
||||||
if Config.DebugMode {
|
if Config.DebugMode {
|
||||||
isDebug = true
|
isDebug = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if Config.Debug.ListenAddr != "" {
|
|
||||||
DebugListenAddr = Config.Debug.ListenAddr
|
|
||||||
} else {
|
|
||||||
DebugListenAddr = "0.0.0.0"
|
|
||||||
}
|
|
||||||
if Config.Debug.ListenPort != 0 {
|
|
||||||
DebugListenPort = Config.Debug.ListenPort
|
|
||||||
} else {
|
|
||||||
DebugListenPort = 10080
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,3 @@
|
|||||||
{
|
{
|
||||||
"debugMode": true,
|
"debugMode": true
|
||||||
"debug": {
|
}
|
||||||
"listenAddr": "0.0.0.0",
|
|
||||||
"listenPort": 10080
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
13
main.go
13
main.go
@@ -7,12 +7,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
listenAddr = "127.0.0.1"
|
|
||||||
listenPort = 10080
|
|
||||||
Type = "tcp"
|
|
||||||
)
|
|
||||||
|
|
||||||
type SoftwareInfo struct {
|
type SoftwareInfo struct {
|
||||||
Name string
|
Name string
|
||||||
Version string
|
Version string
|
||||||
@@ -33,15 +27,10 @@ var softwareInfo SoftwareInfo = SoftwareInfo{
|
|||||||
|
|
||||||
var isDebug bool
|
var isDebug bool
|
||||||
|
|
||||||
var DebugListenAddr string
|
|
||||||
var DebugListenPort int
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
loadConfig()
|
loadConfig()
|
||||||
if isDebug == true {
|
if isDebug == true {
|
||||||
postLog.SetDebugMode(true)
|
postLog.SetDebugMode(true)
|
||||||
listenAddr = DebugListenAddr
|
|
||||||
listenPort = DebugListenPort
|
|
||||||
}
|
}
|
||||||
postLog.Info(fmt.Sprintf("%s %s (Build %d.%s) by %s", softwareInfo.Name, softwareInfo.Version, softwareInfo.BuildVer, softwareInfo.BuildType, softwareInfo.Developer))
|
postLog.Info(fmt.Sprintf("%s %s (Build %d.%s) by %s", softwareInfo.Name, softwareInfo.Version, softwareInfo.BuildVer, softwareInfo.BuildType, softwareInfo.Developer))
|
||||||
|
|
||||||
@@ -50,7 +39,7 @@ func main() {
|
|||||||
// End of command handler
|
// End of command handler
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
err := socket.BootSocket(Type, listenAddr, listenPort)
|
err := socket.BootSocket()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
postLog.Fatal(fmt.Sprintf("Failed to initialize socket server: %v", err))
|
postLog.Fatal(fmt.Sprintf("Failed to initialize socket server: %v", err))
|
||||||
}
|
}
|
||||||
|
|||||||
207
socket/server.go
207
socket/server.go
@@ -1,87 +1,120 @@
|
|||||||
package socket
|
package socket
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"Watchdog_Linux-systemd/postLog"
|
"Watchdog_Linux-systemd/postLog"
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"os"
|
||||||
)
|
"strings"
|
||||||
|
"sync"
|
||||||
var (
|
)
|
||||||
Conn net.Conn
|
|
||||||
CommandHandler func(string) error
|
const SocketPath = "/tmp/super-frpc-watchdog.sock"
|
||||||
)
|
|
||||||
|
var (
|
||||||
func BootSocket(networkType, listenAddr string, listenPort int) error {
|
Conn net.Conn
|
||||||
listen, err := net.Listen(networkType, fmt.Sprintf("%s:%d", listenAddr, listenPort))
|
connMutex sync.Mutex
|
||||||
if err != nil {
|
CommandHandler func(string) error
|
||||||
postLog.Fatal(fmt.Sprintf("[Socket] Failed to listen: %v, err: %v, %v", listenAddr, listenPort, err))
|
)
|
||||||
return fmt.Errorf("failed to listen: %v, err: %v, %v", listenAddr, listenPort, err)
|
|
||||||
}
|
func BootSocket() error {
|
||||||
defer listen.Close()
|
if err := os.RemoveAll(SocketPath); err != nil {
|
||||||
|
return fmt.Errorf("failed to remove stale socket %s: %w", SocketPath, err)
|
||||||
postLog.Info(fmt.Sprintf("Server is running on %s:%d", listenAddr, listenPort))
|
}
|
||||||
|
|
||||||
for {
|
listen, err := net.Listen("unix", SocketPath)
|
||||||
Conn, err = listen.Accept()
|
if err != nil {
|
||||||
if err != nil {
|
postLog.Fatal(fmt.Sprintf("[Socket] Failed to listen on %s: %v", SocketPath, err))
|
||||||
postLog.Error(fmt.Sprintf("Failed to accept: %v, err: %v", Conn, err))
|
return fmt.Errorf("failed to listen on %s: %w", SocketPath, err)
|
||||||
}
|
}
|
||||||
go handleRequest()
|
defer func() {
|
||||||
}
|
listen.Close()
|
||||||
}
|
os.Remove(SocketPath)
|
||||||
|
}()
|
||||||
func handleRequest() {
|
|
||||||
defer Conn.Close()
|
postLog.Info(fmt.Sprintf("Server is running on local socket %s", SocketPath))
|
||||||
|
|
||||||
reader := bufio.NewReader(Conn)
|
for {
|
||||||
|
conn, err := listen.Accept()
|
||||||
for {
|
if err != nil {
|
||||||
data, err := reader.ReadBytes('\n')
|
postLog.Error(fmt.Sprintf("Failed to accept local socket connection: %v", err))
|
||||||
if err != nil {
|
continue
|
||||||
return
|
}
|
||||||
}
|
connMutex.Lock()
|
||||||
|
if Conn != nil {
|
||||||
recvMsg := strings.TrimSpace(string(data))
|
Conn.Close()
|
||||||
|
}
|
||||||
responseMsg := ""
|
Conn = conn
|
||||||
if len(recvMsg) != 0 {
|
connMutex.Unlock()
|
||||||
postLog.Debug(fmt.Sprintf("Received message: %s", recvMsg))
|
|
||||||
if recvMsg == "watchdogAgentConnectionTest" {
|
go handleRequest(conn)
|
||||||
responseMsg = "success"
|
}
|
||||||
} else {
|
}
|
||||||
if CommandHandler != nil {
|
|
||||||
err := CommandHandler(recvMsg)
|
func handleRequest(conn net.Conn) {
|
||||||
if err != nil {
|
defer func() {
|
||||||
responseMsg = fmt.Sprintf("error: %v", err)
|
conn.Close()
|
||||||
} else {
|
connMutex.Lock()
|
||||||
responseMsg = "success"
|
if Conn == conn {
|
||||||
}
|
Conn = nil
|
||||||
} else {
|
}
|
||||||
responseMsg = "error: command handler not initialized"
|
connMutex.Unlock()
|
||||||
}
|
}()
|
||||||
}
|
|
||||||
}
|
reader := bufio.NewReader(conn)
|
||||||
|
|
||||||
Conn.Write([]byte(responseMsg + "\n"))
|
for {
|
||||||
}
|
data, err := reader.ReadBytes('\n')
|
||||||
}
|
if err != nil {
|
||||||
|
return
|
||||||
func SendMsg(msg string) error {
|
}
|
||||||
if Conn == nil {
|
|
||||||
return fmt.Errorf("connection is nil")
|
recvMsg := strings.TrimSpace(string(data))
|
||||||
}
|
|
||||||
|
responseMsg := ""
|
||||||
data := []byte(msg + "\n")
|
if len(recvMsg) != 0 {
|
||||||
n, err := Conn.Write(data)
|
postLog.Debug(fmt.Sprintf("Received message: %s", recvMsg))
|
||||||
if err != nil {
|
if recvMsg == "watchdogAgentConnectionTest" {
|
||||||
return fmt.Errorf("failed to write message: %v", err)
|
responseMsg = "success"
|
||||||
}
|
} else {
|
||||||
|
if CommandHandler != nil {
|
||||||
if n != len(data) {
|
err := CommandHandler(recvMsg)
|
||||||
return fmt.Errorf("incomplete write: wrote %d bytes out of %d", n, len(data))
|
if err != nil {
|
||||||
}
|
responseMsg = fmt.Sprintf("error: %v", err)
|
||||||
|
} else {
|
||||||
return nil
|
responseMsg = "success"
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
responseMsg = "error: command handler not initialized"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := conn.Write([]byte(responseMsg + "\n")); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func SendMsg(msg string) error {
|
||||||
|
connMutex.Lock()
|
||||||
|
conn := Conn
|
||||||
|
connMutex.Unlock()
|
||||||
|
|
||||||
|
if conn == nil {
|
||||||
|
return fmt.Errorf("connection is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
data := []byte(msg + "\n")
|
||||||
|
n, err := conn.Write(data)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to write message: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if n != len(data) {
|
||||||
|
return fmt.Errorf("incomplete write: wrote %d bytes out of %d", n, len(data))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user