Compare commits

...

17 Commits

Author SHA1 Message Date
tracer b536316a84 initial commit 2022-09-27 19:49:16 +02:00
tracer f614837729 initial commit 2022-09-27 19:41:35 +02:00
tracer e9d777ab24 initial commit 2022-09-27 19:38:01 +02:00
tracer 61ec6aaaa5 refactored command registration 2022-09-27 19:13:28 +02:00
tracer 4c80ba1543 added encryption 2022-09-22 19:31:38 +02:00
tracer be6402b4a3 removed a stale blank line ^^ 2022-09-22 19:30:45 +02:00
tracer 8f5a57ee54 made all methods use objects instead of single parameters 2022-09-22 19:11:04 +02:00
tracer 51b9e67ea9 added encryption, constructor property promotion 2022-09-22 18:57:10 +02:00
tracer 8ec1a2942d added password encryption 2022-09-22 18:54:54 +02:00
tracer 790176964d added corrected key import from config 2022-09-22 18:54:23 +02:00
tracer b2115a97a8 removed unused imports 2022-09-22 18:53:29 +02:00
tracer 3c09b4038d added encryption support 2022-09-21 16:02:42 +02:00
tracer 3bc232ef0b fixed a handling bug 2022-09-21 16:01:44 +02:00
tracer cd5361c65b refactored zone deletion 2022-09-21 16:01:14 +02:00
tracer f81b0451b4 added quiet option 2022-09-21 16:00:16 +02:00
tracer 9474a9ebef added quiet option 2022-09-21 15:59:09 +02:00
tracer ccb3479568 added quiet option 2022-09-21 15:58:48 +02:00
14 changed files with 2483 additions and 1895 deletions

13
bin/console Normal file → Executable file
View File

