修复: Reality x25519 密钥改面板内置生成(crypto/ecdh), 不再 exec xray 二进制; 编码与 xray x25519 输出一致(base64url raw 32B), 附曲线自验单测

This commit is contained in:
Muse
2026-10-20 10:18:37 +00:00
parent 7d42b91ed6
commit b9e1345b23
2 changed files with 98 additions and 8 deletions
+15 -8
View File
@@ -6,8 +6,11 @@ import (
"bytes"
"cmp"
"context"
"crypto/ecdh"
"crypto/rand"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/json"
"encoding/pem"
@@ -2305,17 +2308,21 @@ func parseXrayKeyPairOutput(output string) (string, string, error) {
return strings.TrimSpace(first[1]), strings.TrimSpace(second[1]), nil
}
func (s *ServerService) GetNewX25519Cert() (any, error) {
// Run the command
cmd := exec.CommandContext(context.Background(), xray.GetBinaryPath(), "x25519")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
// generateX25519KeyPair returns a fresh X25519 key pair in the exact format
// `xray x25519` prints (base64 RawURLEncoding of the raw 32-byte keys), but
// generated in-process: a control-plane-only panel has no xray binary to shell
// out to, and shelling out made Reality inbound creation fail there.
func generateX25519KeyPair() (privateKey string, publicKey string, err error) {
priv, err := ecdh.X25519().GenerateKey(rand.Reader)
if err != nil {
return nil, err
return "", "", err
}
return base64.RawURLEncoding.EncodeToString(priv.Bytes()),
base64.RawURLEncoding.EncodeToString(priv.PublicKey().Bytes()), nil
}
privateKey, publicKey, err := parseXrayKeyPairOutput(out.String())
func (s *ServerService) GetNewX25519Cert() (any, error) {
privateKey, publicKey, err := generateX25519KeyPair()
if err != nil {
return nil, err
}