Files
umbrix-libcore/custom/status.go

70 lines
1.6 KiB
Go
Raw Normal View History

2023-09-10 20:18:33 +03:30
package main
import "C"
import (
"encoding/json"
2024-01-29 21:55:01 +01:00
"fmt"
2023-09-10 20:18:33 +03:30
"github.com/hiddify/libcore/bridge"
"github.com/hiddify/libcore/config"
2024-03-03 04:15:19 +01:00
pb "github.com/hiddify/libcore/hiddifyrpc"
v2 "github.com/hiddify/libcore/v2"
2023-09-10 20:18:33 +03:30
)
var statusPropagationPort int64
2024-03-03 04:15:19 +01:00
// var status = Stopped
2023-09-10 20:18:33 +03:30
type StatusMessage struct {
Status string `json:"status"`
Alert *string `json:"alert"`
Message *string `json:"message"`
}
2024-03-03 04:15:19 +01:00
func propagateStatus(newStatus pb.CoreState) {
v2.CoreState = newStatus
2023-09-10 20:18:33 +03:30
2024-03-03 04:15:19 +01:00
msg, _ := json.Marshal(StatusMessage{Status: convert2OldState(v2.CoreState)})
2023-09-10 20:18:33 +03:30
bridge.SendStringToPort(statusPropagationPort, string(msg))
}
2024-03-03 04:15:19 +01:00
func convert2OldState(newStatus pb.CoreState) string {
if newStatus == pb.CoreState_STOPPED {
return Stopped
}
if newStatus == pb.CoreState_STARTED {
return Started
}
if newStatus == pb.CoreState_STARTING {
return Starting
}
if newStatus == pb.CoreState_STOPPING {
return Stopping
}
return "Invalid"
}
func stopAndAlert(alert string, err error) (resultErr error) {
defer config.DeferPanicToError("stopAndAlert", func(err error) {
resultErr = err
})
2024-03-03 04:15:19 +01:00
v2.CoreState = pb.CoreState_STOPPED
message := err.Error()
fmt.Printf("Error: %s: %s\n", alert, message)
2024-03-03 04:15:19 +01:00
msg, _ := json.Marshal(StatusMessage{Status: convert2OldState(v2.CoreState), Alert: &alert, Message: &message})
bridge.SendStringToPort(statusPropagationPort, string(msg))
2024-03-09 21:07:15 +01:00
go config.DeactivateTunnelService()
if commandServer != nil {
commandServer.SetService(nil)
}
2024-03-03 04:15:19 +01:00
if v2.Box != nil {
v2.Box.Close()
v2.Box = nil
}
if commandServer != nil {
commandServer.Close()
}
2023-09-22 23:25:38 +03:30
return nil
2023-09-10 20:18:33 +03:30
}