Improved the clientapi deploy script

This commit is contained in:
WolverinDEV 2020-12-02 12:51:13 +01:00
parent 3e7617ea13
commit 1321558ff8

View file

@ -6,50 +6,10 @@
* Time: 16:42 * Time: 16:42
*/ */
$UI_BASE_PATH = "ui-files/";
$UI_RAW_BASE_PATH = "ui-files/raw/"; $UI_RAW_BASE_PATH = "ui-files/raw/";
$CLIENT_BASE_PATH = "files/"; $CLIENT_BASE_PATH = "files/";
function list_dir($base_dir, &$results = array(), $dir = "") { function errorExit($message) {
$files = scandir($base_dir . $dir);
foreach($files as $key => $value){
$path = $base_dir.$dir.DIRECTORY_SEPARATOR.$value;
if(!is_dir($path)) {
$results[] = ($dir ? $dir.DIRECTORY_SEPARATOR : "").$value;
} else if($value != "." && $value != "..") {
list_dir($base_dir, $results, ($dir ? $dir.DIRECTORY_SEPARATOR : "").$value);
}
}
return $results;
}
function endsWith($haystack, $needle) {
// search forward starting from end minus needle length characters
if ($needle === '') {
return true;
}
$diff = \strlen($haystack) - \strlen($needle);
return $diff >= 0 && strpos($haystack, $needle, $diff) !== false;
}
function fdump($name) {
if(endsWith($name, ".php")) {
global $CLIENT;
$CLIENT = true;
chdir(dirname($name));
include $name;
return;
}
$file = fopen($name, "r") or error_exit("missing file \"" . $name . "\".");
echo (fread($file, filesize($name)));
fclose($file);
}
function error_exit($message) {
http_response_code(400); http_response_code(400);
die(json_encode([ die(json_encode([
"success" => false, "success" => false,
@ -57,15 +17,32 @@
])); ]));
} }
function handle_develop_web_request() { function verifyPostSecret() {
global $UI_RAW_BASE_PATH; if(!isset($_POST["secret"])) {
errorExit("Missing required information!");
}
$require_secret = file_get_contents(".deploy_secret");
if($require_secret === false || strlen($require_secret) == 0) {
errorExit("Server missing secret!");
}
if(!is_string($_POST["secret"])) {
errorExit("Invalid secret!");
}
if(strcmp(trim($require_secret), trim($_POST["secret"])) !== 0) {
errorExit("Secret does not match!");
}
}
function handleRequest() {
if(isset($_GET) && isset($_GET["type"])) { if(isset($_GET) && isset($_GET["type"])) {
if ($_GET["type"] == "update-info") { if ($_GET["type"] == "update-info") {
global $CLIENT_BASE_PATH; global $CLIENT_BASE_PATH;
$raw_versions = file_get_contents($CLIENT_BASE_PATH . "/version.json"); $raw_versions = file_get_contents($CLIENT_BASE_PATH . "/version.json");
if($raw_versions === false) { if($raw_versions === false) {
error_exit("Missing file!"); errorExit("Missing file!");
} }
$versions = json_decode($raw_versions, true); $versions = json_decode($raw_versions, true);
@ -79,7 +56,7 @@
$path = $CLIENT_BASE_PATH . $_GET["channel"] . DIRECTORY_SEPARATOR . $_GET["version"] . DIRECTORY_SEPARATOR; $path = $CLIENT_BASE_PATH . $_GET["channel"] . DIRECTORY_SEPARATOR . $_GET["version"] . DIRECTORY_SEPARATOR;
$raw_release_info = file_get_contents($path . "info.json"); $raw_release_info = file_get_contents($path . "info.json");
if($raw_release_info === false) { if($raw_release_info === false) {
error_exit("missing info file (version and/or channel missing. Path was " . $path . ")"); errorExit("missing info file (version and/or channel missing. Path was " . $path . ")");
} }
$release_info = json_decode($raw_release_info); $release_info = json_decode($raw_release_info);
@ -97,7 +74,7 @@
readfile($path . $platform->update); readfile($path . $platform->update);
die(); die();
} }
error_exit("Missing platform, arch or file"); errorExit("Missing platform, arch or file");
} }
else if ($_GET["type"] == "ui-info") { else if ($_GET["type"] == "ui-info") {
global $UI_BASE_PATH; global $UI_BASE_PATH;
@ -128,10 +105,10 @@
global $UI_BASE_PATH; global $UI_BASE_PATH;
if(!isset($_GET["channel"]) || !isset($_GET["version"])) if(!isset($_GET["channel"]) || !isset($_GET["version"]))
error_exit("missing required parameters"); errorExit("missing required parameters");
if($_GET["version"] !== "latest" && !isset($_GET["git-ref"])) if($_GET["version"] !== "latest" && !isset($_GET["git-ref"]))
error_exit("missing required parameters"); errorExit("missing required parameters");
$version_info = file_get_contents($UI_BASE_PATH . "info.json"); $version_info = file_get_contents($UI_BASE_PATH . "info.json");
if($version_info === false) $version_info = array(); if($version_info === false) $version_info = array();
@ -139,7 +116,7 @@
$channel_data = $version_info[$_GET["channel"]]; $channel_data = $version_info[$_GET["channel"]];
if(!isset($channel_data)) if(!isset($channel_data))
error_exit("channel unknown"); errorExit("channel unknown");
$ui_pack = false; $ui_pack = false;
if($_GET["version"] === "latest") { if($_GET["version"] === "latest") {
@ -153,7 +130,7 @@
} }
} }
if($ui_pack === false) if($ui_pack === false)
error_exit("missing version"); errorExit("missing version");
header("Cache-Control: public"); // needed for internet explorer header("Cache-Control: public"); // needed for internet explorer
@ -170,29 +147,34 @@
$read = readfile($ui_pack["file"]); $read = readfile($ui_pack["file"]);
header("Content-Length:" . $read); header("Content-Length:" . $read);
if($read === false) error_exit("internal error: Failed to read file!"); if($read === false) errorExit("internal error: Failed to read file!");
die(); die();
} }
} }
else if($_POST["type"] == "deploy-build") { else if($_POST["type"] == "deploy-build") {
global $CLIENT_BASE_PATH; global $CLIENT_BASE_PATH;
if(!isset($_POST["secret"]) || !isset($_POST["version"]) || !isset($_POST["platform"]) || !isset($_POST["arch"]) || !isset($_POST["update_suffix"]) || !isset($_POST["installer_suffix"])) if(!isset($_POST["version"]) || !isset($_POST["platform"]) || !isset($_POST["arch"]) || !isset($_POST["update_suffix"]) || !isset($_POST["installer_suffix"])) {
error_exit("Missing required information!"); errorExit("Missing required information!");
{
$require_secret = file_get_contents(".deploy_secret");
if($require_secret === false || strlen($require_secret) == 0) error_exit("Server missing secret!");
if(!is_string($_POST["secret"])) error_exit("Invalid secret!");
if(strcmp(trim($require_secret), trim($_POST["secret"])) !== 0)
error_exit("Secret does not match!");
} }
if(!isset($_FILES["update"])) error_exit("Missing update file"); verifyPostSecret();
if($_FILES["update"]["error"] !== UPLOAD_ERR_OK) error_exit("Upload for update failed!");
if(!isset($_FILES["installer"])) error_exit("Missing installer file"); if(!isset($_FILES["update"])) {
if($_FILES["installer"]["error"] !== UPLOAD_ERR_OK) error_exit("Upload for installer failed!"); errorExit("Missing update file");
}
if($_FILES["update"]["error"] !== UPLOAD_ERR_OK) {
errorExit("Upload for update failed!");
}
if(!isset($_FILES["installer"])) {
errorExit("Missing installer file");
}
if($_FILES["installer"]["error"] !== UPLOAD_ERR_OK) {
errorExit("Upload for installer failed!");
}
$json_version = json_decode($_POST["version"], true); $json_version = json_decode($_POST["version"], true);
$version = $json_version["major"] . "." . $json_version["minor"] . "." . $json_version["patch"] . ($json_version["build"] > 0 ? "-" . $json_version["build"] : ""); $version = $json_version["major"] . "." . $json_version["minor"] . "." . $json_version["patch"] . ($json_version["build"] > 0 ? "-" . $json_version["build"] : "");
@ -206,8 +188,14 @@
{ {
$version_info = file_get_contents($path . "info.json"); $version_info = file_get_contents($path . "info.json");
if($version_info === false) $version_info = array(); if($version_info === false) {
else $version_info = json_decode($version_info, true); $version_info = array();
} else {
$version_info = json_decode($version_info, true);
if($version_info === false) {
errorExit("Failed to decode old versions info file");
}
}
for($index = 0; $index < count($version_info); $index++) { for($index = 0; $index < count($version_info); $index++) {
if($version_info[$index]["platform"] == $_POST["platform"] && $version_info[$index]["arch"] == $_POST["arch"]) { if($version_info[$index]["platform"] == $_POST["platform"] && $version_info[$index]["arch"] == $_POST["arch"]) {
@ -215,6 +203,7 @@
break; break;
} }
} }
$info = array(); $info = array();
$info["platform"] = $_POST["platform"]; $info["platform"] = $_POST["platform"];
$info["arch"] = $_POST["arch"]; $info["arch"] = $_POST["arch"];
@ -227,12 +216,19 @@
{ {
$filename = $CLIENT_BASE_PATH . DIRECTORY_SEPARATOR . "version.json"; $filename = $CLIENT_BASE_PATH . DIRECTORY_SEPARATOR . "version.json";
$indexes = file_get_contents($filename); $indexes = file_get_contents($filename);
if($indexes === false) $indexes = array(); if($indexes === false) {
else $indexes = json_decode($indexes, true); $indexes = array();
} else {
$indexes = json_decode($indexes, true);
if($indexes === false) {
errorExit("Failed to decode old latest versions info file");
}
}
$index = &$indexes[$_POST["channel"]]; $index = &$indexes[$_POST["channel"]];
if(!isset($index)) if(!isset($index)) {
$index = array(); $index = array();
}
for($idx = 0; $idx < count($index); $idx++) { for($idx = 0; $idx < count($index); $idx++) {
if($index[$idx]["platform"] == $_POST["platform"] && $index[$idx]["arch"] == $_POST["arch"]) { if($index[$idx]["platform"] == $_POST["platform"] && $index[$idx]["arch"] == $_POST["arch"]) {
@ -260,8 +256,11 @@
else if($_POST["type"] == "deploy-ui-build") { else if($_POST["type"] == "deploy-ui-build") {
global $UI_BASE_PATH; global $UI_BASE_PATH;
if(!isset($_POST["secret"]) || !isset($_POST["channel"]) || !isset($_POST["version"]) || !isset($_POST["git_ref"]) || !isset($_POST["required_client"])) if(!isset($_POST["channel"]) || !isset($_POST["version"]) || !isset($_POST["git_ref"]) || !isset($_POST["required_client"])) {
error_exit("Missing required information!"); errorExit("Missing required information!");
}
verifyPostSecret();
$path = $UI_BASE_PATH . DIRECTORY_SEPARATOR; $path = $UI_BASE_PATH . DIRECTORY_SEPARATOR;
$channeled_path = $UI_BASE_PATH . DIRECTORY_SEPARATOR . $_POST["channel"]; $channeled_path = $UI_BASE_PATH . DIRECTORY_SEPARATOR . $_POST["channel"];
@ -269,23 +268,21 @@
exec("mkdir -p " . $path); exec("mkdir -p " . $path);
exec("mkdir -p " . $channeled_path); exec("mkdir -p " . $channeled_path);
{
$require_secret = file_get_contents(".deploy_secret");
if($require_secret === false || strlen($require_secret) == 0) error_exit("Server missing secret!");
error_log($_POST["secret"]);
error_log(trim($require_secret));
if(!is_string($_POST["secret"])) error_exit("Invalid secret!");
if(strcmp(trim($require_secret), trim($_POST["secret"])) !== 0)
error_exit("Secret does not match!");
}
{ {
$info = file_get_contents($path . "info.json"); $info = file_get_contents($path . "info.json");
if($info === false) $info = array(); if($info === false) {
else $info = json_decode($info, true); $info = array();
} else {
$info = json_decode($info, true);
if($info === false) {
errorExit("failed to decode old info file");
}
}
$channel_info = &$info[$_POST["channel"]]; $channel_info = &$info[$_POST["channel"]];
if(!$channel_info) $channel_info = array(); if(!$channel_info) {
$channel_info = array();
}
$entry = [ $entry = [
"timestamp" => time(), "timestamp" => time(),
@ -314,4 +311,4 @@
])); ]));
} }
handle_develop_web_request(); handleRequest();