From a5b1fbc2d70de5ce25dd68c8cfdff109375bc8eb Mon Sep 17 00:00:00 2001 From: problematicconsumer Date: Sat, 2 Dec 2023 19:52:16 +0330 Subject: [PATCH] Add basic cli --- cmd/cmd_config.go | 78 +++++++++++++++++++++++++++++++++++++++++++++++ cmd/main.go | 46 ++++++++++++++++++++++++++++ go.mod | 3 ++ go.sum | 8 +++++ 4 files changed, 135 insertions(+) create mode 100644 cmd/cmd_config.go create mode 100644 cmd/main.go diff --git a/cmd/cmd_config.go b/cmd/cmd_config.go new file mode 100644 index 0000000..8a79062 --- /dev/null +++ b/cmd/cmd_config.go @@ -0,0 +1,78 @@ +package main + +import ( + "encoding/json" + "fmt" + "os" + "path/filepath" + + "github.com/hiddify/libcore/shared" + "github.com/sagernet/sing-box/log" + "github.com/sagernet/sing-box/option" + + "github.com/spf13/cobra" +) + +var commandBuild = &cobra.Command{ + Use: "build", + Short: "Build configuration", + Args: cobra.ExactArgs(2), + Run: func(cmd *cobra.Command, args []string) { + err := build(args[0], args[1]) + if err != nil { + log.Fatal(err) + } + }, +} + +func init() { + mainCommand.AddCommand(commandBuild) +} + +func build(path string, optionsPath string) error { + if workingDir != "" { + path = filepath.Join(workingDir, path) + optionsPath = filepath.Join(workingDir, optionsPath) + } + options, err := readConfigAt(path) + if err != nil { + return err + } + configOptions, err := readConfigOptionsAt(optionsPath) + if err != nil { + return err + } + config, err := shared.BuildConfigJson(*configOptions, *options) + if err != nil { + return err + } + + fmt.Printf("%s\n", config) + return err +} + +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 readConfigOptionsAt(path string) (*shared.ConfigOptions, error) { + content, err := os.ReadFile(path) + if err != nil { + return nil, err + } + var options shared.ConfigOptions + err = json.Unmarshal(content, &options) + if err != nil { + return nil, err + } + return &options, nil +} diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..3656795 --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,46 @@ +package main + +import ( + "os" + "time" + + "github.com/sagernet/sing-box/log" + + "github.com/spf13/cobra" +) + +var ( + workingDir string + disableColor bool +) + +var mainCommand = &cobra.Command{ + Use: "hiddify-next", + PersistentPreRun: preRun, +} + +func init() { + mainCommand.PersistentFlags().StringVarP(&workingDir, "directory", "D", "", "set working directory") + mainCommand.PersistentFlags().BoolVarP(&disableColor, "disable-color", "", false, "disable color output") +} + +func main() { + if err := mainCommand.Execute(); err != nil { + log.Fatal(err) + } +} + +func preRun(cmd *cobra.Command, args []string) { + if disableColor { + log.SetStdLogger(log.NewFactory(log.Formatter{BaseTime: time.Now(), DisableColors: true}, os.Stderr, nil).Logger()) + } + if workingDir != "" { + _, err := os.Stat(workingDir) + if err != nil { + os.MkdirAll(workingDir, 0o777) + } + if err := os.Chdir(workingDir); err != nil { + log.Fatal(err) + } + } +} diff --git a/go.mod b/go.mod index cb7e895..fd3a646 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/sagernet/sing v0.2.18-0.20231124125253-2dcabf4bfcbc github.com/sagernet/sing-box v1.7.0 github.com/sagernet/sing-dns v0.1.11 + github.com/spf13/cobra v1.8.0 github.com/xmdhs/clash2singbox v0.0.2 gopkg.in/yaml.v3 v3.0.1 ) @@ -32,6 +33,7 @@ require ( github.com/google/btree v1.1.2 // indirect github.com/google/pprof v0.0.0-20231101202521-4ca4178f5c7a // indirect github.com/hashicorp/yamux v0.1.1 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/insomniacslk/dhcp v0.0.0-20231126010706-b0416c0f187a // indirect github.com/josharian/native v1.1.0 // indirect github.com/klauspost/compress v1.15.15 // indirect @@ -75,6 +77,7 @@ require ( github.com/sagernet/wireguard-go v0.0.0-20230807125731-5d4a7ef2dc5f // indirect github.com/sagernet/ws v0.0.0-20231030053741-7d481eb31bed // indirect github.com/scjalliance/comshim v0.0.0-20230315213746-5e51f40bd3b9 // indirect + github.com/spf13/pflag v1.0.5 // indirect github.com/u-root/uio v0.0.0-20230220225925-ffce2a382923 // indirect github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 // indirect github.com/zeebo/blake3 v0.2.3 // indirect diff --git a/go.sum b/go.sum index 4e848d2..33e4462 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,7 @@ github.com/caddyserver/certmagic v0.19.2 h1:HZd1AKLx4592MalEGQS39DKs2ZOAJCEM/xYP github.com/caddyserver/certmagic v0.19.2/go.mod h1:fsL01NomQ6N+kE2j37ZCnig2MFosG+MIO4ztnmG/zz8= github.com/cloudflare/circl v1.3.6 h1:/xbKIqSHbZXHwkhbrhrt2YOHIwYJlXH94E3tI/gDlUg= github.com/cloudflare/circl v1.3.6/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA= +github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/cretz/bine v0.1.0/go.mod h1:6PF6fWAvYtwjRGkAuDEJeWNOv3a2hUouSP/yRYXmvHw= github.com/cretz/bine v0.2.0 h1:8GiDRGlTgz+o8H9DSnsl+5MeBK4HsExxgl6WgzOCuZo= github.com/cretz/bine v0.2.0/go.mod h1:WU4o9QR9wWp8AVKtTM1XD5vUHkEqnf2vVSo6dBqbetI= @@ -50,6 +51,8 @@ github.com/hiddify/hiddify-sing-box v1.4.0-rc.3.0.20231130094503-234b61a469fe h1 github.com/hiddify/hiddify-sing-box v1.4.0-rc.3.0.20231130094503-234b61a469fe/go.mod h1:n6X4n0yGgY5T8jN1ThT7Erjb2H9fYlQUCCqF1mr3P7w= github.com/hiddify/ray2sing v0.0.0-20231126095652-91be355a9a97 h1:rEou3JH0CByobO3a1NpJ8HyIFJtN0vXP09nZm4KXiC8= github.com/hiddify/ray2sing v0.0.0-20231126095652-91be355a9a97/go.mod h1:iTioDkGw9gbEAXGSjCzBW+I4NmJqn0RpTDqge3QFdOo= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/insomniacslk/dhcp v0.0.0-20231126010706-b0416c0f187a h1:biHpNhTkyeXNEzLqTOM6DkIkMedNh/j+ft+POj0Xcko= github.com/insomniacslk/dhcp v0.0.0-20231126010706-b0416c0f187a/go.mod h1:3A9PQ1cunSDF/1rbTq99Ts4pVnycWg+vlPkfeD2NLFI= github.com/josharian/native v1.0.1-0.20221213033349-c1e37c09b531/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w= @@ -107,6 +110,7 @@ github.com/quic-go/qpack v0.4.0 h1:Cr9BXA1sQS2SmDUWjSofMPNKmvF6IiIfDRmgU0w1ZCo= github.com/quic-go/qpack v0.4.0/go.mod h1:UZVnYIfi5GRk+zI9UMaCPsmZ2xKJP7XBUvVyT1Knj9A= github.com/quic-go/qtls-go1-20 v0.4.1 h1:D33340mCNDAIKBqXuAvexTNMUByrYmFYVfKfDN5nfFs= github.com/quic-go/qtls-go1-20 v0.4.1/go.mod h1:X9Nh97ZL80Z+bX/gUXMbipO6OxdiDi58b/fMC9mAL+k= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sagernet/bbolt v0.0.0-20231014093535-ea5cb2fe9f0a h1:+NkI2670SQpQWvkkD2QgdTuzQG263YZ+2emfpeyGqW0= github.com/sagernet/bbolt v0.0.0-20231014093535-ea5cb2fe9f0a/go.mod h1:63s7jpZqcDAIpj8oI/1v4Izok+npJOHACFCU6+huCkM= github.com/sagernet/cloudflare-tls v0.0.0-20230829051644-4a68352d0c4a h1:wZHruBxZCsQLXHAozWpnJBL3wJ/XufDpz0qKtgpSnA4= @@ -155,6 +159,10 @@ github.com/sagernet/ws v0.0.0-20231030053741-7d481eb31bed h1:90a510OeE9siSJoYsI8 github.com/sagernet/ws v0.0.0-20231030053741-7d481eb31bed/go.mod h1:LtfoSK3+NG57tvnVEHgcuBW9ujgE8enPSgzgwStwCAA= github.com/scjalliance/comshim v0.0.0-20230315213746-5e51f40bd3b9 h1:rc/CcqLH3lh8n+csdOuDfP+NuykE0U6AeYSJJHKDgSg= github.com/scjalliance/comshim v0.0.0-20230315213746-5e51f40bd3b9/go.mod h1:a/83NAfUXvEuLpmxDssAXxgUgrEy12MId3Wd7OTs76s= +github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= +github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=