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"
|
2023-07-24 15:44:11 +08:00
|
|
|
"os/user"
|
2024-06-24 09:49:15 +08:00
|
|
|
"runtime/debug"
|
2023-07-24 15:44:11 +08:00
|
|
|
"strconv"
|
2024-06-11 21:16:33 +08:00
|
|
|
"time"
|
2023-04-21 17:29:00 +08:00
|
|
|
|
2023-09-20 14:12:08 +08:00
|
|
|
"github.com/sagernet/sing-box/common/humanize"
|
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-01-17 05:48:33 +08:00
|
|
|
_ "github.com/sagernet/sing-box/include"
|
2024-06-11 21:16:33 +08:00
|
|
|
"github.com/sagernet/sing-box/log"
|
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 (
|
2023-07-29 08:37:10 +08:00
|
|
|
sBasePath string
|
|
|
|
sWorkingPath string
|
|
|
|
sTempPath string
|
|
|
|
sUserID int
|
|
|
|
sGroupID int
|
|
|
|
sTVOS 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)
|
|
|
|
}
|
|
|
|
|
2023-07-29 08:37:10 +08:00
|
|
|
func Setup(basePath string, workingPath string, tempPath string, isTVOS bool) {
|
2023-04-21 17:29:00 +08:00
|
|
|
sBasePath = basePath
|
2023-07-29 08:37:10 +08:00
|
|
|
sWorkingPath = workingPath
|
2023-04-21 17:29:00 +08:00
|
|
|
sTempPath = tempPath
|
2023-07-24 15:44:11 +08:00
|
|
|
sUserID = os.Getuid()
|
|
|
|
sGroupID = os.Getgid()
|
2023-07-29 08:37:10 +08:00
|
|
|
sTVOS = isTVOS
|
2024-01-22 18:27:06 +08:00
|
|
|
os.MkdirAll(sWorkingPath, 0o777)
|
|
|
|
os.MkdirAll(sTempPath, 0o777)
|
2023-07-24 15:44:11 +08:00
|
|
|
}
|
|
|
|
|
2023-07-29 08:37:10 +08:00
|
|
|
func SetupWithUsername(basePath string, workingPath string, tempPath string, username string) error {
|
2023-07-24 15:44:11 +08:00
|
|
|
sBasePath = basePath
|
2023-07-29 08:37:10 +08:00
|
|
|
sWorkingPath = workingPath
|
2023-07-24 15:44:11 +08:00
|
|
|
sTempPath = tempPath
|
|
|
|
sUser, err := user.Lookup(username)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-04-21 17:29:00 +08:00
|
|
|
}
|
2023-07-24 15:44:11 +08:00
|
|
|
sUserID, _ = strconv.Atoi(sUser.Uid)
|
|
|
|
sGroupID, _ = strconv.Atoi(sUser.Gid)
|
2024-01-22 18:27:06 +08:00
|
|
|
os.MkdirAll(sWorkingPath, 0o777)
|
|
|
|
os.MkdirAll(sTempPath, 0o777)
|
|
|
|
os.Chown(sWorkingPath, sUserID, sGroupID)
|
|
|
|
os.Chown(sTempPath, sUserID, sGroupID)
|
2023-07-24 15:44:11 +08:00
|
|
|
return nil
|
2023-04-09 12:39:26 +08:00
|
|
|
}
|
|
|
|
|
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 {
|
2023-09-20 14:12:08 +08:00
|
|
|
return humanize.Bytes(uint64(length))
|
|
|
|
}
|
|
|
|
|
|
|
|
func FormatMemoryBytes(length int64) string {
|
|
|
|
return humanize.MemoryBytes(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)
|
|
|
|
}
|