This commit is contained in:
H1JK 2023-03-04 16:44:10 +08:00
parent a742922c17
commit 849c6a2d22
9 changed files with 65 additions and 63 deletions

View File

@ -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)
}

View File

@ -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")
}

View File

@ -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")
}

View File

@ -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")
}

View File

@ -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

View File

@ -77,11 +77,11 @@ Both if empty.
==Required==
| Method | Password Format |
|---------------|-----------------------------------------|
| none | / |
| 2022 methods | `sing-box generate psk -s <Key Length>` |
| other methods | any string |
| Method | Password Format |
|---------------|---------------------------------------|
| none | / |
| 2022 methods | `sing-box generate rand <Key Length>` |
| other methods | any string |
### Listen Fields

View File

@ -77,8 +77,8 @@ See [Listen Fields](/configuration/shared/listen) for details.
==必填==
| 方法 | 密码格式 |
|---------------|-----------------------------------|
| none | / |
| 2022 methods | `sing-box generate psk -s <密钥长度>` |
| other methods | 任意字符串 |
| 方法 | 密码格式 |
|---------------|---------------------------------|
| none | / |
| 2022 methods | `sing-box generate rand <密钥长度>` |
| other methods | 任意字符串 |

View File

@ -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

View File

@ -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