2022-01-26 19:01:52 +01:00
< ? php declare ( strict_types = 1 );
2022-01-31 21:00:24 +01:00
2022-01-19 21:07:22 +01:00
namespace App\Controller ;
2022-01-26 19:01:52 +01:00
error_reporting ( error_level : E_ALL );
define ( constant_name : 'COLOR_RED' , value : " \033 [31m " );
define ( constant_name : 'COLOR_GREEN' , value : " \033 [32m " );
define ( constant_name : 'COLOR_YELLOW' , value : " \033 [33m " );
define ( constant_name : 'COLOR_BLUE' , value : " \033 [34m " );
define ( constant_name : 'COLOR_DEFAULT' , value : " \033 [39m " );
2022-01-22 16:37:30 +01:00
2022-01-19 21:07:22 +01:00
2022-01-31 21:00:24 +01:00
use App\Entity\Domain ;
2022-02-06 17:59:43 +01:00
use App\Entity\Nameserver ;
2022-02-01 20:41:08 +01:00
use App\Entity\Panel ;
2022-01-31 21:00:24 +01:00
use App\Repository\ApikeyRepository ;
use App\Repository\DomainRepository ;
use App\Repository\NameserverRepository ;
use App\Repository\PanelRepository ;
use Arubacao\TldChecker\Validator ;
use DI\Container ;
use DI\ContainerBuilder ;
use DI\DependencyException ;
use DI\NotFoundException ;
2022-01-19 21:07:22 +01:00
use LucidFrame\Console\ConsoleTable ;
2022-02-06 17:59:43 +01:00
use Monolog\Formatter\LineFormatter ;
use Monolog\Handler\StreamHandler ;
use Monolog\Logger ;
2022-01-19 21:07:22 +01:00
use UnhandledMatchError ;
2022-01-31 21:00:24 +01:00
use function DI\autowire ;
2022-01-19 21:07:22 +01:00
if ( php_sapi_name () !== 'cli' ) {
exit ;
}
/**
*
*/
class BindAPI
{
2022-02-06 17:59:43 +01:00
private Logger $log ;
private ApiController $apiController ;
private ApikeyRepository $apikeyRepository ;
2022-01-19 21:07:22 +01:00
private DomainController $domainController ;
2022-01-31 21:00:24 +01:00
private DomainRepository $domainRepository ;
2022-02-06 17:59:43 +01:00
private NameserverController $nameserverController ;
2022-01-31 21:00:24 +01:00
private NameserverRepository $nameserverRepository ;
2022-02-06 17:59:43 +01:00
private PanelController $panelController ;
2022-01-31 21:00:24 +01:00
private PanelRepository $panelRepository ;
private Container $container ;
2022-01-20 19:55:04 +01:00
2022-01-25 20:31:31 +01:00
/**
2022-01-31 21:00:24 +01:00
* @ throws \DI\DependencyException
* @ throws \DI\NotFoundException
* @ throws \Exception
2022-01-25 20:31:31 +01:00
*/
2022-01-31 21:00:24 +01:00
public function __construct ( private array $config , private int $argumentsCount , private array $arguments )
2022-01-25 20:31:31 +01:00
{
2022-02-06 17:59:43 +01:00
$dateFormat = " Y:m:d H:i:s " ;
$output = " %datetime% %channel%.%level_name% %message% \n " ; // %context% %extra%
$formatter = new LineFormatter ( format : $output , dateFormat : $dateFormat );
$stream = new StreamHandler ( stream : dirname ( path : __DIR__ , levels : 2 ) . '/bindAPI.log' );
$stream -> setFormatter ( formatter : $formatter );
$this -> log = new Logger ( name : 'bindAPI' );
$this -> log -> pushHandler ( handler : $stream );
2022-01-31 21:00:24 +01:00
$containerBuilder = new ContainerBuilder ();
$containerBuilder -> addDefinitions ([
DatabaseConnection :: class => autowire () -> constructorParameter ( parameter : 'config' , value : $this -> config ),
2022-02-06 17:59:43 +01:00
DomainController :: class => autowire ()
-> constructorParameter ( parameter : 'config' , value : $this -> config )
-> constructorParameter ( parameter : 'log' , value : $this -> log ),
DomainRepository :: class => autowire ()
-> constructorParameter ( parameter : 'config' , value : $this -> config )
-> constructorParameter ( parameter : 'log' , value : $this -> log ),
2022-01-31 21:00:24 +01:00
]);
$this -> container = $containerBuilder -> build ();
2022-01-25 20:31:31 +01:00
2022-02-06 17:59:43 +01:00
$this -> apiController = $this -> container -> get ( name : ApiController :: class );
2022-01-31 21:00:24 +01:00
$this -> domainController = $this -> container -> get ( name : DomainController :: class );
$this -> domainRepository = $this -> container -> get ( name : DomainRepository :: class );
2022-02-06 17:59:43 +01:00
$this -> nameserverController = $this -> container -> get ( name : NameserverController :: class );
2022-01-31 21:00:24 +01:00
$this -> nameserverRepository = $this -> container -> get ( name : NameserverRepository :: class );
2022-02-06 17:59:43 +01:00
$this -> panelController = $this -> container -> get ( name : PanelController :: class );
2022-01-31 21:00:24 +01:00
$this -> panelRepository = $this -> container -> get ( name : PanelRepository :: class );
2022-02-06 17:59:43 +01:00
$this -> apikeyRepository = $this -> container -> get ( name : ApikeyRepository :: class );
//$dotenv->required(['DB_HOST', 'DB_NAME', 'DB_USER', 'DB_PASS']);
2022-01-20 19:55:04 +01:00
}
2022-01-19 21:07:22 +01:00
2022-01-25 20:31:31 +01:00
2022-01-19 21:07:22 +01:00
function runCommand ()
{
2022-02-06 17:59:43 +01:00
if ( $this -> config [ 'debug' ]) {
$this -> log -> debug ( message : " runCommand() " );
}
2022-01-23 15:22:20 +01:00
if ( $this -> argumentsCount < 1 ) {
2022-01-19 21:07:22 +01:00
$this -> showUsage ();
2022-01-20 19:55:04 +01:00
exit ( 0 );
2022-01-19 21:07:22 +01:00
}
2022-01-23 15:22:20 +01:00
if ( str_contains ( haystack : $this -> arguments [ 0 ], needle : ':' )) {
2022-01-31 21:00:24 +01:00
[ $command , $subcommand ] = explode ( separator : ':' , string : $this -> arguments [ 0 ]);
2022-01-20 19:55:04 +01:00
} else {
2022-01-23 15:22:20 +01:00
$command = $this -> arguments [ 0 ];
2022-01-20 19:55:04 +01:00
$subcommand = '' ;
}
2022-01-19 21:07:22 +01:00
try {
match ( $command ) {
2022-01-26 19:01:52 +01:00
'check' => $this -> handleChecks ( subcommand : $subcommand ),
'panels' => $this -> handlePanels ( subcommand : $subcommand ),
'apikeys' => $this -> handleApiKeys ( subcommand : $subcommand ),
'domains' => $this -> handleDomains ( subcommand : $subcommand ),
'nameservers' => $this -> handleNameservers ( subcommand : $subcommand )
2022-01-19 21:07:22 +01:00
};
2022-01-31 21:00:24 +01:00
} catch ( UnhandledMatchError ) {
2022-01-19 21:07:22 +01:00
echo 'Unknown command: ' . $command . PHP_EOL ;
exit ( 1 );
}
}
/**
* @ return void
*/
function showUsage () : void
{
2022-02-06 17:59:43 +01:00
if ( $this -> config [ 'debug' ]) {
$this -> log -> debug ( message : " showUsage() " );
}
2022-01-25 20:31:31 +01:00
echo COLOR_YELLOW . 'Usage:' . PHP_EOL ;
echo COLOR_DEFAULT . " \t ./bin/console { options} { arguments} " . PHP_EOL . PHP_EOL ;
echo COLOR_YELLOW . 'Options:' . PHP_EOL ;
echo COLOR_GREEN . " \t -v, --version \t \t " . COLOR_DEFAULT . " Display the version of the API " . PHP_EOL ;
2022-02-06 17:59:43 +01:00
echo COLOR_GREEN . " \t -V, --verbose \t \t " . COLOR_DEFAULT . " All :lists command are auto-verbose " . PHP_EOL . PHP_EOL ;
2022-01-25 20:31:31 +01:00
2022-01-23 15:22:20 +01:00
echo COLOR_YELLOW . " check " . COLOR_DEFAULT . " \t health checks the system can perform " . PHP_EOL ;
echo COLOR_GREEN . " \t check:permissions " . PHP_EOL ;
2022-01-31 21:00:24 +01:00
echo COLOR_GREEN . " \t check:panels { ID} { fix=yes} " . PHP_EOL ;
2022-03-01 16:01:21 +01:00
echo COLOR_GREEN . " \t check:domains " . PHP_EOL ;
echo COLOR_GREEN . " \t check:showincludes " . COLOR_DEFAULT . " Temporary needed until KeyHelp 22.1 " . PHP_EOL ;
2022-02-22 13:57:19 +01:00
2022-01-23 15:22:20 +01:00
echo COLOR_YELLOW . " panels " . COLOR_DEFAULT . " \t all Keyhelp systems configured " . PHP_EOL ;
echo COLOR_GREEN . " \t panels:list " . PHP_EOL ;
echo COLOR_GREEN . " \t panels:create <name> { A=<IPv4>} { AAAA=<IPv6>} { apikey=<API-Key>} " . PHP_EOL ;
echo COLOR_GREEN . " \t panels:update <ID> { name=<name>} { A=<IPv4>} { AAAA=<IPv6>} { apikey=<API-Key>} " . PHP_EOL ;
2022-02-06 17:59:43 +01:00
echo COLOR_GREEN . " \t panels:delete <ID> " . PHP_EOL ;
2022-01-23 15:22:20 +01:00
echo COLOR_GREEN . " \t panels:apiping { <ID>} " . PHP_EOL ;
echo COLOR_YELLOW . " nameservers " . COLOR_DEFAULT . " available nameservers " . PHP_EOL ;
echo COLOR_GREEN . " \t nameservers:list " . PHP_EOL ;
echo COLOR_GREEN . " \t nameservers:create <name> { A=<IPv4>} { AAAA=<IPv6>} { apikey=<API-Key>} " . PHP_EOL ;
2022-02-06 17:59:43 +01:00
echo COLOR_GREEN . " \t nameservers:update <ID> { name=<name>} { A=<IPv4>} { AAAA=<IPv6>} { apikey=<API-Key>} " . PHP_EOL ;
2022-01-23 15:22:20 +01:00
echo COLOR_GREEN . " \t nameservers:delete <ID> " . PHP_EOL ;
echo COLOR_GREEN . " \t nameservers:apiping { <ID>} " . PHP_EOL ;
echo COLOR_YELLOW . " domains " . COLOR_DEFAULT . " domains this server is responsible for " . PHP_EOL ;
echo COLOR_GREEN . " \t domains:list " . PHP_EOL ;
2022-02-21 14:11:47 +01:00
echo COLOR_GREEN . " \t domains:create <name> { panel=<name>} " . PHP_EOL ;
echo COLOR_GREEN . " \t domains:update <ID> { name=<name>} { panel=<name>} " . PHP_EOL ;
2022-01-23 15:22:20 +01:00
echo COLOR_GREEN . " \t domains:delete <ID> " . PHP_EOL ;
2022-03-21 14:32:10 +01:00
echo COLOR_GREEN . " \t domains:dyndns hostname.domain.tld { A=<IPv4>} { AAAA=<IPv6>} " . PHP_EOL ;
2022-01-23 15:22:20 +01:00
2022-02-21 14:11:47 +01:00
echo COLOR_YELLOW . " apikeys " . COLOR_DEFAULT . " \t API keys to access this server " . PHP_EOL ;
2022-01-23 15:22:20 +01:00
echo COLOR_GREEN . " \t apikeys:list " . PHP_EOL ;
2022-01-25 20:31:31 +01:00
echo COLOR_GREEN . " \t apikeys:create { name=<name>} " . PHP_EOL ;
2022-02-06 17:59:43 +01:00
echo COLOR_GREEN . " \t apikeys:update <ID> { name=<name>} " . PHP_EOL ;
2022-01-23 15:22:20 +01:00
echo COLOR_GREEN . " \t apikeys:delete <ID> " . PHP_EOL ;
echo PHP_EOL . " \033 [39me.g. ./bin/console apikeys:list " . PHP_EOL ;
2022-01-20 19:55:04 +01:00
}
2022-01-31 21:00:24 +01:00
function handleChecks ( string $subcommand )
2022-01-22 13:16:26 +01:00
{
2022-02-06 17:59:43 +01:00
if ( $this -> config [ 'debug' ]) {
$this -> log -> debug ( message : " handleChecks() " );
}
2022-01-22 13:16:26 +01:00
try {
match ( $subcommand ) {
2022-01-31 21:00:24 +01:00
'permissions' => $this -> handleCheckPermissions (),
'panels' => $this -> handleCheckPanels (),
2022-03-01 16:01:21 +01:00
'domains' => $this -> handleCheckDomains (),
'showincludes' => $this -> handleCheckShowIncludes (),
2022-01-22 13:16:26 +01:00
};
} catch ( UnhandledMatchError ) {
echo 'Unknown action: ' . $subcommand . PHP_EOL ;
2022-01-31 21:00:24 +01:00
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
2022-01-22 13:16:26 +01:00
}
}
2022-01-20 19:55:04 +01:00
/**
2022-01-31 21:00:24 +01:00
* @ throws \DI\DependencyException
* @ throws \DI\NotFoundException
2022-01-20 19:55:04 +01:00
*/
2022-01-31 21:00:24 +01:00
function handleCheckPermissions ()
2022-01-20 19:55:04 +01:00
{
2022-02-06 17:59:43 +01:00
if ( $this -> config [ 'debug' ]) {
$this -> log -> debug ( message : " handleCheckPermissions() " );
}
2022-01-31 21:00:24 +01:00
$this -> domainController -> checkPermissions ();
2022-01-20 19:55:04 +01:00
}
2022-01-31 21:00:24 +01:00
/**
* @ throws \DI\DependencyException
* @ throws \DI\NotFoundException
2022-01-26 19:01:52 +01:00
*/
2022-01-31 21:00:24 +01:00
function handleCheckPanels ()
2022-01-22 16:37:30 +01:00
{
2022-02-06 17:59:43 +01:00
if ( $this -> config [ 'debug' ]) {
$this -> log -> debug ( message : " handleCheckPanels() " );
}
2022-01-31 21:00:24 +01:00
$id = intval ( value : $this -> arguments [ 1 ] ? ? 0 );
2022-01-22 18:52:17 +01:00
2022-01-31 21:00:24 +01:00
if ( $id != 0 ) {
2022-02-12 19:37:24 +01:00
if ( $panel = $this -> panelRepository -> findByID ( id : $id )) {
2022-01-31 21:00:24 +01:00
$this -> checkSinglePanel ( panel : $panel );
} else {
echo " Unknown panel ID: $id " . PHP_EOL ;
2022-01-25 20:31:31 +01:00
}
} else {
2022-01-31 21:00:24 +01:00
echo " check all … " . PHP_EOL ;
2022-02-01 20:41:08 +01:00
$panels = $this -> panelRepository -> findAll ();
2022-01-31 21:00:24 +01:00
foreach ( $panels as $panel ) {
$this -> checkSinglePanel ( panel : $panel );
}
2022-01-25 20:31:31 +01:00
}
2022-01-31 21:00:24 +01:00
}
2022-02-06 17:59:43 +01:00
2022-01-31 21:00:24 +01:00
/**
2022-02-06 17:59:43 +01:00
* @ param \App\Entity\Panel $panel
2022-01-31 21:00:24 +01:00
*
* @ return void
*/
2022-02-01 20:41:08 +01:00
public function checkSinglePanel ( Panel $panel ) : void
2022-01-31 21:00:24 +01:00
{
2022-02-06 17:59:43 +01:00
if ( $this -> config [ 'debug' ]) {
$this -> log -> debug ( message : " checkSinglePanel() " );
}
2022-02-22 13:57:19 +01:00
echo COLOR_DEFAULT . 'Keyhelp-Panel: ' . COLOR_YELLOW . $panel -> getName ();
if ( $this -> config [ 'verbose' ]) {
if ( empty ( $panel -> getA ())) {
try {
$panelRequest = $this -> apiController -> sendCommand (
requestType : 'GET' ,
serverName : $panel -> getName (),
versionIP : 6 ,
apiKey : $panel -> getApikey (),
command : '/server' ,
serverType : 'panel' );
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
exit ( 1 );
}
} else {
try {
$panelRequest = $this -> apiController -> sendCommand (
requestType : 'GET' ,
serverName : $panel -> getName (),
versionIP : 4 ,
apiKey : $panel -> getApikey (),
command : '/server' ,
serverType : 'panel' );
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
exit ();
}
}
$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' ;
}
echo COLOR_DEFAULT . ' KeyHelp version: ' . $panelVersion . " ( $responseTime seconds) " . PHP_EOL ;
} else {
echo PHP_EOL ;
}
if ( empty ( $panel -> getA ())) {
2022-01-31 21:00:24 +01:00
try {
2022-02-06 17:59:43 +01:00
$result = $this -> apiController -> sendCommand (
2022-01-31 21:00:24 +01:00
requestType : 'GET' ,
2022-02-22 13:57:19 +01:00
serverName : $panel -> getName (),
versionIP : 6 ,
apiKey : $panel -> getApikey (),
command : 'domains' ,
serverType : 'panel'
2022-01-31 21:00:24 +01:00
);
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
2022-01-25 20:31:31 +01:00
}
} else {
2022-01-31 21:00:24 +01:00
try {
2022-02-06 17:59:43 +01:00
$result = $this -> apiController -> sendCommand (
2022-01-31 21:00:24 +01:00
requestType : 'GET' ,
2022-02-22 13:57:19 +01:00
serverName : $panel -> getName (),
versionIP : 4 ,
apiKey : $panel -> getApikey (),
command : 'domains' ,
serverType : 'panel' );
2022-01-31 21:00:24 +01:00
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
2022-01-25 20:31:31 +01:00
}
}
2022-01-31 21:00:24 +01:00
2022-02-22 13:57:19 +01:00
2022-01-31 21:00:24 +01:00
if ( ! empty ( $result [ 'error' ])) {
echo $result [ 'data' ] . PHP_EOL ;
2022-01-25 20:31:31 +01:00
exit ( 1 );
2022-01-31 21:00:24 +01:00
}
if ( ! empty ( $result [ 'data' ])) {
$domains = json_decode ( json : $result [ 'data' ]);
2022-01-25 20:31:31 +01:00
} else {
2022-01-31 21:00:24 +01:00
echo 'No domains found' . PHP_EOL ;
exit ( 1 );
}
$maxDomainName = 0 ;
// TODO this is ugly code ↓↓↓
foreach ( $domains as $domain ) {
2022-02-01 20:41:08 +01:00
if ( $this -> isValidSecondLevelDomain ( domainName : $domain -> domain , panel : $panel -> getName (), parent : $domain -> id_parent_domain ) && ( strlen ( string : $domain -> domain ) > $maxDomainName )) {
2022-01-31 21:00:24 +01:00
$maxDomainName = strlen ( string : $domain -> domain );
}
}
$domainCount = 0 ;
foreach ( $domains as $domain ) {
2022-02-01 20:41:08 +01:00
if ( $this -> isValidSecondLevelDomain ( domainName : $domain -> domain , panel : $panel -> getName (), parent : $domain -> id_parent_domain )) {
2022-02-22 13:57:19 +01:00
echo COLOR_DEFAULT . " Domain: " . COLOR_YELLOW . str_pad ( string : $domain -> domain , length : $maxDomainName );
2022-03-01 16:01:21 +01:00
$this -> checkNS ( domainName : $domain -> domain , panel : $panel );
2022-01-31 21:00:24 +01:00
$domainCount ++ ;
}
}
if ( $domainCount == 0 ) {
2022-02-22 13:57:19 +01:00
echo 'No second level domains found.' . COLOR_DEFAULT . PHP_EOL ;
2022-01-25 20:31:31 +01:00
}
2022-02-22 13:57:19 +01:00
echo PHP_EOL ;
2022-01-25 20:31:31 +01:00
}
2022-01-31 21:00:24 +01:00
function isValidSecondLevelDomain ( string $domainName , string $panel , int $parent ) : bool
2022-01-25 20:31:31 +01:00
{
2022-02-06 17:59:43 +01:00
if ( $this -> config [ 'debug' ]) {
$this -> log -> debug ( message : " isValidSecondLevelDomain() " );
}
2022-01-31 21:00:24 +01:00
// subdomain
if ( $parent != 0 ) {
return false ;
}
2022-01-25 20:31:31 +01:00
2022-01-31 21:00:24 +01:00
// system domain
if ( str_contains ( haystack : $domainName , needle : $panel )) {
return false ;
}
2022-01-26 19:01:52 +01:00
2022-01-31 21:00:24 +01:00
// 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 ;
}
/**
2022-02-13 16:35:02 +01:00
* @ param String $domainName
* @ param \App\Entity\Panel $panel
2022-01-31 21:00:24 +01:00
*
* @ return void
*/
2022-02-13 16:35:02 +01:00
function checkNS ( string $domainName , Panel $panel )
2022-01-31 21:00:24 +01:00
{
2022-02-06 17:59:43 +01:00
if ( $this -> config [ 'debug' ]) {
$this -> log -> debug ( message : " checkNS() " );
}
2022-01-31 21:00:24 +01:00
try {
2022-02-01 20:41:08 +01:00
$nameServers = $this -> nameserverRepository -> findAll ();
2022-01-31 21:00:24 +01:00
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
exit ( 1 );
}
foreach ( $nameServers as $nameServer ) {
try {
2022-02-01 20:41:08 +01:00
echo COLOR_YELLOW . ' ' . $nameServer -> getName ();
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
exit ( 1 );
}
try {
if ( ! empty ( $nameServer -> getName ())) {
2022-02-06 17:59:43 +01:00
$result = $this -> apiController -> sendCommand (
2022-01-31 21:00:24 +01:00
requestType : 'GET' ,
2022-02-22 13:57:19 +01:00
serverName : $nameServer -> getName (),
versionIP : 6 ,
apiKey : $nameServer -> getApikey (),
command : 'domains/name/' . $domainName ,
serverType : 'nameserver' );
2022-01-31 21:00:24 +01:00
} else {
2022-02-06 17:59:43 +01:00
$result = $this -> apiController -> sendCommand (
2022-01-31 21:00:24 +01:00
requestType : 'GET' ,
2022-02-22 13:57:19 +01:00
serverName : $nameServer -> getName (),
versionIP : 4 ,
apiKey : $nameServer -> getApikey (),
command : 'domains/name/' ,
serverType : 'nameserver' . $domainName );
2022-01-23 15:22:20 +01:00
}
2022-01-31 21:00:24 +01:00
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
exit ( 1 );
2022-01-22 18:52:17 +01:00
}
2022-02-12 19:37:24 +01:00
switch ( $result [ 'header' ]) {
case 200 :
2022-02-13 16:35:02 +01:00
echo COLOR_GREEN . ' OK' ;
2022-02-12 19:37:24 +01:00
break ;
case 404 :
2022-02-13 16:35:02 +01:00
echo COLOR_RED . ' ' . $result [ 'header' ] . COLOR_DEFAULT ;
2022-02-12 19:37:24 +01:00
$arguments = $this -> parseArguments ();
if ( ! empty ( $arguments [ 'fix' ]) && $arguments [ 'fix' ] == 'yes' ) {
2022-02-22 16:15:29 +01:00
echo ' trying to fix …' ;
2022-02-12 19:37:24 +01:00
$body = [
2022-03-01 16:01:21 +01:00
'name' => $domainName ,
'panel' => $panel -> getName (),
2022-02-22 13:57:19 +01:00
];
2022-02-12 19:37:24 +01:00
try {
2022-02-13 16:35:02 +01:00
if ( ! empty ( $nameServer -> getAaaa ())) {
$create = $this -> apiController -> sendCommand (
2022-02-12 19:37:24 +01:00
requestType : 'POST' ,
2022-02-22 13:57:19 +01:00
serverName : $nameServer -> getName (),
versionIP : 6 ,
apiKey : $nameServer -> getApikey (),
command : 'domains' ,
serverType : 'nameserver' ,
body : $body );
2022-02-12 19:37:24 +01:00
} else {
2022-02-13 16:35:02 +01:00
$create = $this -> apiController -> sendCommand (
2022-02-12 19:37:24 +01:00
requestType : 'POST' ,
2022-02-22 13:57:19 +01:00
serverName : $nameServer -> getName (),
versionIP : 4 ,
apiKey : $nameServer -> getAPikey (),
command : 'domains' ,
serverType : 'nameserver' ,
body : $body );
2022-02-12 19:37:24 +01:00
}
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
exit ( 1 );
2022-01-31 21:00:24 +01:00
}
2022-02-22 16:15:29 +01:00
if ( $create [ 'header' ] != 201 ) {
2022-02-21 15:34:08 +01:00
die ( " make error handling " );
2022-02-22 16:15:29 +01:00
} else {
2022-02-27 18:26:06 +01:00
echo COLOR_GREEN . 'OK' . COLOR_DEFAULT ;
2022-02-21 15:34:08 +01:00
}
2022-01-31 21:00:24 +01:00
}
2022-02-12 19:37:24 +01:00
break ;
default :
echo 'Server error' . PHP_EOL ;
exit ( 1 );
2022-01-22 16:37:30 +01:00
}
}
2022-01-22 18:52:17 +01:00
echo PHP_EOL ;
2022-01-22 16:37:30 +01:00
}
2022-01-31 21:00:24 +01:00
/**
* @ return array
*/
public function parseArguments () : array
{
2022-02-06 17:59:43 +01:00
if ( $this -> config [ 'debug' ]) {
$this -> log -> debug ( message : " parseArguments() " );
}
2022-01-31 21:00:24 +01:00
$arguments = [];
foreach ( $this -> arguments as $argument ) {
if ( str_contains ( haystack : $argument , needle : '=' )) {
[ $key , $value ] = explode ( separator : '=' , string : $argument );
$arguments [ strtolower ( string : $key )] = $value ;
} else {
$arguments [ strtolower ( string : $argument )] = $argument ;
}
}
return $arguments ;
}
2022-01-22 16:37:30 +01:00
2022-01-20 19:55:04 +01:00
/**
2022-01-31 21:00:24 +01:00
* @ param string $subcommand
*
2022-01-20 19:55:04 +01:00
* @ return void
*/
2022-01-31 21:00:24 +01:00
public function handlePanels ( string $subcommand ) : void
2022-01-20 19:55:04 +01:00
{
2022-02-06 17:59:43 +01:00
if ( $this -> config [ 'debug' ]) {
$this -> log -> debug ( message : " handlePanels() " );
}
2022-01-31 21:00:24 +01:00
try {
match ( $subcommand ) {
'create' => $this -> handlePanelsCreate (),
'list' => $this -> handlePanelsList (),
'update' => $this -> handlePanelsUpdate (),
'delete' => $this -> handlePanelsDelete (),
'apiping' => $this -> handleAPIPing ( type : 'panel' )
};
} catch ( UnhandledMatchError ) {
echo 'Unknown action: ' . $subcommand . PHP_EOL ;
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
2022-01-20 19:55:04 +01:00
}
}
2022-01-22 13:16:26 +01:00
/**
* @ return void
*/
2022-01-31 21:00:24 +01:00
function handlePanelsCreate () : void
2022-01-22 13:16:26 +01:00
{
2022-02-06 17:59:43 +01:00
if ( $this -> config [ 'debug' ]) {
$this -> log -> debug ( message : " handlePanelsCreate() " );
}
2022-01-23 15:22:20 +01:00
$name = $this -> arguments [ 1 ] ? ? '' ;
2022-01-22 13:16:26 +01:00
if ( empty ( $name )) {
2022-01-31 21:00:24 +01:00
echo 'You need to supply the panel name.' . PHP_EOL ;
2022-01-22 13:16:26 +01:00
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 {
2022-01-31 21:00:24 +01:00
echo " $name is no valid DNS domain name. " . PHP_EOL ;
2022-01-22 13:16:26 +01:00
exit ( 1 );
}
$arguments = $this -> parseArguments ();
$a = $arguments [ 'a' ] ? ? '' ;
$aaaa = $arguments [ 'aaaa' ] ? ? '' ;
if ( empty ( $a ) && empty ( $aaaa )) {
echo 'At least one IP address is required.' . PHP_EOL ;
exit ( 0 );
}
2022-01-31 21:00:24 +01:00
$apikey = $arguments [ 'apikey' ] ? ? '' ;
2022-01-22 13:16:26 +01:00
2022-01-31 21:00:24 +01:00
try {
if ( $this -> panelRepository -> findByName ( name : $name )) {
echo " Panel: $name already exists. " . PHP_EOL ;
exit ( 1 );
2022-01-29 14:55:40 +01:00
} else {
2022-01-31 21:00:24 +01:00
$result = $this -> panelRepository -> insert ( name : $name , a : $a , aaaa : $aaaa , apikey : $apikey );
echo " Panel $name has been created with id $result " . PHP_EOL ;
exit ( 0 );
2022-01-20 19:55:04 +01:00
}
2022-01-31 21:00:24 +01:00
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
exit ( 1 );
2022-01-20 19:55:04 +01:00
}
}
/**
* @ return void
*/
function handlePanelsList () : void
{
2022-02-06 17:59:43 +01:00
if ( $this -> config [ 'debug' ]) {
$this -> log -> debug ( message : " handlePanelsList() " );
}
2022-01-20 19:55:04 +01:00
echo 'All available panels:' . PHP_EOL ;
2022-01-31 21:00:24 +01:00
try {
$panels = $this -> panelRepository -> findAll ();
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
}
if ( ! empty ( $panels )) {
2022-01-20 19:55:04 +01:00
$table = new ConsoleTable ();
2022-01-26 19:01:52 +01:00
$table -> setHeaders ( content : [ 'ID' , 'Name' , 'A' , 'AAAA' , 'API Key' ]);
2022-01-20 19:55:04 +01:00
foreach ( $panels as $panel ) {
2022-01-31 21:00:24 +01:00
$row = [];
try {
$token = strtok ( string : $panel -> getApikey (), token : '.' );
$row [] = $panel -> getID ();
$row [] = $panel -> getName ();
$row [] = $panel -> getA ();
$row [] = $panel -> getAaaa ();
$row [] = $token ;
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
exit ( 1 );
}
$table -> addRow ( data : $row );
2022-01-20 19:55:04 +01:00
}
$table -> setPadding ( value : 2 );
$table -> display ();
} else {
echo 'No panels found.' . PHP_EOL ;
exit ( 1 );
}
2022-01-19 21:07:22 +01:00
exit ( 0 );
}
2022-02-06 17:59:43 +01:00
2022-01-31 21:00:24 +01:00
/**
* @ throws \DI\DependencyException
* @ throws \DI\NotFoundException
*/
2022-01-20 19:55:04 +01:00
function handlePanelsUpdate ()
{
2022-02-06 17:59:43 +01:00
if ( $this -> config [ 'debug' ]) {
$this -> log -> debug ( message : " handlePanelsUpdate() " );
}
2022-01-20 19:55:04 +01:00
$arguments = $this -> parseArguments ();
2022-03-21 14:32:10 +01:00
$id = intval ( value : $this -> arguments [ 1 ]) ? ? 0 ;
2022-01-20 19:55:04 +01:00
$name = $arguments [ 'name' ] ? ? '' ;
$a = $arguments [ 'a' ] ? ? '' ;
$aaaa = $arguments [ 'aaaa' ] ? ? '' ;
$apikey = $arguments [ 'apikey' ] ? ? '' ;
if ( $id == 0 ) {
echo 'An ID is required' . PHP_EOL ;
exit ( 1 );
}
2022-03-21 14:32:10 +01:00
if ( ! $this -> panelRepository -> findByID ( id : $id )) {
2022-01-20 19:55:04 +01:00
echo " Panel with ID : $id doesn't exist. " . PHP_EOL ;
exit ( 1 );
}
2022-03-21 14:32:10 +01:00
if ( $this -> panelRepository -> update ( id : $id , name : $name , a : $a , aaaa : $aaaa , apikey : $apikey ) !== false ) {
2022-01-20 19:55:04 +01:00
echo 'Panel has been updated' . PHP_EOL ;
} else {
2022-01-31 21:00:24 +01:00
echo 'Error while updating domain server.' . PHP_EOL ;
}
}
/**
* @ throws \DI\DependencyException
* @ throws \DI\NotFoundException
*/
function handlePanelsDelete ()
{
2022-02-06 17:59:43 +01:00
if ( $this -> config [ 'debug' ]) {
$this -> log -> debug ( message : " handlePanelsDelete() " );
}
2022-01-31 21:00:24 +01:00
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 ;
}
/**
* @ throws \DI\NotFoundException
* @ throws \DI\DependencyException
*/
function handleAPIPing ( string $type )
{
2022-02-06 17:59:43 +01:00
if ( $this -> config [ 'debug' ]) {
$this -> log -> debug ( message : " handleApiPing() " );
}
2022-01-31 21:00:24 +01:00
$error = false ;
$id = $this -> getId ();
if ( $id != 0 ) {
if ( $type == 'panel' ) {
$server = $this -> panelController -> findByID ( id : $id );
} else {
$server = $this -> nameserverController -> findByID ( id : $id );
}
if ( $server ) {
if ( ! $this -> checkPing ( server : $server , type : $type )) {
$error = true ;
}
} else {
if ( $this -> config [ 'verbose' ]) {
echo " Unknown $type ID: $id " . PHP_EOL ;
}
$error = true ;
}
} else {
if ( $type == 'panel' ) {
2022-02-21 14:11:47 +01:00
$servers = $this -> panelRepository -> findAll ();
2022-01-31 21:00:24 +01:00
} else {
2022-02-21 14:11:47 +01:00
$servers = $this -> nameserverRepository -> findAll ();
2022-01-31 21:00:24 +01:00
}
foreach ( $servers as $server ) {
if ( ! $this -> checkPing ( server : $server , type : $type )) {
$error = true ;
}
}
}
echo PHP_EOL ;
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 ;
2022-01-20 19:55:04 +01:00
}
2022-01-31 21:00:24 +01:00
return $id ;
2022-01-20 19:55:04 +01:00
}
2022-01-31 21:00:24 +01:00
/**
2022-02-21 14:11:47 +01:00
* @ param \App\Entity\Panel | \App\Entity\Nameserver $server
* @ param String $type
2022-01-31 21:00:24 +01:00
*
* @ return bool
*/
2022-02-21 14:11:47 +01:00
public function checkPing ( Panel | Nameserver $server , string $type ) : bool
2022-01-20 19:55:04 +01:00
{
2022-01-31 21:00:24 +01:00
$error = false ;
try {
if ( $type == 'nameserver' ) {
2022-02-21 14:11:47 +01:00
$maxName = $this -> nameserverRepository -> getLongestEntry ( field : 'name' );
$maxA = $this -> nameserverRepository -> getLongestEntry ( field : 'a' );
$maxAAAA = $this -> nameserverRepository -> getLongestEntry ( field : 'aaaa' );
2022-01-31 21:00:24 +01:00
} else {
2022-02-21 14:11:47 +01:00
$maxName = $this -> panelRepository -> getLongestEntry ( field : 'name' );
$maxA = $this -> panelRepository -> getLongestEntry ( field : 'a' );
$maxAAAA = $this -> panelRepository -> getLongestEntry ( field : 'aaaa' );
2022-01-31 21:00:24 +01:00
}
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
2022-01-20 19:55:04 +01:00
exit ( 1 );
}
2022-01-31 21:00:24 +01:00
if ( $this -> config [ 'verbose' ]) {
2022-03-01 18:17:57 +01:00
echo COLOR_YELLOW . str_pad ( string : $server -> getName (), length : $maxName );
2022-01-20 19:55:04 +01:00
}
2022-01-31 21:00:24 +01:00
2022-02-21 14:11:47 +01:00
$a = $server -> getA () ? ? '' ;
2022-01-31 21:00:24 +01:00
if ( ! empty ( $a )) {
if ( $this -> config [ 'verbose' ]) {
echo COLOR_DEFAULT . ' ' . str_pad ( string : $a , length : $maxA , pad_type : STR_PAD_LEFT ) . ' ' ;
}
try {
2022-02-06 17:59:43 +01:00
if ( $result = $this -> apiController -> sendCommand (
2022-01-31 21:00:24 +01:00
requestType : 'GET' ,
2022-02-22 13:57:19 +01:00
serverName : $server -> getName (),
versionIP : 4 ,
apiKey : $server -> getApikey (),
command : 'ping' ,
serverType : $type )) {
2022-01-31 21:00:24 +01:00
if ( $this -> config [ 'verbose' ]) {
if ( $result [ 'data' ] == 'pong' ) {
echo COLOR_GREEN . $result [ 'data' ];
} else {
echo COLOR_BLUE . 'skip' ;
}
}
} else {
$error = true ;
}
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
}
}
2022-02-21 14:11:47 +01:00
$aaaa = $server -> getAaaa () ? ? '' ;
2022-01-31 21:00:24 +01:00
if ( ! empty ( $aaaa )) {
if ( $this -> config [ 'verbose' ]) {
echo COLOR_DEFAULT . ' ' . str_pad ( string : $aaaa , length : $maxAAAA , pad_type : STR_PAD_LEFT ) . ' ' ;
}
try {
2022-02-06 17:59:43 +01:00
if ( $result = $this -> apiController -> sendCommand (
2022-01-31 21:00:24 +01:00
requestType : 'GET' ,
2022-02-22 13:57:19 +01:00
serverName : $server -> getName (),
versionIP : 6 ,
apiKey : $server -> getApikey (),
command : 'ping' ,
serverType : $type )) {
2022-01-31 21:00:24 +01:00
if ( $this -> config [ 'verbose' ]) {
if ( $result [ 'data' ] == 'pong' ) {
echo COLOR_GREEN . $result [ 'data' ];
} else {
echo COLOR_BLUE . $result [ 'data' ]; // TODO 'skip';
}
}
} else {
$error = true ;
}
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
exit ( 1 );
}
}
if ( $this -> config [ 'verbose' ]) {
echo PHP_EOL ;
2022-01-20 19:55:04 +01:00
}
2022-01-31 21:00:24 +01:00
return $error ;
2022-01-20 19:55:04 +01:00
}
2022-01-19 21:07:22 +01:00
/**
2022-01-20 19:55:04 +01:00
* @ param string $subcommand
2022-01-19 21:07:22 +01:00
*
* @ return void
*/
2022-01-20 19:55:04 +01:00
public function handleApiKeys ( string $subcommand ) : void
{
try {
match ( $subcommand ) {
'create' => $this -> handleApikeysCreate (),
'list' => $this -> handleApikeysList (),
2022-01-31 21:00:24 +01:00
'update' => $this -> handleApikeysUpdate (),
2022-01-20 19:55:04 +01:00
'delete' => $this -> handleApikeysDelete (),
};
} catch ( UnhandledMatchError ) {
echo 'Unknown action: ' . $subcommand . PHP_EOL ;
2022-01-31 21:00:24 +01:00
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
exit ( 1 );
2022-01-20 19:55:04 +01:00
}
}
/**
* @ return void
*/
function handleApikeysCreate () : void
2022-01-19 21:07:22 +01:00
{
2022-01-25 20:31:31 +01:00
$arguments = $this -> parseArguments ();
$name = $arguments [ 'name' ] ? ? '' ;
2022-01-31 21:00:24 +01:00
try {
$result = $this -> apikeyRepository -> create ( name : $name );
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
exit ( 1 );
}
2022-01-19 21:07:22 +01:00
echo 'API key ' . $result [ 'row' ] . ' has been generated. Store it in a save place, it cannot be recovered.' . PHP_EOL ;
echo " \033 [32m \t " . $result [ 'tokenPrefix' ] . '.' . $result [ 'key' ] . PHP_EOL ;
exit ( 0 );
}
/**
* @ return void
*/
2022-01-20 19:55:04 +01:00
function handleApikeysList () : void
2022-01-19 21:07:22 +01:00
{
echo 'All valid API keys:' . PHP_EOL ;
2022-01-31 21:00:24 +01:00
try {
$keys = $this -> apikeyRepository -> findAll ();
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
exit ( 1 );
}
if ( ! empty ( $keys )) {
2022-01-19 21:07:22 +01:00
$table = new ConsoleTable ();
2022-01-26 19:01:52 +01:00
$table -> setHeaders ( content : [ 'ID' , 'Name' , 'API key prefix' ]);
2022-01-19 21:07:22 +01:00
foreach ( $keys as $key ) {
2022-01-31 21:00:24 +01:00
$row = [];
try {
$row [] = $key -> getID ();
$row [] = $key -> getName ();
$row [] = $key -> getApiTokenPrefix ();
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
exit ( 1 );
}
$table -> addRow ( data : $row );
2022-01-19 21:07:22 +01:00
}
2022-01-20 19:55:04 +01:00
$table -> setPadding ( value : 2 );
2022-01-19 21:07:22 +01:00
$table -> display ();
} else {
echo 'No keys found.' . PHP_EOL ;
}
exit ( 0 );
}
2022-01-31 21:00:24 +01:00
/**
* @ throws \DI\DependencyException
* @ throws \DI\NotFoundException
*/
function handleApikeysUpdate ()
{
$arguments = $this -> parseArguments ();
$id = $this -> arguments [ 1 ] ? ? 0 ;
$name = $arguments [ 'name' ] ? ? '' ;
if ( $id == 0 ) {
echo 'An ID is required' . PHP_EOL ;
exit ( 1 );
}
if ( empty ( $name )) {
echo 'You need to supply the new name.' . 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 ( $this -> apikeyRepository -> update ( id : intval ( value : $id ), name : $name ) !== false ) {
echo 'Apikey has been updated' . PHP_EOL ;
} else {
echo 'Error while updating apikey.' . PHP_EOL ;
}
}
2022-01-19 21:07:22 +01:00
/**
* @ return void
*/
2022-01-20 19:55:04 +01:00
function handleApikeysDelete () : void
2022-01-19 21:07:22 +01:00
{
2022-01-25 20:31:31 +01:00
$id = intval ( value : $this -> arguments [ 1 ] ? ? 0 );
2022-01-19 21:07:22 +01:00
if ( $id == 0 ) {
2022-01-20 19:55:04 +01:00
echo 'You need to add the ID of the API key.' . PHP_EOL ;
2022-01-19 21:07:22 +01:00
exit ( 1 );
}
2022-01-31 21:00:24 +01:00
try {
if ( $this -> apikeyRepository -> findByID ( id : $id )) {
$this -> apikeyRepository -> delete ( id : $id );
echo 'API key ' . $id . ' has been deleted.' . PHP_EOL ;
exit ( 0 );
} else {
echo 'Unknown ID: ' . $id . PHP_EOL ;
exit ( 1 );
}
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
2022-01-19 21:07:22 +01:00
}
}
/**
2022-01-20 19:55:04 +01:00
* @ param string $subcommand
2022-01-19 21:07:22 +01:00
*
* @ return void
*/
2022-01-20 19:55:04 +01:00
public function handleDomains ( string $subcommand ) : void
{
try {
match ( $subcommand ) {
'list' => $this -> handleDomainsList (),
'create' => $this -> handleDomainsCreate (),
'update' => $this -> handleDomainsUpdate (),
'delete' => $this -> handleDomainsDelete (),
2022-03-21 14:32:10 +01:00
'dyndns' => $this -> handleDomainsDynDns (),
2022-01-20 19:55:04 +01:00
};
} catch ( UnhandledMatchError ) {
echo " Unknown Command: $subcommand " . PHP_EOL ;
exit ( 1 );
2022-01-31 21:00:24 +01:00
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
2022-01-20 19:55:04 +01:00
}
}
/**
* @ return void
*/
function handleDomainsList () : void
2022-01-19 21:07:22 +01:00
{
echo 'All available domains:' . PHP_EOL ;
2022-01-31 21:00:24 +01:00
try {
$domains = $this -> domainRepository -> findAll ();
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
exit ( 1 );
}
if ( ! empty ( $domains )) {
2022-01-19 21:07:22 +01:00
$table = new ConsoleTable ();
2022-02-22 13:57:19 +01:00
$table -> setHeaders ( content : [ 'ID' , 'Name' , 'Panel' ]);
2022-01-31 21:00:24 +01:00
/** @var Domain $domain */
2022-01-19 21:07:22 +01:00
foreach ( $domains as $domain ) {
2022-01-31 21:00:24 +01:00
$row = [];
try {
$row [] = $domain -> getId ();
$row [] = $domain -> getName ();
2022-02-22 13:57:19 +01:00
$row [] = $domain -> getPanel ();
2022-01-31 21:00:24 +01:00
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
}
$table -> addRow ( data : $row );
2022-01-19 21:07:22 +01:00
}
2022-01-20 19:55:04 +01:00
$table -> setPadding ( value : 2 );
2022-01-19 21:07:22 +01:00
$table -> display ();
}
exit ( 0 );
}
2022-01-31 21:00:24 +01:00
2022-01-19 21:07:22 +01:00
/**
* @ return void
*/
2022-01-20 19:55:04 +01:00
function handleDomainsCreate () : void
2022-01-19 21:07:22 +01:00
{
2022-01-23 15:22:20 +01:00
$name = $this -> arguments [ 1 ] ? ? " " ;
2022-01-19 21:07:22 +01:00
if ( empty ( $name )) {
echo 'You need to supply the domain name.' . PHP_EOL ;
exit ( 1 );
}
2022-01-20 19:55:04 +01:00
$filteredName = filter_var ( value : $name , filter : FILTER_VALIDATE_DOMAIN , options : FILTER_FLAG_HOSTNAME );
if ( ! empty ( $filteredName ) && str_contains ( haystack : $filteredName , needle : '.' )) {
2022-01-19 21:07:22 +01:00
$name = $filteredName ;
} else {
echo " $name is no valid DNS domain name. " . PHP_EOL ;
exit ( 1 );
}
2022-01-20 19:55:04 +01:00
$arguments = $this -> parseArguments ();
2022-02-12 19:37:24 +01:00
$panel = $arguments [ 'panel' ] ? ? '' ;
2022-02-22 13:57:19 +01:00
if ( empty ( $panel )) {
echo 'You need to supply the panel name.' . PHP_EOL ;
exit ( 1 );
2022-01-19 21:07:22 +01:00
}
2022-01-31 21:00:24 +01:00
try {
if ( $this -> domainRepository -> findByName ( name : $name )) {
echo " Domain: $name already exists. " . PHP_EOL ;
exit ( 1 );
} else {
2022-03-01 16:01:21 +01:00
if ( ! $this -> panelRepository -> findByName ( name : $panel )) {
2022-02-22 13:57:19 +01:00
echo 'Unknown panel: ' . $panel ;
exit ( 1 );
2022-01-31 21:00:24 +01:00
}
2022-02-22 13:57:19 +01:00
$domain = new Domain ( name : $name , panel : $panel );
2022-02-06 17:59:43 +01:00
$result = $this -> domainRepository -> insert ( domain : $domain );
echo " Domain $name has been created with id $result " . PHP_EOL ;
2022-02-12 19:37:24 +01:00
$this -> domainController -> createSlaveZoneFile ( domain : $domain );
2022-01-31 21:00:24 +01:00
exit ( 0 );
}
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
2022-01-19 21:07:22 +01:00
}
}
2022-01-31 21:00:24 +01:00
/**
* @ throws \DI\DependencyException
* @ throws \DI\NotFoundException
*/
2022-01-20 19:55:04 +01:00
function handleDomainsUpdate ()
2022-01-19 21:07:22 +01:00
{
2022-01-20 19:55:04 +01:00
$arguments = $this -> parseArguments ();
2022-01-26 19:35:14 +01:00
$id = intval ( value : $this -> arguments [ 1 ] ? ? 0 );
2022-01-20 19:55:04 +01:00
$name = $arguments [ 'name' ] ? ? '' ;
2022-02-22 13:57:19 +01:00
$panelName = $arguments [ 'panel' ] ? ? '' ;
2022-01-20 19:55:04 +01:00
if ( $id == 0 ) {
echo 'An ID is required' . PHP_EOL ;
exit ( 1 );
}
2022-01-31 21:00:24 +01:00
if ( ! $domain = $this -> domainRepository -> findByID ( id : $id )) {
echo " Domain with ID : $id doesn't exist. " . PHP_EOL ;
exit ( 1 );
2022-01-20 19:55:04 +01:00
}
2022-02-12 19:37:24 +01:00
2022-02-22 13:57:19 +01:00
if ( ! empty ( $panelName )) {
$panel = $this -> panelRepository -> findByName ( name : $panelName );
2022-02-12 19:37:24 +01:00
}
2022-02-22 13:57:19 +01:00
if ( empty ( $name ) && empty ( $panel )) {
2022-02-21 14:11:47 +01:00
echo 'No name or panel given, just recreate the config file' . PHP_EOL ;
$this -> domainController -> createSlaveZoneFile ( domain : $domain );
2022-02-12 19:37:24 +01:00
exit ( 1 );
}
2022-02-22 13:57:19 +01:00
$newDomain = new Domain ( name : $name , panel : $panelName , id : $domain -> getId ());
2022-02-06 17:59:43 +01:00
if ( $this -> domainRepository -> update ( domain : $newDomain ) !== false ) {
2022-01-20 19:55:04 +01:00
echo 'Domain server has been updated' . PHP_EOL ;
2022-02-12 19:37:24 +01:00
$this -> domainController -> createSlaveZoneFile ( domain : $domain );
2022-01-20 19:55:04 +01:00
} else {
echo 'Error while updating domain server.' . PHP_EOL ;
2022-01-19 21:07:22 +01:00
}
}
2022-02-06 17:59:43 +01:00
2022-01-31 21:00:24 +01:00
/**
* @ throws \DI\DependencyException
* @ throws \DI\NotFoundException
*/
function handleDomainsDelete ()
{
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 );
}
2022-02-06 17:59:43 +01:00
if ( ! $domain = $this -> domainRepository -> findByID ( id : $id )) {
2022-01-31 21:00:24 +01:00
echo " There is no domain with ID $id . " . PHP_EOL ;
exit ( 1 );
}
2022-02-12 19:37:24 +01:00
$this -> domainRepository -> delete ( domain : $domain );
2022-02-06 17:59:43 +01:00
$this -> domainController -> deleteZone ( domain : $domain );
2022-01-31 21:00:24 +01:00
echo " The domain with ID $id has been deleted. " . PHP_EOL ;
}
2022-02-06 17:59:43 +01:00
2022-01-31 21:00:24 +01:00
/**
* @ param string $subcommand
*
* @ return void
*/
2022-02-06 17:59:43 +01:00
public
function handleNameservers ( string $subcommand ) : void
2022-01-31 21:00:24 +01:00
{
try {
match ( $subcommand ) {
'create' => $this -> handleNameserversCreate (),
'list' => $this -> handleNameserversList (),
'update' => $this -> handleNameserversUpdate (),
'delete' => $this -> handleNameserversDelete (),
'apiping' => $this -> handleAPIPing ( type : 'nameserver' )
};
} catch ( UnhandledMatchError ) {
echo 'Unknown action: ' . $subcommand . PHP_EOL ;
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
}
}
2022-02-06 17:59:43 +01:00
2022-01-31 21:00:24 +01:00
/**
* @ return void
*/
function handleNameserversCreate () : void
{
$name = $this -> arguments [ 1 ] ? ? '' ;
if ( empty ( $name )) {
echo 'You need to supply the nameserver name.' . PHP_EOL ;
exit ( 1 );
}
$filteredName = filter_var ( value : $name , filter : FILTER_VALIDATE_DOMAIN );
if ( ! empty ( $filteredName ) && str_contains ( haystack : $filteredName , needle : '.' )) {
$name = $filteredName ;
} else {
echo " $name is no valid nameserver name. " . PHP_EOL ;
exit ( 1 );
}
$arguments = $this -> parseArguments ();
$a = $arguments [ 'a' ] ? ? '' ;
$aaaa = $arguments [ 'aaaa' ] ? ? '' ;
if ( empty ( $a ) && empty ( $aaaa )) {
echo 'At least one IP address is required.' . PHP_EOL ;
exit ( 0 );
}
2022-02-06 19:36:20 +01:00
$apikey = $arguments [ 'apikey' ] ? ? '' ;
2022-01-31 21:00:24 +01:00
try {
if ( $this -> nameserverRepository -> findByName ( name : $name )) {
echo " Nameserver: $name already exists. " . PHP_EOL ;
exit ( 1 );
} else {
2022-02-06 17:59:43 +01:00
$nameserver = new Nameserver ( name : $name , a : $a , aaaa : $aaaa , apikey : $apikey );
$result = $this -> nameserverRepository -> insert ( nameserver : $nameserver );
2022-01-31 21:00:24 +01:00
echo " Nameserver $name has been created with id $result " . PHP_EOL ;
exit ( 0 );
}
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
exit ( 1 );
}
}
2022-02-06 17:59:43 +01:00
2022-01-31 21:00:24 +01:00
/**
* @ return void
*/
function handleNameserversList () : void
{
echo 'All available nameservers:' . PHP_EOL ;
try {
$nameservers = $this -> nameserverRepository -> findAll ();
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
}
if ( ! empty ( $nameservers )) {
$table = new ConsoleTable ();
$table -> setHeaders ( content : [ 'ID' , 'Name' , 'A' , 'AAAA' , 'API Key' ]);
foreach ( $nameservers as $nameserver ) {
$row = [];
try {
$token = strtok ( string : $nameserver -> getApiKey (), token : '.' );
$row [] = $nameserver -> getId ();
$row [] = $nameserver -> getName ();
$row [] = $nameserver -> getA ();
$row [] = $nameserver -> getAaaa ();
$row [] = $token ;
} catch ( DependencyException | NotFoundException $e ) {
echo $e -> getMessage ();
exit ( 1 );
}
$table -> addRow ( data : $row );
}
$table -> setPadding ( value : 2 );
$table -> display ();
} else {
echo 'No nameservers found.' . PHP_EOL ;
exit ( 1 );
}
exit ( 0 );
}
2022-01-22 13:16:26 +01:00
2022-02-06 17:59:43 +01:00
2022-01-31 21:00:24 +01:00
/**
* @ throws \DI\DependencyException
* @ throws \DI\NotFoundException
*/
2022-01-22 13:16:26 +01:00
function handleNameserversUpdate ()
{
$arguments = $this -> parseArguments ();
2022-01-31 21:00:24 +01:00
2022-01-23 15:22:20 +01:00
$id = $this -> arguments [ 1 ] ? ? 0 ;
2022-01-22 13:16:26 +01:00
$name = $arguments [ 'name' ] ? ? '' ;
$a = $arguments [ 'a' ] ? ? '' ;
$aaaa = $arguments [ 'aaaa' ] ? ? '' ;
$apikey = $arguments [ 'apikey' ] ? ? '' ;
if ( $id == 0 ) {
echo 'An ID is required' . PHP_EOL ;
exit ( 1 );
}
2022-01-31 21:00:24 +01:00
if ( ! $this -> nameserverRepository -> findByID ( id : intval ( value : $id ))) {
2022-01-22 13:16:26 +01:00
echo " Nameserver with ID : $id doesn't exist. " . PHP_EOL ;
exit ( 1 );
}
2022-01-31 21:00:24 +01:00
if ( $this -> nameserverRepository -> update ( id : intval ( value : $id ), name : $name , a : $a , aaaa : $aaaa , apikey : $apikey ) !== false ) {
2022-01-22 13:16:26 +01:00
echo 'Nameserver server has been updated' . PHP_EOL ;
} else {
echo 'Error while updating nameserver.' . PHP_EOL ;
}
}
2022-02-06 17:59:43 +01:00
2022-01-31 21:00:24 +01:00
/**
* @ throws \DI\DependencyException
* @ throws \DI\NotFoundException
*/
2022-01-22 13:16:26 +01:00
function handleNameserversDelete ()
{
2022-01-23 15:22:20 +01:00
if ( empty ( $this -> arguments [ 1 ])) {
2022-01-22 13:16:26 +01:00
echo " You need to supply an ID. " . PHP_EOL ;
exit ( 1 );
}
2022-01-26 19:01:52 +01:00
$id = intval ( value : $this -> arguments [ 1 ]) ? ? 0 ;
2022-01-22 13:16:26 +01:00
if ( $id == 0 ) {
echo " Nameserver with ID $id not found. " . PHP_EOL ;
exit ( 1 );
}
2022-01-31 21:00:24 +01:00
if ( ! $this -> nameserverRepository -> findByID ( id : $id )) {
2022-01-22 13:16:26 +01:00
echo " There is no nameserver with ID $id . " . PHP_EOL ;
exit ( 1 );
}
2022-01-31 21:00:24 +01:00
$this -> nameserverRepository -> delete ( id : $id );
echo " The nameserver with ID $id has been deleted. " . PHP_EOL ;
2022-01-19 21:07:22 +01:00
}
2022-02-22 13:57:19 +01:00
/**
* @ throws \DI\DependencyException
* @ throws \DI\NotFoundException
*/
2022-03-01 16:01:21 +01:00
function handleCheckShowIncludes ()
2022-02-22 13:57:19 +01:00
{
$nameservers = $this -> nameserverRepository -> findAll ();
2022-03-01 18:17:57 +01:00
echo COLOR_DEFAULT . 'You need to add these lines to ' . COLOR_YELLOW . '/etc/bind/local.bindapi.options' . COLOR_DEFAULT . ' on every panel and make sure' . PHP_EOL ;
2022-02-22 13:57:19 +01:00
echo 'that ' . COLOR_YELLOW . 'include "/etc/bind/local.bindapi.options";' . COLOR_DEFAULT . ' exists in ' . COLOR_YELLOW . '/etc/bind/named.conf.options' . COLOR_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 ' . COLOR_YELLOW . 'named-checkconf' . COLOR_DEFAULT . ' to ensure there were no errors.' . PHP_EOL ;
2022-03-01 18:17:57 +01:00
echo PHP_EOL . 'Run ' . COLOR_YELLOW . 'rndc reload' . COLOR_DEFAULT . ' to activate the changes' . PHP_EOL ;
2022-02-22 13:57:19 +01:00
}
2022-03-01 16:01:21 +01:00
/**
* @ throws \DI\DependencyException
* @ throws \DI\NotFoundException
*/
function handleCheckDomains ()
{
$this -> domainController -> checkDomains ();
}
2022-03-21 14:32:10 +01:00
/**
* @ throws \DI\NotFoundException
* @ throws \DI\DependencyException
*/
private function handleDomainsDynDns ()
{
$hostname = $this -> arguments [ 1 ] ? ? '' ;
if ( empty ( $hostname )) {
echo 'You need to supply at least the hostname' . PHP_EOL ;
exit ( 1 );
}
if ( $this -> config [ 'verbose' ]) {
echo " Updating DynDNS host: $hostname " . PHP_EOL ;
}
$nameserver = $this -> nameserverRepository -> findFirst ();
if ( ! empty ( $nameserver -> getAaaa ())) {
$result = $this -> apiController -> sendCommand (
requestType : 'POST' ,
serverName : $nameserver -> getName (),
versionIP : 6 ,
apiKey : $nameserver -> getApikey (),
command : 'dyndns/' . $hostname ,
serverType : 'nameserver' );
} else {
$result = $this -> apiController -> sendCommand (
requestType : 'POST' ,
serverName : $nameserver -> getName (),
versionIP : 4 ,
apiKey : $nameserver -> getApikey (),
command : 'dyndns/' . $hostname ,
serverType : 'nameserver' );
}
if ( $result [ 'header' ] == 200 ) {
if ( $this -> config [ 'verbose' ]) {
$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 );
}
2022-01-19 21:07:22 +01:00
}