@ -26,11 +26,13 @@ require dirname(path: __DIR__) . '/vendor/autoload.php';
$shortOpts = 'v::'; // version $shortOpts = 'v::'; // version
$shortOpts = 'q::'; // version
$shortOpts .= "V::"; // verbose $shortOpts .= "V::"; // verbose
$shortOpts .= "h::"; // help $shortOpts .= "h::"; // help
$longOpts = [ $longOpts = [
'version::', 'version::',
'quiet::',
'verbose::', 'verbose::',
'help::' 'help::'
]; ];
@ -47,6 +49,13 @@ if (array_key_exists(key: 'h', array: $options) || array_key_exists(key: 'help',
exit(0); exit(0);
} }
if (array_key_exists(key: 'q', array: $options) || array_key_exists(key: 'quiet', array: $options)) {
$quiet = true;
} else {
$quiet = false;
}
if (array_key_exists(key: 'V', array: $options) || array_key_exists(key: 'verbose', array: $options)) { if (array_key_exists(key: 'V', array: $options) || array_key_exists(key: 'verbose', array: $options)) {
$verbose = true; $verbose = true;
} else { } else {
@ -56,8 +65,8 @@ if (array_key_exists(key: 'V', array: $options) || array_key_exists(key: 'verbos
$arguments = array_slice(array: $argv, offset: $restIndex); $arguments = array_slice(array: $argv, offset: $restIndex);
try { try {
$app = new BindAPI(verbose: $verbose ); $app = new BindAPI(verbose: $verbose, quiet: $quiet);
$app->runCommand(argumentsCount: count(value: $arguments), arguments: $arguments); $app->runCommand(arguments: $arguments);
} catch (DependencyException|NotFoundException|Exception $e) { } catch (DependencyException|NotFoundException|Exception $e) {
echo $e->getMessage() . PHP_EOL; echo $e->getMessage() . PHP_EOL;

View File

@ -8,8 +8,12 @@ use App\Repository\DomainRepository;
use App\Repository\DynDNSRepository; use App\Repository\DynDNSRepository;
use DI\Container; use DI\Container;
use DI\ContainerBuilder; use DI\ContainerBuilder;
use DI\DependencyException;
use DI\NotFoundException;
use Exception;
use Monolog\Formatter\LineFormatter; use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler; use Monolog\Handler\StreamHandler;
use Monolog\Level;
use Monolog\Logger; use Monolog\Logger;
use function DI\autowire; use function DI\autowire;
@ -20,12 +24,11 @@ class BindAPI
{ {
private Logger $logger; private Logger $logger;
private Container $container; private Container $container;
/** /**
* @throws \Exception * @throws Exception
*/ */
public function __construct($verbose = false) public function __construct($verbose = false, $quiet = false)
{ {
// init the logger // init the logger
$dateFormat = "Y:m:d H:i:s"; $dateFormat = "Y:m:d H:i:s";
@ -34,9 +37,9 @@ class BindAPI
$debug = (new ConfigController)->getConfig(configKey: 'debug'); $debug = (new ConfigController)->getConfig(configKey: 'debug');
if ($debug) { if ($debug) {
$stream = new StreamHandler(stream: dirname(path: __DIR__, levels: 2) . '/bindAPI.log', level: Logger::DEBUG); $stream = new StreamHandler(stream: dirname(path: __DIR__, levels: 2) . '/bindAPI.log', level: Level::Debug);
} else { } else {
$stream = new StreamHandler(stream: dirname(path: __DIR__, levels: 2) . '/bindAPI.log', level: Logger::INFO); $stream = new StreamHandler(stream: dirname(path: __DIR__, levels: 2) . '/bindAPI.log', level: Level::Info);
} }
$stream->setFormatter(formatter: $formatter); $stream->setFormatter(formatter: $formatter);
@ -48,6 +51,7 @@ class BindAPI
$containerBuilder = new ContainerBuilder(); $containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions([ $containerBuilder->addDefinitions([
ConfigController::class => autowire() ConfigController::class => autowire()
->constructorParameter(parameter: 'quiet', value: $quiet)
->constructorParameter(parameter: 'verbose', value: $verbose), ->constructorParameter(parameter: 'verbose', value: $verbose),
CLIController::class => autowire() CLIController::class => autowire()
->constructorParameter(parameter: 'logger', value: $this->logger), ->constructorParameter(parameter: 'logger', value: $this->logger),
@ -65,8 +69,8 @@ class BindAPI
/** /**
* @throws \DI\DependencyException * @throws DependencyException
* @throws \DI\NotFoundException * @throws NotFoundException
*/ */
public function runCommand(array $arguments): void public function runCommand(array $arguments): void
{ {
@ -77,8 +81,8 @@ class BindAPI
/** /**
* @throws \DI\DependencyException * @throws DependencyException
* @throws \DI\NotFoundException * @throws NotFoundException
*/ */
public function handleRequest(string $requestMethod, array $uri): void public function handleRequest(string $requestMethod, array $uri): void
{ {

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,68 @@
<?php
namespace App\Controller\Commands;
use Closure;
/**
*
*/
class Command
{
public function __construct(
private readonly string $name,
private readonly Closure $callback,
private readonly array $mandatoryParameters = [],
private readonly array $optionalParameters = [],
private readonly string $description = ''
)
{
// no body
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @return array
*/
public function getMandatoryParameters(): array
{
return $this->mandatoryParameters;
}
/**
* @return array
*/
public function getOptionalParameters(): array
{
return $this->optionalParameters;
}
/**
* @return string|null
*/
public function getDescription(): ?string
{
return $this->description;
}
/**
* @return Closure
*/
public function getCallback(): Closure
{
return $this->callback;
}
public function exec(): void
{
call_user_func(callback: $this->callback);
}
}

View File

@ -0,0 +1,68 @@
<?php
namespace App\Controller\Commands;
/**
*
*/
class CommandGroup
{
private array $commands = [];
public function __construct(private readonly string $name, private readonly string $description)
{
// no body
}
public function addCommand(Command $command): ?CommandGroup
{
$this->commands[] = $command;
return $this;
}
public function getName(): string
{
return $this->name;
}
public function printCommands(int $longestCommandLength): void
{
echo COLOR_YELLOW . str_pad(string: $this->name, length: $longestCommandLength + 1) . COLOR_WHITE . $this->description . COLOR_DEFAULT . PHP_EOL;
foreach ($this->commands as $command) {
echo COLOR_GREEN . str_pad(string: ' ', length: $longestCommandLength + 1, pad_type: STR_PAD_LEFT) . $this->name . ':' . $command->getName();
foreach ($command->getMandatoryParameters() as $parameter) {
echo ' <' . $parameter . '>';
}
foreach ($command->getOptionalParameters() as $parameter) {
echo ' {' . $parameter . '}';
}
echo COLOR_WHITE . ' ' . $command->getDescription();
echo COLOR_DEFAULT . PHP_EOL;
}
}
public function findCommandByName(string $command): ?Command
{
foreach ($this->commands as $currentCommand) {
if ($command === $currentCommand->getName()) {
return $currentCommand;
}
}
return null;
}
public function exec(string $subcommand): void
{
if ($command = $this->findCommandByName(command: $subcommand)) {
$command->exec();
} else {
echo COLOR_DEFAULT . 'Command ' . COLOR_YELLOW . $this->name . ':' . $subcommand . COLOR_DEFAULT . ' not found.' . PHP_EOL;
exit(1);
}
}
}

View File

@ -0,0 +1,73 @@
<?php
namespace App\Controller\Commands;
/**
*
*/
class CommandGroupContainer
{
private array $commandGroups = [];
public function addCommandGroup(CommandGroup $commandGroup): CommandGroupContainer
{
$this->commandGroups[] = $commandGroup;
return $this;
}
/**
* @return void
*/
public function printCommands(): void
{
$longestCommandLength = $this->getLongestCommandLength();
foreach ($this->commandGroups as $commandGroup) {
$commandGroup->printCommands($longestCommandLength);
}
}
/**
* @return int
*/
public function getLongestCommandLength(): int
{
$longest = 0;
foreach ($this->commandGroups as $group) {
$len = strlen(string: $group->getName());
if ($len > $longest) {
$longest = $len;
}
}
return $longest;
}
/**
* @param string $command
* @return ?CommandGroup
*/
private function findGroupByName(string $command): ?CommandGroup
{
foreach ($this->commandGroups as $group) {
if ($group->getName() === $command) {
return $group;
}
}
return null;
}
public function run(string $command, string $subcommand): void
{
if ($group = $this->findGroupByName(command: $command)) {
$group->exec(subcommand: $subcommand);
} else {
echo COLOR_DEFAULT . 'Unknown command ' . COLOR_YELLOW . $command . COLOR_DEFAULT . '.' . PHP_EOL;
exit(1);
}
}
}

View File

@ -9,7 +9,7 @@ class ConfigController
{ {
private array $config; private array $config;
public function __construct(bool $verbose = false) { public function __construct(bool $verbose = false, bool $quiet = false) {
$configFile = dirname(path: __DIR__, levels: 2) . "/config.json.local"; $configFile = dirname(path: __DIR__, levels: 2) . "/config.json.local";
if (!file_exists(filename: $configFile)) { if (!file_exists(filename: $configFile)) {
$configFile = dirname(path: __DIR__, levels: 2) . "/config.json"; $configFile = dirname(path: __DIR__, levels: 2) . "/config.json";
@ -41,6 +41,11 @@ class ConfigController
} else { } else {
$this->config['verbose'] = false; $this->config['verbose'] = false;
} }
if ($quiet) {
$this->config['quiet'] = true;
} else {
$this->config['quiet'] = false;
}
} }
public function getConfig(string $configKey): string { public function getConfig(string $configKey): string {

View File

@ -83,7 +83,7 @@ class DomainController
} }
$this->createSlaveZoneFile(domain: $domain); $this->createSlaveZoneFile(domain: $domain);
} }
$this->createIncludeFile(); $this->createIncludeFile();
} }
@ -98,16 +98,30 @@ class DomainController
'name' => $domain->getName() 'name' => $domain->getName()
]; ];
if (!empty($nameserver->getAaaa())) { if (!empty($nameserver->getAaaa())) {
$this->checkController->sendCommand(requestType: 'DELETE', serverName: $nameserver->getName(), versionIP: 6, apiKey: $nameserver->getApikey(), command: 'delete', serverType: 'nameserver', body: $body); $this->checkController->sendCommand(
requestType: 'DELETE',
serverName: $nameserver->getName(),
versionIP: 6,
apiKey: $nameserver->getApikey(),
command: 'delete',
serverType: 'nameserver',
body: $body);
} else { } else {
$this->checkController->sendCommand(requestType: 'DELETE', serverName: $nameserver->getName(), versionIP: 4, apiKey: $nameserver->getApikey(), command: 'delete', serverType: 'nameserver', body: $body); $this->checkController->sendCommand(
requestType: 'DELETE',
serverName: $nameserver->getName(),
versionIP: 4,
apiKey: $nameserver->getApikey(),
command: 'delete',
serverType: 'nameserver',
body: $body);
} }
} }
} }
/** /**
* @param \App\Entity\Domain $domain * @param Domain $domain
* *
* @return void * @return void
*/ */
@ -230,25 +244,34 @@ class DomainController
$domains = $this->domainRepository->findAll(); $domains = $this->domainRepository->findAll();
foreach ($domains as $domain) { foreach ($domains as $domain) {
echo COLOR_YELLOW . str_pad(string: $domain->getName(), length: $maxNameLength + 1) . COLOR_DEFAULT; $idString = '(' . strval(value: $domain->getId()) . ') ';
echo COLOR_YELLOW .
str_pad(string: $domain->getName(), length: $maxNameLength + 1)
. COLOR_DEFAULT
. str_pad(string: $idString, length: 7, pad_type: STR_PAD_LEFT);
$hasError = false;
if ($this->isMasterZone(domain: $domain)) { if ($this->isMasterZone(domain: $domain)) {
echo 'Master Zone lies on this panel.'; echo 'Master Zone lies on this panel.';
} else { } else {
if (!str_contains(haystack: $localZones, needle: $domain->getName())) { if (!str_contains(haystack: $localZones, needle: $domain->getName())) {
echo COLOR_RED . ' is missing in ' . COLOR_YELLOW . $this->localZoneFile . COLOR_DEFAULT; echo COLOR_RED . 'is missing in ' . COLOR_YELLOW . $this->localZoneFile . COLOR_DEFAULT;
$hasError = true;
} else { } else {
echo $domain->getName() . ' exists in ' . COLOR_YELLOW . $this->localZoneFile; echo COLOR_GREEN . 'OK';
} }
$zoneFile = $this->localZonesDir . $domain->getName(); $zoneFile = $this->localZonesDir . $domain->getName();
if (!file_exists(filename: $zoneFile)) { if (!file_exists(filename: $zoneFile)) {
echo "Missing zone file for $zoneFile . Update zone to create it"; echo ' Missing zone file for ' . COLOR_YELLOW . $zoneFile . COLOR_DEFAULT;
$hasError = true;
} }
if ($hasError) {
echo " Update zone (Domain) to create it.";
}
} }
echo COLOR_DEFAULT . PHP_EOL; echo COLOR_DEFAULT . PHP_EOL;
} }
@ -256,7 +279,7 @@ class DomainController
/** /**
* @param \App\Entity\Domain $domain * @param Domain $domain
* *
* @return void * @return void
*/ */
@ -267,11 +290,11 @@ class DomainController
// check if we're a master zone // check if we're a master zone
if ($this->isMasterZone(domain: $domain)) { if ($this->isMasterZone(domain: $domain)) {
echo 'We are zone master for ' . $domainName . PHP_EOL; //echo 'We are zone master for ' . $domainName . PHP_EOL;
exit(1); return;
} }
if ($zonefile = fopen(filename: $this->localZonesDir . $domainName, mode: 'w')) { if ($zoneFile = fopen(filename: $this->localZonesDir . $domainName, mode: 'w')) {
$panelName = $domain->getPanel(); $panelName = $domain->getPanel();
if (!$panel = $this->panelRepository->findByName(name: $panelName)) { if (!$panel = $this->panelRepository->findByName(name: $panelName)) {
echo "Error: Panel $panelName doesn't exist." . PHP_EOL; echo "Error: Panel $panelName doesn't exist." . PHP_EOL;
@ -279,18 +302,18 @@ class DomainController
} }
$a = $panel->getA(); $a = $panel->getA();
$aaaa = $panel->getAaaa(); $aaaa = $panel->getAaaa();
fputs(stream: $zonefile, data: 'zone "' . $domainName . '"' . ' IN {' . PHP_EOL); fputs(stream: $zoneFile, data: 'zone "' . $domainName . '"' . ' IN {' . PHP_EOL);
fputs(stream: $zonefile, data: "\ttype slave;" . PHP_EOL); fputs(stream: $zoneFile, data: "\ttype slave;" . PHP_EOL);
fputs(stream: $zonefile, data: "\tfile \"" . $this->zoneCachePath . $domainName . '.db";' . PHP_EOL); fputs(stream: $zoneFile, data: "\tfile \"" . $this->zoneCachePath . $domainName . '.db";' . PHP_EOL);
fputs(stream: $zonefile, data: "\tmasters {" . PHP_EOL); fputs(stream: $zoneFile, data: "\tmasters {" . PHP_EOL);
if (!empty($a)) { if (!empty($a)) {
fputs(stream: $zonefile, data: "\t\t" . $a . ';' . PHP_EOL); fputs(stream: $zoneFile, data: "\t\t" . $a . ';' . PHP_EOL);
} }
if (!empty($aaaa)) { if (!empty($aaaa)) {
fputs(stream: $zonefile, data: "\t\t" . $aaaa . ';' . PHP_EOL); fputs(stream: $zoneFile, data: "\t\t" . $aaaa . ';' . PHP_EOL);
} }
fputs(stream: $zonefile, data: "\t};" . PHP_EOL); fputs(stream: $zoneFile, data: "\t};" . PHP_EOL);
fputs(stream: $zonefile, data: "};" . PHP_EOL); fputs(stream: $zoneFile, data: "};" . PHP_EOL);
} }
} }

View File

@ -20,11 +20,13 @@ class EncryptionController
*/ */
function safeEncrypt(string $message, string $key): string function safeEncrypt(string $message, string $key): string
{ {
$binKey = sodium_hex2bin(string: $key);
$nonce = random_bytes(length: SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); $nonce = random_bytes(length: SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
$cipher = base64_encode(string: $nonce . sodium_crypto_secretbox(message: $message, nonce: $nonce, key: $key)); $cipher = base64_encode(string: $nonce . sodium_crypto_secretbox(message: $message, nonce: $nonce, key: $binKey));
sodium_memzero(string: $message); sodium_memzero(string: $message);
sodium_memzero(string: $key); sodium_memzero(string: $key);
sodium_memzero(string: $binKey);
return $cipher; return $cipher;
} }
@ -39,19 +41,23 @@ class EncryptionController
*/ */
function safeDecrypt(string $encrypted, string $key): string function safeDecrypt(string $encrypted, string $key): string
{ {
$binKey = sodium_hex2bin(string: $key);
$decoded = base64_decode(string: $encrypted); $decoded = base64_decode(string: $encrypted);
if ($decoded === false) { if ($decoded === false) {
throw new Exception(message: 'Decoding broken. Wrong key?'); throw new Exception(message: 'Decoding broken. Wrong payload.');
} }
if (mb_strlen(string: $decoded, encoding: '8bit') < (SODIUM_CRYPTO_SECRETBOX_NONCEBYTES + SODIUM_CRYPTO_SECRETBOX_MACBYTES)) { if (mb_strlen(string: $decoded, encoding: '8bit') < (SODIUM_CRYPTO_SECRETBOX_NONCEBYTES + SODIUM_CRYPTO_SECRETBOX_MACBYTES)) {
throw new Exception(message: 'Decoding broken. Incomplete message.'); throw new Exception(message: 'Decoding broken. Incomplete message.');
} }
$nonce = mb_substr(string: $decoded, start: 0, length: SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, encoding: '8bit'); $nonce = mb_substr(string: $decoded, start: 0, length: SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, encoding: '8bit');
$ciphertext = mb_substr(string: $decoded, start: SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, length: null, encoding: '8bit'); $ciphertext = mb_substr(string: $decoded, start: SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, length: null, encoding: '8bit');
$plain = sodium_crypto_secretbox_open(ciphertext: $ciphertext, nonce: $nonce, key: $key); $plain = sodium_crypto_secretbox_open(ciphertext: $ciphertext, nonce: $nonce, key: $binKey);
if ($plain === false) { if ($plain === false) {
throw new Exception(message: 'The message was tampered with in transit'); throw new Exception(message: ' Incorrect key.');
} }
sodium_memzero(string: $ciphertext); sodium_memzero(string: $ciphertext);
sodium_memzero(string: $key); sodium_memzero(string: $key);

View File

@ -11,7 +11,6 @@ use App\Repository\DomainRepository;
use App\Repository\DynDNSRepository; use App\Repository\DynDNSRepository;
use App\Repository\PanelRepository; use App\Repository\PanelRepository;
use Monolog\Logger; use Monolog\Logger;
use OpenApi\Annotations as OA;
use OpenApi\Attributes as OAT; use OpenApi\Attributes as OAT;
use UnhandledMatchError; use UnhandledMatchError;
@ -58,13 +57,13 @@ class RequestController
/** /**
* @param \App\Controller\ApiController $apiController * @param ApiController $apiController
* @param \App\Repository\ApikeyRepository $apikeyRepository * @param ApikeyRepository $apikeyRepository
* @param \App\Controller\DomainController $domainController * @param DomainController $domainController
* @param \App\Repository\DomainRepository $domainRepository * @param DomainRepository $domainRepository
* @param \App\Repository\DynDNSRepository $dynDNSRepository * @param DynDNSRepository $dynDNSRepository
* @param \App\Repository\PanelRepository $panelRepository * @param PanelRepository $panelRepository
* @param \Monolog\Logger $logger * @param Logger $logger
*/ */
public function __construct( public function __construct(
private readonly ApiController $apiController, private readonly ApiController $apiController,
@ -289,7 +288,7 @@ class RequestController
{ {
$headers = array_change_key_case(array: getallheaders(), case: CASE_UPPER); $headers = array_change_key_case(array: getallheaders(), case: CASE_UPPER);
$apiKey = $headers['X-API-KEY'] ?? ''; $apiKey = $headers['X-API-KEY'] ?? '';
if (empty($apiKey)) { if (empty($apiKey)) {
$this->status = "401 Unauthorized"; $this->status = "401 Unauthorized";
$this->message = "API key is missing."; $this->message = "API key is missing.";

View File

@ -2,89 +2,106 @@
namespace App\Entity; namespace App\Entity;
use App\Controller\ConfigController;
use App\Controller\EncryptionController;
use Exception;
use SodiumException;
/** /**
* *
*/ */
class Apikey class Apikey
{ {
private int $id;
private string $name; public function __construct(
private string $apiTokenPrefix; private int $id = 0,
private string $apiToken; private string $name = '',
private string $apiTokenPrefix = '',
public function __construct(string $name, string $apiTokenPrefix, string $apiToken, int $id = 0) private string $apiToken = '',
{ private readonly string $passphrase = ''
$this->id = $id; )
$this->name = $name; {
$this->apiTokenPrefix = $apiTokenPrefix; if ($this->passphrase) {
$this->apiToken = $apiToken; $configController = new ConfigController();
} $encryptionController = new EncryptionController();
$encryptionKey = $configController->getConfig(configKey: 'encryptionKey');
/**
* @return String $this->apiTokenPrefix = strtok(string: $this->passphrase, token: '.');
*/
public function getApiToken(): string try {
{ $this->apiToken = $encryptionController->safeEncrypt(message: $this->passphrase, key: $encryptionKey);
return $this->apiToken; } catch (Exception|SodiumException $e) {
} die($e->getMessage() . PHP_EOL);
}
/** }
* @return string }
*/
public function getApiTokenPrefix(): string
{ /**
return $this->apiTokenPrefix; * @return String
} */
public function getApiToken(): string
{
/** return $this->apiToken;
* @return int }
*/
public function getId(): int /**
{ * @return string
return $this->id; */
} public function getApiTokenPrefix(): string
{
/** return $this->apiTokenPrefix;
* @param int $id }
*/
public function setId(int $id): void
{ /**
$this->id = $id; * @return int
} */
public function getId(): int
/** {
* @return String return $this->id;
*/ }
public function getName(): string
{ /**
return $this->name; * @param int $id
} */
public function setId(int $id): void
/** {
* @param string $apiTokenPrefix $this->id = $id;
*/ }
public function setApiTokenPrefix(string $apiTokenPrefix): void
{ /**
$this->apiTokenPrefix = $apiTokenPrefix; * @return String
} */
public function getName(): string
/** {
* @param String $apiToken return $this->name;
*/ }
public function setApiToken(string $apiToken): void
{ /**
$this->apiToken = $apiToken; * @param string $apiTokenPrefix
} */
public function setApiTokenPrefix(string $apiTokenPrefix): void
{
/** $this->apiTokenPrefix = $apiTokenPrefix;
* @param String $name }
*/
public function setName(string $name): void /**
{ * @param String $apiToken
$this->name = $name; */
} public function setApiToken(string $apiToken): void
{
$this->apiToken = $apiToken;
}
/**
* @param String $name
*/
public function setName(string $name): void
{
$this->name = $name;
}
} }

View File

@ -2,7 +2,11 @@
namespace App\Entity; namespace App\Entity;
use App\Controller\ConfigController;
use App\Controller\EncryptionController;
use Exception;
use OpenApi\Attributes as OAT; use OpenApi\Attributes as OAT;
use SodiumException;
/** /**
* *
@ -11,73 +15,109 @@ use OpenApi\Attributes as OAT;
class Nameserver class Nameserver
{ {
private int $id; /**
private String $name; * @param string $name
private String $a; * @param int $id
private String $aaaa; * @param string $a
private String $apikey; * @param string $aaaa
* @param string $passphrase
public function __construct(String $name, int $id = 0, String $a = '', String $aaaa = '', String $apikey = '') * @param string $apikey
* @param string $apikeyPrefix
*/
public function __construct(
private string $name,
private int $id = 0,
private string $a = '',
private string $aaaa = '',
private readonly string $passphrase = '',
private string $apikey = '',
private string $apikeyPrefix = '')
{ {
$this->id = $id; if ($this->passphrase) {
$this->name = $name; $configController = new ConfigController();
$this->a = $a; $encryptionController = new EncryptionController();
$this->aaaa = $aaaa;
$this->apikey = $apikey; $encryptionKey = $configController->getConfig(configKey: 'encryptionKey');
}
[$this->apikeyPrefix] = explode(separator: '.', string: $this->passphrase);
try {
$this->apikey = $encryptionController->safeEncrypt(message: $this->passphrase, key: $encryptionKey);
} catch (Exception|SodiumException $e) {
die($e->getMessage() . PHP_EOL);
}
}
}
/**
* @return string
*/
public function getApikeyPrefix(): string
{
return $this->apikeyPrefix;
}
/**
* @param string $apikeyPrefix
*/
public function setApikeyPrefix(string $apikeyPrefix): void
{
$this->apikeyPrefix = $apikeyPrefix;
}
/** /**
* @return String * @return string
*/ */
#[OAT\Property(type: 'string')] #[OAT\Property(type: 'string')]
public function getA(): string public function getA(): string
{ {
return $this->a; return $this->a;
} }
/** /**
* @param String $a * @param string $a
*/ */
public function setA(string $a): void public function setA(string $a): void
{ {
$this->a = $a; $this->a = $a;
} }
/** /**
* @return String * @return string
*/ */
#[OAT\Property(type: 'string')] #[OAT\Property(type: 'string')]
public function getAaaa(): string public function getAaaa(): string
{ {
return $this->aaaa; return $this->aaaa;
} }
/** /**
* @param String $aaaa * @param string $aaaa
*/ */
public function setAaaa(string $aaaa): void public function setAaaa(string $aaaa): void
{ {
$this->aaaa = $aaaa; $this->aaaa = $aaaa;
} }
/** /**
* @return String * @return string
*/ */
#[OAT\Property(type: 'string')] #[OAT\Property(type: 'string')]
public function getApikey(): string public function getApikey(): string
{ {
return $this->apikey; return $this->apikey;
} }
/** /**
* @param String $apikey * @param string $apikey
*/ */
public function setApikey(string $apikey): void public function setApikey(string $apikey): void
{ {
$this->apikey = $apikey; $this->apikey = $apikey;
} }
/** /**
* @return int * @return int
*/ */
@ -86,7 +126,7 @@ class Nameserver
{ {
return $this->id; return $this->id;
} }
/** /**
* @param int $id * @param int $id
*/ */
@ -94,22 +134,30 @@ class Nameserver
{ {
$this->id = $id; $this->id = $id;
} }
/** /**
* @return String * @return string
*/ */
#[OAT\Property(type: 'string')] #[OAT\Property(type: 'string')]
public function getName(): string public function getName(): string
{ {
return $this->name; return $this->name;
} }
/** /**
* @param String $name * @param string $name
*/ */
public function setName(string $name): void public function setName(string $name): void
{ {
$this->name = $name; $this->name = $name;
} }
/**
* @return string
*/
public function getPassphrase(): string
{
return $this->passphrase;
}
} }

View File

@ -2,133 +2,160 @@
namespace App\Entity; namespace App\Entity;
use App\Controller\ConfigController;
use App\Controller\EncryptionController;
use Exception;
use SodiumException;
/** /**
* *
*/ */
class Panel class Panel
{ {
private int $id; /**
private String $name; * @param string $name
private String $a; * @param int $id
private String $aaaa; * @param string $a
private String $apikey; * @param string $aaaa
private int $self; * @param string $passphrase
* @param string $apikey
/** * @param string $apikeyPrefix
* @param String $name * @param string $self
* @param int $id */
* @param String $a public function __construct(
* @param String $aaaa private string $name,
* @param String $apikey private int $id = 0,
* @param int $self private string $a = '',
*/ private string $aaaa = '',
public function __construct(String $name, int $id = 0, String $a = '', String $aaaa = '', String $apikey = '', int $self = 0) private readonly string $passphrase = '',
{ private string $apikey = '',
$this->id = $id; private string $apikeyPrefix = '',
$this->name = $name; private string $self = 'no',
$this->a = $a; )
$this->aaaa = $aaaa; {
$this->apikey = $apikey; if ($this->passphrase) {
$this->self = $self; $configController = new ConfigController();
} $encryptionController = new EncryptionController();
/** $encryptionKey = $configController->getConfig(configKey: 'encryptionKey');
* @return int
*/ $this->apikeyPrefix = strtok(string: $this->passphrase, token: '.');
public function getSelf(): int
{ try {
return $this->self; $this->apikey = $encryptionController->safeEncrypt(message: $this->passphrase, key: $encryptionKey);
} } catch (Exception|SodiumException $e) {
die($e->getMessage() . PHP_EOL);
/** }
* @param int $self }
*/ }
public function setSelf(int $self): void
{ /**
$this->self = $self; * @return string
} */
public function getPassphrase(): string
{
/** return $this->passphrase;
* @return String }
*/
public function getA(): string
{ /**
return $this->a; * @return string
} */
public function getName(): string
/** {
* @return String return $this->name;
*/ }
public function getAaaa(): string
{ /**
return $this->aaaa; * @param string $name
} */
public function setName(string $name): void
/** {
* @return String $this->name = $name;
*/ }
public function getApikey(): string
{ /**
return $this->apikey; * @return int
} */
public function getId(): int
/** {
* @return int return $this->id;
*/ }
public function getId(): int
{ /**
return $this->id; * @param int $id
} */
public function setId(int $id): void
/** {
* @param int $id $this->id = $id;
*/ }
public function setId(int $id): void
{ /**
$this->id = $id; * @return string
} */
public function getA(): string
/** {
* @return String return $this->a;
*/ }
public function getName(): string
{
return $this->name; /**
} * @return string
*/
public function getSelf(): string
/** {
* @param String $apikey return $this->self;
*/ }
public function setApikey(string $apikey): void
{ /**
$this->apikey = $apikey; * @param string $self
} */
public function setSelf(string $self): void
{
/** $this->self = $self;
* @param String $name }
*/
public function setName(string $name): void
{ /**
$this->name = $name; * @return string
} */
public function getAaaa(): string
/** {
* @param String $a return $this->aaaa;
*/ }
public function setA(string $a): void
{ /**
$this->a = $a; * @return string
} */
public function getApikey(): string
/** {
* @param String $aaaa return $this->apikey;
*/ }
public function setAaaa(string $aaaa): void
{
$this->aaaa = $aaaa; /**
} * @param string $a
*/
public function setA(string $a): void
{
$this->a = $a;
}
/**
* @param string $aaaa
*/
public function setAaaa(string $aaaa): void
{
$this->aaaa = $aaaa;
}
/**
* @return string
*/
public function getApikeyPrefix(): string
{
return $this->apikeyPrefix;
}
} }

View File

@ -3,7 +3,6 @@
namespace App\Repository; namespace App\Repository;
use App\Controller\DatabaseConnection; use App\Controller\DatabaseConnection;
use App\Entity\Domain;
use App\Entity\Panel; use App\Entity\Panel;
use PDO; use PDO;
use PDOException; use PDOException;
@ -13,243 +12,249 @@ use PDOException;
*/ */
class PanelRepository class PanelRepository
{ {
public function __construct(private readonly DatabaseConnection $databaseConnection) public function __construct(private readonly DatabaseConnection $databaseConnection)
{} {
// no body
public function findSelf(): array }
{
$sql = " /**
SELECT id, name, a, aaaa, apikey, self * @return array|null
*/
public function findSelf(): ?array
{
$sql = "
SELECT id, name, a, aaaa, apikey, apikey_prefix, self
FROM " . DatabaseConnection::TABLE_PANELS . " FROM " . DatabaseConnection::TABLE_PANELS . "
WHERE self = 1"; WHERE self = 1";
$panels = []; $panels = [];
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute(); $statement->execute();
while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
$panel = new Panel(name: $result['name'], id: $result['id'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']); $panel = new Panel(name: $result['name'], id: $result['id'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], apikeyPrefix: $result['apikey_prefix'], self: $result['self']);
$panels[] = $panel; $panels[] = $panel;
} }
return $panels; return $panels;
} catch (PDOException $e) { }
exit($e->getMessage());
}
public function findAll(): ?array
{
} $panels = [];
$sql = "
/** SELECT id, name, a, aaaa, apikey, apikey_prefix, self
* @return array
*/
public function findAll(): array
{
$panels = [];
$sql = "
SELECT id, name, a, aaaa, apikey, self
FROM " . DatabaseConnection::TABLE_PANELS . " FROM " . DatabaseConnection::TABLE_PANELS . "
ORDER BY name"; ORDER BY name";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute(); $statement->execute();
while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
$panel = new Panel(name: $result['name'], id: $result['id'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']); $panel = new Panel(name: $result['name'], id: $result['id'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], apikeyPrefix: $result['apikey_prefix'], self: $result['self']);
$panels[] = $panel; $panels[] = $panel;
} }
return $panels; return $panels;
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
} }
/** /**
* @param int $id * @param int $id
* *
* @return null|\App\Entity\Panel * @return null|Panel
*/ */
public function findByID(int $id): ?Panel public function findByID(int $id): ?Panel
{ {
$sql = " $sql = "
SELECT id, name, a, aaaa, apikey, self SELECT id, name, a, aaaa, apikey, apikey_prefix, self
FROM . " . DatabaseConnection::TABLE_PANELS . " FROM . " . DatabaseConnection::TABLE_PANELS . "
WHERE id = :id"; WHERE id = :id";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':id', var: $id); $statement->bindParam(param: ':id', var: $id);
$statement->execute(); $statement->execute();
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']); return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], apikeyPrefix: $result['apikey_prefix'], self: $result['self']);
} else { } else {
return null; return null;
} }
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
} }
/** /**
* @param String $name * @param String $name
* *
* @return \App\Entity\Panel|bool * @return Panel|null
*/ */
public function findByName(string $name): Panel|bool public function findByName(string $name): ?Panel
{ {
$sql = " $sql = "
SELECT id, name, a, aaaa, apikey, self SELECT id, name, a, aaaa, apikey, apikey_prefix, self
FROM " . DatabaseConnection::TABLE_PANELS . " FROM " . DatabaseConnection::TABLE_PANELS . "
WHERE name = :name"; WHERE name = :name";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':name', var: $name); $statement->bindParam(param: ':name', var: $name);
$statement->execute(); $statement->execute();
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']); return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], apikeyPrefix: $result['apikey_prefix'], self: $result['self']);
} else { } else {
return false; return null;
} }
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
} }
/** /**
* @param String $name * @param Panel $panel
* @param String $a * @return int|null
* @param String $aaaa */
* @param String $apikey public function insert(Panel $panel): ?int
* {
* @return string|false $name = $panel->getName();
*/ $a = $panel->getA();
public function insert(string $name, string $a, string $aaaa, String $apikey, int $self): bool|string $aaaa = $panel->getAaaa();
{ $apikey = $panel->getApikey();
$sql = " $apikeyPrefix = $panel->getApikeyPrefix();
INSERT INTO " . DatabaseConnection::TABLE_PANELS . " (name, a, aaaa, apikey, self) $self = $panel->getSelf();
VALUES (:name, :a, :aaaa, :apikey, :self)";
$sql = "
try { INSERT INTO " . DatabaseConnection::TABLE_PANELS . " (name, a, aaaa, apikey, apikey_prefix, self)
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); VALUES (:name, :a, :aaaa, :apikey, :prefix, :self)";
$statement->bindParam(param: ':name', var: $name);
$statement->bindParam(param: ':a', var: $a); try {
$statement->bindParam(param: ':aaaa', var: $aaaa); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':apikey', var: $apikey); $statement->bindParam(param: ':name', var: $name);
$statement->bindParam(param: ':self', var: $self); $statement->bindParam(param: ':a', var: $a);
$statement->execute(); $statement->bindParam(param: ':aaaa', var: $aaaa);
$statement->bindParam(param: ':apikey', var: $apikey);
return $this->databaseConnection->getConnection()->lastInsertId(); $statement->bindParam(param: ':prefix', var: $apikeyPrefix);
} catch (PDOException $e) { $statement->bindParam(param: ':self', var: $self);
exit($e->getMessage()); $statement->execute();
}
} return intval(value: $this->databaseConnection->getConnection()->lastInsertId());
} catch (PDOException $e) {
exit($e->getMessage());
/** }
* @param Int $id }
* @param String $name
* @param String $a
* @param String $aaaa /**
* @param String $apikey * @param Panel $panel
* * @return int|null
* @return false|int */
*/ public function update(Panel $panel): ?int
public function update(int $id, string $name, string $a, string $aaaa, String $apikey, int $self): bool|int {
{ $id = $panel->getId();
$current = $this->findByID(id: $id); $name = $panel->getName();
$a = $panel->getA();
if (empty($name)) { $aaaa = $panel->getAaaa();
$name = $current->getName(); $apikey = $panel->getApikey();
} $apikeyPrefix = $panel->getApikeyPrefix();
if (empty($a)) { $passphrase = $panel->getPassphrase();
$a = $current->getA();
} $current = $this->findByID(id: $id);
if (empty($aaaa)) {
$aaaa = $current->getAaaa();
} if (empty($name)) {
if (empty($apikey)) { $name = $current->getName();
$apikey = $current->getApikey(); }
} if (empty($a)) {
$a = $current->getA();
if (empty($self)) { }
echo "self is empty"; if (empty($aaaa)) {
$self = $current->getSelf(); $aaaa = $current->getAaaa();
} else { }
if ($self == -1) {
$self = 0; if (empty($passphrase)) {
} $apikey = $current->getApikey();
} $apikeyPrefix = $current->getApikeyPrefix();
}
$sql = "
if (empty($self)) {
$self = $current->getSelf();
}
$sql = "
UPDATE " . DatabaseConnection::TABLE_PANELS . " SET UPDATE " . DatabaseConnection::TABLE_PANELS . " SET
name = :name, name = :name,
a = :a, a = :a,
aaaa = :aaaa, aaaa = :aaaa,
apikey = :apikey, apikey = :apikey,
apikey_prefix = :apikey_prefix,
self = :self self = :self
WHERE id = :id"; WHERE id = :id";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: 'id', var: $id); $statement->bindParam(param: 'id', var: $id);
$statement->bindParam(param: 'name', var: $name); $statement->bindParam(param: 'name', var: $name);
$statement->bindParam(param: 'a', var: $a); $statement->bindParam(param: 'a', var: $a);
$statement->bindParam(param: 'aaaa', var: $aaaa); $statement->bindParam(param: 'aaaa', var: $aaaa);
$statement->bindParam(param: 'apikey', var: $apikey); $statement->bindParam(param: 'apikey', var: $apikey);
$statement->bindParam(param: 'self', var: $self); $statement->bindParam(param: 'apikey_prefix', var: $apikeyPrefix);
$statement->execute(); $statement->bindParam(param: 'self', var: $self);
$statement->execute();
return $statement->rowCount();
} catch (PDOException $e) { return intval(value: $statement->rowCount());
echo $e->getMessage(); } catch (PDOException $e) {
return false; echo $e->getMessage();
} return null;
} }
}
/**
* @param $id /**
* * @param $id
* @return int *
*/ * @return int|null
public function delete($id): int */
{ public function delete($id): ?int
$sql = " {
$sql = "
DELETE FROM " . DatabaseConnection::TABLE_PANELS . " DELETE FROM " . DatabaseConnection::TABLE_PANELS . "
WHERE id = :id"; WHERE id = :id";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: 'id', var: $id); $statement->bindParam(param: 'id', var: $id);
$statement->execute(); $statement->execute();
return $statement->rowCount(); return intval(value: $statement->rowCount());
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
} }
/** /**
* @param String $field * @param String $field
* *
* @return int * @return int|null
*/ */
public function getLongestEntry(String $field): int public function getLongestEntry(string $field): ?int
{ {
$sql = " $sql = "
SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_PANELS; SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_PANELS;
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute(); $statement->execute();
$result = $statement->fetch(); $result = $statement->fetch();
return $result['length']; return $result['length'];
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
} }
} }