mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-06-13 21:54:13 +08:00
Add generate command
This commit is contained in:
parent
849c6a2d22
commit
7be194ac6c
@ -1,9 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/sagernet/sing-box/cmd/sing-box/internal/generate"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/sagernet/sing-box/log"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
)
|
||||
|
||||
var commandGenerate = &cobra.Command{
|
||||
@ -12,8 +20,102 @@ var commandGenerate = &cobra.Command{
|
||||
}
|
||||
|
||||
func init() {
|
||||
commandGenerate.AddCommand(generate.CommandGenerateRandom)
|
||||
commandGenerate.AddCommand(generate.CommandGenerateUUID)
|
||||
commandGenerate.AddCommand(generate.CommandGenerateReality)
|
||||
mainCommand.AddCommand(commandGenerate)
|
||||
}
|
||||
|
||||
var (
|
||||
outputBase64 bool
|
||||
outputHex bool
|
||||
)
|
||||
|
||||
var commandGenerateRandom = &cobra.Command{
|
||||
Use: "rand <length>",
|
||||
Short: "Generate random bytes",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
err := generateRandom(args)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
commandGenerateRandom.Flags().BoolVar(&outputBase64, "base64", false, "Generate base64 string")
|
||||
commandGenerateRandom.Flags().BoolVar(&outputHex, "hex", false, "Generate hex string")
|
||||
commandGenerate.AddCommand(commandGenerateRandom)
|
||||
}
|
||||
|
||||
func generateRandom(args []string) error {
|
||||
length, err := strconv.Atoi(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
randomBytes := make([]byte, length)
|
||||
_, err = rand.Read(randomBytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if outputBase64 {
|
||||
_, err = os.Stdout.WriteString(base64.StdEncoding.EncodeToString(randomBytes) + "\n")
|
||||
} else if outputHex {
|
||||
_, err = os.Stdout.WriteString(hex.EncodeToString(randomBytes) + "\n")
|
||||
} else {
|
||||
_, err = os.Stdout.Write(randomBytes)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
var commandGenerateUUID = &cobra.Command{
|
||||
Use: "uuid",
|
||||
Short: "Generate UUID string",
|
||||
Args: cobra.NoArgs,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
err := generateUUID()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
commandGenerate.AddCommand(commandGenerateUUID)
|
||||
}
|
||||
|
||||
func generateUUID() error {
|
||||
newUUID, err := uuid.NewV4()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = os.Stdout.WriteString(newUUID.String() + "\n")
|
||||
return err
|
||||
}
|
||||
|
||||
var commandGenerateWireGuardKeyPair = &cobra.Command{
|
||||
Use: "wg-keypair",
|
||||
Short: "Generate WireGuard key pair",
|
||||
Args: cobra.NoArgs,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
err := generateWireGuardKey()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
commandGenerate.AddCommand(commandGenerateWireGuardKeyPair)
|
||||
}
|
||||
|
||||
func generateWireGuardKey() error {
|
||||
privateKey, err := wgtypes.GeneratePrivateKey()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
os.Stdout.WriteString("PrivateKey: " + privateKey.String() + "\n")
|
||||
os.Stdout.WriteString("PublicKey: " + privateKey.PublicKey().String() + "\n")
|
||||
return nil
|
||||
}
|
||||
|
@ -1,42 +0,0 @@
|
||||
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")
|
||||
}
|
@ -1,57 +0,0 @@
|
||||
package generate
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"os"
|
||||
|
||||
"github.com/sagernet/sing-box/log"
|
||||
F "github.com/sagernet/sing/common/format"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/crypto/curve25519"
|
||||
)
|
||||
|
||||
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 {
|
||||
encoder = hex.EncodeToString
|
||||
}
|
||||
|
||||
var privateKey [curve25519.ScalarSize]byte
|
||||
if input == "" {
|
||||
_, err := rand.Read(privateKey[:])
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
src := []byte(input)
|
||||
n, _ := base64.RawURLEncoding.Decode(privateKey[:], src)
|
||||
if n != curve25519.ScalarSize {
|
||||
n, _ = hex.Decode(privateKey[:], src)
|
||||
if n != curve25519.ScalarSize {
|
||||
log.Fatal("invalid input private key")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
publicKey, err := curve25519.X25519(privateKey[:], curve25519.Basepoint)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
os.Stdout.WriteString(F.ToString(
|
||||
"Private key: ", encoder(privateKey[:]),
|
||||
"\nPublic key: ", encoder(publicKey), "\n"))
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
CommandGenerateReality.Flags().BoolVarP(&outputHex, "hex", "H", false, "print hex format")
|
||||
CommandGenerateReality.Flags().StringVarP(&input, "input", "i", "", "generate from specified private key")
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
package generate
|
||||
|
||||
var (
|
||||
outputHex bool
|
||||
input string
|
||||
)
|
@ -1,37 +0,0 @@
|
||||
package generate
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/sagernet/sing-box/log"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var CommandGenerateUUID = &cobra.Command{
|
||||
Use: "uuid",
|
||||
Short: "Generate a UUID",
|
||||
Args: cobra.NoArgs,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
var (
|
||||
newUUID uuid.UUID
|
||||
err error
|
||||
)
|
||||
|
||||
if input == "" {
|
||||
newUUID, err = uuid.NewV4()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
newUUID = uuid.NewV5(uuid.Nil, input)
|
||||
}
|
||||
|
||||
os.Stdout.WriteString(newUUID.String() + "\n")
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
CommandGenerateUUID.Flags().StringVarP(&input, "input", "i", "", "generate UUID v5 from specified string")
|
||||
}
|
@ -77,11 +77,11 @@ Both if empty.
|
||||
|
||||
==Required==
|
||||
|
||||
| Method | Password Format |
|
||||
|---------------|---------------------------------------|
|
||||
| none | / |
|
||||
| 2022 methods | `sing-box generate rand <Key Length>` |
|
||||
| other methods | any string |
|
||||
| Method | Password Format |
|
||||
|---------------|------------------------------------------------|
|
||||
| none | / |
|
||||
| 2022 methods | `sing-box generate rand --base64 <Key Length>` |
|
||||
| other methods | any string |
|
||||
|
||||
### Listen Fields
|
||||
|
||||
|
@ -77,8 +77,8 @@ See [Listen Fields](/configuration/shared/listen) for details.
|
||||
|
||||
==必填==
|
||||
|
||||
| 方法 | 密码格式 |
|
||||
|---------------|---------------------------------|
|
||||
| none | / |
|
||||
| 2022 methods | `sing-box generate rand <密钥长度>` |
|
||||
| other methods | 任意字符串 |
|
||||
| 方法 | 密码格式 |
|
||||
|---------------|------------------------------------------|
|
||||
| none | / |
|
||||
| 2022 methods | `sing-box generate rand --base64 <密钥长度>` |
|
||||
| other methods | 任意字符串 |
|
@ -319,7 +319,7 @@ Handshake server address and [Dial options](/configuration/shared/dial).
|
||||
|
||||
==Required==
|
||||
|
||||
Private key, generated by `sing-box generate reality-key`.
|
||||
Private key, generated by `sing-box generate wg-keypair`.
|
||||
|
||||
#### public_key
|
||||
|
||||
@ -327,7 +327,7 @@ Private key, generated by `sing-box generate reality-key`.
|
||||
|
||||
==Required==
|
||||
|
||||
Public key, generated by `sing-box generate reality-key`.
|
||||
Public key, generated by `sing-box generate wg-keypair`.
|
||||
|
||||
#### short_id
|
||||
|
||||
|
@ -315,7 +315,7 @@ MAC 密钥。
|
||||
|
||||
==必填==
|
||||
|
||||
私钥,由 `sing-box generate reality-key` 生成。
|
||||
私钥,由 `sing-box generate wg-keypair` 生成。
|
||||
|
||||
#### public_key
|
||||
|
||||
@ -323,7 +323,7 @@ MAC 密钥。
|
||||
|
||||
==必填==
|
||||
|
||||
公钥,由 `sing-box generate reality-key` 生成。
|
||||
公钥,由 `sing-box generate wg-keypair` 生成。
|
||||
|
||||
#### short_id
|
||||
|
||||
|
1
go.mod
1
go.mod
@ -47,6 +47,7 @@ require (
|
||||
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb
|
||||
golang.org/x/net v0.7.0
|
||||
golang.org/x/sys v0.5.0
|
||||
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20230215201556-9c5414ab4bde
|
||||
google.golang.org/grpc v1.53.0
|
||||
google.golang.org/protobuf v1.28.1
|
||||
gvisor.dev/gvisor v0.0.0-20220901235040-6ca97ef2ce1c
|
||||
|
2
go.sum
2
go.sum
@ -266,6 +266,8 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20230215201556-9c5414ab4bde h1:ybF7AMzIUikL9x4LgwEmzhXtzRpKNqngme1VGDWz+Nk=
|
||||
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20230215201556-9c5414ab4bde/go.mod h1:mQqgjkW8GQQcJQsbBvK890TKqUK1DfKWkuBGbOkuMHQ=
|
||||
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f h1:BWUVssLB0HVOSY78gIdvk1dTVYtT1y8SBWtPYuTJ/6w=
|
||||
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f/go.mod h1:RGgjbofJ8xD9Sq1VVhDM1Vok1vRONV+rg+CjzG4SZKM=
|
||||
google.golang.org/grpc v1.53.0 h1:LAv2ds7cmFV/XTS3XG1NneeENYrXGmorPxsBbptIjNc=
|
||||
|
Loading…
x
Reference in New Issue
Block a user