added endpoint /dyndns
Signed-off-by: tracer <tracer@24unix.net>
This commit is contained in:
parent
055431627a
commit
7a15d82a3a
|
@ -7,12 +7,12 @@ error_reporting(error_level: E_ALL);
|
||||||
use App\Entity\Domain;
|
use App\Entity\Domain;
|
||||||
use App\Repository\ApikeyRepository;
|
use App\Repository\ApikeyRepository;
|
||||||
use App\Repository\DomainRepository;
|
use App\Repository\DomainRepository;
|
||||||
|
use App\Repository\PanelRepository;
|
||||||
use DI\Container;
|
use DI\Container;
|
||||||
use DI\ContainerBuilder;
|
use DI\ContainerBuilder;
|
||||||
use Monolog\Formatter\LineFormatter;
|
use Monolog\Formatter\LineFormatter;
|
||||||
use Monolog\Handler\StreamHandler;
|
use Monolog\Handler\StreamHandler;
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
use OpenApi\Generator;
|
|
||||||
use UnhandledMatchError;
|
use UnhandledMatchError;
|
||||||
use function DI\autowire;
|
use function DI\autowire;
|
||||||
use OpenApi\Attributes as OAT;
|
use OpenApi\Attributes as OAT;
|
||||||
|
@ -50,15 +50,18 @@ class RequestController
|
||||||
{
|
{
|
||||||
private Logger $log;
|
private Logger $log;
|
||||||
|
|
||||||
|
private ApiController $apiController;
|
||||||
|
private ApikeyRepository $apikeyRepository;
|
||||||
private DomainController $domainController;
|
private DomainController $domainController;
|
||||||
private DomainRepository $domainRepository;
|
private DomainRepository $domainRepository;
|
||||||
private ApikeyRepository $apikeyRepository;
|
private PanelRepository $panelRepository;
|
||||||
private Container $container;
|
private Container $container;
|
||||||
private string $header;
|
private string $header;
|
||||||
private array $result;
|
private array $result;
|
||||||
private string $status;
|
private string $status;
|
||||||
private string $message;
|
private string $message;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $config
|
* @param array $config
|
||||||
* @param String $requestMethod
|
* @param String $requestMethod
|
||||||
|
@ -95,9 +98,13 @@ class RequestController
|
||||||
]);
|
]);
|
||||||
$this->container = $containerBuilder->build();
|
$this->container = $containerBuilder->build();
|
||||||
|
|
||||||
|
$this->apiController = $this->container->get(name: ApiController::class);
|
||||||
|
$this->apikeyRepository = $this->container->get(name: ApikeyRepository::class);
|
||||||
$this->domainController = $this->container->get(name: DomainController::class);
|
$this->domainController = $this->container->get(name: DomainController::class);
|
||||||
$this->domainRepository = $this->container->get(name: DomainRepository::class);
|
$this->domainRepository = $this->container->get(name: DomainRepository::class);
|
||||||
$this->apikeyRepository = $this->container->get(name: ApikeyRepository::class);
|
$this->panelRepository = $this->container->get(name: PanelRepository::class);
|
||||||
|
// ***
|
||||||
|
//$this->nameserverRepositoy = $this->container->get(name: NameserverRepository::class);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,7 +138,7 @@ class RequestController
|
||||||
description: 'Domain not found.'
|
description: 'Domain not found.'
|
||||||
)]
|
)]
|
||||||
)]
|
)]
|
||||||
public function handleAllDomainsGetRequest(): void
|
private function handleAllDomainsGetRequest(): void
|
||||||
{
|
{
|
||||||
$domains = $this->domainRepository->findAll();
|
$domains = $this->domainRepository->findAll();
|
||||||
$resultDomain = [];
|
$resultDomain = [];
|
||||||
|
@ -146,6 +153,41 @@ class RequestController
|
||||||
$this->result = $resultDomain;
|
$this->result = $resultDomain;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*/
|
||||||
|
private function handlePing()
|
||||||
|
{
|
||||||
|
if ($this->checkPassword()) {
|
||||||
|
$this->header = '200 OK';
|
||||||
|
$this->status = json_encode(value: ['response' => 'pong']);
|
||||||
|
} else {
|
||||||
|
$this->header = '401 Unauthorized';
|
||||||
|
$this->status = json_encode(value: ['message' => 'API key is missing or invalid']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function handleDomains(): void
|
||||||
|
{
|
||||||
|
if ($this->checkPassword()) {
|
||||||
|
try {
|
||||||
|
match ($this->requestMethod) {
|
||||||
|
'GET' => $this->handleDomainsGetRequest(),
|
||||||
|
'POST' => $this->handleDomainsPostRequest(),
|
||||||
|
'PUT' => $this->handleDomainsPutRequest(),
|
||||||
|
'DELETE' => $this->handleDomainsDeleteRequest()
|
||||||
|
};
|
||||||
|
} catch (UnhandledMatchError) {
|
||||||
|
$this->header = '400 Bad Request';
|
||||||
|
$this->status = '400 Bad Request';
|
||||||
|
$this->message = "unknown request method: $this->requestMethod";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @OA\Tag(name = "Server")
|
* @OA\Tag(name = "Server")
|
||||||
|
@ -189,90 +231,69 @@ class RequestController
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#[OAT\Get(
|
#[
|
||||||
path : '/domains/{name}',
|
OAT\Get(
|
||||||
operationId: 'getSingleDomain',
|
path : '/domains/{name}',
|
||||||
description: 'Returns information of a single domain specified by its domain name.',
|
operationId: 'getSingleDomain',
|
||||||
summary : 'Returns a single domain.',
|
description: 'Returns information of a single domain specified by its domain name.',
|
||||||
security : [
|
summary : 'Returns a single domain.',
|
||||||
],
|
security : [
|
||||||
tags : ['Domains'],
|
],
|
||||||
parameters : [
|
tags : ['Domains'],
|
||||||
new OAT\Parameter(name: 'name', in: 'path', required: true, schema: new OAT\Schema(type: 'string')),
|
parameters : [
|
||||||
],
|
new OAT\Parameter(name: 'name', in: 'path', required: true, schema: new OAT\Schema(type: 'string')),
|
||||||
responses : [
|
],
|
||||||
new OAT\Response(
|
responses : [
|
||||||
response : 200,
|
new OAT\Response(
|
||||||
description: 'OK'
|
response : 200,
|
||||||
),
|
description: 'OK'
|
||||||
new OAT\Response(
|
),
|
||||||
response : 401,
|
new OAT\Response(
|
||||||
description: 'API key is missing or invalid.'
|
response : 401,
|
||||||
),
|
description: 'API key is missing or invalid.'
|
||||||
new OAT\Response(
|
),
|
||||||
response : 404,
|
new OAT\Response(
|
||||||
description: 'Domain not found.'
|
response : 404,
|
||||||
)]
|
description: 'Domain not found.'
|
||||||
|
)]
|
||||||
|
|
||||||
)]
|
)]
|
||||||
public function processRequest()
|
public function processRequest()
|
||||||
{
|
{
|
||||||
$command = $this->uri[2];
|
$command = $this->uri[2];
|
||||||
|
|
||||||
if (empty($command) || !(($command == 'domains') || ($command == 'ping') || ($command == 'apidoc'))) {
|
if (empty($command) || !(($command == 'domains') || ($command == 'ping') || ($command == 'apidoc') || ($command == 'dyndns'))) {
|
||||||
$this->header = '404 Not Found';
|
$this->header = '404 Not Found';
|
||||||
$this->status = "404 Not Found";
|
$this->status = "404 Not Found";
|
||||||
$this->message = "Endpoint not found.";
|
$this->message = "Endpoint not found.";
|
||||||
} else {
|
} else {
|
||||||
if ($command == 'apidoc') {
|
try {
|
||||||
$openapi = Generator::scan(sources: [__DIR__ . 'RequestController.php']);
|
match ($command) {
|
||||||
$this->status = 'openapi';
|
'apidoc' => $this->apiDoc(),
|
||||||
$this->result[] = $openapi->toJson();
|
'dyndns' => $this->handleDynDNS(),
|
||||||
} else {
|
'ping' => $this->handlePing(),
|
||||||
if ($this->checkPassword()) {
|
'domains' => $this->handleDomains(),
|
||||||
|
};
|
||||||
|
} catch (UnhandledMatchError) {
|
||||||
|
$this->header = '404 Bad Request';
|
||||||
|
$this->status = '404 Bad Request';
|
||||||
|
$this->message = 'Unknown path: ' . $command;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ($this->uri[2] == "ping") {
|
if (!empty($this->header)) {
|
||||||
$this->header = '200 OK';
|
header(header: $_SERVER['SERVER_PROTOCOL'] . ' ' . $this->header);
|
||||||
$this->status = 'pong';
|
}
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
match ($this->requestMethod) {
|
|
||||||
'GET' => $this->handleDomainGetRequest(),
|
|
||||||
'POST' => $this->handleDomainPostRequest(),
|
|
||||||
'PUT' => $this->handleDomainPutRequest(),
|
|
||||||
'DELETE' => $this->handleDomainDeleteRequest()
|
|
||||||
};
|
|
||||||
} catch (UnhandledMatchError) {
|
|
||||||
$this->header = '400 Bad Request';
|
|
||||||
$this->status = '400 Bad Request';
|
|
||||||
$this->message = "unknown request method: $this->requestMethod";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($this->header)) {
|
if (!empty($this->result)) {
|
||||||
header(header: $_SERVER['SERVER_PROTOCOL'] . ' ' . $this->header);
|
echo json_encode(value: $this->result);
|
||||||
}
|
} elseif (!empty($this->status)) {
|
||||||
if (!empty($this->result)) {
|
echo $this->status;
|
||||||
if (!empty($this->status) && $this->status == 'openapi') {
|
} else {
|
||||||
header(header: 'Content-Type: application/json');
|
echo json_encode(value: [
|
||||||
echo $this->result[0];
|
'status' => $this->status ?? "Error: No status",
|
||||||
} else {
|
'message' => $this->message ?? "Error: No message."
|
||||||
echo json_encode(value: $this->result);
|
]);
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!empty($this->status) && $this->status == 'pong') {
|
|
||||||
echo json_encode(value: [
|
|
||||||
'response' => $this->status
|
|
||||||
]);
|
|
||||||
} else {
|
|
||||||
echo json_encode(value: [
|
|
||||||
'status' => $this->status ?? "Error: No status",
|
|
||||||
'message' => $this->message ?? "Error: No message."
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -280,7 +301,7 @@ class RequestController
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function checkPassword(): bool
|
private function checkPassword(): bool
|
||||||
{
|
{
|
||||||
$headers = array_change_key_case(array: getallheaders(), case: CASE_UPPER);
|
$headers = array_change_key_case(array: getallheaders(), case: CASE_UPPER);
|
||||||
$apiKey = $headers['X-API-KEY'] ?? '';
|
$apiKey = $headers['X-API-KEY'] ?? '';
|
||||||
|
@ -310,12 +331,15 @@ class RequestController
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function handleDomainGetRequest(): void
|
private function handleDomainsGetRequest(): void
|
||||||
{
|
{
|
||||||
if ($this->uri[3] == 'name') {
|
$name = $this->uri[3] ?? '';
|
||||||
|
|
||||||
|
if ($name == 'name') {
|
||||||
if ($result = $this->domainRepository->findByName(name: $this->uri[4])) {
|
if ($result = $this->domainRepository->findByName(name: $this->uri[4])) {
|
||||||
$domain = [
|
$domain = [
|
||||||
'id' => $result->getId(),
|
'id' => $result->getId(),
|
||||||
|
@ -329,10 +353,10 @@ class RequestController
|
||||||
$this->message = "The specified domain was not found.";
|
$this->message = "The specified domain was not found.";
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (empty($this->uri[3])) {
|
if (empty($name)) {
|
||||||
$this->handleAllDomainsGetRequest();
|
$this->handleAllDomainsGetRequest();
|
||||||
} else {
|
} else {
|
||||||
$id = intval(value: $this->uri['3']);
|
$id = intval(value: $name);
|
||||||
if ($id > 0) {
|
if ($id > 0) {
|
||||||
if ($result = $this->domainRepository->findById(id: $id)) {
|
if ($result = $this->domainRepository->findById(id: $id)) {
|
||||||
$domain = [
|
$domain = [
|
||||||
|
@ -360,7 +384,7 @@ class RequestController
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function handleDomainPostRequest(): void
|
private function handleDomainsPostRequest(): void
|
||||||
{
|
{
|
||||||
$name = $_POST['name'] ?? '';
|
$name = $_POST['name'] ?? '';
|
||||||
$panel = $_POST['panel'] ?? '';
|
$panel = $_POST['panel'] ?? '';
|
||||||
|
@ -398,10 +422,10 @@ class RequestController
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function handleDomainPutRequest(): void
|
private function handleDomainsPutRequest(): void
|
||||||
{
|
{
|
||||||
$putData = fopen(filename: 'php://input', mode: 'r');
|
$putData = fopen(filename: 'php://input', mode: 'r');
|
||||||
$data = fread(stream: $putData, length: 512);
|
$data = fread(stream: $putData, length: 8192);
|
||||||
$params = explode(separator: '&', string: $data);
|
$params = explode(separator: '&', string: $data);
|
||||||
|
|
||||||
foreach ($params as $param) {
|
foreach ($params as $param) {
|
||||||
|
@ -435,8 +459,7 @@ class RequestController
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public
|
private function handleDomainsDeleteRequest(): void
|
||||||
function handleDomainDeleteRequest(): void
|
|
||||||
{
|
{
|
||||||
$deleteData = fopen(filename: 'php://input', mode: 'r');
|
$deleteData = fopen(filename: 'php://input', mode: 'r');
|
||||||
$data = fread(stream: $deleteData, length: 512);
|
$data = fread(stream: $deleteData, length: 512);
|
||||||
|
@ -468,4 +491,154 @@ class RequestController
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function apiDoc()
|
||||||
|
{
|
||||||
|
//TODO forward to apidoch …
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param String $host
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
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];
|
||||||
|
}
|
||||||
|
} else if ($count > 2) {
|
||||||
|
$host = $this->getDomain(host: explode(separator: '.', string: $host, limit: 2)[1]);
|
||||||
|
}
|
||||||
|
return $host;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private function handleDynDNS()
|
||||||
|
{
|
||||||
|
if ($this->checkPassword()) {
|
||||||
|
$host = $this->uri[3] ?? '';
|
||||||
|
|
||||||
|
if (empty($host)) {
|
||||||
|
$this->header = '400 Bad Request';
|
||||||
|
$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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$domainName = $this->getDomain(host: $host);
|
||||||
|
$hostName = str_replace(search: '.' . $domainName, replace: '', subject: $host);
|
||||||
|
$domain = $this->domainRepository->findByName(name: $domainName);
|
||||||
|
$panel = $this->panelRepository->findByName(name: $domain->getPanel());
|
||||||
|
|
||||||
|
if (!empty($panel->getAaaa())) {
|
||||||
|
$domainData = $this->apiController->sendCommand(
|
||||||
|
requestType: 'GET',
|
||||||
|
serverName: $panel->getName(),
|
||||||
|
versionIP: 6,
|
||||||
|
apiKey: $panel->getApikey(),
|
||||||
|
command: 'domains/name/' . $domainName,
|
||||||
|
serverType: 'panel');
|
||||||
|
} else {
|
||||||
|
$domainData = $this->apiController->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->apiController->sendCommand(
|
||||||
|
requestType: 'GET',
|
||||||
|
serverName: $panel->getName(),
|
||||||
|
versionIP: 6,
|
||||||
|
apiKey: $panel->getApikey(),
|
||||||
|
command: 'dns/' . $domainID,
|
||||||
|
serverType: 'panel');
|
||||||
|
} else {
|
||||||
|
$dnsData = $this->apiController->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;
|
||||||
|
|
||||||
|
$updateHost = function(object $host) use ($hostName, $a, $aaaa) {
|
||||||
|
if ($host->host == $hostName) {
|
||||||
|
if ($host->type == 'A') {
|
||||||
|
if (!empty($a)) {
|
||||||
|
$host->value = $a;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!empty($aaaa)) {
|
||||||
|
$host->value = $aaaa;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
array_map(callback: $updateHost, array: $others);
|
||||||
|
|
||||||
|
$newDnsData = json_encode(value: [
|
||||||
|
'records' => [
|
||||||
|
'soa' => $soa,
|
||||||
|
'other' => $others
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!empty($panel->getAaaa())) {
|
||||||
|
$result = $this->apiController->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->apiController->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->header = '200 OK';
|
||||||
|
$this->status = json_encode(value: ['message' => 'Domain successfully updated']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue