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->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 { $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'] ?? ''; 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->sendPanelCommand(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'; } } } else { $error = true; } } $aaaa = $panel['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->sendPanelCommand(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'; } } } else { $error = true; } } if ($this->config['verbose']) { echo PHP_EOL; } return $error; } function handleChecks(String $subcommand) { 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(); } function runCommand() { if ($this->argumentsCount < 1) { $this->showUsage(); exit(0); } if (str_contains(haystack: $this->arguments[0], needle: ':')) { [$command, $subcommand] = explode(separator: ':', string: $this->arguments[0]); } else { $command = $this->arguments[0]; $subcommand = ''; } try { match ($command) { 'check' => $this->handleChecks($subcommand), 'panels' => $this->handlePanels($subcommand), 'apikeys' => $this->handleApiKeys($subcommand), 'domains' => $this->handleDomains($subcommand), 'nameservers' => $this->handleNameservers($subcommand) }; } catch(UnhandledMatchError) { echo 'Unknown command: ' . $command . PHP_EOL; exit(1); } } /** * @return void */ 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 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_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=} {panel_id=} {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 {name=}" . PHP_EOL; echo COLOR_GREEN . "\t apikeys:update name=" . PHP_EOL; echo COLOR_GREEN . "\t apikeys:delete " . PHP_EOL; echo PHP_EOL . "\033[39me.g. ./bin/console apikeys:list" . PHP_EOL; } /** * @param string $subcommand * * @return void */ public function handleNameservers(string $subcommand): void { try { match ($subcommand) { 'create' => $this->handleNameserversCreate(), 'list' => $this->handleNameserversList(), 'update' => $this->handleNameserversUpdate(), 'delete' => $this->handleNameserversDelete(), 'apiping' => $this->handleNameserversAPIPing(), }; } catch (UnhandledMatchError) { echo 'Unknown action: ' . $subcommand . PHP_EOL; } } /** * @param string $subcommand * * @return void */ public function handlePanels(string $subcommand): void { try { match ($subcommand) { 'apiping' => $this->handlePanelsAPIPing(), 'create' => $this->handlePanelsCreate(), 'list' => $this->handlePanelsList(), 'update' => $this->handlePanelsUpdate(), 'delete' => $this->handlePanelsDelete(), }; } catch (UnhandledMatchError) { echo 'Unknown action: ' . $subcommand . PHP_EOL; } } 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; } if ($id != 0) { if ($panel = $this->panelController->findByID($id)) { if (!$this->checkPanelPing($panel)) { $error = true; } } else { if ($this->config['verbose']) { echo 'Unknown panel ID: $id' . PHP_EOL; } $error = true; } } else { $panels = $this->panelController->findAll(); foreach ($panels as $panel) { if (!$this->checkPanelPing(panel: $panel)) { $error = true; } } } echo PHP_EOL; if ($error) { exit(1); } else { exit(0); } } /** * @return void */ function handlePanelsCreate(): void { $name = $this->arguments[1] ?? ''; if (empty($name)) { echo 'You need to supply the panel name.' . PHP_EOL; exit(1); } $filteredName = filter_var(value: $name, filter: FILTER_VALIDATE_DOMAIN, options: FILTER_FLAG_HOSTNAME); if (!empty($filteredName) && str_contains(haystack: $filteredName, needle: '.')) { $name = $filteredName; } else { echo "$name is no valid DNS domain name." . PHP_EOL; exit(1); } $arguments = $this->parseArguments(); $a = $arguments['a'] ?? ''; $aaaa = $arguments['aaaa'] ?? ''; if (empty($a) && empty($aaaa)) { echo 'At least one IP address is required.' . PHP_EOL; exit(0); } $apikey = $arguments['apikey'] ?? ''; if ($this->panelController->findByName($name)) { echo "Panel: $name already exists." . PHP_EOL; exit(1); } else { $result = $this->panelController->insert($name, $a, $aaaa, $apikey); echo "Panel $name has been created with id $result" . PHP_EOL; exit(0); } } /** * @return void */ function handleNameserversCreate(): void { $name = $this->arguments[1] ?? ''; if (empty($name)) { echo 'You need to supply the nameserver name.' . PHP_EOL; exit(1); } $filteredName = filter_var(value: $name, filter: FILTER_VALIDATE_DOMAIN, options: FILTER_FLAG_HOSTNAME); if (!empty($filteredName) && str_contains(haystack: $filteredName, needle: '.')) { $name = $filteredName; } else { echo "$name is no valid nameserver name." . PHP_EOL; exit(1); } $arguments = $this->parseArguments(); $a = $arguments['a'] ?? ''; $aaaa = $arguments['aaaa'] ?? ''; if (empty($a) && empty($aaaa)) { echo 'At least one IP address is required.' . PHP_EOL; exit(0); } $apikey = $argiments['apikey'] ?? ''; if ($this->nameserverController->findByName($name)) { echo "Nameserver: $name already exists." . PHP_EOL; exit(1); } else { $result = $this->nameserverController->insert($name, $a, $aaaa, $apikey); echo "Nameserver $name has been created with id $result" . PHP_EOL; exit(0); } } /** * @return array */ public function parseArguments(): array { $arguments = []; foreach ($this->arguments as $argument) { if (str_contains(haystack: $argument, needle: '=')) { [$key, $value] = explode(separator: '=', string: $argument); $arguments[strtolower($key)] = $value; } } return $arguments; } /** * @return void */ function handlePanelsList(): void { echo 'All available panels:' . PHP_EOL; $panels = $this->panelController->findAll(); if ($panels) { $table = new ConsoleTable(); $table->setHeaders(['ID', 'Name', 'A', 'AAAA', 'API Key']); foreach ($panels as $panel) { $token = strtok(string: $panel['apikey'], token: '.'); $table->addRow([$panel['id'], $panel['name'], $panel['a'], $panel['aaaa'], $token]); } $table->setPadding(value: 2); $table->display(); } else { echo 'No panels found.' . PHP_EOL; exit(1); } exit(0); } function handlePanelsUpdate() { $arguments = $this->parseArguments(); $id = $this->arguments[1] ?? 0; $name = $arguments['name'] ?? ''; $a = $arguments['a'] ?? ''; $aaaa = $arguments['aaaa'] ?? ''; $apikey = $arguments['apikey'] ?? ''; if ($id == 0) { echo 'An ID is required' . PHP_EOL; exit(1); } if (!$this->panelController->findByID($id)) { echo "Panel with ID : $id doesn't exist." . PHP_EOL; exit(1); } if ($this->panelController->update($id, $name, $a, $aaaa, $apikey) !== false) { echo 'Panel has been updated' . PHP_EOL; } else { echo 'Error while updating domain server.' . PHP_EOL; } } function handlePanelsDelete() { if (empty($this->arguments[1])) { echo "You need to supply an ID." . PHP_EOL; exit(1); } $id = intval($this->arguments[1]) ?? 0; if ($id == 0) { echo "Panel with ID $id not found." . PHP_EOL; exit(1); } if (!$this->panelController->findByID($id)) { echo "There is no panel with ID $id." . PHP_EOL; exit(1); } $this->panelController->delete($id); echo "The panel with ID $id has been deleted." .PHP_EOL; } /** * @param string $subcommand * * @return void */ public function handleApiKeys(string $subcommand): void { try { match ($subcommand) { 'create' => $this->handleApikeysCreate(), 'list' => $this->handleApikeysList(), 'delete' => $this->handleApikeysDelete(), }; } catch (UnhandledMatchError) { echo 'Unknown action: ' . $subcommand . PHP_EOL; } } /** * @return void */ function handleApikeysCreate(): void { $arguments = $this->parseArguments(); $name = $arguments['name'] ?? ''; $result = $this->apiUsers->create(name: $name); 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); } /** * @return void */ function handleApikeysList(): void { echo 'All valid API keys:' . PHP_EOL; $keys = $this->apiUsers->findAll(); if ($keys) { $table = new ConsoleTable(); $table->setHeaders(['ID', 'Name', 'API key prefix']); foreach ($keys as $key) { $table->addRow([$key['id'], $key['name'], $key['api_token_prefix']]); } $table->setPadding(value: 2); $table->display(); } else { echo 'No keys found.' . PHP_EOL; } exit(0); } /** * @return void */ function handleApikeysDelete(): void { $id = intval(value: $this->arguments[1] ?? 0); if ($id == 0) { echo 'You need to add the ID of the API key.' . PHP_EOL; exit(1); } if ($this->apiUsers->findByID($id)) { $this->apiUsers->delete($id); echo 'API key ' . $id . ' has been deleted.' . PHP_EOL; exit(0); } else { echo 'Unknown ID: ' . $id . PHP_EOL; exit(1); } } /** * @param string $subcommand * * @return void */ public function handleDomains(string $subcommand): void { try { match ($subcommand) { 'list' => $this->handleDomainsList(), 'create' => $this->handleDomainsCreate(), 'update' => $this->handleDomainsUpdate(), 'delete' => $this->handleDomainsDelete(), }; } catch (UnhandledMatchError) { echo "Unknown Command: $subcommand" . PHP_EOL; exit(1); } } /** * @return void */ function handleNameserversList(): void { echo 'All available nameservers:' . PHP_EOL; $domains = $this->nameserverController->findAll(); if ($domains) { $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->setPadding(value: 2); $table->display(); } else { echo 'No nameservers found.' . PHP_EOL; exit(1); } exit(0); } /** * @return void */ function handleDomainsList(): void { echo 'All available domains:' . PHP_EOL; $domains = $this->domainController->findAll(); if ($domains) { $table = new ConsoleTable(); $table->setHeaders(['ID', 'Name', 'Panel', 'A', 'AAAA']); foreach ($domains as $domain) { $table->addRow([$domain['id'], $domain['name'], $domain['panel_id'], $domain['a'], $domain['aaaa']]); } $table->setPadding(value: 2); $table->display(); } else { echo 'No domains found.' . PHP_EOL; exit(1); } exit(0); } /** * @return void */ function handleDomainsCreate(): void { $name = $this->arguments[1] ?? ""; if (empty($name)) { echo 'You need to supply the domain name.' . PHP_EOL; exit(1); } $filteredName = filter_var(value: $name, filter: FILTER_VALIDATE_DOMAIN, options: FILTER_FLAG_HOSTNAME); if (!empty($filteredName) && str_contains(haystack: $filteredName, needle: '.')) { $name = $filteredName; } else { echo "$name is no valid DNS domain name." . PHP_EOL; exit(1); } $arguments = $this->parseArguments(); $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; exit(0); } if ($this->domainController->findByName($name)) { echo "Domain: $name already exists." . PHP_EOL; exit(1); } else { $result = $this->domainController->insert($name, $panelID, $a, $aaaa); echo "Domain $name has been created with id $result" . PHP_EOL; exit(0); } } function handleDomainsUpdate() { $arguments = $this->parseArguments(); $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'] ?? ''; if ($id == 0) { echo 'An ID is required' . PHP_EOL; exit(1); } if (!$this->domainController->findByID($id)) { echo "Domain with ID : $id doesn't exist." . PHP_EOL; exit(1); } if ($this->domainController->update($id, $name, $panelID, $a, $aaaa) !== false) { echo 'Domain server has been updated' . PHP_EOL; } else { echo 'Error while updating domain server.' . PHP_EOL; } } function handleNameserversUpdate() { $arguments = $this->parseArguments(); $id = $this->arguments[1] ?? 0; $name = $arguments['name'] ?? ''; $a = $arguments['a'] ?? ''; $aaaa = $arguments['aaaa'] ?? ''; $apikey = $arguments['apikey'] ?? ''; if ($id == 0) { echo 'An ID is required' . PHP_EOL; exit(1); } if (!$this->nameserverController->findByID($id)) { echo "Nameserver with ID : $id doesn't exist." . PHP_EOL; exit(1); } if ($this->nameserverController->update($id, $name, $a, $aaaa, $apikey) !== false) { echo 'Nameserver server has been updated' . PHP_EOL; } else { echo 'Error while updating nameserver.' . PHP_EOL; } } function handleNameserversDelete() { if (empty($this->arguments[1])) { echo "You need to supply an ID." . PHP_EOL; exit(1); } $id = intval($this->arguments[1]) ?? 0; if ($id == 0) { echo "Nameserver with ID $id not found." . PHP_EOL; exit(1); } if (!$this->nameserverController->findByID($id)) { echo "There is no nameserver with ID $id." . PHP_EOL; exit(1); } $this->nameserverController->delete($id); echo "The nameserver with ID $id has been deleted." .PHP_EOL; } function handleDomainsDelete() { if (empty($this->arguments[1])) { echo "You need to supply an ID." . PHP_EOL; exit(1); } $id = intval($this->arguments[1]) ?? 0; if ($id == 0) { echo "Domain with ID $id not found." . PHP_EOL; exit(1); } if (!$this->domainController->findByID($id)) { echo "There is no domain with ID $id." . PHP_EOL; exit(1); } $this->domainController->delete($id); echo "The domain with ID $id has been deleted." .PHP_EOL; } }