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

188 lines
5.1 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 (
2026-04-02 16:39:34 +08:00
"math"
2023-04-21 17:29:00 +08:00
"os"
2026-04-02 16:39:34 +08:00
"path/filepath"
"runtime"
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
2026-04-08 14:44:14 +08:00
"github.com/sagernet/sing-box/common/networkquality"
"github.com/sagernet/sing-box/common/stun"
2023-03-01 10:37:47 +08:00
C "github.com/sagernet/sing-box/constant"
2026-04-09 19:34:27 +08:00
"github.com/sagernet/sing-box/dns"
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"
2026-04-02 16:39:34 +08:00
"github.com/sagernet/sing-box/service/oomkiller"
2025-04-28 23:12:39 +08:00
"github.com/sagernet/sing/common/byteformats"
2026-04-07 20:52:06 +08:00
E "github.com/sagernet/sing/common/exceptions"
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
2026-04-02 16:39:34 +08:00
sCrashReportSource string
sOOMKillerEnabled bool
sOOMKillerDisabled bool
sOOMMemoryLimit int64
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
2026-04-02 16:39:34 +08:00
CrashReportSource string
OomKillerEnabled bool
OomKillerDisabled bool
OomMemoryLimit int64
2023-07-24 15:44:11 +08:00
}
2026-04-02 16:39:34 +08:00
func applySetupOptions(options *SetupOptions) {
2024-12-26 11:12:48 +08:00
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
2026-04-02 16:39:34 +08:00
sCrashReportSource = options.CrashReportSource
ReloadSetupOptions(options)
}
func ReloadSetupOptions(options *SetupOptions) {
sOOMKillerEnabled = options.OomKillerEnabled
sOOMKillerDisabled = options.OomKillerDisabled
sOOMMemoryLimit = options.OomMemoryLimit
if sOOMKillerEnabled {
if sOOMMemoryLimit == 0 && C.IsIos {
sOOMMemoryLimit = oomkiller.DefaultAppleNetworkExtensionMemoryLimit
}
if sOOMMemoryLimit > 0 {
debug.SetMemoryLimit(sOOMMemoryLimit * 3 / 4)
} else {
debug.SetMemoryLimit(math.MaxInt64)
}
} else {
debug.SetMemoryLimit(math.MaxInt64)
}
}
2026-04-02 16:39:34 +08:00
func Setup(options *SetupOptions) error {
applySetupOptions(options)
os.MkdirAll(sWorkingPath, 0o777)
os.MkdirAll(sTempPath, 0o777)
2026-04-02 16:39:34 +08:00
return redirectStderr(filepath.Join(sWorkingPath, "CrashReport-"+sCrashReportSource+".log"))
}
2026-07-15 19:31:11 +08:00
func SetLocale(localeID string) error {
if !locale.Set(localeID) {
return E.New("unsupported locale: ", localeID)
2026-04-07 20:52:06 +08:00
}
return nil
2024-12-14 00:18:58 +08:00
}
2023-02-22 20:52:57 +08:00
func Version() string {
return C.Version
}
2023-03-01 10:37:47 +08:00
2026-04-02 16:39:34 +08:00
func GoVersion() string {
return runtime.Version() + ", " + runtime.GOOS + "/" + runtime.GOARCH
}
2023-03-01 10:37:47 +08:00
func FormatBytes(length int64) string {
2026-01-08 22:11:40 +08:00
return byteformats.FormatKBytes(uint64(length))
2023-09-20 14:12:08 +08:00
}
func FormatMemoryBytes(length int64) string {
2026-01-08 22:11:40 +08:00
return byteformats.FormatMemoryKBytes(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)
}
2026-04-08 14:44:14 +08:00
func FormatBitrate(bps int64) string {
return networkquality.FormatBitrate(bps)
}
const NetworkQualityDefaultConfigURL = networkquality.DefaultConfigURL
const NetworkQualityDefaultMaxRuntimeSeconds = int32(networkquality.DefaultMaxRuntime / time.Second)
const (
NetworkQualityAccuracyLow = int32(networkquality.AccuracyLow)
NetworkQualityAccuracyMedium = int32(networkquality.AccuracyMedium)
NetworkQualityAccuracyHigh = int32(networkquality.AccuracyHigh)
)
const (
NetworkQualityPhaseIdle = int32(networkquality.PhaseIdle)
NetworkQualityPhaseDownload = int32(networkquality.PhaseDownload)
NetworkQualityPhaseUpload = int32(networkquality.PhaseUpload)
NetworkQualityPhaseDone = int32(networkquality.PhaseDone)
)
const STUNDefaultServer = stun.DefaultServer
const (
STUNPhaseBinding = int32(stun.PhaseBinding)
STUNPhaseNATMapping = int32(stun.PhaseNATMapping)
STUNPhaseNATFiltering = int32(stun.PhaseNATFiltering)
STUNPhaseDone = int32(stun.PhaseDone)
)
const (
NATMappingEndpointIndependent = int32(stun.NATMappingEndpointIndependent)
NATMappingAddressDependent = int32(stun.NATMappingAddressDependent)
NATMappingAddressAndPortDependent = int32(stun.NATMappingAddressAndPortDependent)
)
const (
NATFilteringEndpointIndependent = int32(stun.NATFilteringEndpointIndependent)
NATFilteringAddressDependent = int32(stun.NATFilteringAddressDependent)
NATFilteringAddressAndPortDependent = int32(stun.NATFilteringAddressAndPortDependent)
)
func FormatNATMapping(value int32) string {
return stun.NATMapping(value).String()
}
func FormatNATFiltering(value int32) string {
return stun.NATFiltering(value).String()
}
2026-04-09 19:34:27 +08:00
func FormatFQDN(fqdn string) string {
return dns.FqdnToDomain(fqdn)
}
2023-08-04 17:13:46 +08:00
func ProxyDisplayType(proxyType string) string {
return C.ProxyDisplayName(proxyType)
}