Files
umbrix-libcore/cmd/cmd_config.go

166 lines
4.7 KiB
Go
Raw Normal View History

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-05-29 18:47:04 +02:00
var defaultConfigs config.ConfigOptions
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")
2024-05-29 18:47:04 +02:00
addHConfigFlags(commandBuild)
2023-12-02 19:52:16 +03:30
mainCommand.AddCommand(commandBuild)
2024-05-29 18:47:04 +02:00
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)
}
os.Chdir(workingDir)
2023-12-02 19:52:16 +03:30
}
options, err := readConfigAt(path)
if err != nil {
return err
}
2024-05-29 18:47:04 +02:00
configOptions := &defaultConfigs //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
}
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)
2023-12-02 19:52:16 +03:30
if err != nil {
return nil, err
}
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
}
2024-05-29 18:47:04 +02:00
func addHConfigFlags(commandRun *cobra.Command) {
commandRun.Flags().BoolVar(&defaultConfigs.EnableFullConfig, "full-config", false, "allows including tags other than output")
commandRun.Flags().StringVar(&defaultConfigs.LogLevel, "log", "warn", "log level")
commandRun.Flags().BoolVar(&defaultConfigs.InboundOptions.EnableTun, "tun", false, "Enable Tun")
commandRun.Flags().BoolVar(&defaultConfigs.InboundOptions.EnableTunService, "tun-service", false, "Enable Tun Service")
commandRun.Flags().BoolVar(&defaultConfigs.InboundOptions.SetSystemProxy, "system-proxy", false, "Enable System Proxy")
commandRun.Flags().Uint16Var(&defaultConfigs.InboundOptions.MixedPort, "in-proxy-port", 2334, "Input Mixed Port")
commandRun.Flags().BoolVar(&defaultConfigs.TLSTricks.EnableFragment, "fragment", false, "Enable Fragment")
commandRun.Flags().StringVar(&defaultConfigs.TLSTricks.FragmentSize, "fragment-size", "2-4", "FragmentSize")
commandRun.Flags().StringVar(&defaultConfigs.TLSTricks.FragmentSleep, "fragment-sleep", "2-4", "FragmentSleep")
commandRun.Flags().BoolVar(&defaultConfigs.TLSTricks.EnablePadding, "padding", false, "Enable Padding")
commandRun.Flags().StringVar(&defaultConfigs.TLSTricks.PaddingSize, "padding-size", "1300-1400", "PaddingSize")
commandRun.Flags().BoolVar(&defaultConfigs.TLSTricks.MixedSNICase, "mixed-sni-case", false, "MixedSNICase")
commandRun.Flags().StringVar(&defaultConfigs.RemoteDnsAddress, "dns-remote", "1.1.1.1", "RemoteDNS (1.1.1.1, https://1.1.1.1/dns-query)")
commandRun.Flags().StringVar(&defaultConfigs.DirectDnsAddress, "dns-direct", "1.1.1.1", "DirectDNS (1.1.1.1, https://1.1.1.1/dns-query)")
}