From d760e709775ddb5ab28c5724fcaf967c4373f5c1 Mon Sep 17 00:00:00 2001 From: Nova Date: Sat, 17 May 2025 09:14:58 +0330 Subject: [PATCH] improve extra core path and args handling --- core/server/go.mod | 1 + core/server/go.sum | 2 ++ core/server/internal/process/process.go | 7 +++---- core/server/server.go | 13 ++++++++++++- include/ui/profile/edit_extra_core.ui | 3 +++ include/ui/setting/dialog_manage_routes.ui | 2 +- 6 files changed, 22 insertions(+), 6 deletions(-) diff --git a/core/server/go.mod b/core/server/go.mod index 264acaf..df8f248 100644 --- a/core/server/go.mod +++ b/core/server/go.mod @@ -8,6 +8,7 @@ require ( github.com/Mahdi-zarei/speedtest-go v1.7.12 github.com/dustin/go-humanize v1.0.1 github.com/gofrs/uuid/v5 v5.3.2 + github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 github.com/oschwald/maxminddb-golang v1.13.1 github.com/sagernet/sing v0.6.9 github.com/sagernet/sing-box v1.11.10 diff --git a/core/server/go.sum b/core/server/go.sum index 67b6c8d..6931379 100644 --- a/core/server/go.sum +++ b/core/server/go.sum @@ -48,6 +48,8 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/pprof v0.0.0-20231101202521-4ca4178f5c7a h1:fEBsGL/sjAuJrgah5XqmmYsTLzJp/TO9Lhy39gkverk= github.com/google/pprof v0.0.0-20231101202521-4ca4178f5c7a/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hashicorp/yamux v0.1.2 h1:XtB8kyFOyHXYVFnwT5C3+Bdo8gArse7j2AQ0DA0Uey8= diff --git a/core/server/internal/process/process.go b/core/server/internal/process/process.go index 31f8f8e..88c754e 100644 --- a/core/server/internal/process/process.go +++ b/core/server/internal/process/process.go @@ -4,23 +4,22 @@ import ( "fmt" "github.com/sagernet/sing/common/atomic" "os/exec" - "strings" ) type Process struct { path string - args string + args []string noOut bool cmd *exec.Cmd stopped atomic.Bool } -func NewProcess(path string, args string, noOut bool) *Process { +func NewProcess(path string, args []string, noOut bool) *Process { return &Process{path: path, args: args, noOut: noOut} } func (p *Process) Start() error { - p.cmd = exec.Command(p.path, strings.Split(p.args, " ")...) + p.cmd = exec.Command(p.path, p.args...) p.cmd.Stdout = &pipeLogger{prefix: "Extra Core", noOut: p.noOut} p.cmd.Stderr = &pipeLogger{prefix: "Extra Core", noOut: p.noOut} diff --git a/core/server/server.go b/core/server/server.go index d7c452a..c2a6be0 100644 --- a/core/server/server.go +++ b/core/server/server.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "github.com/google/shlex" "github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/common/settings" "github.com/sagernet/sing-box/experimental/clashapi" @@ -75,7 +76,17 @@ func (s *server) Start(ctx context.Context, in *gen.LoadConfigReq) (out *gen.Err return } _ = f.Close() - args := fmt.Sprintf(in.ExtraProcessArgs, extraConfPath) + args, e := shlex.Split(in.ExtraProcessArgs) + if e != nil { + err = E.Cause(e, "Failed to parse args") + return + } + for idx, arg := range args { + if strings.Contains(arg, "%s") { + args[idx] = fmt.Sprintf(arg, extraConfPath) + break + } + } extraProcess = process.NewProcess(in.ExtraProcessPath, args, in.ExtraNoOut) err = extraProcess.Start() diff --git a/include/ui/profile/edit_extra_core.ui b/include/ui/profile/edit_extra_core.ui index 81a2af5..7300871 100644 --- a/include/ui/profile/edit_extra_core.ui +++ b/include/ui/profile/edit_extra_core.ui @@ -81,6 +81,9 @@ + + true + true diff --git a/include/ui/setting/dialog_manage_routes.ui b/include/ui/setting/dialog_manage_routes.ui index 69bd54e..eaeb841 100644 --- a/include/ui/setting/dialog_manage_routes.ui +++ b/include/ui/setting/dialog_manage_routes.ui @@ -17,7 +17,7 @@ - 1 + 0