Sfoglia il codice sorgente

Add support for macOS system extension

世界 2 anni fa
parent
commit
cadc34f3ad

+ 8 - 0
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

+ 7 - 23
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 {

+ 16 - 8
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 {