Files
sing-box-extended-mirror/experimental/libbox/setup.go
T

88 lines
1.9 KiB
Go
Raw Normal View History

2022-10-25 12:55:00 +08:00
package libbox
2023-03-01 10:37:47 +08:00
import (
2023-04-21 17:29:00 +08:00
"os"
2024-06-24 09:49:15 +08:00
"runtime/debug"
2024-06-11 21:16:33 +08:00
"time"
2023-04-21 17:29:00 +08:00
2023-03-01 10:37:47 +08:00
C "github.com/sagernet/sing-box/constant"
2024-12-14 00:18:58 +08:00
"github.com/sagernet/sing-box/experimental/locale"
2024-06-11 21:16:33 +08:00
"github.com/sagernet/sing-box/log"
2025-04-28 23:12:39 +08:00
"github.com/sagernet/sing/common/byteformats"
2023-03-01 10:37:47 +08:00
)
2022-10-25 12:55:00 +08:00
2023-04-21 17:29:00 +08:00
var (
sBasePath string
sWorkingPath string
sTempPath string
sUserID int
sGroupID int
sFixAndroidStack bool
sCommandServerListenPort uint16
sCommandServerSecret string
sLogMaxLines int
sDebug bool
2023-04-21 17:29:00 +08:00
)
2023-02-22 20:52:57 +08:00
2024-06-24 09:49:15 +08:00
func init() {
debug.SetPanicOnFault(true)
debug.SetTraceback("all")
2024-06-24 09:49:15 +08:00
}
2024-12-26 11:12:48 +08:00
type SetupOptions struct {
BasePath string
WorkingPath string
TempPath string
FixAndroidStack bool
CommandServerListenPort int32
CommandServerSecret string
LogMaxLines int
Debug bool
2023-07-24 15:44:11 +08:00
}
2024-12-26 11:12:48 +08:00
func Setup(options *SetupOptions) error {
sBasePath = options.BasePath
sWorkingPath = options.WorkingPath
sTempPath = options.TempPath
sUserID = os.Getuid()
sGroupID = os.Getgid()
2024-12-26 11:12:48 +08:00
// TODO: remove after fixed
// https://github.com/golang/go/issues/68760
sFixAndroidStack = options.FixAndroidStack
sCommandServerListenPort = uint16(options.CommandServerListenPort)
sCommandServerSecret = options.CommandServerSecret
sLogMaxLines = options.LogMaxLines
sDebug = options.Debug
os.MkdirAll(sWorkingPath, 0o777)
os.MkdirAll(sTempPath, 0o777)
2023-07-24 15:44:11 +08:00
return nil
}
2024-12-14 00:18:58 +08:00
func SetLocale(localeId string) {
locale.Set(localeId)
}
2023-02-22 20:52:57 +08:00
func Version() string {
return C.Version
}
2023-03-01 10:37:47 +08:00
func FormatBytes(length int64) string {
2025-04-28 23:12:39 +08:00
return byteformats.FormatBytes(uint64(length))
2023-09-20 14:12:08 +08:00
}
func FormatMemoryBytes(length int64) string {
2025-04-28 23:12:39 +08:00
return byteformats.FormatMemoryBytes(uint64(length))
2023-03-01 10:37:47 +08:00
}
2023-08-04 17:13:46 +08:00
2024-06-11 21:16:33 +08:00
func FormatDuration(duration int64) string {
return log.FormatDuration(time.Duration(duration) * time.Millisecond)
}
2023-08-04 17:13:46 +08:00
func ProxyDisplayType(proxyType string) string {
return C.ProxyDisplayName(proxyType)
}