Add support for macOS system extension

This commit is contained in:
世界 2023-07-24 15:44:11 +08:00
parent db23a48b36
commit cadc34f3ad
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
3 changed files with 31 additions and 31 deletions

View file

@ -78,6 +78,14 @@ func (s *CommandServer) Start() error {
if err != nil { if err != nil {
return err 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 s.listener = listener
go s.loopConnection(listener) go s.loopConnection(listener)
return nil return nil

View file

@ -18,29 +18,13 @@ func RedirectStderr(path string) error {
if err != nil { if err != nil {
return err return err
} }
err = unix.Dup2(int(outputFile.Fd()), int(os.Stderr.Fd())) if sUserID > 0 {
if err != nil { err = outputFile.Chown(sUserID, sGroupID)
outputFile.Close() if err != nil {
os.Remove(outputFile.Name()) outputFile.Close()
return err 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
} }
err = unix.Dup2(int(outputFile.Fd()), int(os.Stderr.Fd())) err = unix.Dup2(int(outputFile.Fd()), int(os.Stderr.Fd()))
if err != nil { if err != nil {

View file

@ -2,6 +2,8 @@ package libbox
import ( import (
"os" "os"
"os/user"
"strconv"
C "github.com/sagernet/sing-box/constant" C "github.com/sagernet/sing-box/constant"
@ -15,17 +17,23 @@ var (
sGroupID int sGroupID int
) )
func Setup(basePath string, tempPath string, userID int, groupID int) { func Setup(basePath string, tempPath string) {
sBasePath = basePath sBasePath = basePath
sTempPath = tempPath sTempPath = tempPath
sUserID = userID sUserID = os.Getuid()
sGroupID = groupID sGroupID = os.Getgid()
if sUserID == -1 { }
sUserID = os.Getuid()
} func SetupWithUsername(basePath string, tempPath string, username string) error {
if sGroupID == -1 { sBasePath = basePath
sGroupID = os.Getgid() 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 { func Version() string {