bindAPI/src/Controller/RequestController.php

672 lines
24 KiB
PHP

<?php declare(strict_types=1);
namespace App\Controller;
error_reporting(error_level: E_ALL);
use App\Entity\Domain;
use App\Entity\DynDNS;
use App\Repository\ApikeyRepository;
use App\Repository\DomainRepository;
use App\Repository\DynDNSRepository;
use App\Repository\PanelRepository;
use App\Service\ApiClient;
use Monolog\Logger;
use OpenApi\Attributes as OA;
use OpenApi\Attributes\OpenApi;
use OpenApi\Generator;
use UnhandledMatchError;
use function Symfony\Component\String\s;
// TODO attributes for swaggerUI
#[OA\Info(version: VERSION, title: 'bindAPI')]
#[OA\Server(
url: "{schema}://{hostname}/api",
description: "The bindAPI URL.",
variables: [
new OA\ServerVariable(
serverVariable: 'schema',
default: 'https',
enum: ['http', 'https']
),
new OA\ServerVariable(
serverVariable: 'hostname',
default: DEFAULT_NS,
enum: NAMESERVERS
)
]
)]
#[OA\Tag(
name: "Server"
)]
#[OA\SecurityScheme(
securityScheme: "Authorization",
type: "apiKey",
description: "Api Authentication",
name: "X-API-Key",
in: "header"
)]
class RequestController
{
private string $status;
private string $response;
private string $message;
private array $result;
private string $requestMethod;
private array $uri;
// server tag
private string $baseDir;
#[OA\Get(
path: '/ping',
operationId: 'ping',
description: 'Checks for connectivity and valid APIkey',
security: [
['Authorization' => []]
],
tags: ['Server'],
responses: [
new OA\Response(
response: 200,
description: 'OK'
),
new OA\Response(
response: 401,
description: 'API key is missing or invalid.'
)
]
)]
private function handlePing(): void
{
if ($this->validateApiKey()) {
$this->status = '200 OK';
$this->response = 'pong';
} else {
$this->status = '401 Unauthorized';
$this->message = 'API key is missing or invalid';
}
}
#[OA\Get(
path: '/version',
operationId: 'version',
description: 'Check the API version of the nameserver.',
security: [
['Authorization' => []]
],
tags: ['Server'],
responses: [
new OA\Response(
response: 200,
description: 'x.y.z, aka major, minor, patch'
),
new OA\Response(
response: 401,
description: 'API key is missing or invalid.'
)
]
)]
private function getVersion(): void
{
if ($this->validateApiKey()) {
$this->status = '200 OK';
$composerJson = json_decode(json: file_get_contents(filename: $this->baseDir . 'composer.json'));
$version = $composerJson->version;
$buildNumber = $composerJson->build_number;
$this->result = [
'version' => $version,
'buildnumber' => $buildNumber,
];
} else {
$this->status = '401 Unauthorized';
$this->message = 'API key is missing or invalid';
}
}
#[OA\Get(
path: '/domains',
operationId: 'getAllDomains',
description: 'Returns a list of all domains on this server.',
summary: 'List all domains.',
security: [
['Authorization' => []]
],
tags: ['Domains'],
responses: [
new OA\Response(
response: 200,
description: 'OK'
),
new OA\Response(
response: 401,
description: 'API key is missing or invalid.'
),
new OA\Response(
response: 404,
description: 'Domain not found.'
)]
)]
private function handleAllDomainsGetRequest(): void
{
$domains = $this->domainRepository->findAll();
$resultDomain = [];
foreach ($domains as $singleDomain) {
$domain = [
'id' => $singleDomain->getId(),
'name' => $singleDomain->getName(),
'panel' => $singleDomain->getPanel()
];
$resultDomain[] = $domain;
}
$this->result = $resultDomain;
}
/**
*/
/**
* @return void
*/
private function handleDomains(): void
{
if ($this->validateApiKey()) {
try {
match ($this->requestMethod) {
'GET' => $this->handleDomainsGetRequest(),
'POST' => $this->handleDomainsPostRequest(),
'PUT' => $this->handleDomainsPutRequest(),
'DELETE' => $this->handleDomainsDeleteRequest()
};
} catch (UnhandledMatchError) {
$this->status = '400 Bad Request';
$this->message = "unknown request method: $this->requestMethod";
}
}
}
private function validateApiKey(): bool
{
$headers = array_change_key_case(array: getallheaders(), case: CASE_UPPER);
$apiKey = $headers['X-API-KEY'] ?? '';
if (empty($apiKey)) {
$this->status = "401 Unauthorized";
$this->message = "API key is missing.";
return false;
} else {
[$prefix,] = explode(separator: '.', string: $apiKey);
if ($apiResult = $this->apikeyRepository->findByPrefix(prefix: $prefix)) {
$encryptedHash = $apiResult->getApikey();
$encryptionKey = $this->configController->getConfig(configKey: 'encryptionKey');
$decryptedHash = $this->encryptionController->safeDecrypt(encrypted: $encryptedHash, key: $encryptionKey);
if (!password_verify(password: $apiKey, hash: $decryptedHash)) {
$this->status = "401 Unauthorized";
$this->message = "API key mismatch.";
return false;
}
} else {
$this->status = "401 Unauthorized";
$this->message = "Invalid API key.";
return false;
}
}
return true;
}
#[OA\Get(
path: '/domains/{name}',
operationId: 'getSingleDomain',
description: 'Returns information of a single domain specified by its domain name.',
summary: 'Returns a single domain.',
security: [
['Authorization' => []]
],
tags: ['Domains'],
parameters: [
new OA\Parameter(name: 'name', in: 'path', required: true, schema: new OA\Schema(type: 'string')),
],
responses: [
new OA\Response(
response: 200,
description: 'OK'
),
new OA\Response(
response: 401,
description: 'API key is missing or invalid.'
),
new OA\Response(
response: 404,
description: 'Domain not found.'
)]
)]
private function handleDomainsGetRequest(): void
{
$name = $this->uri[3] ?? '';
if ($name == 'name') {
if ($result = $this->domainRepository->findByName(name: $this->uri[4])) {
$domain = [
'id' => $result->getId(),
'name' => $result->getName(),
'panel' => $result->getPanel()
];
$this->result = $domain;
} else {
$this->status = "404 Not Found ";
$this->message = "The specified domain was not found.";
}
} else {
if (empty($name)) {
$this->handleAllDomainsGetRequest();
} else {
$id = intval(value: $name);
if ($id > 0) {
if ($result = $this->domainRepository->findById(id: $id)) {
$domain = [
'id' => $result->getId(),
'name' => $result->getName(),
'panel' => $result->getPanel()
];
$this->result = $domain;
} else {
$this->status = "404 Not Found ";
$this->message = "The specified domain was not found.";
}
} else {
$this->status = "400 Bad Request";
$this->message = "You need to supply an ID or user the /domain/name/<name> path.";
}
}
}
}
/**
* @return void
*/
private function handleDomainsPostRequest(): void
{
$name = $_POST['name'] ?? '';
$panel = $_POST['panel'] ?? '';
if (empty($name)) {
$this->status = "400 Bad Request";
$this->message = "A name is required";
} else {
if (empty($panel)) {
$this->status = "400 Bad Request";
$this->message = "A panel ID is required.";
} else {
if ($this->domainRepository->findByName(name: $name)) {
$this->status = "400 Bad request";
$this->message = "Domain: $name already exists.";
} else {
$domain = new Domain(name: $name, panel: $panel);
if ($result = $this->domainRepository->insert(domain: $domain)) {
$this->status = "201 Created";
$this->domainController->createSlaveZoneFile(domain: $domain);
} else {
$this->status = "500 Server error";
}
$this->message = $result;
}
}
}
}
/**
* @return void
*/
private function handleDomainsPutRequest(): void
{
$putData = fopen(filename: 'php://input', mode: 'r');
$data = fread(stream: $putData, length: 8192);
$params = explode(separator: '&', string: $data);
foreach ($params as $param) {
[$key, $value] = explode(separator: '=', string: $param);
$put[$key] = $value;
}
$id = $put['id'] ?? 0;
$name = $put['name'] ?? '';
$panel = $put['panel'] ?? "";
if ($id == 0) {
$this->status = "400 Bad Request";
$this->message = "An ID is required";
} else {
if (!$this->domainRepository->findByID(id: $id)) {
$this->status = "404 Not Found";
$this->message = "Domain with ID : $id doesn't exist.";
} else {
$domain = new Domain(name: $name, panel: $panel, id: $id);
$this->domainRepository->update(domain: $domain);
$this->status = "201 Updated";
$this->message = "201 Updated";
$this->domainController->createSlaveZoneFile(domain: $domain);
}
}
}
private function handleDomainsDeleteRequest(): void
{
$deleteData = fopen(filename: 'php://input', mode: 'r');
$data = fread(stream: $deleteData, length: 512);
$params = explode(separator: '&', string: $data);
foreach ($params as $param) {
[$key, $value] = explode(separator: '=', string: $param);
$delete[$key] = $value;
}
$id = $delete['id'] ?? 0;
if ($id == 0) {
$this->status = "400 Bad Request";
$this->message = "You need to supply an ID.";
} else {
if (!$domain = $this->domainRepository->findByID(id: $id)) {
$this->status = "400 Bad Request";
$this->message = "There is no domain with ID $id.";
} else {
$this->domainRepository->delete(domain: $domain);
$this->status = "204 No content.";
$this->message = "The domain $id has been deleted.";
}
}
}
private function handleDynDNS(): void
{
$this->logger->debug(message: 'handleDynDNS()');
if ($this->validateApiKey()) {
$host = $this->uri[3] ?? '';
if (empty($host)) {
$this->status = '400 Bad Request';
} else {
$a = $_POST['a'] ?? '';
$aaaa = $_POST['aaaa'] ?? '';
if (empty($a) && empty($aaaa)) {
$address = $_SERVER['REMOTE_ADDR'];
if (filter_var(value: $address, filter: FILTER_VALIDATE_IP, options: FILTER_FLAG_IPV6)) {
$aaaa = $address;
} else {
$a = $address;
}
}
$this->logger->debug(message: 'a: ' . $a);
$this->logger->debug(message: 'aaaa: ' . $aaaa);
$domainName = $this->getDomain(host: $host);
$hostName = str_replace(search: '.' . $domainName, replace: '', subject: $host);
if (!$domain = $this->domainRepository->findByName(name: $domainName)) {
$this->status = '404 Not Found';
$this->message = 'Domain ' . $domainName . ' not found';
} else {
// check if address has changed
if ($dynDNS = $this->dynDNSRepository->findByName(name: $host)) {
$this->logger->debug(message: 'found host: ' . $host);
$this->logger->debug(message: "a: $a");
$this->logger->debug(message: "aaaa: $aaaa");
$ipChanged = false;
if (!empty($a)) {
if ($a != $dynDNS->getA()) {
$this->logger->debug(message: $a . ' != ' . $dynDNS->getA());
$dynDNS->setA(a: $a);
$ipChanged = true;
}
}
if (!empty($aaaa)) {
if ($aaaa != $dynDNS->getAaaa()) {
$this->logger->debug(message: $aaaa . ' != ' . $dynDNS->getAaaa());
$dynDNS->setAaaa(aaaa: $aaaa);
$ipChanged = true;
}
}
if ($ipChanged) {
$this->dynDNSRepository->update(dynDNS: $dynDNS);
}
} else {
$dynDNS = new DynDNS(name: $host, a: $a, aaaa: $aaaa);
$this->dynDNSRepository->insert(dynDNS: $dynDNS);
$ipChanged = true;
}
if ($ipChanged) {
$panel = $this->panelRepository->findByName(name: $domain->getPanel());
if (!empty($panel->getAaaa())) {
$domainData = $this->apiClient->sendCommand(
requestType: 'GET',
serverName: $panel->getName(),
versionIP: 6,
apiKey: $panel->getApikey(),
command: 'domains/name/' . $domainName,
serverType: 'panel');
} else {
$domainData = $this->apiClient->sendCommand(
requestType: 'GET',
serverName: $panel->getName(),
versionIP: 4,
apiKey: $panel->getApikey(),
command: 'domains/name/' . $domainName,
serverType: 'panel');
}
$domainDecodedData = json_decode(json: $domainData['data']);
$domainID = $domainDecodedData->id;
if (!empty($panel->getAaaa())) {
$dnsData = $this->apiClient->sendCommand(
requestType: 'GET',
serverName: $panel->getName(),
versionIP: 6,
apiKey: $panel->getApikey(),
command: 'dns/' . $domainID,
serverType: 'panel');
} else {
$dnsData = $this->apiClient->sendCommand(
requestType: 'GET',
serverName: $panel->getName(),
versionIP: 4,
apiKey: $panel->getApikey(),
command: 'dns/' . $domainID,
serverType: 'panel');
}
$dnsDataDecoded = json_decode(json: $dnsData['data']);
$soa = $dnsDataDecoded->records->soa;
$others = $dnsDataDecoded->records->other;
$hostFound = false;
$updateHost = function (object $host) use ($hostName, $a, $aaaa, &$hostFound) {
if ($host->host == $hostName) {
$hostFound = true;
if ($host->type == 'A') {
if (!empty($a)) {
$host->value = $a;
}
} else {
if (!empty($aaaa)) {
$host->value = $aaaa;
}
}
}
};
array_map(callback: $updateHost, array: $others);
if ($hostFound) {
$newDnsData = json_encode(value: [
'records' => [
'soa' => $soa,
'other' => $others
]
]);
if (!empty($panel->getAaaa())) {
$result = $this->apiClient->sendCommand(
requestType: 'PUT',
serverName: $panel->getName(),
versionIP: 6,
apiKey: $panel->getApikey(),
command: 'dns/' . $domainID,
serverType: 'panel',
body: json_decode(json: $newDnsData, associative: true)
);
} else {
$result = $this->apiClient->sendCommand(
requestType: 'PUT',
serverName: $panel->getName(),
versionIP: 4,
apiKey: $panel->getApikey(),
command: 'dns/' . $domainID,
serverType: 'panel',
body: json_decode(json: $newDnsData, associative: true)
);
}
if ($result['header'] == 200) {
$this->status = '200 OK';
$this->message = 'DynDNS host successfully updated';
}
} else {
$this->status = '404 Not Found';
$this->message = 'Host ' . $hostName . ' not found';
}
} else {
$this->status = '204 No content';
$this->message = 'No content';
}
}
}
}
}
private function getDomain(string $host): string
{
$host = strtolower(string: trim(string: $host));
$count = substr_count(haystack: $host, needle: '.');
if ($count == 2) {
if (strlen(string: explode(separator: '.', string: $host)[1]) > 3) {
$host = explode(separator: '.', string: $host, limit: 2)[1];
}
} elseif ($count > 2) {
$host = $this->getDomain(host: explode(separator: '.', string: $host, limit: 2)[1]);
}
return $host;
}
// private function apiDoc(): void
// {
// $srcDir = dirname(path: __DIR__);
// $requestControllerPath = $srcDir . '/Controller/RequestController.php';
//
// $openApi = Generator::scan(sources: [$requestControllerPath]);
// header(header: 'Content-Type: application/json');
//
// echo $openApi->toJson();
// exit(0);
// }
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 PanelRepository $panelRepository,
private readonly ConfigController $configController,
private readonly EncryptionController $encryptionController,
private readonly Logger $logger)
{
$this->baseDir = dirname(path: __DIR__, levels: 2) . '/';
$this->status = '';
$this->response = '';
$this->message = '';
$this->result = [];
}
public function handleRequest(string $requestMethod, array $uri): void
{
$this->logger->debug(message: "Request: $requestMethod $uri[1]");
$this->requestMethod = strtoupper(string: $requestMethod);
$this->uri = $uri;
$command = $this->uri[2];
// use my router class from address book?
$routes = ['ping', 'version', 'domains', 'apidoc', 'dyndns'];
if (empty($command) || !(in_array(needle: $command, haystack: $routes))) {
$this->status = "404 Not Found";
$this->message = "Endpoint not found.";
} else {
try {
match ($command) {
// server
'ping' => $this->handlePing(),
'version' => $this->getVersion(),
// domains
'domains' => $this->handleDomains(),
'dyndns' => $this->handleDynDNS(),
// 'apidoc' => $this->apiDoc(),
};
} catch (UnhandledMatchError) {
$this->status = '400 Bad Request';
$this->message = 'Unknown path: ' . $command;
}
}
// process api requests
if (!empty($this->status)) {
header(header: $_SERVER['SERVER_PROTOCOL'] . ' ' . $this->status);
}
if (!empty($this->response)) {
echo json_encode(value: [
'response' => $this->response
]);
} elseif (!empty($this->result)) {
echo json_encode(value: $this->result);
} elseif (!empty($this->message)) {
echo json_encode(value: [
'message' => $this->message
]);
} else {
echo json_encode(value: [
'message' => $this->message ?? 'Error: No message.'
]);
}
}
}