diff --git a/webroot/js/ajax/ssh_generate.php b/webroot/js/ajax/ssh_generate.php index 6ea280b7..5a70f51d 100644 --- a/webroot/js/ajax/ssh_generate.php +++ b/webroot/js/ajax/ssh_generate.php @@ -4,20 +4,18 @@ use phpseclib3\Crypt\RSA; -echo "
";
-
 $private = RSA::createKey(2048);
 $public = $private->getPublicKey();
 
-echo "
"; -echo $public->toString('OpenSSH'); -echo "
"; -echo "
"; -if (isset($_GET["type"]) && $_GET["type"] == "ppk") { - echo $private->toString('PuTTY'); -} else { - echo $private; +switch ($_GET["type"]) { + case "key": + break; + case "ppk": + $private = $private->toString("PuTTY"); + break; } -echo "
"; -echo "
"; +echo json_encode([ + "pubkey" => $public->toString('OpenSSH'), + "privkey" => $private +]); diff --git a/webroot/panel/modal/new_key.php b/webroot/panel/modal/new_key.php index 40c509fc..9df7df37 100644 --- a/webroot/panel/modal/new_key.php +++ b/webroot/panel/modal/new_key.php @@ -64,21 +64,13 @@ }); function generateKey(type) { - var pubSection = "
"; - var privSection = "
"; - var endingSection = "
"; - $.ajax({ url: "/js/ajax/ssh_generate.php?type=" + type, success: function(result) { - var pubKey = result.substr(result.indexOf(pubSection) + pubSection.length, - result.indexOf(endingSection) - result.indexOf(pubSection) - pubSection.length); - var privKey = result.substr(result.indexOf(privSection) + privSection.length, - result.indexOf(endingSection, result.indexOf(endingSection) + 1) - - result.indexOf(privSection) - privSection.length); - $("input[type=hidden][name=gen_key]").val(pubKey); - downloadFile(privKey, "privkey." + type); // Force download of private key - + success: function(outputJsonStr) { + const output = JSON.parse(outputJsonStr); + $("input[type=hidden][name=gen_key]").val(output.pubkey); + downloadFile(output.privkey, `privkey.${type}`); // Force download of private key $("#newKeyform").submit(); } }); @@ -104,8 +96,7 @@ function generateKey(type) { key: key }, success: function(result) { - const res = result.replace(key, ""); - if (res == "true") { + if (result == "true") { $("input[id=add-key]").prop("disabled", false); $("textarea[name=key]").css("box-shadow", "none"); } else {