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

View file

@ -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 {

View file

@ -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 {