Refactor shared

This commit is contained in:
problematicconsumer
2024-01-15 17:17:05 +03:30
parent df98c5cab2
commit 8c90556e6a
9 changed files with 118 additions and 111 deletions

View File

@@ -6,7 +6,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"github.com/hiddify/libcore/shared" "github.com/hiddify/libcore/config"
"github.com/sagernet/sing-box/experimental/libbox" "github.com/sagernet/sing-box/experimental/libbox"
"github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
@@ -58,14 +58,14 @@ func build(path string, optionsPath string) error {
if err != nil { if err != nil {
return err return err
} }
configOptions := shared.DefaultConfigOptions() configOptions := config.DefaultConfigOptions()
if optionsPath != "" { if optionsPath != "" {
configOptions, err = readConfigOptionsAt(optionsPath) configOptions, err = readConfigOptionsAt(optionsPath)
if err != nil { if err != nil {
return err return err
} }
} }
config, err := shared.BuildConfigJson(*configOptions, *options) config, err := config.BuildConfigJson(*configOptions, *options)
if err != nil { if err != nil {
return err return err
} }
@@ -95,12 +95,12 @@ func readConfigAt(path string) (*option.Options, error) {
return &options, nil return &options, nil
} }
func readConfigOptionsAt(path string) (*shared.ConfigOptions, error) { func readConfigOptionsAt(path string) (*config.ConfigOptions, error) {
content, err := os.ReadFile(path) content, err := os.ReadFile(path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var options shared.ConfigOptions var options config.ConfigOptions
err = json.Unmarshal(content, &options) err = json.Unmarshal(content, &options)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@@ -1,4 +1,4 @@
package shared package config
import ( import (
"bytes" "bytes"
@@ -8,93 +8,12 @@ import (
"net/netip" "net/netip"
"net/url" "net/url"
"strings" "strings"
"time"
C "github.com/sagernet/sing-box/constant" C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
dns "github.com/sagernet/sing-dns" 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) { func BuildConfigJson(configOpt ConfigOptions, input option.Options) (string, error) {
options := BuildConfig(configOpt, input) options := BuildConfig(configOpt, input)
var buffer bytes.Buffer var buffer bytes.Buffer

View File

@@ -1,4 +1,4 @@
package shared package config
import ( import (
"bytes" "bytes"

88
config/option.go Normal file
View File

@@ -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",
},
}
}

View File

@@ -1,4 +1,4 @@
package shared package config
import ( import (
_ "embed" _ "embed"

View File

@@ -1,4 +1,4 @@
package shared package config
import ( import (
"strconv" "strconv"

View File

@@ -12,13 +12,13 @@ import (
"unsafe" "unsafe"
"github.com/hiddify/libcore/bridge" "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/experimental/libbox"
"github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/log"
) )
var box *libbox.BoxService var box *libbox.BoxService
var configOptions *shared.ConfigOptions var configOptions *config.ConfigOptions
var activeConfigPath *string var activeConfigPath *string
var logFactory *log.Factory var logFactory *log.Factory
@@ -29,7 +29,7 @@ func setupOnce(api unsafe.Pointer) {
//export setup //export setup
func setup(baseDir *C.char, workingDir *C.char, tempDir *C.char, statusPort C.longlong, debug bool) (CErr *C.char) { 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()) 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 //export parse
func parse(path *C.char, tempPath *C.char, debug bool) (CErr *C.char) { 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()) 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 { if err != nil {
return C.CString(err.Error()) return C.CString(err.Error())
} }
@@ -68,11 +68,11 @@ func parse(path *C.char, tempPath *C.char, debug bool) (CErr *C.char) {
//export changeConfigOptions //export changeConfigOptions
func changeConfigOptions(configOptionsJson *C.char) (CErr *C.char) { 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()) CErr = C.CString(err.Error())
}) })
configOptions = &shared.ConfigOptions{} configOptions = &config.ConfigOptions{}
err := json.Unmarshal([]byte(C.GoString(configOptionsJson)), configOptions) err := json.Unmarshal([]byte(C.GoString(configOptionsJson)), configOptions)
if err != nil { if err != nil {
return C.CString(err.Error()) return C.CString(err.Error())
@@ -82,7 +82,7 @@ func changeConfigOptions(configOptionsJson *C.char) (CErr *C.char) {
//export generateConfig //export generateConfig
func generateConfig(path *C.char) (res *C.char) { 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()) res = C.CString("error" + err.Error())
}) })
@@ -93,7 +93,7 @@ func generateConfig(path *C.char) (res *C.char) {
return C.CString(config) 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) content, err := os.ReadFile(path)
if err != nil { if err != nil {
return "", err return "", err
@@ -102,7 +102,7 @@ func generateConfigFromFile(path string, configOpt shared.ConfigOptions) (string
if err != nil { if err != nil {
return "", err return "", err
} }
config, err := shared.BuildConfigJson(configOpt, options) config, err := config.BuildConfigJson(configOpt, options)
if err != nil { if err != nil {
return "", err return "", err
} }
@@ -111,7 +111,7 @@ func generateConfigFromFile(path string, configOpt shared.ConfigOptions) (string
//export start //export start
func start(configPath *C.char, disableMemoryLimit bool) (CErr *C.char) { 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()) CErr = C.CString(err.Error())
}) })
@@ -140,9 +140,9 @@ func startService(delayStart bool) error {
if err != nil { if err != nil {
return stopAndAlert(EmptyConfiguration, err) 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) err = startCommandServer(*logFactory)
if err != nil { if err != nil {
@@ -171,7 +171,7 @@ func startService(delayStart bool) error {
//export stop //export stop
func stop() (CErr *C.char) { func stop() (CErr *C.char) {
defer shared.DeferPanicToError("stop", func(err error) { defer config.DeferPanicToError("stop", func(err error) {
CErr = C.CString(err.Error()) CErr = C.CString(err.Error())
}) })
@@ -202,7 +202,7 @@ func stop() (CErr *C.char) {
//export restart //export restart
func restart(configPath *C.char, disableMemoryLimit bool) (CErr *C.char) { 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()) CErr = C.CString(err.Error())
}) })
log.Debug("[Service] Restarting") log.Debug("[Service] Restarting")
@@ -253,7 +253,7 @@ func stopCommandClient(command C.int) *C.char {
//export selectOutbound //export selectOutbound
func selectOutbound(groupTag *C.char, outboundTag *C.char) (CErr *C.char) { 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()) CErr = C.CString(err.Error())
}) })
@@ -266,7 +266,7 @@ func selectOutbound(groupTag *C.char, outboundTag *C.char) (CErr *C.char) {
//export urlTest //export urlTest
func urlTest(groupTag *C.char) (CErr *C.char) { 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()) CErr = C.CString(err.Error())
}) })

View File

@@ -4,13 +4,13 @@ import (
"encoding/json" "encoding/json"
"os" "os"
"github.com/hiddify/libcore/shared" "github.com/hiddify/libcore/config"
_ "github.com/sagernet/gomobile" _ "github.com/sagernet/gomobile"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
) )
func Parse(path string, tempPath string, debug bool) error { 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) { func BuildConfig(path string, configOptionsJson string) (string, error) {
@@ -23,10 +23,10 @@ func BuildConfig(path string, configOptionsJson string) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
configOptions := &shared.ConfigOptions{} configOptions := &config.ConfigOptions{}
err = json.Unmarshal([]byte(configOptionsJson), configOptions) err = json.Unmarshal([]byte(configOptionsJson), configOptions)
if err != nil { if err != nil {
return "", nil return "", nil
} }
return shared.BuildConfigJson(*configOptions, options) return config.BuildConfigJson(*configOptions, options)
} }