Compare commits

..

No commits in common. "776f4a8acf83f387e88be5b64b9e52f1b76801d5" and "6cf4c835935c8c6f06ad43c22d23b0d90af01d72" have entirely different histories.

2 changed files with 2 additions and 128 deletions

View File

@ -2,10 +2,6 @@
namespace App\Controller;
define('COLOR_YELLOW', "\033[33m");
define('COLOR_GREEN', "\033[32m");
define('COLOR_DEFAULT', "\033[39m");
// TODO add to all Controllers
error_reporting(E_ALL);
@ -31,44 +27,14 @@ class BindAPI
private DomainController $domainController;
private PanelController $panelController;
private NameserverController $nameserverController;
private CheckController $checkController;
public function __construct(private array $config, private int $argc, private array $argv)
public function __construct(private int $argc, private array $argv)
{
$this->dbConnection = (new DatabaseConnection(config: $this->config))->getConnection();
$this->dbConnection = (new DatabaseConnection())->getConnection();
$this->panelController = new PanelController($this->dbConnection);
$this->apiUsers = new ApiUsers($this->dbConnection);
$this->domainController = new DomainController($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;
}
}
}
@ -129,7 +95,6 @@ class BindAPI
echo "\033[32m\tcheck:domains {ID}" . PHP_EOL;
echo "\033[33mpanels\t\033[39mall Keyhelp systems configured" . 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:update <ID> {name=<name>} {A=<IPv4>} {AAAA=<IPv6>} {apikey=<API-Key>}" . PHP_EOL;
echo "\033[32m\tpanels:delete" . PHP_EOL;
@ -180,7 +145,6 @@ class BindAPI
{
try {
match ($subcommand) {
'apiping' => $this->handlePanelsAPIPing(),
'create' => $this->handlePanelsCreate(),
'list' => $this->handlePanelsList(),
'update' => $this->handlePanelsUpdate(),
@ -191,28 +155,6 @@ 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
*/

View File

@ -1,68 +0,0 @@
<?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;
}
}