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-05-31 13:08:55 +02:00
|
|
|
var (
|
|
|
|
|
hiddifySettingPath string
|
|
|
|
|
configPath string
|
|
|
|
|
defaultConfigs config.ConfigOptions = *config.DefaultConfigOptions()
|
|
|
|
|
commandBuildOutputPath string
|
|
|
|
|
)
|
2024-01-15 18:18:53 +03:30
|
|
|
|
2023-12-02 19:52:16 +03:30
|
|
|
var commandBuild = &cobra.Command{
|
|
|
|
|
Use: "build",
|
|
|
|
|
Short: "Build configuration",
|
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2024-05-31 13:08:55 +02:00
|
|
|
|
|
|
|
|
err := build(configPath, hiddifySettingPath)
|
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",
|
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2024-05-31 13:08:55 +02:00
|
|
|
err := check(configPath)
|
2024-01-15 02:27:15 +03:30
|
|
|
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)
|
|
|
|
|
}
|
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-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
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-31 13:08:55 +02:00
|
|
|
if options.Warp2.WireguardConfigStr != "" {
|
|
|
|
|
err := json.Unmarshal([]byte(options.Warp2.WireguardConfigStr), &options.Warp2.WireguardConfig)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-21 19:51:49 +01:00
|
|
|
|
2023-12-02 19:52:16 +03:30
|
|
|
return &options, nil
|
|
|
|
|
}
|
2024-05-29 18:47:04 +02:00
|
|
|
|
|
|
|
|
func addHConfigFlags(commandRun *cobra.Command) {
|
2024-05-31 13:08:55 +02:00
|
|
|
|
|
|
|
|
commandRun.Flags().StringVarP(&configPath, "config", "c", "", "proxy config path or url")
|
|
|
|
|
commandRun.MarkFlagRequired("config")
|
|
|
|
|
commandRun.Flags().StringVarP(&hiddifySettingPath, "hiddify", "d", "", "Hiddify Setting JSON Path")
|
2024-05-29 18:47:04 +02:00
|
|
|
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)")
|
2024-05-31 13:08:55 +02:00
|
|
|
commandRun.Flags().StringVar(&defaultConfigs.ClashApiSecret, "web-secret", "", "Web Server Secret")
|
|
|
|
|
commandRun.Flags().Uint16Var(&defaultConfigs.ClashApiPort, "web-port", 6756, "Web Server Port")
|
2024-05-29 18:47:04 +02:00
|
|
|
}
|