diff --git a/cmd/cmd_config.go b/cmd/cmd_config.go index adea138..0209776 100644 --- a/cmd/cmd_config.go +++ b/cmd/cmd_config.go @@ -7,6 +7,8 @@ import ( "path/filepath" "github.com/hiddify/hiddify-core/config" + pb "github.com/hiddify/hiddify-core/hiddifyrpc" + v2 "github.com/hiddify/hiddify-core/v2" "github.com/sagernet/sing-box/experimental/libbox" "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" @@ -32,6 +34,19 @@ var commandBuild = &cobra.Command{ } }, } +var generateConfig = &cobra.Command{ + Use: "gen", + Short: "gen configuration", + Run: func(cmd *cobra.Command, args []string) { + conf, err := v2.GenerateConfig(&pb.GenerateConfigRequest{ + Path: args[0], + }) + if err != nil { + log.Fatal(err) + } + log.Debug(string(conf.ConfigContent)) + }, +} var commandCheck = &cobra.Command{ Use: "check", @@ -49,6 +64,7 @@ func init() { addHConfigFlags(commandBuild) mainCommand.AddCommand(commandBuild) + mainCommand.AddCommand(generateConfig) } diff --git a/config/parser.go b/config/parser.go index 292c072..d504054 100644 --- a/config/parser.go +++ b/config/parser.go @@ -57,6 +57,7 @@ func ParseConfigContent(contentstr string, debug bool, configOpt *ConfigOptions, } newContent, _ := json.MarshalIndent(jsonObj, "", " ") + return patchConfig(newContent, "SingboxParser", configOpt) } @@ -113,6 +114,7 @@ func patchConfig(content []byte, name string, configOpt *ConfigOptions) ([]byte, } content, _ = json.MarshalIndent(options, "", " ") + fmt.Printf("%s\n", content) return validateResult(content, name) } diff --git a/custom/custom.go b/custom/custom.go index 07fef74..e1ce161 100644 --- a/custom/custom.go +++ b/custom/custom.go @@ -56,10 +56,15 @@ func changeConfigOptions(configOptionsJson *C.char) (CErr *C.char) { //export generateConfig func generateConfig(path *C.char) (res *C.char) { - _, err := v2.GenerateConfig(&pb.GenerateConfigRequest{ + conf, err := v2.GenerateConfig(&pb.GenerateConfigRequest{ Path: C.GoString(path), }) - return emptyOrErrorC(err) + if err != nil { + return emptyOrErrorC(err) + } + fmt.Printf("Config: %+v\n", conf) + fmt.Printf("ConfigContent: %+v\n", conf.ConfigContent) + return C.CString(conf.ConfigContent) } //export start diff --git a/v2/custom.go b/v2/custom.go index 6598717..a41d1cd 100644 --- a/v2/custom.go +++ b/v2/custom.go @@ -229,7 +229,9 @@ func GenerateConfig(in *pb.GenerateConfigRequest) (*pb.GenerateConfigResponse, e Log(pb.LogLevel_FATAL, pb.LogType_CONFIG, err.Error()) StopAndAlert(pb.MessageType_UNEXPECTED_ERROR, err.Error()) }) - + if configOptions == nil { + configOptions = config.DefaultConfigOptions() + } config, err := generateConfigFromFile(in.Path, *configOptions) if err != nil { return nil, err