diff --git a/bindAPI/src/Controller/BindAPI.php b/bindAPI/src/Controller/BindAPI.php index e3b946b..06a01d3 100755 --- a/bindAPI/src/Controller/BindAPI.php +++ b/bindAPI/src/Controller/BindAPI.php @@ -2,8 +2,9 @@ namespace App\Controller; -define('COLOR_YELLOW', "\033[33m"); +define('COLOR_RED', "\033[31m"); define('COLOR_GREEN', "\033[32m"); +define('COLOR_YELLOW', "\033[33m"); define('COLOR_DEFAULT', "\033[39m"); // TODO add to all Controllers @@ -24,22 +25,24 @@ if (php_sapi_name() !== 'cli') { class BindAPI { private DatabaseConnection $databaseConnection; - private ApiUsers $apiUsers; + private ApiKeys $apiUsers; 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 array $config, private int $argumentsCount, private array $arguments) { $this->databaseConnection = new DatabaseConnection(config: $this->config); $this->panelController = new PanelController($this->databaseConnection); - $this->apiUsers = new ApiUsers($this->databaseConnection); + $this->apiUsers = new ApiKeys($this->databaseConnection); $this->domainController = new DomainController($this->databaseConnection); $this->nameserverController = new NameserverController($this->databaseConnection); $this->checkController = new CheckController(); } + /** * @param bool|array $panel * @@ -47,33 +50,61 @@ class BindAPI */ public function checkPing(bool|array $panel): Bool { + // TODO nicer format: select max(length(name)) from panels; + $maxName = $this->panelController->getLongestEntry(field: 'name'); + $maxA = $this->panelController->getLongestEntry(field: 'a'); + $maxAAAA = $this->panelController->getLongestEntry(field: 'aaaa'); + $error = false; + + if ($this->config['verbose']) { + print(COLOR_YELLOW . str_pad(string: $panel['name'], length: $maxName)); + } $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')) { - // TODO if verbose … - print(COLOR_DEFAULT . ' ' . $panel['a'] . ': ' . COLOR_GREEN . 'pong'); + if ($this->config['verbose']) { + echo COLOR_DEFAULT . ' ' . str_pad(string: $a, length: $maxA, pad_type: STR_PAD_LEFT) . ' '; + } + 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_RED . $result; + } + } } 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); + if ($this->config['verbose']) { + echo COLOR_DEFAULT . ' ' . str_pad(string: $aaaa, length: $maxAAAA, pad_type: STR_PAD_LEFT); + } + 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_RED . $result; + } + } } else { $error = true; } } + if ($this->config['verbose']) { + echo PHP_EOL; + } return $error; } - function handleCheck(String $subcommand) + function handleChecks(String $subcommand) { + print("hable"); try { match ($subcommand) { 'permissions' => $this->handleCheckPermissions(), @@ -85,21 +116,21 @@ class BindAPI function handleCheckPermissions() { - $test = fgets(STDIN); + $test = fgets(stream: STDIN); echo "received: $test" . PHP_EOL; } function runCommand() { - if ($this->argc < 2) { + if ($this->argumentsCount < 1) { $this->showUsage(); exit(0); } - if (str_contains(haystack: $this->argv[1], needle: ':')) { - [$command, $subcommand] = explode(separator: ':', string: $this->argv[1]); + if (str_contains(haystack: $this->arguments[0], needle: ':')) { + [$command, $subcommand] = explode(separator: ':', string: $this->arguments[0]); } else { - $command = $this->argv[1]; + $command = $this->arguments[0]; $subcommand = ''; } @@ -123,31 +154,38 @@ class BindAPI function showUsage(): void { echo 'Usage' . PHP_EOL; - echo "\033[33mcheck\t\t\033[39mhealth checks the system can perform" . PHP_EOL; - echo "\033[32m\tcheck:permissions" . PHP_EOL; - echo "\033[32m\tcheck:panel {ID}" . PHP_EOL; - 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 {}" . PHP_EOL; - echo "\033[32m\tpanels:create {A=} {AAAA=} {apikey=}" . PHP_EOL; - echo "\033[32m\tpanels:update {name=} {A=} {AAAA=} {apikey=}" . PHP_EOL; - echo "\033[32m\tpanels:delete" . PHP_EOL; - echo "\033[33mapikeys\t\033[39mAPI keys for other nameservers" . PHP_EOL; - echo "\033[32m\tapikeys:list" . PHP_EOL; - echo "\033[32m\tapikeys:create" . PHP_EOL; - echo "\033[32m\tapikeys:delete " . PHP_EOL; - echo "\033[33mnameservers\t\033[39mother nameservers" . PHP_EOL; - echo "\033[32m\tnameservers:list" . PHP_EOL; - echo "\033[32m\tnameservers:create {A=} {AAAA=} {apikey=}" . PHP_EOL; - echo "\033[32m\tnameservers:update {name=} {A=} {AAAA=} {apikey=}" . PHP_EOL; - echo "\033[32m\tnameservers:delete " . PHP_EOL; - echo "\033[33mdomains\t\033[39mdomains this server is responsible for" . PHP_EOL; - echo "\033[32m\tdomains:list" . PHP_EOL; - echo "\033[32m\tdomains:create {A=} {AAAA=}" . PHP_EOL; - echo "\033[32m\tdomains:update {name=} {A=} {AAAA=}" . PHP_EOL; - echo "\033[32m\tdomains:delete " . PHP_EOL; - echo PHP_EOL . "\033[39me.g. ./bin/console apikey:list" . 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}" . 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; + echo COLOR_GREEN . "\t panels:create {A=} {AAAA=} {apikey=}" . PHP_EOL; + echo COLOR_GREEN . "\t panels:update {name=} {A=} {AAAA=} {apikey=}" . PHP_EOL; + echo COLOR_GREEN . "\t panels:delete" . PHP_EOL; + echo COLOR_GREEN . "\t panels:apiping {}" . PHP_EOL; + + echo COLOR_YELLOW . "nameservers" . COLOR_DEFAULT . " available nameservers" . PHP_EOL; + echo COLOR_GREEN . "\t nameservers:list" . PHP_EOL; + echo COLOR_GREEN . "\t nameservers:create {A=} {AAAA=} {apikey=}" . PHP_EOL; + echo COLOR_GREEN . "\t nameservers:update {name=} {A=} {AAAA=} {apikey=}" . PHP_EOL; + echo COLOR_GREEN . "\t nameservers:delete " . PHP_EOL; + echo COLOR_GREEN . "\t nameservers:apiping {}" . PHP_EOL; + + echo COLOR_YELLOW . "domains" . COLOR_DEFAULT . " domains this server is responsible for" . PHP_EOL; + echo COLOR_GREEN . "\t domains:list" . PHP_EOL; + echo COLOR_GREEN . "\t domains:create {A=} {AAAA=}" . PHP_EOL; + echo COLOR_GREEN . "\t domains:update {name=} {A=} {AAAA=}" . PHP_EOL; + echo COLOR_GREEN . "\t domains:delete " . PHP_EOL; + + 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" . PHP_EOL; + echo COLOR_GREEN . "\t apikeys:update " . PHP_EOL; + echo COLOR_GREEN . "\t apikeys:delete " . PHP_EOL; + + echo PHP_EOL . "\033[39me.g. ./bin/console apikeys:list" . PHP_EOL; } @@ -196,11 +234,17 @@ class BindAPI { $error = false; - $id = $this->argv[2] ?? 0; + $id = $this->arguments[1] ?? 0; if ($id != 0) { - $panel = $this->panelController->findByID($id); - if (!$this->checkPing($panel)) { + if ($panel = $this->panelController->findByID($id)) { + if (!$this->checkPing($panel)) { + $error = true; + } + } else { + if ($this->config['verbose']) { + echo 'Unknown panel ID: $id' . PHP_EOL; + } $error = true; } } else { @@ -225,7 +269,7 @@ class BindAPI */ function handlePanelsCreate(): void { - $name = $this->argv[2] ?? ""; + $name = $this->arguments[0] ?? ''; if (empty($name)) { echo 'You need to supply the panel name.' . PHP_EOL; exit(1); @@ -247,7 +291,7 @@ class BindAPI echo 'At least one IP address is required.' . PHP_EOL; exit(0); } - $apikey = $argiments['apikey'] ?? ''; + $apikey = $arguments['apikey'] ?? ''; if ($this->panelController->findByName($name)) { echo "Panel: $name already exists." . PHP_EOL; @@ -265,7 +309,7 @@ class BindAPI */ function handleNameserversCreate(): void { - $name = $this->argv[2] ?? ''; + $name = $this->arguments[1] ?? ''; if (empty($name)) { echo 'You need to supply the nameserver name.' . PHP_EOL; exit(1); @@ -306,7 +350,7 @@ class BindAPI public function parseArguments(): array { $arguments = []; - foreach ($this->argv as $argument) { + foreach ($this->arguments as $argument) { if (str_contains(haystack: $argument, needle: '=')) { [$key, $value] = explode(separator: '=', string: $argument); $arguments[strtolower($key)] = $value; @@ -342,7 +386,7 @@ class BindAPI { $arguments = $this->parseArguments(); - $id = $this->argv[2] ?? 0; + $id = $this->arguments[1] ?? 0; $name = $arguments['name'] ?? ''; $a = $arguments['a'] ?? ''; $aaaa = $arguments['aaaa'] ?? ''; @@ -365,12 +409,12 @@ class BindAPI function handlePanelsDelete() { - if (empty($this->argv[2])) { + if (empty($this->arguments[1])) { echo "You need to supply an ID." . PHP_EOL; exit(1); } - $id = intval($this->argv[2]) ?? 0; + $id = intval($this->arguments[1]) ?? 0; if ($id == 0) { echo "Panel with ID $id not found." . PHP_EOL; exit(1); @@ -421,9 +465,9 @@ class BindAPI $keys = $this->apiUsers->findAll(); if ($keys) { $table = new ConsoleTable(); - $table->setHeaders(['ID', 'API key prefix']); + $table->setHeaders(['ID', 'Name', 'API key prefix']); foreach ($keys as $key) { - $table->addRow([$key['id'], $key['api_token_prefix']]); + $table->addRow([$key['id'], $key['name'], $key['api_token_prefix']]); } $table->setPadding(value: 2); $table->display(); @@ -438,7 +482,7 @@ class BindAPI */ function handleApikeysDelete(): void { - $id = intval($this->argv[2]) ?? 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); @@ -478,7 +522,7 @@ class BindAPI */ function handleNameserversList(): void { - echo 'All available nameserver:' . PHP_EOL; + echo 'All available nameservers:' . PHP_EOL; $domains = $this->nameserverController->findAll(); if ($domains) { $table = new ConsoleTable(); @@ -523,7 +567,7 @@ class BindAPI */ function handleDomainsCreate(): void { - $name = $this->argv[2] ?? ""; + $name = $this->arguments[1] ?? ""; if (empty($name)) { echo 'You need to supply the domain name.' . PHP_EOL; exit(1); @@ -560,7 +604,7 @@ class BindAPI { $arguments = $this->parseArguments(); - $id = $this->argv[2] ?? 0; + $id = $this->arguments[1] ?? 0; $name = $arguments['name'] ?? ''; $a = $arguments['a'] ?? ''; $aaaa = $arguments['aaaa'] ?? ''; @@ -585,8 +629,8 @@ class BindAPI function handleNameserversUpdate() { $arguments = $this->parseArguments(); - - $id = $this->argv[2] ?? 0; + + $id = $this->arguments[1] ?? 0; $name = $arguments['name'] ?? ''; $a = $arguments['a'] ?? ''; $aaaa = $arguments['aaaa'] ?? ''; @@ -609,12 +653,12 @@ class BindAPI function handleNameserversDelete() { - if (empty($this->argv[2])) { + if (empty($this->arguments[1])) { echo "You need to supply an ID." . PHP_EOL; exit(1); } - $id = intval($this->argv[2]) ?? 0; + $id = intval($this->arguments[1]) ?? 0; if ($id == 0) { echo "Nameserver with ID $id not found." . PHP_EOL; exit(1); @@ -630,12 +674,12 @@ class BindAPI function handleDomainsDelete() { - if (empty($this->argv[2])) { + if (empty($this->arguments[1])) { echo "You need to supply an ID." . PHP_EOL; exit(1); } - $id = intval($this->argv[2]) ?? 0; + $id = intval($this->arguments[1]) ?? 0; if ($id == 0) { echo "Domain with ID $id not found." . PHP_EOL; exit(1);