Add start delay

This commit is contained in:
problematicconsumer
2023-10-27 14:36:00 +03:30
parent 57b239c0f3
commit a390c7fc76
2 changed files with 23 additions and 5 deletions

View File

@@ -1,12 +1,16 @@
package main package main
import "github.com/sagernet/sing-box/experimental/libbox" import (
"github.com/sagernet/sing-box/experimental/libbox"
"github.com/sagernet/sing-box/log"
)
var commandServer *libbox.CommandServer var commandServer *libbox.CommandServer
type CommandServerHandler struct{} type CommandServerHandler struct{}
func (csh *CommandServerHandler) ServiceReload() error { func (csh *CommandServerHandler) ServiceReload() error {
log.Trace("[Command Server Handler] Reloading service")
propagateStatus(Starting) propagateStatus(Starting)
if commandServer != nil { if commandServer != nil {
commandServer.SetService(nil) commandServer.SetService(nil)
@@ -16,18 +20,21 @@ func (csh *CommandServerHandler) ServiceReload() error {
box.Close() box.Close()
box = nil box = nil
} }
return startService() return startService(true)
} }
func (csh *CommandServerHandler) GetSystemProxyStatus() *libbox.SystemProxyStatus { func (csh *CommandServerHandler) GetSystemProxyStatus() *libbox.SystemProxyStatus {
log.Trace("[Command Server Handler] Getting system proxy status")
return &libbox.SystemProxyStatus{Available: true, Enabled: false} return &libbox.SystemProxyStatus{Available: true, Enabled: false}
} }
func (csh *CommandServerHandler) SetSystemProxyEnabled(isEnabled bool) error { func (csh *CommandServerHandler) SetSystemProxyEnabled(isEnabled bool) error {
return nil log.Trace("[Command Server Handler] Setting system proxy status")
return csh.ServiceReload()
} }
func startCommandServer() error { func startCommandServer() error {
log.Trace("[Command Server Handler] Starting command server")
commandServer = libbox.NewCommandServer(&CommandServerHandler{}, 300) commandServer = libbox.NewCommandServer(&CommandServerHandler{}, 300)
return commandServer.Start() return commandServer.Start()
} }

View File

@@ -7,11 +7,13 @@ import "C"
import ( import (
"encoding/json" "encoding/json"
"os" "os"
"time"
"unsafe" "unsafe"
"github.com/hiddify/libcore/bridge" "github.com/hiddify/libcore/bridge"
"github.com/hiddify/libcore/shared" "github.com/hiddify/libcore/shared"
"github.com/sagernet/sing-box/experimental/libbox" "github.com/sagernet/sing-box/experimental/libbox"
"github.com/sagernet/sing-box/log"
) )
var box *libbox.BoxService var box *libbox.BoxService
@@ -71,14 +73,14 @@ func start(configPath *C.char, disableMemoryLimit bool) (CErr *C.char) {
activeConfigPath = &path activeConfigPath = &path
libbox.SetMemoryLimit(!disableMemoryLimit) libbox.SetMemoryLimit(!disableMemoryLimit)
err := startService() err := startService(false)
if err != nil { if err != nil {
return C.CString(err.Error()) return C.CString(err.Error())
} }
return C.CString("") return C.CString("")
} }
func startService() error { func startService(delayStart bool) error {
content, err := os.ReadFile(*activeConfigPath) content, err := os.ReadFile(*activeConfigPath)
if err != nil { if err != nil {
return stopAndAlert(EmptyConfiguration, err) return stopAndAlert(EmptyConfiguration, err)
@@ -100,6 +102,11 @@ func startService() error {
if err != nil { if err != nil {
return stopAndAlert(CreateService, err) return stopAndAlert(CreateService, err)
} }
if delayStart {
time.Sleep(250 * time.Millisecond)
}
err = instance.Start() err = instance.Start()
if err != nil { if err != nil {
return stopAndAlert(StartService, err) return stopAndAlert(StartService, err)
@@ -147,6 +154,7 @@ func restart(configPath *C.char, disableMemoryLimit bool) (CErr *C.char) {
defer shared.DeferPanicToError("restart", func(err error) { defer shared.DeferPanicToError("restart", func(err error) {
CErr = C.CString(err.Error()) CErr = C.CString(err.Error())
}) })
log.Debug("[Service] Restarting")
if status != Started { if status != Started {
return C.CString("") return C.CString("")
@@ -159,6 +167,9 @@ func restart(configPath *C.char, disableMemoryLimit bool) (CErr *C.char) {
if C.GoString(err) != "" { if C.GoString(err) != "" {
return err return err
} }
time.Sleep(250 * time.Millisecond)
err = start(configPath, disableMemoryLimit) err = start(configPath, disableMemoryLimit)
if C.GoString(err) != "" { if C.GoString(err) != "" {
return err return err