new: add hiddifyrpc

This commit is contained in:
Hiddify
2024-03-03 04:15:19 +01:00
parent 0aa2c99599
commit a4b067006a
22 changed files with 3715 additions and 29 deletions

View File

@@ -7,10 +7,13 @@ import (
"github.com/hiddify/libcore/bridge"
"github.com/hiddify/libcore/config"
pb "github.com/hiddify/libcore/hiddifyrpc"
v2 "github.com/hiddify/libcore/v2"
)
var statusPropagationPort int64
var status = Stopped
// var status = Stopped
type StatusMessage struct {
Status string `json:"status"`
@@ -18,30 +21,46 @@ type StatusMessage struct {
Message *string `json:"message"`
}
func propagateStatus(newStatus string) {
status = newStatus
func propagateStatus(newStatus pb.CoreState) {
v2.CoreState = newStatus
msg, _ := json.Marshal(StatusMessage{Status: status})
msg, _ := json.Marshal(StatusMessage{Status: convert2OldState(v2.CoreState)})
bridge.SendStringToPort(statusPropagationPort, string(msg))
}
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
})
status = Stopped
v2.CoreState = pb.CoreState_STOPPED
message := err.Error()
fmt.Printf("Error: %s: %s\n", alert, message)
msg, _ := json.Marshal(StatusMessage{Status: status, Alert: &alert, Message: &message})
msg, _ := json.Marshal(StatusMessage{Status: convert2OldState(v2.CoreState), Alert: &alert, Message: &message})
bridge.SendStringToPort(statusPropagationPort, string(msg))
config.DeactivateTunnelService()
if commandServer != nil {
commandServer.SetService(nil)
}
if box != nil {
box.Close()
box = nil
if v2.Box != nil {
v2.Box.Close()
v2.Box = nil
}
if commandServer != nil {
commandServer.Close()