Update config flow
This commit is contained in:
@@ -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())
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user