Add status command client

This commit is contained in:
problematicconsumer
2023-08-28 13:08:43 +03:30
parent cb1d08734a
commit 219ca7ced9
3 changed files with 130 additions and 0 deletions

View File

@@ -6,13 +6,20 @@ package main
import "C"
import (
"os"
"unsafe"
"github.com/hiddify/libcore/bridge"
"github.com/hiddify/libcore/shared"
"github.com/sagernet/sing-box/experimental/libbox"
)
var box *libbox.BoxService
//export setupOnce
func setupOnce(api unsafe.Pointer) {
bridge.InitializeDartApi(api)
}
//export setup
func setup(baseDir *C.char, workingDir *C.char, tempDir *C.char) {
Setup(C.GoString(baseDir), C.GoString(workingDir), C.GoString(tempDir))
@@ -48,12 +55,25 @@ func create(configPath *C.char) *C.char {
shared.SaveCurrentConfig(sWorkingPath, options)
err = startCommandServer()
if err != nil {
return C.CString(err.Error())
}
instance, err := NewService(options)
if err != nil {
return C.CString(err.Error())
}
box = instance
commandServer.SetService(box)
if err != nil {
instance.Close()
box = nil
return C.CString(err.Error())
}
return C.CString("")
}
@@ -62,6 +82,7 @@ func start() *C.char {
if box == nil {
return C.CString("instance not found")
}
err := box.Start()
if err != nil {
return C.CString(err.Error())
@@ -76,10 +97,37 @@ func stop() *C.char {
return C.CString("instance not found")
}
commandServer.SetService(nil)
err := box.Close()
if err != nil {
return C.CString(err.Error())
}
box = nil
err = commandServer.Close()
if err != nil {
return C.CString(err.Error())
}
commandServer = nil
return C.CString("")
}
//export startCommandClient
func startCommandClient(command C.int, port C.longlong) *C.char {
err := StartCommand(int32(command), int64(port))
if err != nil {
return C.CString(err.Error())
}
return C.CString("")
}
//export stopCommandClient
func stopCommandClient(command C.int) *C.char {
err := StopCommand(int32(command))
if err != nil {
return C.CString(err.Error())
}
return C.CString("")
}