mirror of
https://github.com/rekryt/iplist.git
synced 2025-10-13 16:59:36 +03:00
Initial commit
This commit is contained in:
60
src/App/Controller/AbstractController.php
Normal file
60
src/App/Controller/AbstractController.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace OpenCCK\App\Controller;
|
||||
|
||||
use Amp\Http\HttpStatus;
|
||||
use Amp\Http\Server\Request;
|
||||
use Amp\Http\Server\Response;
|
||||
|
||||
use Throwable;
|
||||
|
||||
abstract class AbstractController implements ControllerInterface {
|
||||
private int $httpStatus = HttpStatus::OK;
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param array $headers
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function __construct(protected Request $request, protected array $headers = []) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Response
|
||||
*/
|
||||
public function __invoke(): Response {
|
||||
return new Response(status: $this->httpStatus, headers: $this->headers, body: $this->getBody());
|
||||
}
|
||||
|
||||
abstract public function getBody(): string;
|
||||
|
||||
public function setHeaders(array $headers): AbstractController {
|
||||
$this->headers = $headers;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $httpStatus
|
||||
*/
|
||||
public function setHttpStatus(int $httpStatus): void {
|
||||
$this->httpStatus = $httpStatus;
|
||||
}
|
||||
|
||||
public function redirect(string $url, bool $permanently = false): void {
|
||||
$this->httpStatus = $permanently ? HttpStatus::MOVED_PERMANENTLY : HttpStatus::SEE_OTHER;
|
||||
$this->headers = array_merge($this->headers ?? [], ['location' => $url]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getBaseURL(): string {
|
||||
$schemePort = ['http' => 80, 'https' => 443];
|
||||
return $this->request->getUri()->getScheme() .
|
||||
'://' .
|
||||
$this->request->getUri()->getHost() .
|
||||
($schemePort[$this->request->getUri()->getScheme()] !== $this->request->getUri()->getPort()
|
||||
? ':' . $this->request->getUri()->getPort()
|
||||
: '');
|
||||
}
|
||||
}
|
35
src/App/Controller/AbstractIPListController.php
Normal file
35
src/App/Controller/AbstractIPListController.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace OpenCCK\App\Controller;
|
||||
|
||||
use Amp\ByteStream\BufferException;
|
||||
use Amp\Http\Server\Request;
|
||||
|
||||
use OpenCCK\App\Service\IPListService;
|
||||
use OpenCCK\Infrastructure\API\App;
|
||||
|
||||
use Monolog\Logger;
|
||||
use Throwable;
|
||||
|
||||
abstract class AbstractIPListController extends AbstractController {
|
||||
protected Logger $logger;
|
||||
protected IPListService $service;
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param array $headers
|
||||
* @throws BufferException
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function __construct(protected Request $request, protected array $headers = []) {
|
||||
parent::__construct($request, $this->headers);
|
||||
|
||||
$this->logger = App::getLogger();
|
||||
$this->service = IPListService::getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
abstract public function getBody(): string;
|
||||
}
|
19
src/App/Controller/ControllerInterface.php
Normal file
19
src/App/Controller/ControllerInterface.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace OpenCCK\App\Controller;
|
||||
|
||||
use Amp\Http\Server\Request;
|
||||
use Amp\Http\Server\Response;
|
||||
|
||||
interface ControllerInterface {
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param array $headers
|
||||
*/
|
||||
public function __construct(Request $request, array $headers = []);
|
||||
|
||||
/**
|
||||
* @return Response
|
||||
*/
|
||||
public function __invoke(): Response;
|
||||
}
|
32
src/App/Controller/JsonController.php
Normal file
32
src/App/Controller/JsonController.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace OpenCCK\App\Controller;
|
||||
|
||||
class JsonController extends AbstractIPListController {
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getBody(): string {
|
||||
$this->setHeaders(['content-type' => 'application/json']);
|
||||
|
||||
$site = $this->request->getQueryParameter('site') ?? '';
|
||||
$data = $this->request->getQueryParameter('data') ?? '';
|
||||
if ($site == '') {
|
||||
if ($data == '') {
|
||||
return json_encode($this->service->sites);
|
||||
} else {
|
||||
$result = [];
|
||||
foreach ($this->service->sites as $site) {
|
||||
$result[$site->name] = $site->$data;
|
||||
}
|
||||
return json_encode($result);
|
||||
}
|
||||
} else {
|
||||
if ($data == '') {
|
||||
return json_encode($this->service->sites[$site]);
|
||||
} else {
|
||||
return json_encode($this->service->sites[$site]->$data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
23
src/App/Controller/MainController.php
Normal file
23
src/App/Controller/MainController.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace OpenCCK\App\Controller;
|
||||
|
||||
class MainController extends AbstractIPListController {
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getBody(): string {
|
||||
$this->setHeaders(['content-type' => 'text/html; charset=utf-8']);
|
||||
return $this->renderTemplate('index');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $template
|
||||
* @return string
|
||||
*/
|
||||
private function renderTemplate(string $template): string {
|
||||
ob_start();
|
||||
include PATH_ROOT . '/src/App/Template/' . ucfirst($template) . 'Template.php';
|
||||
return ob_get_clean();
|
||||
}
|
||||
}
|
41
src/App/Controller/MikrotikController.php
Normal file
41
src/App/Controller/MikrotikController.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace OpenCCK\App\Controller;
|
||||
|
||||
class MikrotikController extends AbstractIPListController {
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getBody(): string {
|
||||
$this->setHeaders(['content-type' => 'text/plain']);
|
||||
|
||||
$site = $this->request->getQueryParameter('site') ?? '';
|
||||
$data = $this->request->getQueryParameter('data') ?? '';
|
||||
if ($data == '') {
|
||||
return "# Error: The 'data' GET parameter is required in the URL to access this page, but it cannot have the value 'All'";
|
||||
}
|
||||
$response = '/ip firewall address-list' . "\n";
|
||||
if ($site == '') {
|
||||
foreach ($this->service->sites as $site) {
|
||||
$response .= $this->render($site->name, $site->$data);
|
||||
}
|
||||
return $response;
|
||||
} else {
|
||||
return $response . $this->render($site, $this->service->sites[$site]->$data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $site
|
||||
* @param iterable $array
|
||||
* @return string
|
||||
*/
|
||||
private function render(string $site, iterable $array): string {
|
||||
$response = '';
|
||||
$listName = str_replace(' ', '', $site);
|
||||
foreach ($array as $item) {
|
||||
$response .= 'add list=' . $listName . ' address=' . $item . ' comment=' . $listName . "\n";
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
}
|
36
src/App/Controller/TextController.php
Normal file
36
src/App/Controller/TextController.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace OpenCCK\App\Controller;
|
||||
|
||||
class TextController extends AbstractIPListController {
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getBody(): string {
|
||||
$this->setHeaders(['content-type' => 'text/plain']);
|
||||
|
||||
$site = $this->request->getQueryParameter('site') ?? '';
|
||||
$data = $this->request->getQueryParameter('data') ?? '';
|
||||
if ($data == '') {
|
||||
return "# Error: The 'data' GET parameter is required in the URL to access this page, but it cannot have the value 'All'";
|
||||
}
|
||||
|
||||
$response = '';
|
||||
if ($site == '') {
|
||||
foreach ($this->service->sites as $site) {
|
||||
$response .= $this->render($site->name, $site->$data);
|
||||
}
|
||||
return $response;
|
||||
} else {
|
||||
return $this->render($site, $this->service->sites[$site]->$data);
|
||||
}
|
||||
}
|
||||
|
||||
private function render(string $name, iterable $array): string {
|
||||
$response = '# ' . $name . ' ' . date('Y-m-d H:i:s') . "\n";
|
||||
foreach ($array as $item) {
|
||||
$response .= $item . "\n";
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user