From b43301ca0bc1f5b1ab669f47c0fa78752cf29742 Mon Sep 17 00:00:00 2001 From: problematicconsumer Date: Sat, 19 Aug 2023 14:09:49 +0330 Subject: [PATCH] Update config flow --- custom/custom.go | 25 +++++++++++++------------ custom/service.go | 6 +----- mobile/mobile.go | 24 +++++++++++++++++++++--- shared/converter.go | 38 -------------------------------------- 4 files changed, 35 insertions(+), 58 deletions(-) delete mode 100644 shared/converter.go diff --git a/custom/custom.go b/custom/custom.go index cc00d7d..425320f 100644 --- a/custom/custom.go +++ b/custom/custom.go @@ -5,8 +5,9 @@ package main */ import "C" import ( + "os" + "github.com/hiddify/libcore/shared" - B "github.com/sagernet/sing-box/experimental/libbox" ) var box *BoxService @@ -18,14 +19,9 @@ func setup(baseDir *C.char, workingDir *C.char, tempDir *C.char) { Setup(C.GoString(baseDir), C.GoString(workingDir), C.GoString(tempDir)) } -//export checkConfig -func checkConfig(configPath *C.char) *C.char { - configContent, err := shared.ConvertToSingbox(C.GoString(configPath), templateOptions) - if err != nil { - return C.CString(err.Error()) - } - - err = B.CheckConfig(string(configContent)) +//export parse +func parse(path *C.char) *C.char { + err := shared.ParseConfig(C.GoString(path)) if err != nil { return C.CString(err.Error()) } @@ -35,13 +31,18 @@ func checkConfig(configPath *C.char) *C.char { //export create func create(configPath *C.char) *C.char { path := C.GoString(configPath) - - configContent, err := shared.ConvertToSingbox(path, templateOptions) + content, err := os.ReadFile(path) if err != nil { return C.CString(err.Error()) } + options, err := parseConfig(string(content)) + if err != nil { + return C.CString(err.Error()) + } + overrides := shared.ConfigOverrides{ExcludeTunInbound: true, IncludeMixedInbound: true, IncludeLogOutput: true, LogLevel: "info", IncludeLogTimestamp: false, ClashApiPort: 9090} + options = shared.ApplyOverrides(options, overrides) - instance, err := NewService(string(configContent)) + instance, err := NewService(options) if err != nil { return C.CString(err.Error()) } diff --git a/custom/service.go b/custom/service.go index da853bd..1c05590 100644 --- a/custom/service.go +++ b/custom/service.go @@ -34,11 +34,7 @@ func Setup(basePath string, workingPath string, tempPath string) { sGroupID = os.Getgid() } -func NewService(configContent string) (*BoxService, error) { - options, err := parseConfig(configContent) - if err != nil { - return nil, err - } +func NewService(options option.Options) (*BoxService, error) { ctx, cancel := context.WithCancel(context.Background()) ctx = filemanager.WithDefault(ctx, sWorkingPath, sTempPath, sUserID, sGroupID) ctx = service.ContextWithPtr(ctx, urltest.NewHistoryStorage()) diff --git a/mobile/mobile.go b/mobile/mobile.go index 5c32445..1cc0542 100644 --- a/mobile/mobile.go +++ b/mobile/mobile.go @@ -1,12 +1,30 @@ package mobile import ( + "encoding/json" + "os" + "github.com/hiddify/libcore/shared" _ "github.com/sagernet/gomobile/event/key" + "github.com/sagernet/sing-box/option" ) -func ConvertToSingbox(path string) (string, error) { - options := shared.ConfigTemplateOptions{IncludeTunInbound: true, IncludeMixedInbound: false, IncludeLogOutput: false} - config, err := shared.ConvertToSingbox(path, options) +func Parse(path string) error { + return shared.ParseConfig(path) +} + +func ApplyOverrides(path string) (string, error) { + fileContent, err := os.ReadFile(path) + if err != nil { + return "", err + } + var options option.Options + err = options.UnmarshalJSON(fileContent) + if err != nil { + return "", err + } + overrides := shared.ConfigOverrides{ExcludeTunInbound: false, IncludeMixedInbound: false, IncludeLogOutput: false, LogLevel: "", IncludeLogTimestamp: false, ClashApiPort: 9090} + options = shared.ApplyOverrides(options, overrides) + config, err := json.Marshal(options) return string(config), err } diff --git a/shared/converter.go b/shared/converter.go deleted file mode 100644 index b2a5bcc..0000000 --- a/shared/converter.go +++ /dev/null @@ -1,38 +0,0 @@ -package shared - -import ( - _ "embed" - "fmt" - "os" - - "github.com/xmdhs/clash2singbox/convert" - "github.com/xmdhs/clash2singbox/model/clash" - "gopkg.in/yaml.v3" -) - -func ConvertToSingbox(path string, options ConfigTemplateOptions) ([]byte, error) { - clashConfig := clash.Clash{} - fileContent, err := os.ReadFile(path) - if err != nil { - return nil, err - } - err = yaml.Unmarshal(fileContent, &clashConfig) - if err != nil { - fmt.Printf("unmarshal error %s", err) - return nil, err - } - sbConfig, err := convert.Clash2sing(clashConfig) - if err != nil { - fmt.Printf("convert error %s", err) - return nil, err - } - - output := defaultTemplate(options) - output, err = convert.Patch(output, sbConfig, "", "", nil) - if err != nil { - fmt.Printf("patch error %s", err) - return output, err - } - - return output, nil -}