summaryrefslogtreecommitdiffstats
path: root/apis/debugrequest.inc.php
blob: 35b163324b513d2e321f60d50b903549c33df492 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php

function handleRequest() {
	$DEBUG_SETTINGS = Property::getRemoteDebugConfig();
	$DEBUG_REQUEST = explode(":", file_get_contents("php://input"), 2);
	if (!$DEBUG_SETTINGS['enabled'] || !checkValid($DEBUG_REQUEST)) {
		http_response_code(403);
		exit(1);
	}
	$task = runRelayTask($DEBUG_SETTINGS);
	waitForRelay($task);
	exit(0);
}

function waitForRelay($task) {
	while(true) {
		$task = Taskmanager::status($task["id"]);
		if( !$task || $task["statusCode"] == "TASK_ERROR" || $task["statusCode"] == "TASK_FINISHED") {
			http_response_code(500);
			exit(1);
			break;
		}
		elseif ($task["data"]["listenPort"] != -1) {
			$PORT = $task["data"]["listenPort"];
			echo "PORT=$PORT;";
			Property::addToList("DispatchedRelay", $task["id"]);
			break;
		}
	}
	//echo print_r($task);
	exit(1);
}

function runRelayTask($DEBUG_SETTINGS) {
	$data = array("ip" => $DEBUG_SETTINGS["debugServer"], "port" => $DEBUG_SETTINGS["port"]);
	return Taskmanager::submit("RemoteDebug", $data);
}

function checkEnabled() {
	return $DEBUG_SETTINGS["enabled"];
}

function checkValid($DEBUG_REQUEST) {
	return Database::queryFirst("SELECT machineuuid, clientip FROM machine WHERE machineuuid = :uuid AND clientip = :ip", 
		array(":uuid" => $DEBUG_REQUEST[0], ":ip" => $_SERVER["REMOTE_ADDR"]));
}

handleRequest();