Refactor shared
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -1,4 +1,4 @@
|
||||
package shared
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
88
config/option.go
Normal file
88
config/option.go
Normal 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",
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package shared
|
||||
package config
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
@@ -1,4 +1,4 @@
|
||||
package shared
|
||||
package config
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
@@ -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())
|
||||
})
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user