github.com/sagernet/sing-box@v1.9.0-rc.20/experimental/libbox/setup.go (about) 1 package libbox 2 3 import ( 4 "os" 5 "os/user" 6 "strconv" 7 8 "github.com/sagernet/sing-box/common/humanize" 9 C "github.com/sagernet/sing-box/constant" 10 _ "github.com/sagernet/sing-box/include" 11 ) 12 13 var ( 14 sBasePath string 15 sWorkingPath string 16 sTempPath string 17 sUserID int 18 sGroupID int 19 sTVOS bool 20 ) 21 22 func Setup(basePath string, workingPath string, tempPath string, isTVOS bool) { 23 sBasePath = basePath 24 sWorkingPath = workingPath 25 sTempPath = tempPath 26 sUserID = os.Getuid() 27 sGroupID = os.Getgid() 28 sTVOS = isTVOS 29 os.MkdirAll(sWorkingPath, 0o777) 30 os.MkdirAll(sTempPath, 0o777) 31 } 32 33 func SetupWithUsername(basePath string, workingPath string, tempPath string, username string) error { 34 sBasePath = basePath 35 sWorkingPath = workingPath 36 sTempPath = tempPath 37 sUser, err := user.Lookup(username) 38 if err != nil { 39 return err 40 } 41 sUserID, _ = strconv.Atoi(sUser.Uid) 42 sGroupID, _ = strconv.Atoi(sUser.Gid) 43 os.MkdirAll(sWorkingPath, 0o777) 44 os.MkdirAll(sTempPath, 0o777) 45 os.Chown(sWorkingPath, sUserID, sGroupID) 46 os.Chown(sTempPath, sUserID, sGroupID) 47 return nil 48 } 49 50 func Version() string { 51 return C.Version 52 } 53 54 func FormatBytes(length int64) string { 55 return humanize.Bytes(uint64(length)) 56 } 57 58 func FormatMemoryBytes(length int64) string { 59 return humanize.MemoryBytes(uint64(length)) 60 } 61 62 func ProxyDisplayType(proxyType string) string { 63 return C.ProxyDisplayName(proxyType) 64 }