sing-box/experimental/libbox/setup.go

51 lines
942 B
Go
Raw Normal View History

2022-10-25 04:55:00 +00:00
package libbox
2023-03-01 02:37:47 +00:00
import (
2023-04-21 09:29:00 +00:00
"os"
2023-07-24 07:44:11 +00:00
"os/user"
"strconv"
2023-04-21 09:29:00 +00:00
2023-03-01 02:37:47 +00:00
C "github.com/sagernet/sing-box/constant"
"github.com/dustin/go-humanize"
)
2022-10-25 04:55:00 +00:00
2023-04-21 09:29:00 +00:00
var (
2023-07-29 00:37:10 +00:00
sBasePath string
sWorkingPath string
sTempPath string
sUserID int
sGroupID int
sTVOS bool
2023-04-21 09:29:00 +00:00
)
2023-02-22 12:52:57 +00:00
2023-07-29 00:37:10 +00:00
func Setup(basePath string, workingPath string, tempPath string, isTVOS bool) {
2023-04-21 09:29:00 +00:00
sBasePath = basePath
2023-07-29 00:37:10 +00:00
sWorkingPath = workingPath
2023-04-21 09:29:00 +00:00
sTempPath = tempPath
2023-07-24 07:44:11 +00:00
sUserID = os.Getuid()
sGroupID = os.Getgid()
2023-07-29 00:37:10 +00:00
sTVOS = isTVOS
2023-07-24 07:44:11 +00:00
}
2023-07-29 00:37:10 +00:00
func SetupWithUsername(basePath string, workingPath string, tempPath string, username string) error {
2023-07-24 07:44:11 +00:00
sBasePath = basePath
2023-07-29 00:37:10 +00:00
sWorkingPath = workingPath
2023-07-24 07:44:11 +00:00
sTempPath = tempPath
sUser, err := user.Lookup(username)
if err != nil {
return err
2023-04-21 09:29:00 +00:00
}
2023-07-24 07:44:11 +00:00
sUserID, _ = strconv.Atoi(sUser.Uid)
sGroupID, _ = strconv.Atoi(sUser.Gid)
return nil
}
2023-02-22 12:52:57 +00:00
func Version() string {
return C.Version
}
2023-03-01 02:37:47 +00:00
func FormatBytes(length int64) string {
2023-03-16 03:15:55 +00:00
return humanize.IBytes(uint64(length))
2023-03-01 02:37:47 +00:00
}