mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-10-14 19:44:02 +00:00
cmd: add Ed25519 key generation and Lua script signing
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"github.com/xtls/xray-core/main/commands/all/api"
|
||||
"github.com/xtls/xray-core/main/commands/all/base64"
|
||||
"github.com/xtls/xray-core/main/commands/all/convert"
|
||||
"github.com/xtls/xray-core/main/commands/all/lua"
|
||||
"github.com/xtls/xray-core/main/commands/all/tls"
|
||||
"github.com/xtls/xray-core/main/commands/base"
|
||||
)
|
||||
@@ -14,9 +15,11 @@ func init() {
|
||||
api.CmdAPI,
|
||||
base64.CmdBase64,
|
||||
convert.CmdConvert,
|
||||
lua.CmdLua,
|
||||
tls.CmdTLS,
|
||||
cmdUUID,
|
||||
cmdX25519,
|
||||
cmdEd25519,
|
||||
cmdWG,
|
||||
cmdMLDSA65,
|
||||
cmdMLKEM768,
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package all
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
|
||||
"github.com/xtls/xray-core/main/commands/base"
|
||||
)
|
||||
|
||||
var cmdEd25519 = &base.Command{
|
||||
UsageLine: `{{.Exec}} ed25519 [-i "private key seed (Base64URL)"]`,
|
||||
Short: "Generate an Ed25519 key pair",
|
||||
Long: `
|
||||
Generate an Ed25519 key pair, or restore it from a 32-byte private key seed
|
||||
with -i. PrivateKey (seed) and PublicKey use unpadded Base64URL.
|
||||
`,
|
||||
}
|
||||
|
||||
var inputEd25519 = cmdEd25519.Flag.String("i", "", "Ed25519 private key seed in unpadded Base64URL.")
|
||||
|
||||
func init() {
|
||||
cmdEd25519.Run = executeEd25519
|
||||
}
|
||||
|
||||
func executeEd25519(cmd *base.Command, args []string) {
|
||||
if len(args) != 0 {
|
||||
cmd.Usage()
|
||||
}
|
||||
|
||||
var privateKey ed25519.PrivateKey
|
||||
if *inputEd25519 == "" {
|
||||
_, key, err := ed25519.GenerateKey(rand.Reader)
|
||||
if err != nil {
|
||||
base.Fatalf("failed to generate Ed25519 key: %s", err)
|
||||
}
|
||||
privateKey = key
|
||||
} else {
|
||||
encoding := base64.RawURLEncoding.Strict()
|
||||
seed, err := encoding.DecodeString(*inputEd25519)
|
||||
if err != nil || len(seed) != ed25519.SeedSize || encoding.EncodeToString(seed) != *inputEd25519 {
|
||||
base.Fatalf("invalid Ed25519 private key seed: expected 32 bytes in unpadded Base64URL")
|
||||
}
|
||||
privateKey = ed25519.NewKeyFromSeed(seed)
|
||||
}
|
||||
|
||||
if _, err := fmt.Printf("PrivateKey: %s\nPublicKey: %s\n",
|
||||
base64.RawURLEncoding.EncodeToString(privateKey.Seed()),
|
||||
base64.RawURLEncoding.EncodeToString(privateKey.Public().(ed25519.PublicKey))); err != nil {
|
||||
base.Fatalf("failed to write Ed25519 keys: %s", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package lua
|
||||
|
||||
import "github.com/xtls/xray-core/main/commands/base"
|
||||
|
||||
// CmdLua holds the Lua script commands.
|
||||
var CmdLua = &base.Command{
|
||||
UsageLine: "{{.Exec}} lua",
|
||||
Short: "Manage Lua scripts",
|
||||
Long: "{{.Exec}} {{.LongName}} provides tools for local Lua scripts.",
|
||||
Commands: []*base.Command{
|
||||
cmdSign,
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package lua
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
xlua "github.com/xtls/xray-core/common/lua"
|
||||
"github.com/xtls/xray-core/main/commands/base"
|
||||
)
|
||||
|
||||
var cmdSign = &base.Command{
|
||||
UsageLine: "{{.Exec}} lua sign [-key seed] [-o output-file] <input-file>",
|
||||
Short: "Sign a local Lua script for privileged execution",
|
||||
Long: `
|
||||
Sign a local Lua file using a 32-byte Ed25519 private key seed in unpadded
|
||||
Base64URL. Use {{.Exec}} ed25519 to generate a key pair.
|
||||
Write back to the input file, or use -o to write another file (- for stdout).
|
||||
`,
|
||||
}
|
||||
|
||||
var (
|
||||
signKey = cmdSign.Flag.String("key", "", "Ed25519 private key seed in unpadded Base64URL.")
|
||||
signOutput = cmdSign.Flag.String("o", "", "Output file, or - for stdout. Default: input file.")
|
||||
)
|
||||
|
||||
func init() {
|
||||
cmdSign.Run = executeSign
|
||||
}
|
||||
|
||||
func executeSign(cmd *base.Command, args []string) {
|
||||
if len(args) != 1 {
|
||||
cmd.Usage()
|
||||
}
|
||||
|
||||
info, err := os.Stat(args[0])
|
||||
if err != nil {
|
||||
base.Fatalf("failed to inspect Lua file: %s", err)
|
||||
}
|
||||
if !info.Mode().IsRegular() {
|
||||
base.Fatalf("Lua input is not a regular file: %s", args[0])
|
||||
}
|
||||
|
||||
code, err := os.ReadFile(args[0])
|
||||
if err != nil {
|
||||
base.Fatalf("failed to read Lua file: %s", err)
|
||||
}
|
||||
|
||||
signed, err := xlua.SignPrivilegedScript(code, *signKey)
|
||||
if err != nil {
|
||||
base.Fatalf("failed to sign Lua file: %s", err)
|
||||
}
|
||||
|
||||
if *signOutput == "-" {
|
||||
if _, err := os.Stdout.Write(signed); err != nil {
|
||||
base.Fatalf("failed to write signed Lua file: %s", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
output := *signOutput
|
||||
if output == "" {
|
||||
output = args[0]
|
||||
}
|
||||
if err := writeSignedFile(output, signed, info.Mode().Perm()); err != nil {
|
||||
base.Fatalf("failed to write signed Lua file: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func writeSignedFile(path string, code []byte, mode os.FileMode) error {
|
||||
if info, err := os.Stat(path); err == nil {
|
||||
if !info.Mode().IsRegular() {
|
||||
return &os.PathError{Op: "write", Path: path, Err: os.ErrInvalid}
|
||||
}
|
||||
mode = info.Mode().Perm()
|
||||
path, err = filepath.EvalSymlinks(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
file, err := os.CreateTemp(filepath.Dir(path), ".xray-lua-sign-*")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.Remove(file.Name())
|
||||
defer file.Close()
|
||||
if _, err := file.Write(code); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.Chmod(mode); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(file.Name(), path)
|
||||
}
|
||||
Reference in New Issue
Block a user