added nameservers

This commit is contained in:
tracer 2022-01-20 19:55:04 +01:00
parent 136569b428
commit 28763504f0
1 changed files with 317 additions and 117 deletions

View File

@ -22,31 +22,54 @@ if (php_sapi_name() !== 'cli') {
*/ */
class BindAPI class BindAPI
{ {
private PDO $dbConnection; private PDO $dbConnection;
private ApiUsers $apiUsers; private ApiUsers $apiUsers;
private DomainController $domainController; private DomainController $domainController;
private PanelController $panelController;
public function __construct(private int $argc, private array $argv) public function __construct(private int $argc, private array $argv)
{ {
$this->dbConnection = (new DatabaseConnection())->getConnection(); $this->dbConnection = (new DatabaseConnection())->getConnection();
$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);
} }
function handleCheck(String $subcommand)
{
try {
match ($subcommand) {
'permissions' => $this->handleCheckPermissions(),
};
} catch (UnhandledMatchError) {
echo 'Unknown action: ' . $subcommand . PHP_EOL;
}
}
function handleCheckPermissions()
{
$test = fgets(STDIN);
echo "received: $test" . PHP_EOL;
}
function runCommand() function runCommand()
{ {
if ($this->argc < 2) { if ($this->argc < 2) {
$this->showUsage(); $this->showUsage();
exit(0);
} }
[$command, $subcommand] = explode(':', $this->argv[1]); if (str_contains(haystack: $this->argv[1], needle: ':')) {
[$command, $subcommand] = explode(separator: ':', string: $this->argv[1]);
} else {
$command = $this->argv[1];
$subcommand = '';
}
try { try {
match ($command) { match ($command) {
'check' => $this->handleChecks($subcommand),
'panels' => $this->handlePanels($subcommand),
'apikeys' => $this->handleApiKeys($subcommand), 'apikeys' => $this->handleApiKeys($subcommand),
'domains' => $this->handleDomains($subcommand) 'domains' => $this->handleDomains($subcommand)
}; };
@ -56,159 +79,179 @@ class BindAPI
} }
} }
/** /**
* @return void * @return void
*/ */
function showUsage(): void function showUsage(): void
{ {
echo 'Usage' . PHP_EOL; echo 'Usage' . PHP_EOL;
echo "\033[33mapikeys" . 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: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;
echo "\033[33mapikeys\t\033[39mAPI keys for other nameservers" . PHP_EOL;
echo "\033[32m\tapikeys:list" . PHP_EOL; echo "\033[32m\tapikeys:list" . PHP_EOL;
echo "\033[32m\tapikeys:create" . PHP_EOL; echo "\033[32m\tapikeys:create" . PHP_EOL;
echo "\033[32m\tapikeys:delete {ID}" . PHP_EOL; echo "\033[32m\tnameservers:delete <ID>" . PHP_EOL;
echo "\033[33mdomains" . PHP_EOL; echo "\033[33mnameservers\t\033[39mother nameservers" . PHP_EOL;
echo "\033[32m\tnameservers:list" . PHP_EOL;
echo "\033[32m\tnameservers:create" . PHP_EOL;
echo "\033[32m\tnameservers:delete <ID>" . 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:list" . PHP_EOL;
echo "\033[32m\tdomains:create <name> {A=<IPv4>} {AAAA=<IPv6>}" . PHP_EOL; echo "\033[32m\tdomains:create <name> {A=<IPv4>} {AAAA=<IPv6>}" . PHP_EOL;
echo "\033[32m\tdomains:update <ID> {name} {A=<IPv4>} {AAAA=<IPv6>}" . PHP_EOL; echo "\033[32m\tdomains:update <ID> {name=<name>} {A=<IPv4>} {AAAA=<IPv6>}" . PHP_EOL;
echo "\033[32m\tdomains:delete" . PHP_EOL; echo "\033[32m\tdomains:delete" . PHP_EOL;
echo "\033[32m\tdomains:check" . PHP_EOL; echo "\033[32m\tdomains:check" . PHP_EOL;
echo PHP_EOL . "\033[39me.g. ./bin/console apikey:list" . PHP_EOL; echo PHP_EOL . "\033[39me.g. ./bin/console apikey:list" . PHP_EOL;
exit(0);
} }
/** /**
* @param \App\Controller\ApiUsers $apiUsers * @param string $subcommand
* *
* @return void * @return void
*/ */
function handleApikeysCreate(ApiUsers $apiUsers): void public function handlePanels(string $subcommand): void
{ {
$result = $apiUsers->create(); try {
echo 'API key ' . $result['row'] . ' has been generated. Store it in a save place, it cannot be recovered.' . PHP_EOL; match ($subcommand) {
echo "\033[32m\t" . $result['tokenPrefix'] . '.' . $result['key'] . PHP_EOL; 'create' => $this->handlePanelsCreate(),
exit(0); 'list' => $this->handlePanelsList(),
} 'update' => $this->handlePanelsUpdate(),
'delete' => $this->handlePanelsDelete(),
};
/** } catch (UnhandledMatchError) {
* @param ApiUsers $apiUsers echo 'Unknown action: ' . $subcommand . PHP_EOL;
*
* @return void
*/
function handleApikeysList(ApiUsers $apiUsers): void
{
echo 'All valid API keys:' . PHP_EOL;
$keys = $apiUsers->findAll();
if ($keys) {
$table = new ConsoleTable();
$table->setHeaders(['ID', 'API key prefix']);
foreach ($keys as $key) {
$table->addRow([$key['id'], $key['api_token_prefix']]);
}
$table->setPadding(2);
$table->display();
} else {
echo 'No keys found.' . PHP_EOL;
}
exit(0);
}
/**
* @param ApiUsers $apiUsers
*
* @return void
*/
function handleApikeysDelete(ApiUsers $apiUsers): void
{
$id = $argv[2] ?? 0;
if ($id == 0) {
echo 'You need to add the ID of the token.' . PHP_EOL;
exit(1);
}
if ($apiUsers->findByID($id)) {
$apiUsers->delete($id);
echo 'Token ' . $id . ' has been deleted.' . PHP_EOL;
exit(0);
} else {
echo 'Unknown ID: ' . $id . PHP_EOL;
exit(1);
} }
} }
/** /**
* @param \App\Controller\DomainController $domainController
*
* @return void * @return void
*/ */
function handleDomainsList(DomainController $domainController): void function handlePanelsCreate(): void
{
echo 'All available domains:' . PHP_EOL;
$domains = $domainController->findAll();
if ($domains) {
$table = new ConsoleTable();
$table->setHeaders(['ID', 'Name', 'A', 'AAAA']);
foreach ($domains as $domain) {
$table->addRow([$domain['id'], $domain['name'], $domain['a'], $domain['aaaa']]);
}
$table->setPadding(2);
$table->display();
} else {
echo 'No domains found.' . PHP_EOL;
exit(1);
}
exit(0);
}
/**
* @param \App\Controller\DomainController $domainController
*
* @return void
*/
function handleDomainCreate(DomainController $domainController): void
{ {
$name = $this->argv[2] ?? ""; $name = $this->argv[2] ?? "";
if (empty($name)) { if (empty($name)) {
echo 'You need to supply the domain name.' . PHP_EOL; echo 'You need to supply the panel name.' . PHP_EOL;
exit(1); exit(1);
} }
$filteredName = filter_var($name, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME); $filteredName = filter_var(value: $name, filter: FILTER_VALIDATE_DOMAIN, options: FILTER_FLAG_HOSTNAME);
if (!empty($filteredName) && str_contains($filteredName, '.')) { if (!empty($filteredName) && str_contains(haystack: $filteredName, needle: '.')) {
$name = $filteredName; $name = $filteredName;
} else { } else {
echo "$name is no valid DNS domain name." . PHP_EOL; echo "$name is no valid DNS domain name." . PHP_EOL;
exit(1); exit(1);
} }
foreach ($this->argv as $argument) { $arguments = $this->parseArguments();
if (str_contains($argument, '=')) {
[$key, $value] = explode('=', $argument); $a = $arguments['a'] ?? '';
$arguments[$key] = $value; $aaaa = $arguments['aaaa'] ?? '';
}
}
$a = $arguments['a'] ?? "";
$aaaa = $arguments['aaaa'] ?? "";
if (empty($a) && empty($aaaa)) { if (empty($a) && empty($aaaa)) {
echo 'At least one IP address is required.'; echo 'At least one IP address is required.' . PHP_EOL;
exit(0); exit(0);
} }
$apikey = $argiments['apikey'] ?? '';
if ($domainController->findByName($name)) { if ($this->panelController->findByName($name)) {
echo "Domain: $name already exists." . PHP_EOL; echo "Panel: $name already exists." . PHP_EOL;
exit(1); exit(1);
} else { } else {
$dcResult = $domainController->insert($name, $a, $aaaa); $result = $this->panelController->insert($name, $a, $aaaa, $apikey);
echo "Domain $name has been created" . PHP_EOL; echo "Panel $name has been created with id $result" . PHP_EOL;
exit(0); exit(0);
} }
} }
/**
* @return array
*/
public function parseArguments(): array
{
$arguments = [];
foreach ($this->argv 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->argv[2] ?? 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->argv[2])) {
echo "You need to supply an ID." . PHP_EOL;
exit(1);
}
$id = intval($this->argv[2]) ?? 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 * @param string $subcommand
* *
@ -218,15 +261,67 @@ class BindAPI
{ {
try { try {
match ($subcommand) { match ($subcommand) {
'create' => $this->handleApikeysCreate($this->apiUsers), 'create' => $this->handleApikeysCreate(),
'list' => $this->handleApikeysList($this->apiUsers), 'list' => $this->handleApikeysList(),
'delete' => $this->handleApikeysDelete($this->apiUsers), 'delete' => $this->handleApikeysDelete(),
}; };
} catch (UnhandledMatchError $error) { } catch (UnhandledMatchError) {
echo 'Unknown action: ' . $subcommand . PHP_EOL; echo 'Unknown action: ' . $subcommand . PHP_EOL;
} }
} }
/**
* @return void
*/
function handleApikeysCreate(): void
{
$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);
}
/**
* @return void
*/
function handleApikeysList(): void
{
echo 'All valid API keys:' . PHP_EOL;
$keys = $this->apiUsers->findAll();
if ($keys) {
$table = new ConsoleTable();
$table->setHeaders(['ID', 'API key prefix']);
foreach ($keys as $key) {
$table->addRow([$key['id'], $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($this->argv[2]) ?? 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 * @param string $subcommand
* *
@ -236,13 +331,118 @@ class BindAPI
{ {
try { try {
match ($subcommand) { match ($subcommand) {
'list' => $this->handleDomainsList($this->domainController), 'list' => $this->handleDomainsList(),
'create' => $this->handleDomainCreate($this->domainController), 'create' => $this->handleDomainsCreate(),
'update' => $this->handleDomainsUpdate(),
'delete' => $this->handleDomainsDelete(),
}; };
} catch (UnhandledMatchError $error) { } catch (UnhandledMatchError) {
echo "Unknown Command: $subcommand" . PHP_EOL; echo "Unknown Command: $subcommand" . PHP_EOL;
exit(1); exit(1);
} }
} }
/**
* @return void
*/
function handleDomainsList(): void
{
echo 'All available domains:' . PHP_EOL;
$domains = $this->domainController->findAll();
if ($domains) {
$table = new ConsoleTable();
$table->setHeaders(['ID', 'Name', 'A', 'AAAA']);
foreach ($domains as $domain) {
$table->addRow([$domain['id'], $domain['name'], $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->argv[2] ?? "";
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'] ?? "";
if (empty($a) && empty($aaaa)) {
echo 'At least one IP address 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, $a, $aaaa);
echo "Domain $name has been created with id $result" . PHP_EOL;
exit(0);
}
}
function handleDomainsUpdate()
{
$arguments = $this->parseArguments();
$id = $this->argv[2] ?? 0;
$name = $arguments['name'] ?? '';
$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, $a, $aaaa) !== false) {
echo 'Domain server has been updated' . PHP_EOL;
} else {
echo 'Error while updating domain server.' . PHP_EOL;
}
}
function handleDomainsDelete()
{
if (empty($this->argv[2])) {
echo "You need to supply an ID." . PHP_EOL;
exit(1);
}
$id = intval($this->argv[2]) ?? 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;
}
} }