summaryrefslogtreecommitdiffstats
path: root/apis/debugrequest.inc.php
blob: 5c1323baed47c5b5e9e44570e0a4d39eef776d71 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php

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

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

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

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

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

handleRequest();

// $debug_settings = Property::getRemoteDebugConfig();
// if ($debug_settings["enabled"] !== true) {
//   http_response_code(403);
//   exit;
// }

// $debug_request = explode(":", file_get_contents("php://input"), 2);

// $uuid = $debug_request[0];
// $port = 5900 + $debug_request[1];

// $validclient = Database::queryFirst("SELECT machineuuid, clientip FROM machine WHERE machineuuid = :uuid AND clientip = :ip", array(":uuid" => "$debug_request[0]", ":ip" => $_SERVER["REMOTE_ADDR"]));

// if ($validclient == false) {
//   http_response_code(400);
// } else {
//   http_response_code(200);
//   $data = array("hosts" => array($_SERVER["REMOTE_ADDR"], $debug_settings["debugServer"]), 
// 	"ports" => array($port, intval($debug_settings["port"])), "descs" => array("bwlpclient", "debugserver"));
//   $taskstruct = Taskmanager::submit('DispatchRelay', $data);
//   Property::addToList("DispatchedRelay", $taskstruct['id']);
// }