From 849c6a2d22ef056ce072a3a5988c85cc1e63316f Mon Sep 17 00:00:00 2001 From: H1JK Date: Sat, 4 Mar 2023 16:44:10 +0800 Subject: [PATCH] Renames --- cmd/sing-box/cmd_generate.go | 4 +- cmd/sing-box/internal/generate/psk.go | 42 ------------------- cmd/sing-box/internal/generate/rand.go | 42 +++++++++++++++++++ .../generate/{x25519.go => reality.go} | 11 ++--- cmd/sing-box/internal/generate/uuid.go | 1 + docs/configuration/inbound/shadowsocks.md | 10 ++--- docs/configuration/inbound/shadowsocks.zh.md | 10 ++--- docs/configuration/shared/tls.md | 4 +- docs/configuration/shared/tls.zh.md | 4 +- 9 files changed, 65 insertions(+), 63 deletions(-) delete mode 100644 cmd/sing-box/internal/generate/psk.go create mode 100644 cmd/sing-box/internal/generate/rand.go rename cmd/sing-box/internal/generate/{x25519.go => reality.go} (75%) diff --git a/cmd/sing-box/cmd_generate.go b/cmd/sing-box/cmd_generate.go index 207f23ed..82eeafbe 100644 --- a/cmd/sing-box/cmd_generate.go +++ b/cmd/sing-box/cmd_generate.go @@ -12,8 +12,8 @@ var commandGenerate = &cobra.Command{ } func init() { - commandGenerate.AddCommand(generate.CommandGeneratePSK) + commandGenerate.AddCommand(generate.CommandGenerateRandom) commandGenerate.AddCommand(generate.CommandGenerateUUID) - commandGenerate.AddCommand(generate.CommandGenerateX25519) + commandGenerate.AddCommand(generate.CommandGenerateReality) mainCommand.AddCommand(commandGenerate) } diff --git a/cmd/sing-box/internal/generate/psk.go b/cmd/sing-box/internal/generate/psk.go deleted file mode 100644 index 720655a2..00000000 --- a/cmd/sing-box/internal/generate/psk.go +++ /dev/null @@ -1,42 +0,0 @@ -package generate - -import ( - "crypto/rand" - "encoding/base64" - "encoding/hex" - "os" - - "github.com/sagernet/sing-box/log" - - "github.com/spf13/cobra" -) - -var CommandGeneratePSK = &cobra.Command{ - Use: "psk", - Short: "Generate a random PSK", - Run: func(cmd *cobra.Command, args []string) { - if size > 0 { - encoder := base64.StdEncoding.EncodeToString - if outputHex { - encoder = hex.EncodeToString - } - - psk := make([]byte, size) - _, err := rand.Read(psk) - if err != nil { - log.Fatal(err) - } - - os.Stdout.WriteString(encoder(psk) + "\n") - } else { - cmd.Help() - } - }, -} - -var size int - -func init() { - CommandGeneratePSK.Flags().BoolVarP(&outputHex, "hex", "H", false, "print hex format") - CommandGeneratePSK.Flags().IntVarP(&size, "size", "s", 0, "PSK size") -} diff --git a/cmd/sing-box/internal/generate/rand.go b/cmd/sing-box/internal/generate/rand.go new file mode 100644 index 00000000..83e948b2 --- /dev/null +++ b/cmd/sing-box/internal/generate/rand.go @@ -0,0 +1,42 @@ +package generate + +import ( + "crypto/rand" + "encoding/base64" + "encoding/hex" + "os" + "strconv" + + "github.com/sagernet/sing-box/log" + + "github.com/spf13/cobra" +) + +var CommandGenerateRandom = &cobra.Command{ + Use: "rand", + Short: "Generate random bytes", + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + length, err := strconv.Atoi(args[0]) + if err != nil { + log.Fatal(err) + } + + encoder := base64.StdEncoding.EncodeToString + if outputHex { + encoder = hex.EncodeToString + } + + bs := make([]byte, length) + _, err = rand.Read(bs) + if err != nil { + log.Fatal(err) + } + + os.Stdout.WriteString(encoder(bs) + "\n") + }, +} + +func init() { + CommandGenerateRandom.Flags().BoolVarP(&outputHex, "hex", "H", false, "print hex format") +} diff --git a/cmd/sing-box/internal/generate/x25519.go b/cmd/sing-box/internal/generate/reality.go similarity index 75% rename from cmd/sing-box/internal/generate/x25519.go rename to cmd/sing-box/internal/generate/reality.go index a372667b..c0e50718 100644 --- a/cmd/sing-box/internal/generate/x25519.go +++ b/cmd/sing-box/internal/generate/reality.go @@ -13,9 +13,10 @@ import ( "golang.org/x/crypto/curve25519" ) -var CommandGenerateX25519 = &cobra.Command{ - Use: "x25519", - Short: "Generate a X25519 key pair", +var CommandGenerateReality = &cobra.Command{ + Use: "reality-key", + Short: "Generate a REALITY protocol X25519 key pair", + Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { encoder := base64.RawURLEncoding.EncodeToString if outputHex { @@ -51,6 +52,6 @@ var CommandGenerateX25519 = &cobra.Command{ } func init() { - CommandGenerateX25519.Flags().BoolVarP(&outputHex, "hex", "H", false, "print hex format") - CommandGenerateX25519.Flags().StringVarP(&input, "input", "i", "", "generate from specified private key") + CommandGenerateReality.Flags().BoolVarP(&outputHex, "hex", "H", false, "print hex format") + CommandGenerateReality.Flags().StringVarP(&input, "input", "i", "", "generate from specified private key") } diff --git a/cmd/sing-box/internal/generate/uuid.go b/cmd/sing-box/internal/generate/uuid.go index 07bf68a8..fba4c73d 100644 --- a/cmd/sing-box/internal/generate/uuid.go +++ b/cmd/sing-box/internal/generate/uuid.go @@ -12,6 +12,7 @@ import ( var CommandGenerateUUID = &cobra.Command{ Use: "uuid", Short: "Generate a UUID", + Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { var ( newUUID uuid.UUID diff --git a/docs/configuration/inbound/shadowsocks.md b/docs/configuration/inbound/shadowsocks.md index daebfb12..fc065cab 100644 --- a/docs/configuration/inbound/shadowsocks.md +++ b/docs/configuration/inbound/shadowsocks.md @@ -77,11 +77,11 @@ Both if empty. ==Required== -| Method | Password Format | -|---------------|-----------------------------------------| -| none | / | -| 2022 methods | `sing-box generate psk -s ` | -| other methods | any string | +| Method | Password Format | +|---------------|---------------------------------------| +| none | / | +| 2022 methods | `sing-box generate rand ` | +| other methods | any string | ### Listen Fields diff --git a/docs/configuration/inbound/shadowsocks.zh.md b/docs/configuration/inbound/shadowsocks.zh.md index 7aa78ab9..cfddc65b 100644 --- a/docs/configuration/inbound/shadowsocks.zh.md +++ b/docs/configuration/inbound/shadowsocks.zh.md @@ -77,8 +77,8 @@ See [Listen Fields](/configuration/shared/listen) for details. ==必填== -| 方法 | 密码格式 | -|---------------|-----------------------------------| -| none | / | -| 2022 methods | `sing-box generate psk -s <密钥长度>` | -| other methods | 任意字符串 | \ No newline at end of file +| 方法 | 密码格式 | +|---------------|---------------------------------| +| none | / | +| 2022 methods | `sing-box generate rand <密钥长度>` | +| other methods | 任意字符串 | \ No newline at end of file diff --git a/docs/configuration/shared/tls.md b/docs/configuration/shared/tls.md index d201e859..c3fe4b0a 100644 --- a/docs/configuration/shared/tls.md +++ b/docs/configuration/shared/tls.md @@ -319,7 +319,7 @@ Handshake server address and [Dial options](/configuration/shared/dial). ==Required== -Private key, generated by `sing-box generate x25519`. +Private key, generated by `sing-box generate reality-key`. #### public_key @@ -327,7 +327,7 @@ Private key, generated by `sing-box generate x25519`. ==Required== -Public key, generated by `sing-box generate x25519`. +Public key, generated by `sing-box generate reality-key`. #### short_id diff --git a/docs/configuration/shared/tls.zh.md b/docs/configuration/shared/tls.zh.md index fc867253..f12d3b9e 100644 --- a/docs/configuration/shared/tls.zh.md +++ b/docs/configuration/shared/tls.zh.md @@ -315,7 +315,7 @@ MAC 密钥。 ==必填== -私钥,由 `sing-box generate x25519` 生成。 +私钥,由 `sing-box generate reality-key` 生成。 #### public_key @@ -323,7 +323,7 @@ MAC 密钥。 ==必填== -公钥,由 `sing-box generate x25519` 生成。 +公钥,由 `sing-box generate reality-key` 生成。 #### short_id