setup.go 1.4 KB

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