mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-08-15 15:50:59 +00:00
fix(server): guard xray key-generator output parsing
GetNewX25519Cert, GetNewmldsa65 and GetNewmlkem768 parsed xray's stdout by reading lines[0], lines[1] and each line's second colon-separated field without any length check — unlike GetNewEchCert, which already guards its line count. If the xray binary printed fewer than two lines or reformatted its labels (a version change, or a silent failure that emitted nothing), the fixed slice index panicked and the handler 500'd. Extract the shared parsing into parseXrayKeyPairOutput, which length guards the line count and each label split and returns an error instead of panicking, then route all three generators through it.
This commit is contained in:
@@ -2023,6 +2023,24 @@ func (s *ServerService) UpdateGeofile(fileName string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// parseXrayKeyPairOutput reads the two-line "Label: value" output that xray's
|
||||
// key-generation subcommands (x25519, mldsa65, mlkem768) print and returns the
|
||||
// two values. Short or label-less output yields an error instead of panicking
|
||||
// on an out-of-range slice index, so a future xray version that changes the
|
||||
// format degrades to a 500 with a message rather than a crash.
|
||||
func parseXrayKeyPairOutput(output string) (string, string, error) {
|
||||
lines := strings.Split(output, "\n")
|
||||
if len(lines) < 2 {
|
||||
return "", "", common.NewError("unexpected key generator output")
|
||||
}
|
||||
first := strings.Split(lines[0], ":")
|
||||
second := strings.Split(lines[1], ":")
|
||||
if len(first) < 2 || len(second) < 2 {
|
||||
return "", "", common.NewError("unexpected key generator output")
|
||||
}
|
||||
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")
|
||||
@@ -2033,13 +2051,10 @@ func (s *ServerService) GetNewX25519Cert() (any, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
lines := strings.Split(out.String(), "\n")
|
||||
|
||||
privateKeyLine := strings.Split(lines[0], ":")
|
||||
publicKeyLine := strings.Split(lines[1], ":")
|
||||
|
||||
privateKey := strings.TrimSpace(privateKeyLine[1])
|
||||
publicKey := strings.TrimSpace(publicKeyLine[1])
|
||||
privateKey, publicKey, err := parseXrayKeyPairOutput(out.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
keyPair := map[string]any{
|
||||
"privateKey": privateKey,
|
||||
@@ -2059,13 +2074,10 @@ func (s *ServerService) GetNewmldsa65() (any, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
lines := strings.Split(out.String(), "\n")
|
||||
|
||||
SeedLine := strings.Split(lines[0], ":")
|
||||
VerifyLine := strings.Split(lines[1], ":")
|
||||
|
||||
seed := strings.TrimSpace(SeedLine[1])
|
||||
verify := strings.TrimSpace(VerifyLine[1])
|
||||
seed, verify, err := parseXrayKeyPairOutput(out.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
keyPair := map[string]any{
|
||||
"seed": seed,
|
||||
@@ -2381,13 +2393,10 @@ func (s *ServerService) GetNewmlkem768() (any, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
lines := strings.Split(out.String(), "\n")
|
||||
|
||||
SeedLine := strings.Split(lines[0], ":")
|
||||
ClientLine := strings.Split(lines[1], ":")
|
||||
|
||||
seed := strings.TrimSpace(SeedLine[1])
|
||||
client := strings.TrimSpace(ClientLine[1])
|
||||
seed, client, err := parseXrayKeyPairOutput(out.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
keyPair := map[string]any{
|
||||
"seed": seed,
|
||||
|
||||
Reference in New Issue
Block a user