feat(watchdog): improve watchdog instance tracking

- Add Close() function to watchdog for graceful shutdown
- Update instance message format for better consistency
- Add watchdog status tracking in StatusInfo
- Include watchdog field in FrpcInstance struct and database
- Change port fields from string to int in FrpcProxyInfo
- Add auth_token support in InstanceInfo
This commit is contained in:
2026-04-05 22:44:53 +08:00
parent f006c2307a
commit 37c10f1c07
6 changed files with 75 additions and 31 deletions
+21 -6
View File
@@ -10,6 +10,7 @@ import (
"path/filepath"
"strconv"
"super-frpc/postLog"
"super-frpc/watchdog"
"time"
)
@@ -123,6 +124,7 @@ func CreateInstanceHandler(w http.ResponseWriter, r *http.Request) {
BootAtStart: req.BootAtStart,
RunUser: runUser,
ConfigPath: configPath,
Watchdog: 0,
}
if err := DBAddFrpcInstance(instance); err != nil {
@@ -459,7 +461,7 @@ func handleSystemConfigModify(w http.ResponseWriter, r *http.Request, instance F
func GetUserInstances(userID int) ([]FrpcInstance, error) {
rows, err := frpcDB.Query(`
SELECT id, userID, name, bootAtStart, runUser, configPath, createdAt
SELECT id, userID, name, bootAtStart, runUser, configPath, createdAt, watchdog
FROM frpcInstances WHERE userID = ?
`, userID)
if err != nil {
@@ -472,7 +474,7 @@ func GetUserInstances(userID int) ([]FrpcInstance, error) {
var instance FrpcInstance
var createdAtStr string
if err := rows.Scan(
&instance.ID, &instance.UserID, &instance.Name, &instance.BootAtStart, &instance.RunUser, &instance.ConfigPath, &createdAtStr,
&instance.ID, &instance.UserID, &instance.Name, &instance.BootAtStart, &instance.RunUser, &instance.ConfigPath, &createdAtStr, &instance.Watchdog,
); err != nil {
return nil, err
}
@@ -490,6 +492,16 @@ func getStringFromMap(m map[string]interface{}, key string) string {
return ""
}
func getNumFromMap(m map[string]interface{}, key string) int {
if v, ok := m[key].(int); ok {
return v
}
if v, ok := m[key].(float64); ok {
return int(v)
}
return 0
}
func ListInstancesHandler(w http.ResponseWriter, r *http.Request) {
userID, err := Auth(w, r, http.MethodGet)
if err != nil {
@@ -583,7 +595,7 @@ func StartInstanceHandler(w http.ResponseWriter, r *http.Request) {
return
}
switch initType {
switch initType { // Lunch frpc instance by init system
case "windows":
if err := StartWindowsService(serviceName); err != nil {
postLog.Error(fmt.Sprintf("[StartInstanceHandler] Failed to start Windows service %s: %v", serviceName, err))
@@ -591,7 +603,6 @@ func StartInstanceHandler(w http.ResponseWriter, r *http.Request) {
return
}
postLog.Info(fmt.Sprintf("[StartInstanceHandler] Windows service %s started successfully", serviceName))
SendSuccessResponse(w, "Instance started successfully", nil)
case "systemd":
if err := StartSystemdService(serviceName); err != nil {
@@ -600,7 +611,6 @@ func StartInstanceHandler(w http.ResponseWriter, r *http.Request) {
return
}
postLog.Info(fmt.Sprintf("[StartInstanceHandler] Systemd service %s started successfully", serviceName))
SendSuccessResponse(w, "Instance started successfully", nil)
case "init.d":
if err := StartInitDService(serviceName); err != nil {
@@ -609,13 +619,18 @@ func StartInstanceHandler(w http.ResponseWriter, r *http.Request) {
return
}
postLog.Info(fmt.Sprintf("[StartInstanceHandler] Init.d service %s started successfully", serviceName))
SendSuccessResponse(w, "Instance started successfully", nil)
default:
postLog.Error(fmt.Sprintf("[StartInstanceHandler] Unsupported init system: %s", initType))
SendErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Unsupported init system: %s", initType))
return
}
if is.watchdogConnected {
watchdog.AddInstance(serviceName)
}
SendSuccessResponse(w, "Instance started successfully", nil)
}
func StopInstanceHandler(w http.ResponseWriter, r *http.Request) {