From cadc34f3ad06a87eb272a6053a9d0f4ba9fec637 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Mon, 24 Jul 2023 15:44:11 +0800 Subject: [PATCH] Add support for macOS system extension --- experimental/libbox/command_server.go | 8 +++++++ experimental/libbox/log.go | 30 +++++++-------------------- experimental/libbox/setup.go | 24 ++++++++++++++------- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/experimental/libbox/command_server.go b/experimental/libbox/command_server.go index dcedb7e5..f8aa03e2 100644 --- a/experimental/libbox/command_server.go +++ b/experimental/libbox/command_server.go @@ -78,6 +78,14 @@ func (s *CommandServer) Start() error { if err != nil { return err } + if sUserID > 0 { + err = os.Chown(s.sockPath, sUserID, sGroupID) + if err != nil { + listener.Close() + os.Remove(s.sockPath) + return err + } + } s.listener = listener go s.loopConnection(listener) return nil diff --git a/experimental/libbox/log.go b/experimental/libbox/log.go index e7fa129a..18332ef9 100644 --- a/experimental/libbox/log.go +++ b/experimental/libbox/log.go @@ -18,29 +18,13 @@ func RedirectStderr(path string) error { if err != nil { return err } - err = unix.Dup2(int(outputFile.Fd()), int(os.Stderr.Fd())) - if err != nil { - outputFile.Close() - os.Remove(outputFile.Name()) - return err - } - stderrFile = outputFile - return nil -} - -func RedirectStderrAsUser(path string, uid, gid int) error { - if stats, err := os.Stat(path); err == nil && stats.Size() > 0 { - _ = os.Rename(path, path+".old") - } - outputFile, err := os.Create(path) - if err != nil { - return err - } - err = outputFile.Chown(uid, gid) - if err != nil { - outputFile.Close() - os.Remove(outputFile.Name()) - return err + if sUserID > 0 { + err = outputFile.Chown(sUserID, sGroupID) + if err != nil { + outputFile.Close() + os.Remove(outputFile.Name()) + return err + } } err = unix.Dup2(int(outputFile.Fd()), int(os.Stderr.Fd())) if err != nil { diff --git a/experimental/libbox/setup.go b/experimental/libbox/setup.go index e9caf773..83ae0424 100644 --- a/experimental/libbox/setup.go +++ b/experimental/libbox/setup.go @@ -2,6 +2,8 @@ package libbox import ( "os" + "os/user" + "strconv" C "github.com/sagernet/sing-box/constant" @@ -15,17 +17,23 @@ var ( sGroupID int ) -func Setup(basePath string, tempPath string, userID int, groupID int) { +func Setup(basePath string, tempPath string) { sBasePath = basePath sTempPath = tempPath - sUserID = userID - sGroupID = groupID - if sUserID == -1 { - sUserID = os.Getuid() - } - if sGroupID == -1 { - sGroupID = os.Getgid() + sUserID = os.Getuid() + sGroupID = os.Getgid() +} + +func SetupWithUsername(basePath string, tempPath string, username string) error { + sBasePath = basePath + sTempPath = tempPath + sUser, err := user.Lookup(username) + if err != nil { + return err } + sUserID, _ = strconv.Atoi(sUser.Uid) + sGroupID, _ = strconv.Atoi(sUser.Gid) + return nil } func Version() string {