From 30675bb52fc59c721a1a72e93dad88e906742dfc Mon Sep 17 00:00:00 2001 From: Christopher Date: Sat, 24 Oct 2020 16:31:40 +0200 Subject: [PATCH] add api --- api.php | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ index.php | 3 --- 2 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 api.php diff --git a/api.php b/api.php new file mode 100644 index 0000000..2924691 --- /dev/null +++ b/api.php @@ -0,0 +1,80 @@ +getAllContainers(); + $container = NULL; + + foreach($allContainers as $thisContainer) + { + if($thisContainer['Id'] == trim($_REQUEST['CONTAINER']) || trim($_REQUEST['CONTAINER']) == ltrim($thisContainer['Names']['0'], '/')) + { + if(isset($thisContainer['Labels']['remotepass'])) + { + if(trim($thisContainer['Labels']['remotepass']) == trim($_REQUEST['KEY'])) + { + $container = $thisContainer; + } + } + } + } + } + } + + if($container == NULL) + die("access denied!"); + + if(isset($_REQUEST['METODE'])) + { + if(trim($_REQUEST['METODE']) != "") + { + if(strtoupper($_REQUEST['METODE']) == "START") + { + $dockerClient->startContainer($container['Id']); + } + + if(strtoupper($_REQUEST['METODE']) == "STOP") + { + $dockerClient->stopContainer($container['Id']); + } + + if(strtoupper($_REQUEST['METODE']) == "KILL") + { + $dockerClient->killContainer($container['Id']); + } + + if(strtoupper($_REQUEST['METODE']) == "RESTART") + { + $dockerClient->killContainer($container['Id']); + $dockerClient->startContainer($container['Id']); + } + + if(strtoupper($_REQUEST['METODE']) == "COMMAND") + { + if(isset($_REQUEST['COMMAND'])) + { + if(trim($_REQUEST['COMMAND']) != "") + { + $filename = "/tmp/command".time().".txt"; + $command = trim($_REQUEST['COMMAND']); + + file_put_contents($filename, $command."\n"); + + system('cat '.$filename.' | socat EXEC:"docker attach '.$container['Id'].'",pty STDIN'); + unlink($filename); + } + } + } + } + } + +?> \ No newline at end of file diff --git a/index.php b/index.php index 4055eb1..a104c2b 100644 --- a/index.php +++ b/index.php @@ -10,9 +10,6 @@ include_once("classen/HTML.php"); include_once("classen/helper.php"); include_once("classen/docker.php"); -//$_SESSION['CONTAINER'] = "Game.OpenSimulator.Twix"; -//$_SESSION['LOGIN'] = "true"; - if(isset($_REQUEST['logout'])) if($_REQUEST['logout'] == '1') $_SESSION = array();