2440 lines
87 KiB
PHP
2440 lines
87 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace App\Controller;
|
|
|
|
error_reporting(error_level: E_ALL);
|
|
|
|
use App\Controller\Commands\Command;
|
|
use App\Controller\Commands\CommandGroup;
|
|
use App\Controller\Commands\CommandGroupContainer;
|
|
use App\Entity\Apikey;
|
|
use App\Entity\Domain;
|
|
use App\Entity\DynDNS;
|
|
use App\Entity\KeyHelp\KeyHelpDomain;
|
|
use App\Entity\Nameserver;
|
|
use App\Entity\Panel;
|
|
use App\Repository\ApikeyRepository;
|
|
use App\Repository\DomainRepository;
|
|
use App\Repository\DynDNSRepository;
|
|
use App\Repository\NameserverRepository;
|
|
use App\Repository\PanelRepository;
|
|
use App\Repository\SettingsRepository;
|
|
use App\Service\ApiClient;
|
|
use App\Utilities\Colors;
|
|
use Arubacao\TldChecker\Validator;
|
|
use Exception;
|
|
use JsonMapper;
|
|
use JsonMapper_Exception;
|
|
use LucidFrame\Console\ConsoleTable;
|
|
use Odan\Migration\Command\GenerateCommand;
|
|
use Phinx\Console\PhinxApplication;
|
|
use SodiumException;
|
|
use Symfony\Component\Console\Input\ArrayInput;
|
|
use Symfony\Component\Console\Output\ConsoleOutput;
|
|
|
|
if (php_sapi_name() !== 'cli') {
|
|
exit;
|
|
}
|
|
|
|
|
|
class CLIController
|
|
{
|
|
private array $arguments;
|
|
private CommandGroupContainer $commandGroupContainer;
|
|
private string $baseDir;
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function __construct(
|
|
private readonly ApiClient $apiClient,
|
|
private readonly ApikeyRepository $apikeyRepository,
|
|
private readonly DomainController $domainController,
|
|
private readonly DomainRepository $domainRepository,
|
|
private readonly DynDNSRepository $dynDNSRepository,
|
|
private readonly NameserverRepository $nameserverRepository,
|
|
private readonly PanelRepository $panelRepository,
|
|
private readonly ConfigController $configController,
|
|
private readonly EncryptionController $encryptionController,
|
|
private readonly SettingsRepository $settingsRepository,
|
|
private $logger,
|
|
private readonly bool $quiet
|
|
)
|
|
{
|
|
$env = $this->configController->getConfig(configKey: 'env');
|
|
$devEnvs = ['dev', 'test'];
|
|
|
|
$this->baseDir = dirname(path: __DIR__, levels: 2) . '/';
|
|
|
|
$apikeyGroup = (new CommandGroup(name: 'apikeys', description: 'API keys to access this bindAPI'))
|
|
->addCommand(command: new Command(
|
|
name: 'list',
|
|
callback: function () {
|
|
$this->apikeysList();
|
|
})
|
|
)
|
|
->addCommand(command: new Command(
|
|
name: 'create',
|
|
callback: function () {
|
|
$this->apikeysCreate();
|
|
},
|
|
mandatoryParameters: ['name'])
|
|
)
|
|
->addCommand(command: new Command(
|
|
name: 'update',
|
|
callback: function () {
|
|
$this->apikeysUpdate();
|
|
},
|
|
mandatoryParameters: ['ID',],
|
|
optionalParameters: ['name=<name>']))
|
|
->addCommand(command: new Command(
|
|
name: 'delete',
|
|
callback: function () {
|
|
$this->apikeysDelete();
|
|
},
|
|
mandatoryParameters: ['ID']
|
|
));
|
|
$cronGroup = (new CommandGroup(name: 'cron', description: 'Run zone file maintenance'))
|
|
->addCommand(command: new Command(
|
|
name: 'run',
|
|
callback: function () {
|
|
$this->cronRun();
|
|
}));
|
|
$panelsGroup = (new CommandGroup(name: 'panels', description: 'all KeyHelp systems configured'))
|
|
->addCommand(command: new Command(
|
|
name: 'list',
|
|
callback: function () {
|
|
$this->panelsList();
|
|
}))
|
|
->addCommand(command: new Command(
|
|
name: 'create',
|
|
callback: function () {
|
|
$this->panelsCreate();
|
|
},
|
|
mandatoryParameters: ['name'],
|
|
optionalParameters: ['A=<IPv4>', 'AAAA=<IPv6>', 'apikey=<API-Key>', 'self=<yes|no>']))
|
|
->addCommand(command: new Command(
|
|
name: 'update',
|
|
callback: function () {
|
|
$this->panelsUpdate();
|
|
},
|
|
mandatoryParameters: ['ID'],
|
|
optionalParameters: ['name=<name>', 'A=<IPv4>', 'AAAA=<IPv6>', 'apikey=<API-Key>', 'self=<yes|no>']))
|
|
->addCommand(command: new Command(
|
|
name: 'delete',
|
|
callback: function () {
|
|
$this->panelsDelete();
|
|
},
|
|
mandatoryParameters: ['ID']))
|
|
->addCommand(command: new Command(
|
|
name: 'apiping',
|
|
callback: function () {
|
|
$this->apiPing(type: 'panel');
|
|
},
|
|
optionalParameters: ['ID']));
|
|
$domainsGroup = (new CommandGroup(name: 'domains', description: 'configured domains'))
|
|
->addCommand(command: new Command(
|
|
name: 'list',
|
|
callback: function () {
|
|
$this->domainsList();
|
|
}))
|
|
->addCommand(command: new Command(
|
|
name: 'update',
|
|
callback: function () {
|
|
$this->domainsUpdate();
|
|
},
|
|
// mandatoryParameters: ['name'],
|
|
// optionalParameters: ['A=<IPv4>', 'AAAA=<IPv6>', 'apikey=<API-Key>'])))
|
|
description: 'Update zone files'));
|
|
$nameserversGroup = (new CommandGroup(name: 'nameservers', description: 'available nameservers'))
|
|
->addCommand(command: new Command(
|
|
name: 'list',
|
|
callback: function () {
|
|
$this->nameserversList();
|
|
}))
|
|
->addCommand(command: new Command(
|
|
name: 'create',
|
|
callback: function () {
|
|
$this->nameserversCreate();
|
|
},
|
|
mandatoryParameters: ['name'],
|
|
optionalParameters: ['A=<IPv4>', 'AAAA=<IPv6>', 'apikey=<API-Key>']))
|
|
->addCommand(command: new Command(
|
|
name: 'update',
|
|
callback: function () {
|
|
$this->nameserversUpdate();
|
|
},
|
|
mandatoryParameters: ['ID'],
|
|
optionalParameters: ['name=<name>', 'A=<IPv4>', 'AAAA=<IPv6>', 'apikey=<API-Key>']))
|
|
->addCommand(command: new Command(
|
|
name: 'delete',
|
|
callback: function () {
|
|
$this->nameserversDelete();
|
|
},
|
|
mandatoryParameters: ['ID']))
|
|
->addCommand(command: new Command(
|
|
name: 'apiping',
|
|
callback: function () {
|
|
$this->apiPing(type: 'nameserver');
|
|
},
|
|
optionalParameters: ['ID']));
|
|
$dyndnsGroup = (new CommandGroup(name: 'dyndns', description: 'handle DynDNS entries'))
|
|
->addCommand(command: new Command(
|
|
name: 'list',
|
|
callback: function () {
|
|
$this->dynDnsList();
|
|
}))
|
|
->addCommand(command: new Command(
|
|
name: 'create',
|
|
callback: function () {
|
|
$this->dynDnsCreate();
|
|
},
|
|
mandatoryParameters: ['hostname.example.com', 'password'],
|
|
optionalParameters: ['A=<IPv4>', 'AAAA=<IPv6>'],
|
|
description: 'FQDN within a domain where this server is master'))
|
|
->addCommand(command: new Command(
|
|
name: 'update',
|
|
callback: function () {
|
|
$this->dynDnyUpdate();
|
|
},
|
|
mandatoryParameters: ['hostname.example.com',],
|
|
optionalParameters: ['password=<password>', 'A=<IPv4>', 'AAAA=<IPv6>']))
|
|
->addCommand(command: new Command(
|
|
name: 'delete',
|
|
callback: function () {
|
|
$this->dynDnsDelete();
|
|
},
|
|
mandatoryParameters: ['ID']));
|
|
$checkGroup = (new CommandGroup(name: 'check', description: 'health checks the system can perform'))
|
|
->addCommand(command: new Command(
|
|
name: 'cache',
|
|
callback: function () {
|
|
$this->checkCache();
|
|
},
|
|
description: 'Clear the opcache. Note: It doesn\'t call composer clear-cache.'))
|
|
->addCommand(command: new Command(
|
|
name: 'permissions',
|
|
callback: function () {
|
|
$this->checkPermissions();
|
|
},
|
|
description: 'check file permissions'))
|
|
->addCommand(command: new Command(
|
|
name: 'panel',
|
|
callback: function () {
|
|
$this->checkPanel();
|
|
},
|
|
description: 'Validate setting for this panel'))
|
|
->addCommand(command: new Command(
|
|
name: 'panels',
|
|
callback: function () {
|
|
$this->checkPanels();
|
|
},
|
|
optionalParameters: ['ID', 'fix=xes']))
|
|
->addCommand(command: new Command(
|
|
name: 'nameserver',
|
|
callback: function () {
|
|
$this->checkNameserver();
|
|
},
|
|
description: 'Validate setting for this panel'))
|
|
->addCommand(command: new Command(
|
|
name: 'nameservers',
|
|
callback: function () {
|
|
$this->checkNameservers();
|
|
},
|
|
description: 'Check version on nameservers'))
|
|
->addCommand(command: new Command(
|
|
name: 'bootstrap',
|
|
callback: function () {
|
|
$this->checkBootstrap();
|
|
},
|
|
description: 'Check version on nameservers'))
|
|
->addCommand(command: new Command(
|
|
name: 'domains',
|
|
callback: function () {
|
|
$this->checkDomains();
|
|
}))
|
|
->addCommand(command: new Command(
|
|
name: 'mail',
|
|
callback: function () {
|
|
$this->checkMail();
|
|
}))
|
|
->addCommand(command: new Command(
|
|
name: 'showincludes',
|
|
callback: function () {
|
|
$this->checkShowIncludes();
|
|
},
|
|
description: 'Shows needed setting on panels'))
|
|
->addCommand(command: new Command(
|
|
name: 'generatekey',
|
|
callback: function () {
|
|
$this->checkGenerateKey();
|
|
},
|
|
description: 'Generates a a new key for encryption'))
|
|
->addCommand(command: new Command(
|
|
name: 'setup',
|
|
callback: function () {
|
|
$this->checkSetup();
|
|
},
|
|
mandatoryParameters: ['username'],
|
|
description: 'Adapt filesystem permissions (requires elaborated permissions)'));
|
|
|
|
if (in_array(needle: $env, haystack: $devEnvs)) {
|
|
$checkVersionCommand = new Command(
|
|
name: 'version',
|
|
callback: function () {
|
|
$this->checkVersion();
|
|
},
|
|
description: 'Sync the versions from composer to the database');
|
|
$checkGroup->addCommand(command: $checkVersionCommand);
|
|
}
|
|
|
|
$migrationsGroup = (new CommandGroup(name: 'migrations', description: 'maintain database migrations'))
|
|
->addCommand(command: new Command(
|
|
name: 'status',
|
|
callback: function () {
|
|
$this->migrationsStatus();
|
|
},
|
|
description: 'List information about migrations'
|
|
))
|
|
->addCommand(command: new Command(
|
|
name: 'migrate',
|
|
callback: function () {
|
|
$this->migrationsMigrate();
|
|
},
|
|
description: 'Apply a new migration file'
|
|
));
|
|
|
|
|
|
if (in_array(needle: $env, haystack: $devEnvs)) {
|
|
$makeMigrationsCommand = new Command(
|
|
name: 'make',
|
|
callback: function () {
|
|
$this->migrationsMake();
|
|
},
|
|
description: 'Build a new migration file'
|
|
);
|
|
$migrationsGroup->addCommand(command: $makeMigrationsCommand);
|
|
}
|
|
|
|
$this->commandGroupContainer = (new CommandGroupContainer())
|
|
->addCommandGroup(commandGroup: $apikeyGroup)
|
|
->addCommandGroup($cronGroup)
|
|
->addCommandGroup($panelsGroup)
|
|
->addCommandGroup($domainsGroup)
|
|
->addCommandGroup($nameserversGroup)
|
|
->addCommandGroup($dyndnsGroup)
|
|
->addCommandGroup($checkGroup)
|
|
->addCommandGroup($migrationsGroup);
|
|
|
|
|
|
// ->addCommandGroup(commandGroup: (new CommandGroup(name: 'webmail', description: 'manage webmail setup'))
|
|
// ->addCommand(command: new Command(
|
|
// name: 'check',
|
|
// callback: function () {
|
|
// $this->webmailCheck();
|
|
// },
|
|
// mandatoryParameters: ['example.com']
|
|
// ))
|
|
// ->addCommand(command: new Command(
|
|
// name: 'create',
|
|
// callback: function () {
|
|
// $this->webmailCreate();
|
|
// },
|
|
// mandatoryParameters: ['example.com']))
|
|
// ->addCommand(command: new Command(
|
|
// name: 'delete',
|
|
// callback: function () {
|
|
// $this->webmailDelete();
|
|
// },
|
|
// mandatoryParameters: ['ID'])));
|
|
}
|
|
|
|
|
|
function runCheckSetup(): void
|
|
{
|
|
if (!$this->domainController->checkPermissions()) {
|
|
echo Colors::RED . 'You need to setup the bindAPI permission first.' . Colors::DEFAULT . PHP_EOL;
|
|
echo 'Run ' . Colors::YELLOW . './bin/console check:setup' . Colors::DEFAULT . ' as root or with sudo.' . PHP_EOL;
|
|
}
|
|
}
|
|
|
|
|
|
function runCommand(array $arguments): void
|
|
{
|
|
if (count($arguments) < 1) {
|
|
$this->showUsage();
|
|
exit(0);
|
|
}
|
|
|
|
$this->logger->debug(message: "runCommand()");
|
|
|
|
|
|
if (str_contains(haystack: $arguments[0], needle: ':')) {
|
|
[$command, $subcommand] = explode(separator: ':', string: $arguments[0]);
|
|
} else {
|
|
$command = $arguments[0];
|
|
$subcommand = '';
|
|
}
|
|
|
|
$this->arguments = $this->parseArguments(arguments: $arguments);
|
|
$this->commandGroupContainer->run(command: $command, subcommand: $subcommand);
|
|
}
|
|
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
function showUsage(): void
|
|
{
|
|
$this->logger->debug(message: "showUsage()");
|
|
|
|
$debug = $this->configController->getConfig(configKey: 'debug');
|
|
|
|
echo Colors::YELLOW . 'Usage:' . PHP_EOL;
|
|
echo Colors::DEFAULT . "\t./bin/console {options} {arguments}" . PHP_EOL . PHP_EOL;
|
|
|
|
echo Colors::YELLOW . 'Options:' . PHP_EOL;
|
|
echo Colors::GREEN . "\t-v, --version\t\t" . Colors::DEFAULT . "Display the version of the API" . PHP_EOL;
|
|
echo Colors::GREEN . "\t-q, --quiet\t\t" . Colors::DEFAULT . "No output to stdout, for cronjobs" . PHP_EOL . PHP_EOL;
|
|
|
|
echo Colors::YELLOW . 'Arguments: ' . Colors::WHITE . '<mandatory> {optional}' . PHP_EOL;
|
|
|
|
$this->commandGroupContainer->printCommands();
|
|
|
|
echo PHP_EOL . "\033[39me.g. ./bin/console apikeys:list" . PHP_EOL;
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
public function checkPermissions(): void
|
|
{
|
|
$this->logger->debug(message: "checkPermissions()");
|
|
|
|
if (!$this->domainController->checkPermissions()) {
|
|
if (!$this->quiet) {
|
|
echo PHP_EOL . Colors::DEFAULT;
|
|
echo 'Missing permissions, please run ' . Colors::YELLOW . './bin/console check:setup' . Colors::DEFAULT . ' as root or with sudo.' . PHP_EOL;
|
|
}
|
|
exit(1);
|
|
} else {
|
|
exit(0);
|
|
}
|
|
}
|
|
|
|
|
|
function checkSetup(): void
|
|
{
|
|
if (empty($this->arguments[1])) {
|
|
echo 'You need to supply a username.' . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
$uid = posix_getuid();
|
|
if ($uid != 0) {
|
|
echo 'You need to run this as root or with sudo.' . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
$username = $this->arguments[1];
|
|
if (!$impersonatedUser = posix_getpwnam(username: $username)) {
|
|
echo 'Cannot find user: ' . $username . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
$impersonatedUserId = $impersonatedUser['uid'];
|
|
|
|
// add user to group bind
|
|
exec(command: "adduser $username bind", output: $output, result_code: $return);
|
|
if ($return != 0) {
|
|
echo 'Could not add user to bind group.' . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
|
|
// for /etc/bind/local.zones file
|
|
if (!file_exists(filename: $this->domainController->localZoneFile)) {
|
|
echo 'Could not find ' . Colors::YELLOW . $this->domainController->localZoneFile . Colors::DEFAULT . '.' . PHP_EOL;
|
|
echo 'Creating …';
|
|
touch(filename: $this->domainController->localZoneFile);
|
|
if (!file_exists(filename: $this->domainController->localZoneFile)) {
|
|
echo 'Could not create ' . $this->domainController->localZoneFile . '.' . PHP_EOL;
|
|
exit(1);
|
|
} else {
|
|
echo ' done.' . PHP_EOL;
|
|
echo 'Setting owner …';
|
|
if (chown(filename: $this->domainController->localZoneFile, user: 'bind')) {
|
|
echo " done." . PHP_EOL;
|
|
}
|
|
echo 'Setting permissions …';
|
|
if (chmod(filename: $this->domainController->localZoneFile, permissions: 0664)) {
|
|
echo " done." . PHP_EOL;
|
|
}
|
|
}
|
|
} else {
|
|
// check /etc/bind/local.zones permissions
|
|
echo 'Found ' . Colors::YELLOW . $this->domainController->localZoneFile . Colors::DEFAULT . '.' . PHP_EOL;
|
|
|
|
echo 'Check permissions for ' . Colors::YELLOW . $this->domainController->localZoneFile . Colors::DEFAULT . ' …';
|
|
|
|
// dont repeat yourself, use check from DomainController FIXME
|
|
$this->domainController->checkPermissions(impersonatedUserId: $impersonatedUserId);
|
|
}
|
|
|
|
// /etc/bind/local.zones file must be included in /etc/bind/named.conf.local
|
|
$namedConfLocal = file_get_contents(filename: $this->domainController->namedConfLocalFile);
|
|
if (str_contains(haystack: $namedConfLocal, needle: $this->domainController->localZoneFile)) {
|
|
echo 'Found ' . Colors::YELLOW . $this->domainController->localZoneFile . Colors::DEFAULT . ' included ' . Colors::YELLOW . $this->domainController->namedConfLocalFile . Colors::DEFAULT . '.' . PHP_EOL;
|
|
} else {
|
|
echo 'Could not find ' . Colors::YELLOW . $this->domainController->localZoneFile . Colors::DEFAULT . ' in ' . Colors::YELLOW . $this->domainController->namedConfLocalFile . Colors::DEFAULT . '.' . PHP_EOL;
|
|
echo 'Adding …';
|
|
$namedConfLocal .= PHP_EOL . 'include "' . $this->domainController->localZoneFile . '";' . PHP_EOL;
|
|
file_put_contents(filename: $this->domainController->namedConfLocalFile, data: $namedConfLocal);
|
|
if (str_contains(haystack: $namedConfLocal, needle: $this->domainController->localZoneFile)) {
|
|
echo ' done.' . PHP_EOL;
|
|
} else {
|
|
echo 'Could not add ' . Colors::YELLOW . $this->domainController->localZoneFile . Colors::DEFAULT . ' to ' . Colors::YELLOW . $this->domainController->namedConfLocalFile . Colors::DEFAULT . '.' . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
// check /etc/bind/zones exists
|
|
echo 'Check for ' . Colors::YELLOW . $this->domainController->localZonesDir . Colors::DEFAULT . ' …';
|
|
if (is_dir(filename: $this->domainController->localZonesDir)) {
|
|
echo " exists." . PHP_EOL;
|
|
} else {
|
|
echo ' Could not find ' . Colors::YELLOW . $this->domainController->localZonesDir . Colors::DEFAULT . '.' . PHP_EOL;
|
|
echo 'Creating …';
|
|
mkdir(directory: $this->domainController->localZonesDir, permissions: 0775, recursive: true);
|
|
echo ' done.' . PHP_EOL;
|
|
echo 'Setting owner …';
|
|
if (chown(filename: $this->domainController->localZonesDir, user: 'bind')) {
|
|
echo " done." . PHP_EOL;
|
|
}
|
|
echo 'Setting permissions …';
|
|
if (chmod(filename: $this->domainController->localZonesDir, permissions: 0774)) {
|
|
echo " done." . PHP_EOL;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
function checkPanels(): void
|
|
{
|
|
$this->logger->debug(message: "checkPanels()");
|
|
|
|
$id = intval(value: $this->arguments[1] ?? 0);
|
|
|
|
if ($id != 0) {
|
|
if ($panel = $this->panelRepository->findByID(id: $id)) {
|
|
$this->checkSinglePanel(panel: $panel);
|
|
} else {
|
|
echo "Unknown panel ID $id" . PHP_EOL;
|
|
}
|
|
} else {
|
|
if (!$this->quiet) {
|
|
echo "check all …" . PHP_EOL;
|
|
}
|
|
$panels = $this->panelRepository->findAll();
|
|
foreach ($panels as $panel) {
|
|
$this->checkSinglePanel(panel: $panel);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @param Panel $panel
|
|
*
|
|
* @return void
|
|
*/
|
|
public
|
|
function checkSinglePanel(Panel $panel): void
|
|
{
|
|
$this->logger->debug(message: "checkSinglePanel()");
|
|
|
|
if (!$this->quiet) {
|
|
echo Colors::DEFAULT . 'KeyHelp-Panel: ' . Colors::YELLOW . $panel->getName() . Colors::DEFAULT;
|
|
}
|
|
|
|
$encryptionKey = $this->configController->getConfig(configKey: 'encryptionKey');
|
|
$decryptedKey = $this->encryptionController->safeDecrypt(encrypted: $panel->getApikey(), key: $encryptionKey);
|
|
|
|
$f = $panel->getA();
|
|
if (!$this->quiet) {
|
|
echo Colors::DEFAULT . ' IPv4: ' . Colors::YELLOW . $f . Colors::DEFAULT;
|
|
}
|
|
|
|
if (!empty($panel->getA())) {
|
|
$panelRequest = $this->apiClient->sendCommand(
|
|
requestType: 'GET',
|
|
serverName: $panel->getName(),
|
|
versionIP: 4,
|
|
apiKey: $decryptedKey,
|
|
command: '/server',
|
|
serverType: 'panel');
|
|
} else {
|
|
$panelRequest = $this->apiClient->sendCommand(
|
|
requestType: 'GET',
|
|
serverName: $panel->getName(),
|
|
versionIP: 6,
|
|
apiKey: $decryptedKey,
|
|
command: '/server',
|
|
serverType: 'panel');
|
|
}
|
|
$panelData = json_decode(json: $panelRequest['data']);
|
|
if (!empty($panelData)) {
|
|
$panelVersion = $panelData->meta->panel_version;
|
|
$responseTime = sprintf("%0.3f", $panelRequest['responseTime']);
|
|
} else {
|
|
$panelVersion = 'n/a';
|
|
$responseTime = 'n/a';
|
|
}
|
|
if (!$this->quiet) {
|
|
echo Colors::DEFAULT . ' KeyHelp version: ' . $panelVersion . " ($responseTime seconds)" . PHP_EOL;
|
|
}
|
|
|
|
if (empty($panel->getA())) {
|
|
$result = $this->apiClient->sendCommand(
|
|
requestType: 'GET',
|
|
serverName: $panel->getName(),
|
|
versionIP: 6,
|
|
apiKey: $decryptedKey,
|
|
command: 'domains?sort=domain&subdomains=false',
|
|
serverType: 'panel'
|
|
);
|
|
} else {
|
|
$result = $this->apiClient->sendCommand(
|
|
requestType: 'GET',
|
|
serverName: $panel->getName(),
|
|
versionIP: 4,
|
|
apiKey: $decryptedKey,
|
|
command: 'domains?sort=domain&subdomains=false',
|
|
serverType: 'panel');
|
|
}
|
|
|
|
if (!empty($result['error'])) {
|
|
if (!$this->quiet) {
|
|
echo $result['data'] . PHP_EOL;
|
|
}
|
|
exit(1);
|
|
}
|
|
if (!empty($result['data'])) {
|
|
$domains = json_decode(json: $result['data']);
|
|
} else {
|
|
if (!$this->quiet) {
|
|
echo 'No domains found' . PHP_EOL;
|
|
}
|
|
exit(1);
|
|
}
|
|
|
|
$maxDomainNameLength = 0;
|
|
$tmpDomainList = [];
|
|
|
|
$mapper = new JsonMapper();
|
|
|
|
if (count($domains) > 0) {
|
|
foreach ($domains as $domain) {
|
|
$mapper->bExceptionOnUndefinedProperty = true;
|
|
$mapper->bStrictNullTypes = false;
|
|
try {
|
|
$domainObject = $mapper->map(json: $domain, object: new KeyHelpDomain());
|
|
} catch (JsonMapper_Exception $e) {
|
|
exit($e->getMessage() . PHP_EOL);
|
|
}
|
|
$tmpDomainList[] = $domainObject;
|
|
if (strlen(string: $domain->domain) > $maxDomainNameLength) {
|
|
$maxDomainNameLength = strlen(string: $domain->domain);
|
|
}
|
|
}
|
|
}
|
|
|
|
$domainCount = 0;
|
|
foreach ($tmpDomainList as $domain) {
|
|
if (!$this->quiet) {
|
|
echo Colors::DEFAULT . " Domain: " . Colors::YELLOW . str_pad(string: $domain->getDomain(), length: $maxDomainNameLength);
|
|
}
|
|
|
|
if (!$domain->isSubdomain()) {
|
|
$this->checkNS(domainName: $domain->getDomain(), panel: $panel);
|
|
$domainCount++;
|
|
}
|
|
}
|
|
|
|
if ($domainCount == 0) {
|
|
if (!$this->quiet) {
|
|
echo 'No second level domains found.' . Colors::DEFAULT . PHP_EOL;
|
|
}
|
|
}
|
|
if (!$this->quiet) {
|
|
echo PHP_EOL;
|
|
}
|
|
|
|
try {
|
|
sodium_memzero(string: $decryptedKey);
|
|
} catch (SodiumException $e) {
|
|
exit($e->getMessage() . PHP_EOL);
|
|
}
|
|
}
|
|
|
|
function isSubDomain(Domain $domain): bool
|
|
{
|
|
$this->logger->debug(message: "isSubDomain()");
|
|
|
|
// valid second level domain
|
|
if (!Validator::endsWithTld(value: $domain)) {
|
|
return false;
|
|
}
|
|
|
|
$domainParts = explode(separator: '.', string: $domain->getName());
|
|
$reversedParts = array_reverse(array: $domainParts);
|
|
$testDomain = '';
|
|
$foundDomain = '';
|
|
foreach ($reversedParts as $part) {
|
|
if ($testDomain) {
|
|
$testDomain = $part . '.' . $testDomain;
|
|
} else {
|
|
$testDomain = $part;
|
|
}
|
|
if ($this->domainRepository->findByName(name: $testDomain)) {
|
|
$foundDomain = $testDomain;
|
|
echo $part . PHP_EOL;
|
|
}
|
|
}
|
|
|
|
echo "found domain ***" . $foundDomain . PHP_EOL;
|
|
|
|
/*
|
|
// system domain
|
|
if (str_contains(haystack: $domainName, needle: $panel)) {
|
|
return false;
|
|
}
|
|
|
|
|
|
// no second level domain
|
|
if (substr_count(haystack: $domainName, needle: '.') > 1) {
|
|
return false;
|
|
}
|
|
*/
|
|
return true;
|
|
}
|
|
|
|
|
|
function isValidSecondLevelDomain(string $domainName, string $panel, int $parent): bool
|
|
{
|
|
$this->logger->debug(message: "isValidSecondLevelDomain()");
|
|
|
|
// subdomain
|
|
if ($parent != 0) {
|
|
return false;
|
|
}
|
|
|
|
// system domain
|
|
if (str_contains(haystack: $domainName, needle: $panel)) {
|
|
return false;
|
|
}
|
|
|
|
// valid second level domain
|
|
if (!Validator::endsWithTld(value: $domainName)) {
|
|
return false;
|
|
}
|
|
|
|
// no second level domain
|
|
if (substr_count(haystack: $domainName, needle: '.') > 1) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param String $domainName
|
|
* @param Panel $panel
|
|
*
|
|
* @return void
|
|
*/
|
|
function checkNS(string $domainName, Panel $panel): void
|
|
{
|
|
$this->logger->debug(message: "checkNS()");
|
|
|
|
$nameservers = $this->nameserverRepository->findAll();
|
|
foreach ($nameservers as $nameserver) {
|
|
|
|
$encryptionKey = $this->configController->getConfig(configKey: 'encryptionKey');
|
|
$decryptedKey = $this->encryptionController->safeDecrypt(encrypted: $nameserver->getApikey(), key: $encryptionKey);
|
|
|
|
if (!$this->quiet) {
|
|
echo Colors::YELLOW . ' ' . $nameserver->getName();
|
|
}
|
|
if (!empty($nameserver->getA())) {
|
|
$result = $this->apiClient->sendCommand(
|
|
requestType: 'GET',
|
|
serverName: $nameserver->getName(),
|
|
versionIP: 4,
|
|
apiKey: $decryptedKey,
|
|
command: 'domains/name/' . $domainName,
|
|
serverType: 'nameserver');
|
|
} else {
|
|
$result = $this->apiClient->sendCommand(
|
|
requestType: 'GET',
|
|
serverName: $nameserver->getName(),
|
|
versionIP: 6,
|
|
apiKey: $decryptedKey(),
|
|
command: 'domains/name/',
|
|
serverType: 'nameserver' . $domainName);
|
|
}
|
|
|
|
switch ($result['header']) {
|
|
case 200:
|
|
if (!$this->quiet) {
|
|
echo Colors::GREEN . ' OK';
|
|
}
|
|
break;
|
|
case 401:
|
|
if (!$this->quiet) {
|
|
echo Color::RED . ' Error: ' . Colors::YELLOW . $result['data'] . Colors::DEFAULT . PHP_EOL;
|
|
}
|
|
exit(1);
|
|
case 404:
|
|
if (!$this->quiet) {
|
|
echo Color::RED . ' ' . $result['header'] . Colors::DEFAULT;
|
|
}
|
|
if (!empty($this->arguments['fix']) && $this->arguments['fix'] == 'yes') {
|
|
if (!$this->quiet) {
|
|
echo ' trying to fix …';
|
|
}
|
|
$body = [
|
|
'name' => $domainName,
|
|
'panel' => $panel->getName(),
|
|
];
|
|
$encryptionKey = $this->configController->getConfig(configKey: 'encryptionKey');
|
|
$decryptedKey = $this->encryptionController->safeDecrypt(encrypted: $nameserver->getApikey(), key: $encryptionKey);
|
|
|
|
if (!empty($nameserver->getAaaa())) {
|
|
$create = $this->apiClient->sendCommand(
|
|
requestType: 'POST',
|
|
serverName: $nameserver->getName(),
|
|
versionIP: 6,
|
|
apiKey: $decryptedKey,
|
|
command: 'domains',
|
|
serverType: 'nameserver',
|
|
body: $body);
|
|
} else {
|
|
$create = $this->apiClient->sendCommand(
|
|
requestType: 'POST',
|
|
serverName: $nameserver->getName(),
|
|
versionIP: 4,
|
|
apiKey: $decryptedKey(),
|
|
command: 'domains',
|
|
serverType: 'nameserver',
|
|
body: $body);
|
|
}
|
|
if ($create['header'] != 201) {
|
|
print_r(value: $create);
|
|
die("make error handling");
|
|
} else {
|
|
if (!$this->quiet) {
|
|
echo Colors::GREEN . 'OK' . Colors::DEFAULT;
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
if (!$this->quiet) {
|
|
echo 'Server error' . PHP_EOL;
|
|
}
|
|
exit(1);
|
|
}
|
|
}
|
|
if (!$this->quiet) {
|
|
echo PHP_EOL;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param array $arguments
|
|
* @return array
|
|
*/
|
|
public
|
|
function parseArguments(array $arguments): array
|
|
{
|
|
$this->logger->debug(message: "parseArguments()");
|
|
|
|
$parsedArguments = [];
|
|
$parseCount = 0;
|
|
foreach ($arguments as $argument) {
|
|
if (str_contains(haystack: $argument, needle: '=')) {
|
|
[$key, $value] = explode(separator: '=', string: $argument);
|
|
$parsedArguments[strtolower(string: $key)] = $value;
|
|
$parsedArguments[$parseCount++] = $value;
|
|
} else {
|
|
$parsedArguments[strtolower(string: $argument)] = $argument;
|
|
$parsedArguments[$parseCount++] = $argument;
|
|
}
|
|
}
|
|
return $parsedArguments;
|
|
}
|
|
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
function panelsCreate(): void
|
|
{
|
|
$this->logger->debug(message: "panelsCreate()");
|
|
|
|
$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);
|
|
}
|
|
|
|
$a = $this->arguments['a'] ?? '';
|
|
$aaaa = $this->arguments['aaaa'] ?? '';
|
|
if (empty($a) && empty($aaaa)) {
|
|
echo 'At least one IP address is required.' . PHP_EOL;
|
|
exit(0);
|
|
}
|
|
$apikey = $this->arguments['apikey'] ?? '';
|
|
$self = $this->arguments['self'] ?? 'no';
|
|
|
|
|
|
if ($this->panelRepository->findByName(name: $name)) {
|
|
echo "Panel: $name already exists." . PHP_EOL;
|
|
exit(1);
|
|
} else {
|
|
$panel = new Panel(name: $name, a: $a, aaaa: $aaaa, passphrase: $apikey, self: $self);
|
|
$result = $this->panelRepository->insert(panel: $panel);
|
|
echo "Panel $name has been created with id $result" . PHP_EOL;
|
|
exit(0);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
function panelsList(): void
|
|
{
|
|
$this->logger->debug(message: "panelsList()");
|
|
|
|
$panels = $this->panelRepository->findAll();
|
|
if (!empty($panels)) {
|
|
echo 'All available panels:' . PHP_EOL;
|
|
$table = new ConsoleTable();
|
|
$table->setHeaders(content: ['ID', 'Name', 'A', 'AAAA', 'API Key (Prefix)', 'This Panel']);
|
|
|
|
foreach ($panels as $panel) {
|
|
$row = [];
|
|
$row[] = $panel->getID();
|
|
$row[] = $panel->getName();
|
|
$row[] = $panel->getA();
|
|
$row[] = $panel->getAaaa();
|
|
$row[] = $panel->getApikeyPrefix();
|
|
$row[] = ucfirst(string: $panel->getSelf());
|
|
$table->addRow(data: $row);
|
|
}
|
|
$table->setPadding(value: 2);
|
|
$table->display();
|
|
} else {
|
|
echo 'No panels found.' . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
exit(0);
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
function panelsUpdate(): void
|
|
{
|
|
$this->logger->debug(message: "panelsUpdate()");
|
|
|
|
$id = intval(value: $this->arguments[1] ?? 0);
|
|
$name = $this->arguments['name'] ?? '';
|
|
$a = $this->arguments['a'] ?? '';
|
|
$aaaa = $this->arguments['aaaa'] ?? '';
|
|
$apikey = $this->arguments['apikey'] ?? '';
|
|
$self = $this->arguments['self'] ?? '';
|
|
|
|
if ($id == 0) {
|
|
if (!$this->quiet) {
|
|
echo 'An ID is required' . PHP_EOL;
|
|
}
|
|
exit(1);
|
|
}
|
|
|
|
if (!$this->panelRepository->findByID(id: $id)) {
|
|
if (!$this->quiet) {
|
|
echo "Panel with ID : $id doesn't exist." . PHP_EOL;
|
|
}
|
|
exit(1);
|
|
}
|
|
|
|
if ($apikey) {
|
|
// sanity check for panel apikey
|
|
if (!str_contains(haystack: $apikey, needle: '.')) {
|
|
$this->exitInvalidPanelApiKey();
|
|
} else {
|
|
[$prefix, $key] = explode(separator: '.', string: $apikey);
|
|
|
|
$prefixLen = strlen(string: $prefix);
|
|
if ($prefixLen != 8) {
|
|
$errorMessage = 'The prefix must be 8 characters long';
|
|
$this->exitInvalidPanelApiKey(details: $errorMessage);
|
|
|
|
}
|
|
|
|
// FIXME check validation state of panel and nameserver keys
|
|
var_dump($prefix, $key);
|
|
echo 'Length of prefix: ' . strlen($prefix) . PHP_EOL;
|
|
echo 'Length of key: ' . strlen($key) . PHP_EOL;
|
|
|
|
die();
|
|
}
|
|
$panel = new Panel(name: $name, id: $id, a: $a, aaaa: $aaaa, passphrase: $apikey, self: $self);
|
|
} else {
|
|
$panel = new Panel(name: $name, id: $id, a: $a, aaaa: $aaaa, self: $self);
|
|
}
|
|
if ($this->panelRepository->update(panel: $panel) !== false) {
|
|
if (!$this->quiet) {
|
|
echo 'Panel ' . Colors::YELLOW . $id . Colors::DEFAULT . ' has been updated' . PHP_EOL;
|
|
}
|
|
} else {
|
|
if (!$this->quiet) {
|
|
echo 'Error while updating domain server.' . PHP_EOL;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
*/
|
|
function panelsDelete(): void
|
|
{
|
|
$this->logger->debug(message: "panelsDelete()");
|
|
|
|
if (empty($this->arguments[1])) {
|
|
echo "You need to supply an ID." . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
|
|
$id = intval(value: $this->arguments[1]) ?? 0;
|
|
if ($id == 0) {
|
|
echo "Panel with ID $id not found." . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
if (!$this->panelRepository->findByID(id: $id)) {
|
|
echo "There is no panel with ID $id." . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
$this->panelRepository->delete(id: $id);
|
|
echo "The panel with ID $id has been deleted." . PHP_EOL;
|
|
}
|
|
|
|
/**
|
|
*/
|
|
function apiPing(string $type): void
|
|
{
|
|
$this->logger->debug(message: "apiPing()");
|
|
|
|
$error = false;
|
|
|
|
$id = $this->getId();
|
|
|
|
if ($id != 0) {
|
|
if ($type == 'panel') {
|
|
$server = $this->panelRepository->findByID(id: $id);
|
|
} else {
|
|
$server = $this->nameserverRepository->findByID(id: $id);
|
|
}
|
|
|
|
if ($server) {
|
|
if (!$this->checkPing(server: $server, type: $type)) {
|
|
$error = true;
|
|
}
|
|
} else {
|
|
if (!$this->quiet) {
|
|
echo "Unknown $type ID $id" . PHP_EOL;
|
|
}
|
|
$error = true;
|
|
}
|
|
} else {
|
|
if ($type == 'panel') {
|
|
$servers = $this->panelRepository->findAll();
|
|
} else {
|
|
$servers = $this->nameserverRepository->findAll();
|
|
}
|
|
if ($servers) {
|
|
foreach ($servers as $server) {
|
|
if (!$this->checkPing(server: $server, type: $type)) {
|
|
$error = true;
|
|
}
|
|
}
|
|
} else {
|
|
echo 'No Servers of type ' . $type . ' defined';
|
|
}
|
|
}
|
|
if ($error) {
|
|
exit(1);
|
|
} else {
|
|
exit(0);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @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;
|
|
}
|
|
|
|
public function checkPing(Panel|Nameserver $server, string $type): bool
|
|
{
|
|
$this->logger->debug(message: "checkPing() - server, type: " . $server->getName() . ', ' . $type);
|
|
$error = false;
|
|
|
|
if ($type == 'nameserver') {
|
|
$maxName = $this->nameserverRepository->getLongestEntry(field: 'name');
|
|
$maxA = $this->nameserverRepository->getLongestEntry(field: 'a');
|
|
$maxAAAA = $this->nameserverRepository->getLongestEntry(field: 'aaaa');
|
|
} else {
|
|
$maxName = $this->panelRepository->getLongestEntry(field: 'name');
|
|
$maxA = $this->panelRepository->getLongestEntry(field: 'a');
|
|
$maxAAAA = $this->panelRepository->getLongestEntry(field: 'aaaa');
|
|
}
|
|
|
|
if (!$this->quiet) {
|
|
echo Colors::YELLOW . str_pad(string: $server->getName(), length: $maxName);
|
|
}
|
|
|
|
$encryptionKey = $this->configController->getConfig(configKey: 'encryptionKey');
|
|
$decryptedKey = $this->encryptionController->safeDecrypt(encrypted: $server->getApikey(), key: $encryptionKey);
|
|
echo 'server: ' . $server->getName() . PHP_EOL;
|
|
|
|
$a = $server->getA() ?? '';
|
|
if (!empty($a)) {
|
|
$this->logger->debug("check a");
|
|
if (!$this->quiet) {
|
|
echo Colors::DEFAULT . ' ' . str_pad(string: $a, length: $maxA, pad_type: STR_PAD_LEFT) . ' ';
|
|
}
|
|
if ($result = $this->apiClient->sendCommand(
|
|
requestType: 'GET',
|
|
serverName: $server->getName(),
|
|
versionIP: 4,
|
|
apiKey: $decryptedKey,
|
|
command: 'ping',
|
|
serverType: $type)) {
|
|
if (!$this->quiet) {
|
|
if ($result['data'] == 'pong') {
|
|
echo ' ' . Colors::GREEN . $result['data'];
|
|
} else {
|
|
echo Colors::BLUE . ' skip' . Colors::DEFAULT;
|
|
if (!$this->quiet) {
|
|
echo ' ' . $result['data'];
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
$error = true;
|
|
}
|
|
}
|
|
$aaaa = $server->getAaaa() ?? '';
|
|
if (!empty($aaaa)) {
|
|
$this->logger->debug("check aaaa");
|
|
if (!$this->quiet) {
|
|
echo Colors::DEFAULT . ' ' . str_pad(string: $aaaa, length: $maxAAAA, pad_type: STR_PAD_LEFT) . ' ';
|
|
}
|
|
if ($result = $this->apiClient->sendCommand(
|
|
requestType: 'GET',
|
|
serverName: $server->getName(),
|
|
versionIP: 6,
|
|
apiKey: $decryptedKey,
|
|
command: 'ping',
|
|
serverType: $type)) {
|
|
if (!$this->quiet) {
|
|
if ($result['data'] == 'pong') {
|
|
echo ' ' . Colors::GREEN . $result['data'];
|
|
} else {
|
|
echo Colors::BLUE . ' xxskip' . Colors::DEFAULT;
|
|
if (!$this->quiet) {
|
|
echo ' ' . $result['data'];
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
$error = true;
|
|
}
|
|
}
|
|
try {
|
|
sodium_memzero(string: $decryptedKey);
|
|
} catch (SodiumException $e) {
|
|
exit($e->getMessage() . PHP_EOL);
|
|
}
|
|
|
|
if (!$this->quiet) {
|
|
echo Colors::DEFAULT . PHP_EOL;
|
|
}
|
|
return !$error;
|
|
}
|
|
|
|
|
|
public function checkNameserverVersion(Nameserver $server): bool
|
|
{
|
|
$this->logger->debug(message: "checkNameServerVersion() - server: " . $server->getName());
|
|
$error = false;
|
|
$skipAAAA = false;
|
|
|
|
$encryptionKey = $this->configController->getConfig(configKey: 'encryptionKey');
|
|
$decryptedKey = $this->encryptionController->safeDecrypt(encrypted: $server->getApikey(), key: $encryptionKey);
|
|
|
|
$a = $server->getA() ?? '';
|
|
if (!empty($a)) {
|
|
$this->logger->debug("check a version");
|
|
if ($result = $this->apiClient->sendCommand(
|
|
requestType: 'GET',
|
|
serverName: $server->getName(),
|
|
versionIP: 4,
|
|
apiKey: $decryptedKey,
|
|
command: 'version',
|
|
serverType: 'nameserver')) {
|
|
if (!$this->quiet) {
|
|
if ($result['error'] === false) {
|
|
$data = $result['data'];
|
|
$decodedData = json_decode(json: $data);
|
|
echo Colors::DEFAULT . ' Version: ' . Colors::YELLOW . $decodedData->version;
|
|
echo Colors::DEFAULT . ' (Build: ' . Colors::YELLOW . $decodedData->buildnumber . Colors::DEFAULT . ')' . PHP_EOL;
|
|
} else {
|
|
echo Color::RED . ' Error: ' . Colors::YELLOW . $result['data'] . Colors::DEFAULT . PHP_EOL;
|
|
}
|
|
// var_dump($result);
|
|
$skipAAAA = true;
|
|
// if ($result['data'] == 'pong') {
|
|
// echo ' ' . Colors::GREEN . $result['data'];
|
|
// } else {
|
|
// echo Colors::BLUE . ' skip' . Colors::DEFAULT;
|
|
// if (!$this->configController->getConfig(configKey: 'quiet')) {
|
|
// echo ' ' . $result['data'];
|
|
// }
|
|
// }
|
|
}
|
|
return true;
|
|
} else {
|
|
$error = true;
|
|
}
|
|
}
|
|
$aaaa = $server->getAaaa() ?? '';
|
|
if (!empty($aaaa) && !$skipAAAA) {
|
|
$this->logger->debug("check aaaa version");
|
|
if ($result = $this->apiClient->sendCommand(
|
|
requestType: 'GET',
|
|
serverName: $server->getName(),
|
|
versionIP: 6,
|
|
apiKey: $decryptedKey,
|
|
command: 'version',
|
|
serverType: 'nameserver')) {
|
|
if (!$this->quiet) {
|
|
echo $result;
|
|
// if ($result['data'] == 'pong') {
|
|
// echo ' ' . Colors::GREEN . $result['data'];
|
|
// } else {
|
|
// echo Colors::BLUE . ' xxskip' . Colors::DEFAULT;
|
|
// if (!$this->configController->getConfig(configKey: 'quiet')) {
|
|
// echo ' ' . $result['data'];
|
|
// }
|
|
// }
|
|
}
|
|
} else {
|
|
$error = true;
|
|
}
|
|
}
|
|
try {
|
|
sodium_memzero(string: $decryptedKey);
|
|
} catch (SodiumException $e) {
|
|
exit($e->getMessage() . PHP_EOL);
|
|
}
|
|
|
|
if (!$this->quiet) {
|
|
echo Colors::DEFAULT . PHP_EOL;
|
|
}
|
|
return !$error;
|
|
}
|
|
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
function apikeysCreate(): void
|
|
{
|
|
$name = $this->arguments[1] ?? '';
|
|
if (empty($name)) {
|
|
echo 'You need to supply a descriptive name for the apikey.' . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
|
|
$apiKeyPrefix = uniqid();
|
|
try {
|
|
$apikeyRand = bin2hex(string: random_bytes(length: 24));
|
|
} catch (Exception $e) {
|
|
exit($e->getMessage() . PHP_EOL);
|
|
}
|
|
|
|
$passphrase = password_hash(password: $apiKeyPrefix . '.' . $apikeyRand, algo: PASSWORD_ARGON2ID);
|
|
|
|
$apikey = new Apikey(name: $name, apikeyPrefix: $apiKeyPrefix, passphrase: $passphrase);
|
|
|
|
$result = $this->apikeyRepository->insert(apikey: $apikey);
|
|
|
|
if ($name) {
|
|
echo 'API key ' . Colors::YELLOW . $name . Colors::DEFAULT;
|
|
} else {
|
|
echo 'Unnamed API key ';
|
|
}
|
|
|
|
echo ' with ID ' . Colors::YELLOW . $result . Colors::DEFAULT . ' has been generated. Store it in a safe place, it cannot be recovered.' . PHP_EOL;
|
|
echo Colors::YELLOW . $apiKeyPrefix . '.' . $apikeyRand . Colors::DEFAULT . PHP_EOL;
|
|
exit(0);
|
|
}
|
|
|
|
function apikeysList(): void
|
|
{
|
|
$keys = $this->apikeyRepository->findAll();
|
|
if (!empty($keys)) {
|
|
$table = new ConsoleTable();
|
|
$table->setHeaders(content: ['ID', 'Name', 'API key prefix', 'Created at']);
|
|
foreach ($keys as $key) {
|
|
$row = [];
|
|
$row[] = $key->getID();
|
|
$row[] = $key->getName();
|
|
$row[] = $key->getApikeyPrefix();
|
|
$row[] = $key->getCreatedAt();
|
|
$table->addRow(data: $row);
|
|
}
|
|
$table->setPadding(value: 2);
|
|
$table->display();
|
|
} else {
|
|
echo 'No keys found.' . PHP_EOL;
|
|
}
|
|
exit(0);
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
function apikeysUpdate(): void
|
|
{
|
|
$id = intval(value: $this->arguments[1]) ?? 0;
|
|
$name = $this->arguments['name'] ?? '';
|
|
|
|
if ($id == 0) {
|
|
echo 'An ID is required' . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
if (!$this->apikeyRepository->findByID(id: intval(value: $id))) {
|
|
echo "Apikeys with ID : $id doesn't exist." . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
if (!$name) {
|
|
echo 'You need tu supply a name.' . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
|
|
$apikey = new Apikey(id: $id, name: $name);
|
|
if ($this->apikeyRepository->update(apikey: $apikey) !== false) {
|
|
echo 'Apikey has been updated' . PHP_EOL;
|
|
} else {
|
|
echo 'Error while updating apikey.' . PHP_EOL;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
function apikeysDelete(): 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->apikeyRepository->findByID(id: $id)) {
|
|
$this->apikeyRepository->delete(id: $id);
|
|
echo 'API key ' . Colors::YELLOW . $id . Colors::DEFAULT . ' has been deleted.' . PHP_EOL;
|
|
exit(0);
|
|
} else {
|
|
echo 'Unknown ID ' . Colors::YELLOW . $id . '.' . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
function domainsList(): void
|
|
{
|
|
$domains = $this->domainRepository->findAll();
|
|
if (!empty($domains)) {
|
|
echo 'All available domains:' . PHP_EOL;
|
|
$table = new ConsoleTable();
|
|
$table->setHeaders(content: ['ID', 'Name', 'Panel', 'Type']);
|
|
/** @var Domain $domain */
|
|
foreach ($domains as $domain) {
|
|
$row = [];
|
|
$row[] = $domain->getId();
|
|
$row[] = $domain->getName();
|
|
$row[] = $domain->getPanel();
|
|
$row[] = $this->domainController->isMasterZone(domain: $domain) ? 'MASTER' : 'SLAVE';
|
|
$table->addRow(data: $row);
|
|
}
|
|
$table->setPadding(value: 2);
|
|
$table->display();
|
|
} else {
|
|
echo 'No domains found.' . PHP_EOL;
|
|
}
|
|
exit(0);
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
function dynDnsList(): void
|
|
{
|
|
$dyndns = $this->dynDNSRepository->findAll();
|
|
if (!empty($dyndns)) {
|
|
echo 'All available DynDNS hosts:' . PHP_EOL;
|
|
$table = new ConsoleTable();
|
|
$table->setHeaders(content: ['ID', 'Name', 'Panel', 'Type']);
|
|
/** @var \App\Entity\DynDNS $dyndnsHost */
|
|
foreach ($dyndns as $dyndnsHost) {
|
|
$row = [];
|
|
$row[] = $dyndnsHost->getId();
|
|
$row[] = $dyndnsHost->getName();
|
|
$table->addRow(data: $row);
|
|
}
|
|
$table->setPadding(value: 2);
|
|
$table->display();
|
|
} else {
|
|
echo 'No DynDNS hosts found.' . PHP_EOL;
|
|
}
|
|
exit(0);
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
function dynDnsCreate(): void
|
|
{
|
|
$name = $this->arguments[1] ?? '';
|
|
if (empty($name)) {
|
|
echo 'You need to supply the FQDN (hostname).' . 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);
|
|
}
|
|
|
|
$password = $this->arguments[2] ?? '';
|
|
|
|
var_dump($this->arguments);
|
|
|
|
die();
|
|
$domainParts = explode(separator: '.', string: $name);
|
|
$reversedParts = array_reverse(array: $domainParts);
|
|
$testDomain = '';
|
|
$foundDomain = '';
|
|
foreach ($reversedParts as $part) {
|
|
if ($testDomain) {
|
|
$testDomain = $part . '.' . $testDomain;
|
|
} else {
|
|
$testDomain = $part;
|
|
}
|
|
if ($this->domainRepository->findByName(name: $testDomain)) {
|
|
$foundDomain = $testDomain;
|
|
echo $part . PHP_EOL;
|
|
}
|
|
}
|
|
if (!$foundDomain) {
|
|
echo 'No matching domain found for this panel.' . PHP_EOL;
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (!$this->quiet) {
|
|
echo "Found domain: " . Colors::YELLOW . $foundDomain . Colors::DEFAULT . PHP_EOL;
|
|
}
|
|
|
|
// get host
|
|
|
|
if ($this->dynDNSRepository->findByName(name: $name)) {
|
|
if (!$this->quiet) {
|
|
echo "DynDNS host " . Colors::YELLOW . $name . Colors::DEFAULT . "already exists." . PHP_EOL;
|
|
exit(0);
|
|
}
|
|
} else {
|
|
if (!$this->quiet) {
|
|
echo "DynDNS host " . Colors::YELLOW . $name . Colors::DEFAULT . "will be created." . PHP_EOL;
|
|
// insert in db
|
|
$dyndnsHost = new DynDNS(name: $name);
|
|
// why is the property set in the cunstructor and afterwards again? FIXME
|
|
$dyndnsHost->setName(name: $name);
|
|
}
|
|
}
|
|
|
|
|
|
// check on NS
|
|
// => add if missing
|
|
|
|
|
|
if ($this->domainRepository->findByName(name: $name)) {
|
|
echo "Domain: $name already exists." . PHP_EOL;
|
|
exit(1);
|
|
} else {
|
|
if (!$this->panelRepository->findByName(name: $panel)) {
|
|
echo 'Unknown panel: ' . Colors::YELLOW . $panel . Colors::DEFAULT . '.' . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
$domain = new Domain(name: $name, panel: $panel);
|
|
$result = $this->domainRepository->insert(domain: $domain);
|
|
echo 'Domain' . Colors::YELLOW . $name . Colors::DEFAULT . ' has been created with id ' . Colors::YELLOW . $result . Colors::DEFAULT . '.' . PHP_EOL;
|
|
$this->domainController->createSlaveZoneFile(domain: $domain);
|
|
exit(0);
|
|
}
|
|
}
|
|
|
|
function domainsCreate(): void
|
|
{
|
|
// check if we're correctly setup
|
|
if (!$this->domainController->checkPermissions()) {
|
|
echo 'You need to setup the bindAPI first.' . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
|
|
$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);
|
|
}
|
|
|
|
$panel = $this->arguments['panel'] ?? '';
|
|
|
|
if (empty($panel)) {
|
|
echo 'You need to supply the panel name.' . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
|
|
if ($this->domainRepository->findByName(name: $name)) {
|
|
echo "Domain: $name already exists." . PHP_EOL;
|
|
exit(1);
|
|
} else {
|
|
if (!$this->panelRepository->findByName(name: $panel)) {
|
|
echo 'Unknown panel: ' . Colors::YELLOW . $panel . Colors::DEFAULT . '.' . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
$domain = new Domain(name: $name, panel: $panel);
|
|
$result = $this->domainRepository->insert(domain: $domain);
|
|
echo 'Domain' . Colors::YELLOW . $name . Colors::DEFAULT . ' has been created with id ' . Colors::YELLOW . $result . Colors::DEFAULT . '.' . PHP_EOL;
|
|
$this->domainController->createSlaveZoneFile(domain: $domain);
|
|
exit(0);
|
|
}
|
|
}
|
|
|
|
/**
|
|
*/
|
|
// function domainsUpdate(): void
|
|
// {
|
|
// // check if we're correctly setup
|
|
// if (!$this->domainController->checkPermissions()) {
|
|
// echo 'You need to setup the bindAPI first.' . PHP_EOL;
|
|
// exit(1);
|
|
// }
|
|
//
|
|
// $id = intval(value: $this->arguments[1] ?? 0);
|
|
// $name = $this->arguments['name'] ?? '';
|
|
// $panelName = $this->arguments['panel'] ?? '';
|
|
//
|
|
// if ($id == 0) {
|
|
// echo 'An ID is required' . PHP_EOL;
|
|
// exit(1);
|
|
// }
|
|
// if (!$domain = $this->domainRepository->findByID(id: $id)) {
|
|
// echo "Domain with ID : $id doesn't exist." . PHP_EOL;
|
|
// exit(1);
|
|
// }
|
|
//
|
|
// if (!empty($panelName)) {
|
|
// $panel = $this->panelRepository->findByName(name: $panelName);
|
|
// }
|
|
//
|
|
// if (empty($name) && empty($panel)) {
|
|
// echo Colors::DEFAULT . 'No name or panel given, just recreate the config file' . PHP_EOL;
|
|
// $this->domainController->updateSlaveZones();
|
|
// exit(1);
|
|
// }
|
|
// $newDomain = new Domain(name: $name, panel: $panelName, id: $domain->getId());
|
|
//
|
|
// if ($this->domainRepository->update(domain: $newDomain) !== false) {
|
|
// echo 'Domain server has been updated' . PHP_EOL;
|
|
// $this->domainController->updateSlaveZones();
|
|
// } else {
|
|
// echo 'Error while updating domain server.' . PHP_EOL;
|
|
// }
|
|
// }
|
|
|
|
|
|
/**
|
|
*/
|
|
function domainsDelete(): void
|
|
{
|
|
if (empty($this->arguments[1])) {
|
|
echo "You need to supply an ID." . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
|
|
$id = intval(value: $this->arguments[1]) ?? 0;
|
|
if ($id == 0) {
|
|
echo "Domain with ID $id not found." . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
if (!$domain = $this->domainRepository->findByID(id: $id)) {
|
|
echo "There is no domain with ID $id." . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
$this->domainRepository->delete(domain: $domain);
|
|
$this->domainController->deleteZone(domain: $domain);
|
|
echo "The domain with ID $id has been deleted." . PHP_EOL;
|
|
}
|
|
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
function nameserversCreate(): 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);
|
|
if (!empty($filteredName) && str_contains(haystack: $filteredName, needle: '.')) {
|
|
$name = $filteredName;
|
|
} else {
|
|
echo "$name is no valid nameserver name." . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
|
|
|
|
$a = $this->arguments['a'] ?? '';
|
|
$aaaa = $this->arguments['aaaa'] ?? '';
|
|
if (empty($a) && empty($aaaa)) {
|
|
echo 'At least one IP address is required.' . PHP_EOL;
|
|
exit(0);
|
|
}
|
|
$apikey = $this->arguments['apikey'] ?? '';
|
|
|
|
if (empty($apikey)) {
|
|
echo 'An API key is required.' . PHP_EOL;
|
|
exit(0);
|
|
}
|
|
|
|
$self = $this->arguments['self'] ?? '';
|
|
|
|
if ($this->nameserverRepository->findByName(name: $name)) {
|
|
echo "Nameserver: $name already exists." . PHP_EOL;
|
|
exit(1);
|
|
} else {
|
|
$nameserver = new Nameserver(name: $name, a: $a, aaaa: $aaaa, passphrase: $apikey, self: $self);
|
|
$result = $this->nameserverRepository->insert(nameserver: $nameserver);
|
|
echo 'Nameserver ' . Colors::YELLOW . $name . Colors::DEFAULT . ' has been created with id ' . Colors::YELLOW . $result . Colors::DEFAULT . PHP_EOL;
|
|
$this->createOpenAPIBootstrap();
|
|
exit(0);
|
|
}
|
|
}
|
|
|
|
|
|
public function createOpenAPIBootstrap(): void
|
|
{
|
|
$basePath = $this->baseDir . '/public/openapi/';
|
|
|
|
$bootStrapFile = $basePath . 'bootstrap.php';
|
|
$allNameservers = $this->nameserverRepository->findAll();
|
|
|
|
$nameservers = [];
|
|
$defaultNS = '';
|
|
|
|
foreach ($allNameservers as $ns) {
|
|
$nameservers[] = $ns->getName();
|
|
if ($ns->getSelf() === 'yes') {
|
|
$defaultNS = $ns->getName();
|
|
}
|
|
}
|
|
|
|
// Improvement in generating file content
|
|
$nameserverList = implode(separator: "', '", array: $nameservers);
|
|
|
|
$currentDBVersion = $this->settingsRepository->findByName(name: 'version');
|
|
if ($dbVersion = json_decode(json: $currentDBVersion)) {
|
|
$versionSting = $dbVersion->version->major . '.' . $dbVersion->version->minor . '.' . $dbVersion->version->patch;
|
|
} else {
|
|
if (!$this->quiet) {
|
|
echo Color::RED . 'Error: ' . Colors::DEFAULT . 'Check version settings.' . PHP_EOL;
|
|
}
|
|
exit(1);
|
|
}
|
|
|
|
|
|
$fileContent = "<?php
|
|
|
|
const DEFAULT_NS = '{$defaultNS}';
|
|
const NAMESERVERS = ['{$nameserverList}'];
|
|
const VERSION = '{$versionSting}';
|
|
" . PHP_EOL;
|
|
|
|
try {
|
|
file_put_contents(filename: $bootStrapFile, data: $fileContent);
|
|
} catch (Exception $e) {
|
|
if (!$this->quiet) {
|
|
echo 'Error: ' . $e->getMessage() . PHP_EOL;
|
|
}
|
|
}
|
|
shell_exec(command: $this->baseDir . 'vendor/bin/openapi src/Controller/RequestController.php -b public/openapi/bootstrap.php -o public/openapi/bindapi.json');
|
|
}
|
|
|
|
function nameserversList(): void
|
|
{
|
|
$nameservers = $this->nameserverRepository->findAll();
|
|
if (!empty($nameservers)) {
|
|
echo 'All available nameservers:' . PHP_EOL;
|
|
$table = new ConsoleTable();
|
|
$table->setHeaders(content: ['ID', 'Name', 'A', 'AAAA', 'API Key', 'self']);
|
|
|
|
foreach ($nameservers as $nameserver) {
|
|
$row = [];
|
|
$row[] = $nameserver->getId();
|
|
$row[] = $nameserver->getName();
|
|
$row[] = $nameserver->getA();
|
|
$row[] = $nameserver->getAaaa();
|
|
$row[] = $nameserver->getApikeyPrefix();
|
|
$row[] = $nameserver->getSelf();
|
|
$table->addRow(data: $row);
|
|
}
|
|
$table->setPadding(value: 2);
|
|
$table->display();
|
|
} else {
|
|
echo 'No nameservers found.' . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
exit(0);
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
function nameserversUpdate(): void
|
|
{
|
|
$id = $this->arguments[1] ?? 0;
|
|
$name = $this->arguments['name'] ?? '';
|
|
$a = $this->arguments['a'] ?? '';
|
|
$aaaa = $this->arguments['aaaa'] ?? '';
|
|
$apikey = $this->arguments['apikey'] ?? '';
|
|
$self = $this->arguments['self'] ?? '';
|
|
|
|
|
|
if ($id == 0) {
|
|
echo 'An ID is required.' . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
if (!$this->nameserverRepository->findByID(id: intval(value: $id))) {
|
|
echo 'Nameserver with ID ' . Colors::YELLOW . $id . Colors::DEFAULT . " doesn't exist." . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
|
|
if ($apikey) {
|
|
$nameserver = new Nameserver(name: $name, id: intval(value: $id), a: $a, aaaa: $aaaa, passphrase: $apikey, self: $self);
|
|
} else {
|
|
$nameserver = new Nameserver(name: $name, id: intval(value: $id), a: $a, aaaa: $aaaa, self: $self);
|
|
}
|
|
|
|
if ($this->nameserverRepository->update(nameserver: $nameserver) !== false) {
|
|
$this->createOpenAPIBootstrap();
|
|
if (!$this->quiet) {
|
|
echo 'Nameserver ' . Colors::YELLOW . $id . Colors::DEFAULT . ' has been updated.' . PHP_EOL;
|
|
}
|
|
} else {
|
|
if (!$this->quiet) {
|
|
echo 'Error while updating nameserver ' . Colors::YELLOW . $id . '.' . PHP_EOL;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
function nameserversDelete(): void
|
|
{
|
|
if (empty($this->arguments[1])) {
|
|
if (!$this->quiet) {
|
|
echo "You need to supply an ID." . PHP_EOL;
|
|
}
|
|
exit(1);
|
|
}
|
|
|
|
$id = intval(value: $this->arguments[1] ?? 0);
|
|
if ($id == 0) {
|
|
if (!$this->quiet) {
|
|
echo 'Nameserver with ID ' . Colors::YELLOW . $id . Colors::DEFAULT . ' not found.' . PHP_EOL;
|
|
}
|
|
exit(1);
|
|
}
|
|
if (!$this->nameserverRepository->findByID(id: $id)) {
|
|
if (!$this->quiet) {
|
|
echo 'There is no nameserver with ID ' . Colors::YELLOW . $id . Colors::DEFAULT . '.' . PHP_EOL;
|
|
}
|
|
exit(1);
|
|
}
|
|
$this->nameserverRepository->delete(id: $id);
|
|
$this->createOpenAPIBootstrap();
|
|
|
|
if (!$this->quiet) {
|
|
echo 'The nameserver with ID ' . Colors::YELLOW . $id . Colors::DEFAULT . ' has been deleted.' . PHP_EOL;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
function checkShowIncludes(): void
|
|
{
|
|
$nameservers = $this->nameserverRepository->findAll();
|
|
|
|
if (count($nameservers) === 0) {
|
|
echo 'No nameservers found.' . PHP_EOL;
|
|
echo 'You first need to setup the system.' . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
|
|
|
|
echo Colors::DEFAULT . 'You need to add these lines to ' . Colors::YELLOW . '/etc/bind/local.bindapi.options' . Colors::DEFAULT . ' on every panel and make sure' . PHP_EOL;
|
|
echo 'that ' . Colors::YELLOW . 'include "/etc/bind/local.bindapi.options";' . Colors::DEFAULT . ' exists in ' . Colors::YELLOW . '/etc/bind/named.conf.options' . Colors::DEFAULT . '.' . PHP_EOL;
|
|
$ip = [];
|
|
foreach ($nameservers as $nameserver) {
|
|
if (!empty($nameserver->getA())) {
|
|
$ip[] = $nameserver->getA();
|
|
}
|
|
if (!empty($nameserver->getAaaa())) {
|
|
$ip[] = $nameserver->getAaaa();
|
|
}
|
|
}
|
|
echo PHP_EOL . 'allow-transfer {' . PHP_EOL;
|
|
foreach ($ip as $currentIp)
|
|
echo "\t$currentIp;" . PHP_EOL;
|
|
echo '};';
|
|
echo PHP_EOL . 'also-notify {' . PHP_EOL;
|
|
foreach ($ip as $currentIp)
|
|
echo "\t$currentIp;" . PHP_EOL;
|
|
echo '};' . PHP_EOL;
|
|
echo PHP_EOL . 'After the modification feel free to run ' . Colors::YELLOW . 'named-checkconf' . Colors::DEFAULT . ' to ensure there were no errors.' . PHP_EOL;
|
|
echo PHP_EOL . 'Run ' . Colors::YELLOW . 'rndc reload' . Colors::DEFAULT . ' to activate the changes.' . PHP_EOL;
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
*/
|
|
function checkDomains(): void
|
|
{
|
|
$this->domainController->checkDomains();
|
|
}
|
|
|
|
public function exitInvalidPanelApiKey(string $details = null): void
|
|
{
|
|
if (!$this->quiet) {
|
|
if ($details) {
|
|
echo Colors::YELLOW . $details . Colors::DEFAULT . PHP_EOL;
|
|
}
|
|
echo 'This is no valid panel apikey. A valid key looks like this one:' . PHP_EOL;
|
|
echo Colors::YELLOW . 'A7hjZx52.u8Rzj2S5KUvqozPlQwh4k3eDCrLikL8ZYlcdPr488QkbOW2JaS6Hg5syNllgnNOpQv6TntNMzt62LiH5CTlrMovRQhMcwZzM5dOfLKzqEePFRv1y6qZ7CT9' . PHP_EOL;
|
|
}
|
|
exit(1);
|
|
}
|
|
|
|
public function exitInvalidNameserverApiKey(string $details = null): void
|
|
{
|
|
echo 'fixme';
|
|
}
|
|
|
|
/**
|
|
*/
|
|
private function dynDnsPush(): void
|
|
{
|
|
$hostName = $this->arguments[1] ?? '';
|
|
|
|
if (empty($hostName)) {
|
|
echo 'You need to supply at least the hostname' . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
|
|
if (!$this->quiet) {
|
|
echo "Updating DynDNS host: $hostName" . PHP_EOL;
|
|
}
|
|
|
|
echo 'here';
|
|
$domain = $this->domainRepository->findByHost(host: $hostName);
|
|
print_r(value: $domain);
|
|
echo 'there';
|
|
// we need the panel who is master for zone
|
|
$panel = $this->panelRepository->findByName(name: $domain->getPanel());
|
|
|
|
// which NS belongs to that panel
|
|
|
|
$encryptionKey = $this->configController->getConfig(configKey: 'encryptionKey');
|
|
$decryptedKey = $this->encryptionController->safeDecrypt(encrypted: $panel->getApikey(), key: $encryptionKey);
|
|
|
|
if (!empty($panel->getAaaa())) {
|
|
$result = $this->apiClient->sendCommand(
|
|
requestType: 'POST',
|
|
serverName: $panel->getName(),
|
|
versionIP: 6,
|
|
apiKey: $decryptedKey,
|
|
command: 'dyndns/' . $hostName,
|
|
serverType: 'nameserver');
|
|
} else {
|
|
$result = $this->apiClient->sendCommand(
|
|
requestType: 'POST',
|
|
serverName: $panel->getName(),
|
|
versionIP: 4,
|
|
apiKey: $decryptedKey(),
|
|
command: 'dyndns/' . $hostName,
|
|
serverType: 'nameserver');
|
|
}
|
|
|
|
if ($result['header'] == 200) {
|
|
if (!$this->quiet) {
|
|
$data = $result['data'];
|
|
$decodedData = json_decode(json: $data, associative: true);
|
|
echo $decodedData['message'] . PHP_EOL;
|
|
}
|
|
} else {
|
|
echo 'Something went wrong:' . PHP_EOL;
|
|
print_r(value: $result);
|
|
exit(1);
|
|
}
|
|
exit(0);
|
|
}
|
|
|
|
/**
|
|
*/
|
|
private
|
|
function checkGenerateKey(): void
|
|
{
|
|
echo 'This generates a fresh encryption key.' . PHP_EOL;
|
|
echo 'Copy it to config.json.' . PHP_EOL;
|
|
echo 'Note: You must update all API-Keys for panels and nameservers after changing the key!' . PHP_EOL;
|
|
|
|
try {
|
|
$key = sodium_bin2hex(string: sodium_crypto_secretbox_keygen());
|
|
echo 'Suggested new key : "' . Colors::YELLOW . $key . Colors::DEFAULT . '".' . PHP_EOL;
|
|
echo PHP_EOL;
|
|
exit(0);
|
|
} catch (SodiumException $e) {
|
|
exit($e->getMessage() . PHP_EOL);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
private function domainsUpdate(): void
|
|
{
|
|
$this->logger->debug(message: "domainsUpdate()");
|
|
|
|
$panels = $this->panelRepository->findAll();
|
|
|
|
|
|
foreach ($panels as $panel) {
|
|
if (!$this->quiet) {
|
|
echo Colors::DEFAULT . 'Checking panel ' . Colors::YELLOW . $panel->getName() . Colors::DEFAULT . PHP_EOL;
|
|
$longestEntry = $this->domainRepository->getLongestEntry(field: 'name');
|
|
}
|
|
$encryptionKey = $this->configController->getConfig(configKey: 'encryptionKey');
|
|
$decryptedKey = $this->encryptionController->safeDecrypt(encrypted: $panel->getApikey(), key: $encryptionKey);
|
|
|
|
$currentDomains = $this->domainRepository->findByPanel(name: $panel->getName());
|
|
|
|
if (empty($panel->getA())) {
|
|
$result = $this->apiClient->sendCommand(
|
|
requestType: 'GET',
|
|
serverName: $panel->getName(),
|
|
versionIP: 6,
|
|
apiKey: $decryptedKey,
|
|
command: 'domains?sort=domain&subdomains=false',
|
|
serverType: 'panel'
|
|
);
|
|
} else {
|
|
$result = $this->apiClient->sendCommand(
|
|
requestType: 'GET',
|
|
serverName: $panel->getName(),
|
|
versionIP: 4,
|
|
apiKey: $decryptedKey,
|
|
command: 'domains?sort=domain&subdomains=false',
|
|
serverType: 'panel');
|
|
}
|
|
|
|
if (!empty($result['error'])) {
|
|
echo 'Error: ' . $result['data'] . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
if (!empty($result['data'])) {
|
|
$domains = json_decode(json: $result['data']);
|
|
} else {
|
|
echo 'No domains found' . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
|
|
$domainCount = 0;
|
|
if (count($domains) > 0) {
|
|
foreach ($domains as $domain) {
|
|
$domainCount++;
|
|
if (!$this->quiet) {
|
|
echo Colors::YELLOW . ' ' . str_pad(string: $domain->domain, length: $longestEntry + 1, pad_type: STR_PAD_RIGHT);
|
|
}
|
|
if ($currentDomain = $this->domainRepository->findByName(name: $domain->domain)) {
|
|
$currentPanel = $currentDomain->getPanel();
|
|
$panelName = $panel->getName();
|
|
if (strcmp(string1: $currentPanel, string2: $panelName)) {
|
|
$currentDomain->setPanel(panel: $panelName);
|
|
$this->domainRepository->update(domain: $currentDomain);
|
|
if (!$this->quiet) {
|
|
echo Colors::DEFAULT . ' updated to: ' . Colors::YELLOW . $panelName;
|
|
}
|
|
}
|
|
if (!$this->quiet) {
|
|
echo Colors::GREEN . ' OK' . Colors::DEFAULT . PHP_EOL;
|
|
}
|
|
} else {
|
|
$newDomain = new Domain(name: $domain->domain, panel: $panel->getName());
|
|
$result = $this->domainRepository->insert(domain: $newDomain);
|
|
if (!$this->quiet) {
|
|
echo Colors::DEFAULT . ' has been created with id ' . Colors::YELLOW . $result . Colors::DEFAULT . '.' . PHP_EOL;
|
|
}
|
|
}
|
|
unset($currentDomains[$domain->domain]);
|
|
}
|
|
}
|
|
if ($domainCount == 0) {
|
|
echo 'No second level domains found.' . Colors::DEFAULT . PHP_EOL;
|
|
}
|
|
|
|
//clean up stale domains
|
|
if (count($currentDomains) > 0) {
|
|
echo 'Dropping stale domains:' . PHP_EOL;
|
|
foreach ($currentDomains as $domain) {
|
|
echo $domain->getName() . PHP_EOL;
|
|
$this->domainRepository->delete(domain: $domain);
|
|
}
|
|
}
|
|
|
|
}
|
|
if (!$this->quiet) {
|
|
echo 'Creating slave zone files' . PHP_EOL;
|
|
}
|
|
$this->domainController->updateSlaveZones();
|
|
}
|
|
|
|
|
|
public function webmailCheck(): void
|
|
{
|
|
if (empty($this->arguments[1])) {
|
|
if (!$this->quiet) {
|
|
echo Colors::DEFAULT . 'You need to supply a domain name.' . PHP_EOL;
|
|
}
|
|
exit(1);
|
|
} else {
|
|
$domainName = $this->arguments[1];
|
|
}
|
|
|
|
if (!$this->quiet) {
|
|
echo Colors::DEFAULT . 'Checking domain ' . Colors::YELLOW . $domainName . Colors::DEFAULT . '.' . PHP_EOL;
|
|
}
|
|
|
|
if (!$domain = $this->domainRepository->findByName(name: $domainName)) {
|
|
if (!$this->quiet) {
|
|
echo Colors::DEFAULT . 'Domain ' . $domainName . ' not found on this server.' . PHP_EOL;
|
|
}
|
|
exit(1);
|
|
}
|
|
|
|
if (!$this->domainController->isMasterZone(domain: $domain)) {
|
|
if (!$this->quiet) {
|
|
echo 'This server is not responsible for ' . Colors::YELLOW . $domainName . Colors::DEFAULT . '.' . PHP_EOL;
|
|
}
|
|
exit(1);
|
|
}
|
|
|
|
$panel = $this->panelRepository->getSelf();
|
|
|
|
$encryptionKey = $this->configController->getConfig(configKey: 'encryptionKey');
|
|
$decryptedKey = $this->encryptionController->safeDecrypt(encrypted: $panel->getApikey(), key: $encryptionKey);
|
|
|
|
$webmailDomain = 'webmail.' . $domainName;
|
|
|
|
if (!empty($panel->getAAAA())) {
|
|
if (!$this->quiet) {
|
|
echo 'Check using IPv6: ' . Colors::YELLOW . $panel->getAaaa() . '.' . Colors::DEFAULT . PHP_EOL;
|
|
}
|
|
$result = $this->apiClient->sendCommand(
|
|
requestType: 'GET',
|
|
serverName: $panel->getName(),
|
|
versionIP: 6,
|
|
apiKey: $decryptedKey,
|
|
command: 'domains/name/' . $webmailDomain,
|
|
serverType: 'panel');
|
|
} else {
|
|
if (!$this->quiet) {
|
|
echo 'Check using IPv4: ' . Colors::YELLOW . $panel->getA() . Colors::DEFAULT . PHP_EOL;
|
|
}
|
|
$result = $this->apiClient->sendCommand(
|
|
requestType: 'GET',
|
|
serverName: $panel->getName(),
|
|
versionIP: 4,
|
|
apiKey: $decryptedKey,
|
|
command: 'domains/name/' . $webmailDomain,
|
|
serverType: 'panel');
|
|
}
|
|
|
|
if ($result['header'] === 404) {
|
|
if (!$this->quiet) {
|
|
echo 'The domain ' . Colors::YELLOW . $webmailDomain . Colors::DEFAULT . " doesn't exist." . PHP_EOL;
|
|
}
|
|
exit(1);
|
|
} else {
|
|
if (!$this->quiet) {
|
|
echo 'Found ' . Colors::YELLOW . $webmailDomain . Colors::DEFAULT . '.' . PHP_EOL;
|
|
}
|
|
}
|
|
|
|
if ($v4 = dns_get_record(hostname: $webmailDomain, type: DNS_A)[0]) {
|
|
if (!$this->quiet) {
|
|
echo "Found IPv4 entry: " . Colors::YELLOW . $v4['ip'] . Colors::DEFAULT . '.' . PHP_EOL;
|
|
}
|
|
$v4Test = $this->apiClient->fileGetContents(url: $webmailDomain, versionIP: 4);
|
|
|
|
if ($v4Test['error']) {
|
|
if (!$this->quiet) {
|
|
echo 'There was an error: ' . Colors::YELLOW . $v4Test['errorMessage'] . Colors::DEFAULT . '.';
|
|
}
|
|
exit(1);
|
|
} else {
|
|
if (!$this->quiet) {
|
|
echo 'Successfully connected to webserver via ' . Colors::YELLOW . 'IPv4' . Colors::DEFAULT . '.' . PHP_EOL;
|
|
}
|
|
}
|
|
} else {
|
|
if (!$this->quiet) {
|
|
echo "Found no IPv4 entry for " . Colors::YELLOW . $webmailDomain . Colors::DEFAULT . PHP_EOL;
|
|
}
|
|
}
|
|
|
|
|
|
if ($v6 = dns_get_record(hostname: $webmailDomain, type: DNS_AAAA)[0]) {
|
|
if (!$this->quiet) {
|
|
echo "Found IPv6 entry: " . Colors::YELLOW . $v6['ipv6'] . Colors::DEFAULT . '.' . PHP_EOL;
|
|
}
|
|
$v6Test = $this->apiClient->fileGetContents(url: $webmailDomain, versionIP: 6);
|
|
|
|
if ($v6Test['error']) {
|
|
if (!$this->quiet) {
|
|
echo 'There was an error: ' . Colors::YELLOW . $v6Test['errorMessage'] . Colors::DEFAULT . '.';
|
|
}
|
|
exit(1);
|
|
} else {
|
|
if (!$this->quiet) {
|
|
echo 'Successfully connected to webserver via ' . Colors::YELLOW . 'IPv6' . Colors::DEFAULT . '.' . PHP_EOL;
|
|
}
|
|
}
|
|
} else {
|
|
if (!$this->quiet) {
|
|
echo "Found no IPv6 entry for " . Colors::YELLOW . $webmailDomain . Colors::DEFAULT . '.' . PHP_EOL;
|
|
}
|
|
}
|
|
|
|
// TODO check that at least IPv4 or IP6 exists?
|
|
|
|
|
|
$domainData = json_decode(json: $result['data']);
|
|
$apacheData = $domainData->apache;
|
|
$httpsDirectives = $apacheData->https_directives . PHP_EOL;
|
|
|
|
if (!str_contains(haystack: $httpsDirectives, needle: '# bindAPI - webmailer')) {
|
|
if (!$this->quiet) {
|
|
echo 'Generated config is missing.' . PHP_EOL;
|
|
}
|
|
exit(1);
|
|
} else {
|
|
if (!$this->quiet) {
|
|
echo 'Generated config is valid.' . PHP_EOL;
|
|
}
|
|
exit(0);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function webmailCreate(): bool
|
|
{
|
|
|
|
// TODO
|
|
|
|
/*
|
|
$webmailConfig = '# bindAPI - webmailer' . PHP_EOL;
|
|
$webmailConfig .= 'SSLProxyEngine On' . PHP_EOL;
|
|
$webmailConfig .= 'ProxyPass /.well-known/ !' . PHP_EOL;
|
|
$webmailConfig .= 'ProxyPass "/" "https://webmail' . $panel->getName() . '"' . PHP_EOL;
|
|
$webmailConfig .= '## bindAPI - webmailer' . PHP_EOL;
|
|
|
|
echo $webmailConfig;
|
|
|
|
//$httpsDirectives += $w
|
|
*/
|
|
|
|
}
|
|
|
|
private function checkMail(): void
|
|
{
|
|
echo 'Not yet implemented.' . PHP_EOL;
|
|
}
|
|
|
|
private function checkVersion(): void
|
|
{
|
|
$update = false;
|
|
|
|
$composerFile = $this->baseDir . DIRECTORY_SEPARATOR . 'composer.json';
|
|
$composerJson = json_decode(json: file_get_contents(filename: $composerFile), associative: false);
|
|
|
|
$fileVersion = $composerJson->version;
|
|
$fileBuildNumber = $composerJson->build_number;
|
|
[$major, $minor, $patch] = explode(separator: '.', string: $fileVersion);
|
|
$version = [
|
|
'major' => $major,
|
|
'minor' => $minor,
|
|
'patch' => $patch
|
|
];
|
|
$json = json_encode(value: ['version' => $version]);
|
|
|
|
echo "File version:\t\t$fileVersion" . PHP_EOL;
|
|
echo "File build number:\t$fileBuildNumber" . PHP_EOL;
|
|
|
|
$this->settingsRepository->set(name: 'version', value: $json);
|
|
$this->settingsRepository->set(name: 'buildnumber', value: $fileBuildNumber);
|
|
|
|
$currentDBVersion = $this->settingsRepository->findByName(name: 'version');
|
|
$dbVersion = json_decode(json: $currentDBVersion);
|
|
$currentDBBuildnumber = $this->settingsRepository->findByName(name: 'buildnumber');
|
|
|
|
echo "DB version:\t\t";
|
|
echo $dbVersion->version->major . '.';
|
|
echo $dbVersion->version->minor . '.';
|
|
echo $dbVersion->version->patch . PHP_EOL;
|
|
echo "DB build number:\t$currentDBBuildnumber" . PHP_EOL;
|
|
}
|
|
|
|
private function dynDnyUpdate(): void
|
|
{
|
|
echo 'Not yet implemented.' . PHP_EOL;
|
|
}
|
|
|
|
private function dynDnsDelete(): void
|
|
{
|
|
echo 'Not yet implemented.' . PHP_EOL;
|
|
}
|
|
|
|
private function webmailDelete(): void
|
|
{
|
|
}
|
|
|
|
private function migrationsStatus(): void
|
|
{
|
|
$phinx = new PhinxApplication('Phinx', '0.9.2');
|
|
|
|
// use DI later when moving to symfony
|
|
// $input = new \Symfony\Component\Console\Input\ArgvInput();
|
|
// $output = new \Symfony\Component\Console\Output\ConsoleOutput();
|
|
|
|
echo 'show migration status' . PHP_EOL;
|
|
$command = $phinx->find(name: 'status'); // change 'migrate' to 'status'
|
|
|
|
$arguments = [
|
|
'command' => 'status',
|
|
];
|
|
|
|
$phinxOutput = new ConsoleOutput();
|
|
$returnCode = $command->run(input: new ArrayInput(parameters: $arguments), output: $phinxOutput);
|
|
echo 'return code: ' . $returnCode . PHP_EOL;
|
|
}
|
|
|
|
private function migrationsMake()
|
|
{
|
|
$phinx = new PhinxApplication();
|
|
|
|
$phinx->add(new GenerateCommand());
|
|
|
|
$arguments = [
|
|
'command' => 'generate',
|
|
'--overwrite' => true,
|
|
];
|
|
$output = new ConsoleOutput();
|
|
$returnCode = $phinx->run(input: new ArrayInput(parameters: $arguments), output: $output);
|
|
}
|
|
|
|
private function migrationsMigrate(): void
|
|
{
|
|
$phinx = new PhinxApplication('Phinx', '0.9.2');
|
|
|
|
echo 'show migration status' . PHP_EOL;
|
|
$command = $phinx->find(name: 'migrate');
|
|
|
|
$arguments = [
|
|
'command' => 'migrate',
|
|
];
|
|
|
|
$phinxOutput = new ConsoleOutput();
|
|
$returnCode = $command->run(input: new ArrayInput(parameters: $arguments), output: $phinxOutput);
|
|
}
|
|
|
|
private function checkPanel()
|
|
{
|
|
$self = $this->panelRepository->findSelf();
|
|
|
|
$panelCount = count($self);
|
|
|
|
if ($panelCount != 1) {
|
|
if ($panelCount == 0) {
|
|
echo 'No panel marked as this server.' . PHP_EOL;
|
|
echo 'Use ' . Colors::YELLOW . 'panels:update <ID> self=yes ' . Colors::DEFAULT . 'to mark this panel.' . PHP_EOL;
|
|
} else {
|
|
echo 'Only one panel can be marked as self.' . PHP_EOL;
|
|
echo 'Use ' . Colors::YELLOW . 'panels:update <ID> self=no ' . Colors::DEFAULT . 'to remove the stale panel' . PHP_EOL;
|
|
}
|
|
} else {
|
|
if (!$this->quiet) {
|
|
echo 'Only one panel is designated as our host' . Colors::GREEN . ' OK' . Colors::DEFAULT . PHP_EOL;
|
|
$self = $this->panelRepository->getSelf();
|
|
$selfName = $self->getName();
|
|
echo 'The panel is configured to be ' . Colors::YELLOW . $selfName;
|
|
$hostname = gethostname();
|
|
if (strcmp(string1: $hostname, string2: $selfName) === 0) {
|
|
echo Colors::DEFAULT . ' and that seems to be valid.' . PHP_EOL;
|
|
} else {
|
|
echo PHP_EOL;
|
|
echo $selfName . Colors::DEFAULT . ' and ' . Colors::YELLOW . $hostname . Colors::DEFAULT . ' differ.' . PHP_EOL;
|
|
echo 'Name mismatch, maybe panel:self not set?' . PHP_EOL;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private function checkNameserver()
|
|
{
|
|
$self = $this->panelRepository->findSelf();
|
|
|
|
$nameserverCount = count($self);
|
|
|
|
if ($nameserverCount != 1) {
|
|
if ($nameserverCount == 0) {
|
|
if (!$this->quiet) {
|
|
echo 'No nameserver marked as this server.' . PHP_EOL;
|
|
echo 'The setting is used to generate the default nameserver in SwaggerUI.' . PHP_EOL;
|
|
echo 'Use ' . Colors::YELLOW . 'nameservers:update <ID> self=yes ' . Colors::DEFAULT . 'to mark this nameserver.' . PHP_EOL;
|
|
exit(1);
|
|
}
|
|
} else {
|
|
if (!$this->quiet) {
|
|
echo 'Only one nameserver should be marked as self.' . PHP_EOL;
|
|
echo 'The setting is used to generate the default nameserver in SwaggerUI.' . PHP_EOL;
|
|
echo 'Use ' . Colors::YELLOW . 'nameservers:update <ID> self=no ' . Colors::DEFAULT . 'to remove the stale nameserver' . PHP_EOL;
|
|
}
|
|
}
|
|
} else {
|
|
if (!$this->quiet) {
|
|
echo 'Only one nameserver is designated as our host' . Colors::GREEN . ' OK' . Colors::DEFAULT . PHP_EOL;
|
|
}
|
|
}
|
|
}
|
|
|
|
private function cronRun()
|
|
{
|
|
$this->logger->info(message: 'cronRun()');
|
|
$this->checkPanel();
|
|
$this->checkNameserver();
|
|
$this->domainsUpdate();
|
|
}
|
|
|
|
private function checkNameservers()
|
|
{
|
|
if (!$this->quiet) {
|
|
echo 'Check nameserver versions:' . PHP_EOL;
|
|
}
|
|
|
|
$nameservers = $this->nameserverRepository->findAll();
|
|
foreach ($nameservers as $nameserver) {
|
|
if (!$this->quiet) {
|
|
echo Colors::YELLOW . $nameserver->getName();
|
|
$this->checkNameserverVersion(server: $nameserver);
|
|
}
|
|
}
|
|
}
|
|
|
|
private function checkCache()
|
|
{
|
|
if (function_exists(function: 'opcache_reset')) {
|
|
opcache_reset();
|
|
echo 'Cache cleared.' . PHP_EOL;
|
|
} else {
|
|
echo Color::RED . 'Errror: ' . Colors::DEFAULT . 'Missing function opcache_clear()' . PHP_EOL;
|
|
}
|
|
}
|
|
|
|
private function checkBootstrap()
|
|
{
|
|
$this->createOpenAPIBootstrap();
|
|
if (!$this->quiet) {
|
|
echo 'Created new bootsrap.php' . PHP_EOL;
|
|
}
|
|
}
|
|
}
|