setup.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package libbox
  2. import (
  3. "os"
  4. "runtime/debug"
  5. "time"
  6. C "github.com/sagernet/sing-box/constant"
  7. "github.com/sagernet/sing-box/experimental/locale"
  8. "github.com/sagernet/sing-box/log"
  9. "github.com/sagernet/sing/common/byteformats"
  10. )
  11. var (
  12. sBasePath string
  13. sWorkingPath string
  14. sTempPath string
  15. sUserID int
  16. sGroupID int
  17. sFixAndroidStack bool
  18. sCommandServerListenPort uint16
  19. sCommandServerSecret string
  20. sLogMaxLines int
  21. sDebug bool
  22. )
  23. func init() {
  24. debug.SetPanicOnFault(true)
  25. debug.SetTraceback("all")
  26. }
  27. type SetupOptions struct {
  28. BasePath string
  29. WorkingPath string
  30. TempPath string
  31. FixAndroidStack bool
  32. CommandServerListenPort int32
  33. CommandServerSecret string
  34. LogMaxLines int
  35. Debug bool
  36. }
  37. func Setup(options *SetupOptions) error {
  38. sBasePath = options.BasePath
  39. sWorkingPath = options.WorkingPath
  40. sTempPath = options.TempPath
  41. sUserID = os.Getuid()
  42. sGroupID = os.Getgid()
  43. // TODO: remove after fixed
  44. // https://github.com/golang/go/issues/68760
  45. sFixAndroidStack = options.FixAndroidStack
  46. sCommandServerListenPort = uint16(options.CommandServerListenPort)
  47. sCommandServerSecret = options.CommandServerSecret
  48. sLogMaxLines = options.LogMaxLines
  49. sDebug = options.Debug
  50. os.MkdirAll(sWorkingPath, 0o777)
  51. os.MkdirAll(sTempPath, 0o777)
  52. return nil
  53. }
  54. func SetLocale(localeId string) {
  55. locale.Set(localeId)
  56. }
  57. func Version() string {
  58. return C.Version
  59. }
  60. func FormatBytes(length int64) string {
  61. return byteformats.FormatBytes(uint64(length))
  62. }
  63. func FormatMemoryBytes(length int64) string {
  64. return byteformats.FormatMemoryBytes(uint64(length))
  65. }
  66. func FormatDuration(duration int64) string {
  67. return log.FormatDuration(time.Duration(duration) * time.Millisecond)
  68. }
  69. func ProxyDisplayType(proxyType string) string {
  70. return C.ProxyDisplayName(proxyType)
  71. }