67 lines
1.1 KiB
Go
67 lines
1.1 KiB
Go
package watchdog
|
|
|
|
import (
|
|
"fmt"
|
|
"super-frpc/postLog"
|
|
"time"
|
|
)
|
|
|
|
func Connect() bool {
|
|
if IsConnected() {
|
|
return true
|
|
}
|
|
|
|
if err := Init(); err != nil {
|
|
return false
|
|
}
|
|
|
|
deadline := time.Now().Add(5 * time.Second)
|
|
var lastErr error
|
|
for {
|
|
if err := localSocketConnect(); err == nil {
|
|
break
|
|
} else {
|
|
lastErr = err
|
|
}
|
|
|
|
if time.Now().After(deadline) {
|
|
postLog.Error(fmt.Sprintf("[watchdog] failed to connect before timeout: %v", lastErr))
|
|
return false
|
|
}
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
}
|
|
|
|
response, err := sendMsg("watchdogAgentConnectionTest", 3)
|
|
if err != nil {
|
|
postLog.Error(fmt.Sprintf("[watchdog] connection test failed: %v", err))
|
|
Destroy()
|
|
return false
|
|
}
|
|
|
|
if response != "success" {
|
|
postLog.Error(fmt.Sprintf("[watchdog] connection test returned unexpected response: %s", response))
|
|
Destroy()
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func Disconnect() bool {
|
|
if !IsConnected() {
|
|
return true
|
|
}
|
|
|
|
err := Destroy()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
KillWatchdogProcess()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
return true
|
|
}
|