From 8c90556e6a5bf919a73bfdfa7da21bff7aed4424 Mon Sep 17 00:00:00 2001 From: problematicconsumer Date: Mon, 15 Jan 2024 17:17:05 +0330 Subject: [PATCH] Refactor shared --- cmd/cmd_config.go | 10 +-- {shared => config}/config.go | 83 +---------------------- {shared => config}/config.json.template | 0 {shared => config}/debug.go | 2 +- config/option.go | 88 +++++++++++++++++++++++++ {shared => config}/parser.go | 2 +- {shared => config}/rules.go | 2 +- custom/custom.go | 34 +++++----- mobile/mobile.go | 8 +-- 9 files changed, 118 insertions(+), 111 deletions(-) rename {shared => config}/config.go (75%) rename {shared => config}/config.json.template (100%) rename {shared => config}/debug.go (97%) create mode 100644 config/option.go rename {shared => config}/parser.go (99%) rename {shared => config}/rules.go (99%) diff --git a/cmd/cmd_config.go b/cmd/cmd_config.go index 94ee207..8a842ee 100644 --- a/cmd/cmd_config.go +++ b/cmd/cmd_config.go @@ -6,7 +6,7 @@ import ( "os" "path/filepath" - "github.com/hiddify/libcore/shared" + "github.com/hiddify/libcore/config" "github.com/sagernet/sing-box/experimental/libbox" "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" @@ -58,14 +58,14 @@ func build(path string, optionsPath string) error { if err != nil { return err } - configOptions := shared.DefaultConfigOptions() + configOptions := config.DefaultConfigOptions() if optionsPath != "" { configOptions, err = readConfigOptionsAt(optionsPath) if err != nil { return err } } - config, err := shared.BuildConfigJson(*configOptions, *options) + config, err := config.BuildConfigJson(*configOptions, *options) if err != nil { return err } @@ -95,12 +95,12 @@ func readConfigAt(path string) (*option.Options, error) { return &options, nil } -func readConfigOptionsAt(path string) (*shared.ConfigOptions, error) { +func readConfigOptionsAt(path string) (*config.ConfigOptions, error) { content, err := os.ReadFile(path) if err != nil { return nil, err } - var options shared.ConfigOptions + var options config.ConfigOptions err = json.Unmarshal(content, &options) if err != nil { return nil, err diff --git a/shared/config.go b/config/config.go similarity index 75% rename from shared/config.go rename to config/config.go index b876b21..2bf7756 100644 --- a/shared/config.go +++ b/config/config.go @@ -1,4 +1,4 @@ -package shared +package config import ( "bytes" @@ -8,93 +8,12 @@ import ( "net/netip" "net/url" "strings" - "time" C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-box/option" dns "github.com/sagernet/sing-dns" ) -type ConfigOptions struct { - ExecuteAsIs bool `json:"execute-config-as-is"` - LogLevel string `json:"log-level"` - ResolveDestination bool `json:"resolve-destination"` - IPv6Mode option.DomainStrategy `json:"ipv6-mode"` - RemoteDnsAddress string `json:"remote-dns-address"` - RemoteDnsDomainStrategy option.DomainStrategy `json:"remote-dns-domain-strategy"` - DirectDnsAddress string `json:"direct-dns-address"` - DirectDnsDomainStrategy option.DomainStrategy `json:"direct-dns-domain-strategy"` - MixedPort uint16 `json:"mixed-port"` - LocalDnsPort uint16 `json:"local-dns-port"` - MTU uint32 `json:"mtu"` - StrictRoute bool `json:"strict-route"` - TUNStack string `json:"tun-stack"` - ConnectionTestUrl string `json:"connection-test-url"` - URLTestInterval option.Duration `json:"url-test-interval"` - EnableClashApi bool `json:"enable-clash-api"` - ClashApiPort uint16 `json:"clash-api-port"` - EnableTun bool `json:"enable-tun"` - SetSystemProxy bool `json:"set-system-proxy"` - BypassLAN bool `json:"bypass-lan"` - AllowConnectionFromLAN bool `json:"allow-connection-from-lan"` - EnableFakeDNS bool `json:"enable-fake-dns"` - EnableDNSRouting bool `json:"enable-dns-routing"` - IndependentDNSCache bool `json:"independent-dns-cache"` - GeoIPPath string `json:"geoip-path"` - GeoSitePath string `json:"geosite-path"` - Rules []Rule `json:"rules"` - TLSTricks -} - -type TLSTricks struct { - EnableFragment bool `json:"enable-tls-fragment"` - FragmentSize string `json:"tls-fragment-size"` - FragmentSleep string `json:"tls-fragment-sleep"` - EnableMixedSNICase bool `json:"enable-tls-mixed-sni-case"` - EnablePadding bool `json:"enable-tls-padding"` - PaddingSize string `json:"tls-padding-size"` -} - -func DefaultConfigOptions() *ConfigOptions { - return &ConfigOptions{ - ExecuteAsIs: false, - LogLevel: "info", - ResolveDestination: false, - IPv6Mode: option.DomainStrategy(dns.DomainStrategyAsIS), - RemoteDnsAddress: "1.1.1.1", - RemoteDnsDomainStrategy: option.DomainStrategy(dns.DomainStrategyAsIS), - DirectDnsAddress: "1.1.1.1", - DirectDnsDomainStrategy: option.DomainStrategy(dns.DomainStrategyAsIS), - MixedPort: 2334, - LocalDnsPort: 6450, - MTU: 9000, - StrictRoute: true, - TUNStack: "mixed", - ConnectionTestUrl: "https://cp.cloudflare.com/", - URLTestInterval: option.Duration(10 * time.Minute), - EnableClashApi: true, - ClashApiPort: 6756, - EnableTun: true, - SetSystemProxy: true, - BypassLAN: false, - AllowConnectionFromLAN: false, - EnableFakeDNS: false, - EnableDNSRouting: false, - IndependentDNSCache: false, - GeoIPPath: "geoip.db", - GeoSitePath: "geosite.db", - Rules: []Rule{}, - TLSTricks: TLSTricks{ - EnableFragment: false, - FragmentSize: "10-100", - FragmentSleep: "50-200", - EnableMixedSNICase: false, - EnablePadding: false, - PaddingSize: "100-200", - }, - } -} - func BuildConfigJson(configOpt ConfigOptions, input option.Options) (string, error) { options := BuildConfig(configOpt, input) var buffer bytes.Buffer diff --git a/shared/config.json.template b/config/config.json.template similarity index 100% rename from shared/config.json.template rename to config/config.json.template diff --git a/shared/debug.go b/config/debug.go similarity index 97% rename from shared/debug.go rename to config/debug.go index e95041a..8c1e214 100644 --- a/shared/debug.go +++ b/config/debug.go @@ -1,4 +1,4 @@ -package shared +package config import ( "bytes" diff --git a/config/option.go b/config/option.go new file mode 100644 index 0000000..d12b7f1 --- /dev/null +++ b/config/option.go @@ -0,0 +1,88 @@ +package config + +import ( + "time" + + "github.com/sagernet/sing-box/option" + dns "github.com/sagernet/sing-dns" +) + +type ConfigOptions struct { + ExecuteAsIs bool `json:"execute-config-as-is"` + LogLevel string `json:"log-level"` + ResolveDestination bool `json:"resolve-destination"` + IPv6Mode option.DomainStrategy `json:"ipv6-mode"` + RemoteDnsAddress string `json:"remote-dns-address"` + RemoteDnsDomainStrategy option.DomainStrategy `json:"remote-dns-domain-strategy"` + DirectDnsAddress string `json:"direct-dns-address"` + DirectDnsDomainStrategy option.DomainStrategy `json:"direct-dns-domain-strategy"` + MixedPort uint16 `json:"mixed-port"` + LocalDnsPort uint16 `json:"local-dns-port"` + MTU uint32 `json:"mtu"` + StrictRoute bool `json:"strict-route"` + TUNStack string `json:"tun-stack"` + ConnectionTestUrl string `json:"connection-test-url"` + URLTestInterval option.Duration `json:"url-test-interval"` + EnableClashApi bool `json:"enable-clash-api"` + ClashApiPort uint16 `json:"clash-api-port"` + EnableTun bool `json:"enable-tun"` + SetSystemProxy bool `json:"set-system-proxy"` + BypassLAN bool `json:"bypass-lan"` + AllowConnectionFromLAN bool `json:"allow-connection-from-lan"` + EnableFakeDNS bool `json:"enable-fake-dns"` + EnableDNSRouting bool `json:"enable-dns-routing"` + IndependentDNSCache bool `json:"independent-dns-cache"` + GeoIPPath string `json:"geoip-path"` + GeoSitePath string `json:"geosite-path"` + Rules []Rule `json:"rules"` + TLSTricks +} + +type TLSTricks struct { + EnableFragment bool `json:"enable-tls-fragment"` + FragmentSize string `json:"tls-fragment-size"` + FragmentSleep string `json:"tls-fragment-sleep"` + EnableMixedSNICase bool `json:"enable-tls-mixed-sni-case"` + EnablePadding bool `json:"enable-tls-padding"` + PaddingSize string `json:"tls-padding-size"` +} + +func DefaultConfigOptions() *ConfigOptions { + return &ConfigOptions{ + ExecuteAsIs: false, + LogLevel: "info", + ResolveDestination: false, + IPv6Mode: option.DomainStrategy(dns.DomainStrategyAsIS), + RemoteDnsAddress: "1.1.1.1", + RemoteDnsDomainStrategy: option.DomainStrategy(dns.DomainStrategyAsIS), + DirectDnsAddress: "1.1.1.1", + DirectDnsDomainStrategy: option.DomainStrategy(dns.DomainStrategyAsIS), + MixedPort: 2334, + LocalDnsPort: 6450, + MTU: 9000, + StrictRoute: true, + TUNStack: "mixed", + ConnectionTestUrl: "https://cp.cloudflare.com/", + URLTestInterval: option.Duration(10 * time.Minute), + EnableClashApi: true, + ClashApiPort: 6756, + EnableTun: true, + SetSystemProxy: true, + BypassLAN: false, + AllowConnectionFromLAN: false, + EnableFakeDNS: false, + EnableDNSRouting: false, + IndependentDNSCache: false, + GeoIPPath: "geoip.db", + GeoSitePath: "geosite.db", + Rules: []Rule{}, + TLSTricks: TLSTricks{ + EnableFragment: false, + FragmentSize: "10-100", + FragmentSleep: "50-200", + EnableMixedSNICase: false, + EnablePadding: false, + PaddingSize: "100-200", + }, + } +} diff --git a/shared/parser.go b/config/parser.go similarity index 99% rename from shared/parser.go rename to config/parser.go index 6a6f92c..b5ef618 100644 --- a/shared/parser.go +++ b/config/parser.go @@ -1,4 +1,4 @@ -package shared +package config import ( _ "embed" diff --git a/shared/rules.go b/config/rules.go similarity index 99% rename from shared/rules.go rename to config/rules.go index 19672eb..8ce7a91 100644 --- a/shared/rules.go +++ b/config/rules.go @@ -1,4 +1,4 @@ -package shared +package config import ( "strconv" diff --git a/custom/custom.go b/custom/custom.go index 653c564..32b8091 100644 --- a/custom/custom.go +++ b/custom/custom.go @@ -12,13 +12,13 @@ import ( "unsafe" "github.com/hiddify/libcore/bridge" - "github.com/hiddify/libcore/shared" + "github.com/hiddify/libcore/config" "github.com/sagernet/sing-box/experimental/libbox" "github.com/sagernet/sing-box/log" ) var box *libbox.BoxService -var configOptions *shared.ConfigOptions +var configOptions *config.ConfigOptions var activeConfigPath *string var logFactory *log.Factory @@ -29,7 +29,7 @@ func setupOnce(api unsafe.Pointer) { //export setup func setup(baseDir *C.char, workingDir *C.char, tempDir *C.char, statusPort C.longlong, debug bool) (CErr *C.char) { - defer shared.DeferPanicToError("setup", func(err error) { + defer config.DeferPanicToError("setup", func(err error) { CErr = C.CString(err.Error()) }) @@ -55,11 +55,11 @@ func setup(baseDir *C.char, workingDir *C.char, tempDir *C.char, statusPort C.lo //export parse func parse(path *C.char, tempPath *C.char, debug bool) (CErr *C.char) { - defer shared.DeferPanicToError("parse", func(err error) { + defer config.DeferPanicToError("parse", func(err error) { CErr = C.CString(err.Error()) }) - err := shared.ParseConfig(C.GoString(path), C.GoString(tempPath), debug) + err := config.ParseConfig(C.GoString(path), C.GoString(tempPath), debug) if err != nil { return C.CString(err.Error()) } @@ -68,11 +68,11 @@ func parse(path *C.char, tempPath *C.char, debug bool) (CErr *C.char) { //export changeConfigOptions func changeConfigOptions(configOptionsJson *C.char) (CErr *C.char) { - defer shared.DeferPanicToError("changeConfigOptions", func(err error) { + defer config.DeferPanicToError("changeConfigOptions", func(err error) { CErr = C.CString(err.Error()) }) - configOptions = &shared.ConfigOptions{} + configOptions = &config.ConfigOptions{} err := json.Unmarshal([]byte(C.GoString(configOptionsJson)), configOptions) if err != nil { return C.CString(err.Error()) @@ -82,7 +82,7 @@ func changeConfigOptions(configOptionsJson *C.char) (CErr *C.char) { //export generateConfig func generateConfig(path *C.char) (res *C.char) { - defer shared.DeferPanicToError("generateConfig", func(err error) { + defer config.DeferPanicToError("generateConfig", func(err error) { res = C.CString("error" + err.Error()) }) @@ -93,7 +93,7 @@ func generateConfig(path *C.char) (res *C.char) { return C.CString(config) } -func generateConfigFromFile(path string, configOpt shared.ConfigOptions) (string, error) { +func generateConfigFromFile(path string, configOpt config.ConfigOptions) (string, error) { content, err := os.ReadFile(path) if err != nil { return "", err @@ -102,7 +102,7 @@ func generateConfigFromFile(path string, configOpt shared.ConfigOptions) (string if err != nil { return "", err } - config, err := shared.BuildConfigJson(configOpt, options) + config, err := config.BuildConfigJson(configOpt, options) if err != nil { return "", err } @@ -111,7 +111,7 @@ func generateConfigFromFile(path string, configOpt shared.ConfigOptions) (string //export start func start(configPath *C.char, disableMemoryLimit bool) (CErr *C.char) { - defer shared.DeferPanicToError("start", func(err error) { + defer config.DeferPanicToError("start", func(err error) { CErr = C.CString(err.Error()) }) @@ -140,9 +140,9 @@ func startService(delayStart bool) error { if err != nil { return stopAndAlert(EmptyConfiguration, err) } - options = shared.BuildConfig(*configOptions, options) + options = config.BuildConfig(*configOptions, options) - shared.SaveCurrentConfig(sWorkingPath, options) + config.SaveCurrentConfig(sWorkingPath, options) err = startCommandServer(*logFactory) if err != nil { @@ -171,7 +171,7 @@ func startService(delayStart bool) error { //export stop func stop() (CErr *C.char) { - defer shared.DeferPanicToError("stop", func(err error) { + defer config.DeferPanicToError("stop", func(err error) { CErr = C.CString(err.Error()) }) @@ -202,7 +202,7 @@ func stop() (CErr *C.char) { //export restart func restart(configPath *C.char, disableMemoryLimit bool) (CErr *C.char) { - defer shared.DeferPanicToError("restart", func(err error) { + defer config.DeferPanicToError("restart", func(err error) { CErr = C.CString(err.Error()) }) log.Debug("[Service] Restarting") @@ -253,7 +253,7 @@ func stopCommandClient(command C.int) *C.char { //export selectOutbound func selectOutbound(groupTag *C.char, outboundTag *C.char) (CErr *C.char) { - defer shared.DeferPanicToError("selectOutbound", func(err error) { + defer config.DeferPanicToError("selectOutbound", func(err error) { CErr = C.CString(err.Error()) }) @@ -266,7 +266,7 @@ func selectOutbound(groupTag *C.char, outboundTag *C.char) (CErr *C.char) { //export urlTest func urlTest(groupTag *C.char) (CErr *C.char) { - defer shared.DeferPanicToError("urlTest", func(err error) { + defer config.DeferPanicToError("urlTest", func(err error) { CErr = C.CString(err.Error()) }) diff --git a/mobile/mobile.go b/mobile/mobile.go index cee8e0d..5a9a037 100644 --- a/mobile/mobile.go +++ b/mobile/mobile.go @@ -4,13 +4,13 @@ import ( "encoding/json" "os" - "github.com/hiddify/libcore/shared" + "github.com/hiddify/libcore/config" _ "github.com/sagernet/gomobile" "github.com/sagernet/sing-box/option" ) func Parse(path string, tempPath string, debug bool) error { - return shared.ParseConfig(path, tempPath, debug) + return config.ParseConfig(path, tempPath, debug) } func BuildConfig(path string, configOptionsJson string) (string, error) { @@ -23,10 +23,10 @@ func BuildConfig(path string, configOptionsJson string) (string, error) { if err != nil { return "", err } - configOptions := &shared.ConfigOptions{} + configOptions := &config.ConfigOptions{} err = json.Unmarshal([]byte(configOptionsJson), configOptions) if err != nil { return "", nil } - return shared.BuildConfigJson(*configOptions, options) + return config.BuildConfigJson(*configOptions, options) }