Compare commits
2 Commits
6cf4c83593
...
776f4a8acf
Author | SHA1 | Date |
---|---|---|
tracer | 776f4a8acf | |
tracer | 63c5eacb95 |
|
@ -2,6 +2,10 @@
|
||||||
|
|
||||||
namespace App\Controller;
|
namespace App\Controller;
|
||||||
|
|
||||||
|
define('COLOR_YELLOW', "\033[33m");
|
||||||
|
define('COLOR_GREEN', "\033[32m");
|
||||||
|
define('COLOR_DEFAULT', "\033[39m");
|
||||||
|
|
||||||
// TODO add to all Controllers
|
// TODO add to all Controllers
|
||||||
error_reporting(E_ALL);
|
error_reporting(E_ALL);
|
||||||
|
|
||||||
|
@ -27,14 +31,44 @@ class BindAPI
|
||||||
private DomainController $domainController;
|
private DomainController $domainController;
|
||||||
private PanelController $panelController;
|
private PanelController $panelController;
|
||||||
private NameserverController $nameserverController;
|
private NameserverController $nameserverController;
|
||||||
|
private CheckController $checkController;
|
||||||
|
|
||||||
public function __construct(private int $argc, private array $argv)
|
public function __construct(private array $config, private int $argc, private array $argv)
|
||||||
{
|
{
|
||||||
$this->dbConnection = (new DatabaseConnection())->getConnection();
|
$this->dbConnection = (new DatabaseConnection(config: $this->config))->getConnection();
|
||||||
$this->panelController = new PanelController($this->dbConnection);
|
$this->panelController = new PanelController($this->dbConnection);
|
||||||
$this->apiUsers = new ApiUsers($this->dbConnection);
|
$this->apiUsers = new ApiUsers($this->dbConnection);
|
||||||
$this->domainController = new DomainController($this->dbConnection);
|
$this->domainController = new DomainController($this->dbConnection);
|
||||||
$this->nameserverController = new NameserverController($this->dbConnection);
|
$this->nameserverController = new NameserverController($this->dbConnection);
|
||||||
|
$this->checkController = new CheckController();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool|array $panel
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function checkPing(bool|array $panel): void
|
||||||
|
{
|
||||||
|
$a = $panel['a'] ?? '';
|
||||||
|
print(COLOR_YELLOW . $panel['name']);
|
||||||
|
if (!empty($a)) {
|
||||||
|
if ($this->checkController->sendCommand(serverName: $panel['name'], versionIP: 4, apiKey: $panel['apikey'], command: 'ping')) {
|
||||||
|
// if verbose …
|
||||||
|
print(COLOR_DEFAULT . ' ' . $panel['a'] . ': ' . COLOR_GREEN . 'pong');
|
||||||
|
} else {
|
||||||
|
$error = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$aaaa = $panel['aaaa'] ?? '';
|
||||||
|
if (!empty($aaaa)) {
|
||||||
|
if ($this->checkController->sendCommand(serverName: $panel['name'], versionIP: 6, apiKey: $panel['apikey'], command: 'ping')) {
|
||||||
|
// if verbose …
|
||||||
|
print(COLOR_DEFAULT . ' ' . $panel['aaaa'] . ': ' . COLOR_GREEN . 'pong' . PHP_EOL);
|
||||||
|
} else {
|
||||||
|
$error = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -95,6 +129,7 @@ class BindAPI
|
||||||
echo "\033[32m\tcheck:domains {ID}" . PHP_EOL;
|
echo "\033[32m\tcheck:domains {ID}" . PHP_EOL;
|
||||||
echo "\033[33mpanels\t\033[39mall Keyhelp systems configured" . PHP_EOL;
|
echo "\033[33mpanels\t\033[39mall Keyhelp systems configured" . PHP_EOL;
|
||||||
echo "\033[32m\tpanels:list" . PHP_EOL;
|
echo "\033[32m\tpanels:list" . PHP_EOL;
|
||||||
|
echo "\033[32m\tpanels:apiping {<ID>}" . PHP_EOL;
|
||||||
echo "\033[32m\tpanels:create <name> {A=<IPv4>} {AAAA=<IPv6>} {apikey=<API-Key>}" . PHP_EOL;
|
echo "\033[32m\tpanels:create <name> {A=<IPv4>} {AAAA=<IPv6>} {apikey=<API-Key>}" . PHP_EOL;
|
||||||
echo "\033[32m\tpanels:update <ID> {name=<name>} {A=<IPv4>} {AAAA=<IPv6>} {apikey=<API-Key>}" . PHP_EOL;
|
echo "\033[32m\tpanels:update <ID> {name=<name>} {A=<IPv4>} {AAAA=<IPv6>} {apikey=<API-Key>}" . PHP_EOL;
|
||||||
echo "\033[32m\tpanels:delete" . PHP_EOL;
|
echo "\033[32m\tpanels:delete" . PHP_EOL;
|
||||||
|
@ -145,6 +180,7 @@ class BindAPI
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
match ($subcommand) {
|
match ($subcommand) {
|
||||||
|
'apiping' => $this->handlePanelsAPIPing(),
|
||||||
'create' => $this->handlePanelsCreate(),
|
'create' => $this->handlePanelsCreate(),
|
||||||
'list' => $this->handlePanelsList(),
|
'list' => $this->handlePanelsList(),
|
||||||
'update' => $this->handlePanelsUpdate(),
|
'update' => $this->handlePanelsUpdate(),
|
||||||
|
@ -155,6 +191,28 @@ class BindAPI
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function handlePanelsAPIPing()
|
||||||
|
{
|
||||||
|
$error = false;
|
||||||
|
$id = $this->argv[2] ?? 0;
|
||||||
|
|
||||||
|
if ($id != 0) {
|
||||||
|
$panel = $this->panelController->findByID($id);
|
||||||
|
$this->checkPing($panel);
|
||||||
|
} else {
|
||||||
|
$panels = $this->panelController->findAll();
|
||||||
|
foreach ($panels as $panel) {
|
||||||
|
$this->checkPing(panel: $panel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
print(PHP_EOL);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -0,0 +1,68 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Controller;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class CheckController
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param String $serverName
|
||||||
|
* @param int $versionIP
|
||||||
|
* @param String $apiKey
|
||||||
|
* @param String $command
|
||||||
|
*
|
||||||
|
* @return bool|array
|
||||||
|
*/
|
||||||
|
function sendCommand(String $serverName, int $versionIP, String $apiKey, String $command) : Bool|Array
|
||||||
|
{
|
||||||
|
$result = false;
|
||||||
|
$curl = curl_init();
|
||||||
|
curl_setopt($curl, option: CURLOPT_URL, value: "https://$serverName/api/v2/" . $command);
|
||||||
|
curl_setopt($curl, option: CURLOPT_RETURNTRANSFER, value: 1);
|
||||||
|
curl_setopt($curl, option: CURLOPT_TIMEOUT_MS, value: 2000);
|
||||||
|
|
||||||
|
if ($versionIP == 4) {
|
||||||
|
curl_setopt($curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V4);
|
||||||
|
} else {
|
||||||
|
curl_setopt($curl, option: CURLOPT_IPRESOLVE, value: CURL_IPRESOLVE_V6);
|
||||||
|
}
|
||||||
|
|
||||||
|
curl_setopt($curl, option: CURLOPT_HTTPHEADER, value: ["X-API-Key:$apiKey"]);
|
||||||
|
|
||||||
|
if ($resultJSON = curl_exec($curl)) {
|
||||||
|
$httpResponse = curl_getinfo($curl)['http_code'];
|
||||||
|
|
||||||
|
switch($httpResponse) {
|
||||||
|
case 200:
|
||||||
|
$apiResult = json_decode($resultJSON);
|
||||||
|
if ($command == "ping" ) {
|
||||||
|
if ($apiResult->response == "pong") {
|
||||||
|
$result = true;
|
||||||
|
} else {
|
||||||
|
print("Error: $httpResponse" . PHP_EOL);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$result = $apiResult;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 401:
|
||||||
|
print("Missing or wrong API Key" . PHP_EOL);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
print("Unhandled error: " . $httpResponse . PHP_EOL);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
$error = curl_error($curl);
|
||||||
|
print("Unknown error: $error" . PHP_EOL);
|
||||||
|
}
|
||||||
|
|
||||||
|
curl_close($curl);
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue