49 lines
1.2 KiB
PHP
Executable File
49 lines
1.2 KiB
PHP
Executable File
#!/usr/bin/php
|
|
<?php
|
|
|
|
class Openvpn implements IPluggable
|
|
{
|
|
private PluginContext $context;
|
|
|
|
public function restart()
|
|
{
|
|
//restart ovpn
|
|
return shell_exec($this->context->getConfig()["ovpn"]["restart_cmd"]);
|
|
}
|
|
|
|
public function init(PluginContext $context): void
|
|
{
|
|
$this->context = $context;
|
|
$this->checkConfig();
|
|
}
|
|
|
|
private function checkConfig(): void
|
|
{
|
|
$config = $this->context->getConfig();
|
|
if (!isset($config["ovpn"])) {
|
|
$config["ovpn"] = $this->context->getMetadata()["config"];
|
|
}
|
|
}
|
|
|
|
public function getRoutingConfig(): array
|
|
{
|
|
$networks = (new NetworkConfigReader())->getConfigs();
|
|
$data = [];
|
|
|
|
//add new routes
|
|
foreach ($this->context->getConfig() ["networks"] as $key) {
|
|
if (isset($networks[$key])) {
|
|
foreach ($networks[$key]["networks"] as $route) {
|
|
$parts = explode("/", $route);
|
|
$mask = long2ip(-1 << (32 - (int)$parts[1]));
|
|
$dst = $parts[0];
|
|
$data[] = "push \"route {$dst} {$mask}\"";
|
|
}
|
|
}
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
}
|
|
|