86 lines
3.6 KiB
PHP
86 lines
3.6 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace MageSail\Magesail\Controller\Adminhtml\Tunnel;
|
|
|
|
use Magento\Backend\App\Action;
|
|
use Magento\Backend\App\Action\Context;
|
|
use Magento\Framework\App\Config\ScopeConfigInterface;
|
|
use Magento\Framework\Controller\ResultFactory;
|
|
use MageSail\Magesail\Model\MagesailLog;
|
|
use MageSail\Magesail\Model\TunnelManager;
|
|
use MageSail\Magesail\Model\TunnelStatus;
|
|
|
|
class Index extends Action
|
|
{
|
|
public function __construct(
|
|
Context $context,
|
|
private readonly ScopeConfigInterface $scopeConfig,
|
|
private readonly TunnelManager $tunnelManager,
|
|
private readonly TunnelStatus $tunnelStatus,
|
|
private readonly MagesailLog $magesailLog
|
|
) {
|
|
parent::__construct($context);
|
|
}
|
|
|
|
public function execute()
|
|
{
|
|
$action = $this->getRequest()->getParam('action');
|
|
$isRunning = $this->tunnelStatus->isRunning();
|
|
|
|
if ($action === 'start') {
|
|
if ($isRunning) {
|
|
$this->messageManager->addErrorMessage(__('Tunnel is already running.'));
|
|
return $this->_redirect('*/*/index');
|
|
}
|
|
$secure = (bool) $this->scopeConfig->getValue('magesail/settings/mode');
|
|
$port = $this->scopeConfig->getValue('magesail/settings/port') ?: 80;
|
|
$port = filter_var($port, FILTER_VALIDATE_INT);
|
|
if (!$port || !\in_array($port, [80, 443], true)) {
|
|
$this->messageManager->addErrorMessage(__('Invalid port. Must be 80 or 443.'));
|
|
return $this->_redirect('*/*/index');
|
|
}
|
|
$result = $this->tunnelManager->start((int) $port, $secure);
|
|
if ($result['ok']) {
|
|
$this->magesailLog->add(sprintf('Tunnel started PID %s port %s', $result['pid'], $port));
|
|
$this->messageManager->addSuccessMessage(__(
|
|
'Tunnel started (PID: %1). Key will appear once ready. Logs: %2',
|
|
$result['pid'],
|
|
$this->tunnelStatus->getLogFilePath()
|
|
));
|
|
} else {
|
|
$msg = match ($result['error'] ?? '') {
|
|
'already_running' => __('Tunnel is already running.'),
|
|
'invalid_port' => __('Invalid port. Must be 80 or 443.'),
|
|
'pid_capture_failed' => __('Failed to capture PID: %1', $result['detail'] ?? ''),
|
|
default => __('Error starting tunnel: %1', $result['error'] ?? 'unknown'),
|
|
};
|
|
$this->messageManager->addErrorMessage($msg);
|
|
$this->magesailLog->add('Start failed: ' . ($result['error'] ?? 'unknown'));
|
|
}
|
|
} elseif ($action === 'stop') {
|
|
$stop = $this->tunnelManager->stop();
|
|
if ($stop['ok']) {
|
|
$this->magesailLog->add('Tunnel stopped');
|
|
$this->messageManager->addSuccessMessage(__('Tunnel stopped.'));
|
|
} else {
|
|
if (($stop['error'] ?? '') === 'not_running') {
|
|
$this->messageManager->addNoticeMessage(__('No tunnel running.'));
|
|
} else {
|
|
$this->messageManager->addErrorMessage(__('Error stopping tunnel: %1', $stop['error'] ?? ''));
|
|
}
|
|
}
|
|
}
|
|
|
|
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
|
|
$resultPage->setActiveMenu('MageSail_Magesail::tunnel');
|
|
$resultPage->getConfig()->getTitle()->prepend(__('Holesail Tunnel'));
|
|
return $resultPage;
|
|
}
|
|
|
|
protected function _isAllowed()
|
|
{
|
|
return $this->_authorization->isAllowed('MageSail_Magesail::tunnel');
|
|
}
|
|
}
|