2024-03-09 15:49:09 +01:00
|
|
|
package cmd
|
2023-12-02 19:52:16 +03:30
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
2024-03-22 16:25:56 +00:00
|
|
|
"github.com/hiddify/hiddify-core/config"
|
2024-01-15 02:27:15 +03:30
|
|
|
"github.com/sagernet/sing-box/experimental/libbox"
|
2023-12-02 19:52:16 +03:30
|
|
|
"github.com/sagernet/sing-box/log"
|
|
|
|
|
"github.com/sagernet/sing-box/option"
|
|
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
)
|
|
|
|
|
|
2024-01-15 18:18:53 +03:30
|
|
|
var commandBuildOutputPath string
|
|
|
|
|
|
2023-12-02 19:52:16 +03:30
|
|
|
var commandBuild = &cobra.Command{
|
|
|
|
|
Use: "build",
|
|
|
|
|
Short: "Build configuration",
|
2024-01-15 02:16:22 +03:30
|
|
|
Args: cobra.MinimumNArgs(1),
|
2023-12-02 19:52:16 +03:30
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2024-01-15 02:16:22 +03:30
|
|
|
var optionsPath string
|
|
|
|
|
if len(args) > 1 {
|
|
|
|
|
optionsPath = args[1]
|
|
|
|
|
}
|
|
|
|
|
err := build(args[0], optionsPath)
|
2023-12-02 19:52:16 +03:30
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-15 02:27:15 +03:30
|
|
|
var commandCheck = &cobra.Command{
|
|
|
|
|
Use: "check",
|
|
|
|
|
Short: "Check configuration",
|
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
|
err := check(args[0])
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 19:52:16 +03:30
|
|
|
func init() {
|
2024-01-15 18:18:53 +03:30
|
|
|
commandBuild.Flags().StringVarP(&commandBuildOutputPath, "output", "o", "", "write result to file path instead of stdout")
|
2023-12-02 19:52:16 +03:30
|
|
|
mainCommand.AddCommand(commandBuild)
|
2024-01-15 02:27:15 +03:30
|
|
|
mainCommand.AddCommand(commandCheck)
|
2023-12-02 19:52:16 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func build(path string, optionsPath string) error {
|
|
|
|
|
if workingDir != "" {
|
|
|
|
|
path = filepath.Join(workingDir, path)
|
2024-01-15 02:16:22 +03:30
|
|
|
if optionsPath != "" {
|
|
|
|
|
optionsPath = filepath.Join(workingDir, optionsPath)
|
|
|
|
|
}
|
2024-01-26 02:18:21 +01:00
|
|
|
os.Chdir(workingDir)
|
2023-12-02 19:52:16 +03:30
|
|
|
}
|
|
|
|
|
options, err := readConfigAt(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2024-01-15 17:17:05 +03:30
|
|
|
configOptions := config.DefaultConfigOptions()
|
2024-01-15 02:16:22 +03:30
|
|
|
if optionsPath != "" {
|
|
|
|
|
configOptions, err = readConfigOptionsAt(optionsPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2023-12-02 19:52:16 +03:30
|
|
|
}
|
2024-01-15 17:17:05 +03:30
|
|
|
config, err := config.BuildConfigJson(*configOptions, *options)
|
2023-12-02 19:52:16 +03:30
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2024-01-15 18:18:53 +03:30
|
|
|
if commandBuildOutputPath != "" {
|
|
|
|
|
outputPath, _ := filepath.Abs(filepath.Join(workingDir, commandBuildOutputPath))
|
2024-01-29 21:55:01 +01:00
|
|
|
err = os.WriteFile(outputPath, []byte(config), 0644)
|
2024-01-15 18:18:53 +03:30
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
fmt.Println("result successfully written to ", outputPath)
|
2024-01-29 14:26:10 +01:00
|
|
|
// libbox.Setup(outputPath, workingDir, workingDir, true)
|
|
|
|
|
// instance, err := NewService(*patchedOptions)
|
2024-01-15 18:18:53 +03:30
|
|
|
} else {
|
|
|
|
|
os.Stdout.WriteString(config)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2023-12-02 19:52:16 +03:30
|
|
|
}
|
|
|
|
|
|
2024-01-15 02:27:15 +03:30
|
|
|
func check(path string) error {
|
|
|
|
|
content, err := os.ReadFile(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return libbox.CheckConfig(string(content))
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 19:52:16 +03:30
|
|
|
func readConfigAt(path string) (*option.Options, error) {
|
|
|
|
|
content, err := os.ReadFile(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
var options option.Options
|
|
|
|
|
err = options.UnmarshalJSON(content)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &options, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-09 15:49:09 +01:00
|
|
|
func readConfigBytes(content []byte) (*option.Options, error) {
|
|
|
|
|
var options option.Options
|
|
|
|
|
err := options.UnmarshalJSON(content)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &options, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-15 17:17:05 +03:30
|
|
|
func readConfigOptionsAt(path string) (*config.ConfigOptions, error) {
|
2023-12-02 19:52:16 +03:30
|
|
|
content, err := os.ReadFile(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2024-01-15 17:17:05 +03:30
|
|
|
var options config.ConfigOptions
|
2023-12-02 19:52:16 +03:30
|
|
|
err = json.Unmarshal(content, &options)
|
2024-02-21 19:51:49 +01:00
|
|
|
|
2023-12-02 19:52:16 +03:30
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2024-02-21 19:51:49 +01:00
|
|
|
if options.Warp.WireguardConfigStr != "" {
|
|
|
|
|
err := json.Unmarshal([]byte(options.Warp.WireguardConfigStr), &options.Warp.WireguardConfig)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 19:52:16 +03:30
|
|
|
return &options, nil
|
|
|
|
|
}
|