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

@@ -1,8 +1,11 @@
package main
import (
pb "github.com/hiddify/libcore/hiddifyrpc"
v2 "github.com/hiddify/libcore/v2"
"github.com/sagernet/sing-box/experimental/libbox"
"github.com/sagernet/sing-box/log"
)
var commandServer *libbox.CommandServer
@@ -13,14 +16,14 @@ type CommandServerHandler struct {
func (csh *CommandServerHandler) ServiceReload() error {
csh.logger.Trace("Reloading service")
propagateStatus(Starting)
propagateStatus(pb.CoreState_STARTING)
if commandServer != nil {
commandServer.SetService(nil)
commandServer = nil
}
if box != nil {
box.Close()
box = nil
if v2.Box != nil {
v2.Box.Close()
v2.Box = nil
}
return startService(true)
}

View File

@@ -15,12 +15,15 @@ import (
"github.com/hiddify/libcore/bridge"
"github.com/hiddify/libcore/config"
pb "github.com/hiddify/libcore/hiddifyrpc"
v2 "github.com/hiddify/libcore/v2"
"github.com/sagernet/sing-box/experimental/libbox"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
)
var box *libbox.BoxService
// var v2.Box *libbox.BoxService
var configOptions *config.ConfigOptions
var activeConfigPath *string
var logFactory *log.Factory
@@ -132,10 +135,10 @@ func start(configPath *C.char, disableMemoryLimit bool) (CErr *C.char) {
CErr = C.CString(err.Error())
})
if status != Stopped {
if v2.CoreState != pb.CoreState_STOPPED {
return C.CString("")
}
propagateStatus(Starting)
propagateStatus(pb.CoreState_STARTING)
path := C.GoString(configPath)
activeConfigPath = &path
@@ -184,10 +187,10 @@ func startService(delayStart bool) error {
if err != nil {
return stopAndAlert(StartService, err)
}
box = instance
commandServer.SetService(box)
v2.Box = instance
commandServer.SetService(v2.Box)
propagateStatus(Started)
propagateStatus(pb.CoreState_STARTED)
return nil
}
@@ -198,29 +201,29 @@ func stop() (CErr *C.char) {
CErr = C.CString(err.Error())
})
config.DeactivateTunnelService()
if status != Started {
if v2.CoreState != pb.CoreState_STARTED {
stopAndAlert("Already Stopped", nil)
return C.CString("")
}
if box == nil {
if v2.Box == nil {
return C.CString("instance not found")
}
propagateStatus(Stopping)
propagateStatus(pb.CoreState_STOPPING)
commandServer.SetService(nil)
err := box.Close()
err := v2.Box.Close()
if err != nil {
stopAndAlert("Unexpected Error in Close!", err)
return C.CString(err.Error())
}
box = nil
v2.Box = nil
err = commandServer.Close()
if err != nil {
stopAndAlert("Unexpected Error in Stop CommandServer/!", err)
return C.CString(err.Error())
}
commandServer = nil
propagateStatus(Stopped)
propagateStatus(pb.CoreState_STOPPED)
return C.CString("")
}
@@ -232,10 +235,10 @@ func restart(configPath *C.char, disableMemoryLimit bool) (CErr *C.char) {
})
log.Debug("[Service] Restarting")
if status != Started {
if v2.CoreState != pb.CoreState_STARTED {
return C.CString("")
}
if box == nil {
if v2.Box == nil {
return C.CString("instance not found")
}
@@ -244,7 +247,7 @@ func restart(configPath *C.char, disableMemoryLimit bool) (CErr *C.char) {
return err
}
propagateStatus(Starting)
propagateStatus(pb.CoreState_STARTING)
time.Sleep(250 * time.Millisecond)

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()