pfatt/src/Commands/Startup.php
James Gilliland 7c6fac34a0 Rename method to make more sense with supplicant logic
This doesn't really work but its fun and will be useful to dig into.
2022-03-11 21:33:37 -06:00

99 lines
3 KiB
PHP

<?php
declare(strict_types=1);
namespace Pfatt\Commands;
use Pfatt\Service\Config;
use Pfatt\Service\IfConfig;
use Pfatt\Service\KldLoad;
use Pfatt\Service\NgCtl;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
final class Startup extends Command
{
protected static $defaultName = 'startup';
protected static $defaultDescription = 'Start netgraph connetion';
protected Config $config;
protected LoggerInterface $logger;
protected NgCtl $ngControl;
private IfConfig $ifConfig;
private KldLoad $kldLoad;
public function __construct(
Config $config,
LoggerInterface $logger,
NgCtl $ngControl,
IfConfig $ifConfig,
KldLoad $kldLoad
) {
parent::__construct('startup');
$this->config = $config;
$this->logger = $logger;
$this->ngControl = $ngControl;
$this->ifConfig = $ifConfig;
$this->kldLoad = $kldLoad;
}
/**
* {@inheritDoc}
*/
protected function execute(
InputInterface $input,
OutputInterface $output
): int {
switch ($this->getVariant()) {
case 'opnsense':
$this->logger->info('loading netgraph kernel modules...');
$this->kldLoad->execute('netgraph');
$this->kldLoad->execute('ng_ether');
$this->kldLoad->execute('ng_etf');
$this->kldLoad->execute('ng_vlan');
$this->kldLoad->execute('ng_eiface');
$this->kldLoad->execute('ng_one2many');
$this->logger->info('OK!');
break;
case 'pfsense':
default:
$attach = function (string $interface): Process {
return new Process([
'/usr/local/bin/php',
'-r',
'"pfSense_ngctl_attach(\'.\', \'' . $interface . '\');"'
]);
};
$this->logger->info('attaching interfaces to ng_ether...');
$attach($this->config->getOntInterface())->mustRun();
$attach($this->config->getRgInterface())->mustRun();
$this->logger->info('OK!');
break;
}
$this->ngControl->createBridge(
$this->config->getOntInterface(),
$this->config->getRgInterface(),
$this->config->getRgEthernetMac()
);
$this->ifConfig->start(
$this->config->getOntInterface(),
$this->config->getRgInterface()
);
$this->logger->info("ngeth0 should now be available to configure as your pfSense WAN");
$this->logger->info("Done");
return Command::SUCCESS;
}
/**
* @return string
*/
private function getVariant(): string
{
// @todo detect the variant to trigger different behaviors.
return 'opnsense';
}
}