Compare commits

..

No commits in common. "3a14eeb3c50a5a401110c1c3786df188bb3a0eb8" and "58d1e828a668aa642dd6efabd87f38dcd2d9d264" have entirely different histories.

2 changed files with 35 additions and 268 deletions

View File

@ -5,12 +5,12 @@ namespace App\Controller;
define('COLOR_RED', "\033[31m");
define('COLOR_GREEN', "\033[32m");
define('COLOR_YELLOW', "\033[33m");
define('COLOR_BLUE', "\033[34m");
define('COLOR_DEFAULT', "\033[39m");
// TODO add to all Controllers
error_reporting(error_level: E_ALL);
// 61e6ce5dd8a1b.bc1c314ce364f6878084c254fe4c6345801c43a49bb8eb71
use LucidFrame\Console\ConsoleTable;
use UnhandledMatchError;
@ -37,93 +37,18 @@ class BindAPI
$this->databaseConnection = new DatabaseConnection(config: $this->config);
$this->panelController = new PanelController($this->databaseConnection);
$this->apiUsers = new ApiKeys($this->databaseConnection);
$this->domainController = new DomainController($this->databaseConnection, $this->panelController);
$this->domainController = new DomainController($this->databaseConnection);
$this->nameserverController = new NameserverController($this->databaseConnection);
$this->checkController = new CheckController();
}
/**
* @return int|void
*/
public function getId()
{
if (!empty($this->arguments[1])) {
$id = intval(value: $this->arguments[1] ?? 0);
if ($id != $this->arguments[1]) {
echo 'ID has to be a number.' . PHP_EOL;
exit(1);
}
} else {
$id = 0;
}
return $id;
}
/**
* @param bool|array $panel
*
* @return bool
*/
public function checkNSPing(bool|array $nameserver): Bool
{
$maxName = $this->nameserverController->getLongestEntry(field: 'name');
$maxA = $this->nameserverController->getLongestEntry(field: 'a');
$maxAAAA = $this->nameserverController->getLongestEntry(field: 'aaaa');
$error = false;
if ($this->config['verbose']) {
print(COLOR_YELLOW . str_pad(string: $nameserver['name'], length: $maxName));
}
$a = $nameserver['a'] ?? '';
if (!empty($a)) {
if ($this->config['verbose']) {
echo COLOR_DEFAULT . ' ' . str_pad(string: $a, length: $maxA, pad_type: STR_PAD_LEFT) . ' ';
}
if ($result = $this->checkController->sendCommand(serverName: $nameserver['name'], versionIP: 4, apiKey: $nameserver['apikey'], command: 'ping', type: 'nameserver')) {
if ($this->config['verbose']) {
if ($result == 'pong') {
echo COLOR_GREEN . $result;
} else {
echo COLOR_BLUE . $result; // TODO 'skip';
}
}
} else {
$error = true;
}
}
$aaaa = $nameserver['aaaa'] ?? '';
if (!empty($aaaa)) {
if ($this->config['verbose']) {
echo COLOR_DEFAULT . ' ' . str_pad(string: $aaaa, length: $maxAAAA, pad_type: STR_PAD_LEFT) . ' ';
}
if ($result = $this->checkController->sendCommand(serverName: $nameserver['name'], versionIP: 6, apiKey: $nameserver['apikey'], command: 'ping', type: 'nameserver')) {
if ($this->config['verbose']) {
if ($result == 'pong') {
echo COLOR_GREEN . $result;
} else {
echo COLOR_BLUE . $result; // TODO 'skip';
}
}
} else {
$error = true;
}
}
if ($this->config['verbose']) {
echo PHP_EOL;
}
return $error;
}
/**
* @param bool|array $panel
*
* @return bool
*/
public function checkPanelPing(bool|array $panel): Bool
public function checkPing(bool|array $panel): Bool
{
$maxName = $this->panelController->getLongestEntry(field: 'name');
$maxA = $this->panelController->getLongestEntry(field: 'a');
@ -140,12 +65,12 @@ class BindAPI
if ($this->config['verbose']) {
echo COLOR_DEFAULT . ' ' . str_pad(string: $a, length: $maxA, pad_type: STR_PAD_LEFT) . ' ';
}
if ($result = $this->checkController->sendPanelCommand(serverName: $panel['name'], versionIP: 4, apiKey: $panel['apikey'], command: 'ping')) {
if ($result = $this->checkController->sendCommand(serverName: $panel['name'], versionIP: 4, apiKey: $panel['apikey'], command: 'ping')) {
if ($this->config['verbose']) {
if ($result == 'pong') {
echo COLOR_GREEN . $result;
} else {
echo COLOR_BLUE . 'skip';
echo COLOR_RED . $result;
}
}
} else {
@ -155,14 +80,14 @@ class BindAPI
$aaaa = $panel['aaaa'] ?? '';
if (!empty($aaaa)) {
if ($this->config['verbose']) {
echo COLOR_DEFAULT . ' ' . str_pad(string: $aaaa, length: $maxAAAA, pad_type: STR_PAD_LEFT) . ' ';
echo COLOR_DEFAULT . ' ' . str_pad(string: $aaaa, length: $maxAAAA, pad_type: STR_PAD_LEFT);
}
if ($result = $this->checkController->sendPanelCommand(serverName: $panel['name'], versionIP: 6, apiKey: $panel['apikey'], command: 'ping')) {
if ($result = $this->checkController->sendCommand(serverName: $panel['name'], versionIP: 6, apiKey: $panel['apikey'], command: 'ping')) {
if ($this->config['verbose']) {
if ($result == 'pong') {
echo COLOR_GREEN . $result;
} else {
echo COLOR_BLUE . 'skip';
echo COLOR_RED . $result;
}
}
} else {
@ -178,101 +103,22 @@ class BindAPI
function handleChecks(String $subcommand)
{
print("hable");
try {
match ($subcommand) {
'permissions' => $this->handleCheckPermissions(),
'panels' => $this->handleCheckPanels(),
};
} catch (UnhandledMatchError) {
echo 'Unknown action: ' . $subcommand . PHP_EOL;
}
}
/**
* @param String $domain
*
* @return bool|void
*/
function checkNS(String $domain)
{
$nameServers = $this->nameserverController->findAll();
$i = 1;
foreach($nameServers as $nameServer) {
print("nameserver $i");
$i++;
if (!empty($nameServer['aaaa'])) {
$result = $this->checkController->sendCommand(serverName: $nameServer['name'],
versionIP: 6,
apiKey: $nameServer['apikey'],
command: 'domains/' . $domain,
type: 'nameserver');
} else {
$result = $this->checkController->sendCommand(serverName: $nameServer['name'],
versionIP: 4,
apiKey: $nameServer['apikey'],
command: 'domains',
type: 'nameserver' . $domain);
}
if ($result['header'] == 200) {
print("OK");
} else {
print("missing");
}
}
echo PHP_EOL;
}
function handleCheckPanels()
{
$id = $this->getId();
$maxDomain = $this->panelController->getLongestEntry(field:'name');
print("$maxDomain");
die();
if ($id != 0) {
if ($panel = $this->panelController->findByID($id)) {
if (!empty($panel['aaaa'])) {
$result = $this->checkController->sendCommand(serverName: $panel['name'],
versionIP: 6,
apiKey: $panel['apikey'],
command: 'domains',
type: 'panel');
} else {
$result = $this->checkController->sendCommand(serverName: $panel['name'],
versionIP: 4,
apiKey: $panel['apikey'],
command: 'domains',
type: 'panel');
}
$domains = json_decode($result['data']);
foreach ($domains as $domain) {
if ($domain->id_parent_domain == 0 && !str_contains(haystack: $domain->domain, needle: $panel['name'])) {
echo PHP_EOL . COLOR_DEFAULT . "check: " . COLOR_YELLOW . str_pad($domain->domain, $maxDomain);
if ($this->checkNS($domain->domain)) {
echo COLOR_GREEN . ' OK';
} else {
echo COLOR_RED . 'Missing';
}
}
}
} else {
echo "Unknown Panel ID: $id" . PHP_EOL;
exit(1);
}
}
}
function handleCheckPermissions()
{
$this->domainController->checkPermissions();
$test = fgets(stream: STDIN);
echo "received: $test" . PHP_EOL;
}
function runCommand()
{
if ($this->argumentsCount < 1) {
@ -306,17 +152,11 @@ class BindAPI
*/
function showUsage(): void
{
echo COLOR_YELLOW . 'Usage:' . PHP_EOL;
echo COLOR_DEFAULT . "\t./bin/console {options} {arguments}" . PHP_EOL . PHP_EOL;
echo COLOR_YELLOW . 'Options:' . PHP_EOL;
echo COLOR_GREEN . "\t-v, --version\t\t" . COLOR_DEFAULT . "Display the version of the API" . PHP_EOL;
echo COLOR_GREEN . "\t-V, --verbose\t\t" . COLOR_DEFAULT . "All :list command are auto-verbose" . PHP_EOL . PHP_EOL;
echo 'Usage' . PHP_EOL;
echo COLOR_YELLOW . "check" . COLOR_DEFAULT . "\t health checks the system can perform" . PHP_EOL;
echo COLOR_GREEN . "\t check:permissions" . PHP_EOL;
echo COLOR_GREEN . "\t check:panels {ID} {fix}" . PHP_EOL;
echo COLOR_GREEN . "\t check:domains {ID} {fix}" . PHP_EOL;
echo COLOR_GREEN . "\t check:panels {ID}" . PHP_EOL;
echo COLOR_GREEN . "\t check:domains {ID}" . PHP_EOL;
echo COLOR_YELLOW . "panels" . COLOR_DEFAULT . "\t all Keyhelp systems configured" . PHP_EOL;
echo COLOR_GREEN . "\t panels:list" . PHP_EOL;
@ -328,7 +168,7 @@ class BindAPI
echo COLOR_YELLOW . "nameservers" . COLOR_DEFAULT . " available nameservers" . PHP_EOL;
echo COLOR_GREEN . "\t nameservers:list" . PHP_EOL;
echo COLOR_GREEN . "\t nameservers:create <name> {A=<IPv4>} {AAAA=<IPv6>} {apikey=<API-Key>}" . PHP_EOL;
echo COLOR_GREEN . "\t nameservers:update <ID> {name=<name>} {panel_id=<ID>} {A=<IPv4>} {AAAA=<IPv6>} {apikey=<API-Key>}" . PHP_EOL;
echo COLOR_GREEN . "\t nameservers:update <ID> {name=<name>} {A=<IPv4>} {AAAA=<IPv6>} {apikey=<API-Key>}" . PHP_EOL;
echo COLOR_GREEN . "\t nameservers:delete <ID>" . PHP_EOL;
echo COLOR_GREEN . "\t nameservers:apiping {<ID>}" . PHP_EOL;
@ -340,8 +180,8 @@ class BindAPI
echo COLOR_YELLOW . "apikeys" . COLOR_DEFAULT . "\t API keys for other nameservers" . PHP_EOL;
echo COLOR_GREEN . "\t apikeys:list" . PHP_EOL;
echo COLOR_GREEN . "\t apikeys:create {name=<name>}" . PHP_EOL;
echo COLOR_GREEN . "\t apikeys:update <ID> name=<name>" . PHP_EOL;
echo COLOR_GREEN . "\t apikeys:create" . PHP_EOL;
echo COLOR_GREEN . "\t apikeys:update <ID>" . PHP_EOL;
echo COLOR_GREEN . "\t apikeys:delete <ID>" . PHP_EOL;
echo PHP_EOL . "\033[39me.g. ./bin/console apikeys:list" . PHP_EOL;
@ -361,8 +201,6 @@ class BindAPI
'list' => $this->handleNameserversList(),
'update' => $this->handleNameserversUpdate(),
'delete' => $this->handleNameserversDelete(),
'apiping' => $this->handleNameserversAPIPing(),
};
} catch (UnhandledMatchError) {
echo 'Unknown action: ' . $subcommand . PHP_EOL;
@ -391,70 +229,15 @@ class BindAPI
}
function handleNameserversAPIPing()
{
// TODO get apikey from stdin
//$test = fgets(stream: STDIN);
//echo "received: $test" . PHP_EOL;
$error = false;
if (!empty($this->arguments[1])) {
$id = intval(value:$this->arguments[1] ?? 0);
if ($id != $this->arguments[1]) {
echo 'ID has to be a number.' . PHP_EOL;
exit(1);
}
} else {
$id = 0;
}
if ($id != 0) {
if ($nameserver = $this->nameserverController->findByID($id)) {
if (!$this->checkNSPing($nameserver)) {
$error = true;
}
} else {
if ($this->config['verbose']) {
echo 'Unknown panel ID: $id' . PHP_EOL;
}
$error = true;
}
} else {
$nameservers = $this->nameserverController->findAll();
foreach ($nameservers as $nameserver) {
if (!$this->checkNSPing(nameserver: $nameserver)) {
$error = true;
}
}
}
echo PHP_EOL;
if ($error) {
exit(1);
} else {
exit(0);
}
}
function handlePanelsAPIPing()
{
$error = false;
if (!empty($this->arguments[1])) {
$id = intval(value: $this->arguments[1] ?? 0);
if ($id != $this->arguments[1]) {
echo 'ID has to be a number.' . PHP_EOL;
exit(1);
}
} else {
$id = 0;
}
$id = $this->arguments[1] ?? 0;
if ($id != 0) {
if ($panel = $this->panelController->findByID($id)) {
if (!$this->checkPanelPing($panel)) {
if (!$this->checkPing($panel)) {
$error = true;
}
} else {
@ -466,7 +249,7 @@ class BindAPI
} else {
$panels = $this->panelController->findAll();
foreach ($panels as $panel) {
if (!$this->checkPanelPing(panel: $panel)) {
if (!$this->checkPing(panel: $panel)) {
$error = true;
}
}
@ -666,10 +449,7 @@ class BindAPI
*/
function handleApikeysCreate(): void
{
$arguments = $this->parseArguments();
$name = $arguments['name'] ?? '';
$result = $this->apiUsers->create(name: $name);
$result = $this->apiUsers->create();
echo 'API key ' . $result['row'] . ' has been generated. Store it in a save place, it cannot be recovered.' . PHP_EOL;
echo "\033[32m\t" . $result['tokenPrefix'] . '.' . $result['key'] . PHP_EOL;
exit(0);
@ -701,7 +481,7 @@ class BindAPI
*/
function handleApikeysDelete(): void
{
$id = intval(value: $this->arguments[1] ?? 0);
$id = intval($this->arguments[1]) ?? 0;
if ($id == 0) {
echo 'You need to add the ID of the API key.' . PHP_EOL;
exit(1);
@ -747,8 +527,7 @@ class BindAPI
$table = new ConsoleTable();
$table->setHeaders(['ID', 'Name', 'A', 'AAAA', 'API Key']);
foreach ($domains as $domain) {
$token = strtok(string: $domain['apikey'], token: '.');
$table->addRow([$domain['id'], $domain['name'], $domain['a'], $domain['aaaa'], $token]);
$table->addRow([$domain['id'], $domain['name'], $domain['a'], $domain['aaaa'], $domain['apikey']]);
}
$table->setPadding(value: 2);
$table->display();
@ -769,9 +548,9 @@ class BindAPI
$domains = $this->domainController->findAll();
if ($domains) {
$table = new ConsoleTable();
$table->setHeaders(['ID', 'Name', 'Panel', 'A', 'AAAA']);
$table->setHeaders(['ID', 'Name', 'A', 'AAAA']);
foreach ($domains as $domain) {
$table->addRow([$domain['id'], $domain['name'], $domain['panel_id'], $domain['a'], $domain['aaaa']]);
$table->addRow([$domain['id'], $domain['name'], $domain['a'], $domain['aaaa']]);
}
$table->setPadding(value: 2);
$table->display();
@ -805,10 +584,8 @@ class BindAPI
$a = $arguments['a'] ?? "";
$aaaa = $arguments['aaaa'] ?? "";
$panelID = $arguments['panel_id'] ?? 0;
if (empty($a) && empty($aaaa) && empty($panelID)) {
echo 'At least one IP address or panel ID is required.' . PHP_EOL;
if (empty($a) && empty($aaaa)) {
echo 'At least one IP address is required.' . PHP_EOL;
exit(0);
}
@ -816,7 +593,7 @@ class BindAPI
echo "Domain: $name already exists." . PHP_EOL;
exit(1);
} else {
$result = $this->domainController->insert($name, $panelID, $a, $aaaa);
$result = $this->domainController->insert($name, $a, $aaaa);
echo "Domain $name has been created with id $result" . PHP_EOL;
exit(0);
}
@ -828,8 +605,6 @@ class BindAPI
$id = $this->arguments[1] ?? 0;
$name = $arguments['name'] ?? '';
print_r($arguments); //findme
$panelID = intval(value: $arguments['panel_id'] ?? 0);
$a = $arguments['a'] ?? '';
$aaaa = $arguments['aaaa'] ?? '';
@ -841,7 +616,7 @@ class BindAPI
echo "Domain with ID : $id doesn't exist." . PHP_EOL;
exit(1);
}
if ($this->domainController->update($id, $name, $panelID, $a, $aaaa) !== false) {
if ($this->domainController->update($id, $name, $a, $aaaa) !== false) {
echo 'Domain server has been updated' . PHP_EOL;
} else {
echo 'Error while updating domain server.' . PHP_EOL;

View File

@ -14,18 +14,13 @@ class CheckController
* @param int $versionIP
* @param String $apiKey
* @param String $command
* @param String $type
*
* @return array
* @return String
*/
function sendCommand(String $serverName, int $versionIP, String $apiKey, String $command, String $type): array
function sendCommand(String $serverName, int $versionIP, String $apiKey, String $command): String
{
$curl = curl_init();
if ($type == "panel") {
curl_setopt($curl, option: CURLOPT_URL, value: "https://$serverName/api/v2/" . $command);
} else {
curl_setopt($curl, option: CURLOPT_URL, value: "https://$serverName/api/" . $command);
}
curl_setopt($curl, option: CURLOPT_RETURNTRANSFER, value: 1);
curl_setopt($curl, option: CURLOPT_TIMEOUT_MS, value: 1000);
@ -47,7 +42,7 @@ class CheckController
if ($apiResult->response == "pong") {
$result = $apiResult->response;
} else {
$result = $apiResult;
$result = $resultJSON;
}
} else {
$result = $resultJSON;
@ -63,10 +58,7 @@ class CheckController
$result = curl_error($curl);
}
curl_close($curl);
return [
'data' => $result,
'header' => $httpResponse ?? ''
];
return $result;
}
}