Skip to content

Commit

Permalink
Allow enable nat mode from docker compose #721
Browse files Browse the repository at this point in the history
  • Loading branch information
jorikfon committed May 7, 2024
1 parent 47b62a7 commit 016911d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/AdminCabinet/Forms/NetworkEditForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

namespace MikoPBX\AdminCabinet\Forms;

use MikoPBX\Common\Models\LanInterfaces;
use MikoPBX\Common\Models\PbxSettings;
use MikoPBX\Common\Models\PbxSettingsConstants;
use MikoPBX\Common\Providers\TranslationProvider;
Expand Down Expand Up @@ -63,7 +64,7 @@ public function initialize($entity = null, $options = null): void

// topology
$cheskArr = ['value' => null];
if ($entity->topology == 'private') {
if ($entity->topology == LanInterfaces::TOPOLOGY_PRIVATE) {
$cheskArr = ['checked' => 'checked', 'value' => null];
}
$this->add(new Check('usenat', $cheskArr));
Expand Down
31 changes: 30 additions & 1 deletion src/Core/System/DockerEntrypoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

use Error;
use JsonException;
use MikoPBX\Common\Models\LanInterfaces;
use MikoPBX\Common\Models\PbxSettingsConstants;
use Phalcon\Di;
use ReflectionClass;
Expand Down Expand Up @@ -227,6 +228,11 @@ private function applyEnvironmentSettings(): void
case PbxSettingsConstants::GNATS_HTTP_PORT:
$this->updateJsonSettings('gnats', 'httpPort', intval($envValue));
break;
case PbxSettingsConstants::ENABLE_USE_NAT:
if ($envValue==='1'){
$this->enableNat();
}
break;
default:
$this->updateDBSetting($dbKey, $envValue);
break;
Expand All @@ -235,6 +241,29 @@ private function applyEnvironmentSettings(): void
}
}

/**
* Updates the topology of LAN interfaces designated as public (internet-facing) to private
* by executing an SQLite update command directly via the shell.
*
* This method finds the path of the SQLite3 executable and constructs a command to update
* the `topology` field of all entries in the `m_LanInterfaces` table where `internet` is '1'.
* The new topology value is set from the `LanInterfaces::TOPOLOGY_PRIVATE` constant.
*
*/
private function enableNat(): void
{
$sqlite3 = Util::which('sqlite3');
$dbPath = self::PATH_DB;
$out = [];
$private = LanInterfaces::TOPOLOGY_PRIVATE;
$command = "$sqlite3 $dbPath \"UPDATE m_LanInterfaces SET topology='$private' WHERE internet='1'\"";
$res = Processes::mwExec($command, $out);
if ($res === 0) {
SystemMessages::sysLogMsg(__METHOD__, " - Update topology to '$private' in m_LanInterfaces", LOG_INFO);
} else {
SystemMessages::sysLogMsg(__METHOD__, " - Update topology failed: " . implode($out) . PHP_EOL . 'Command:' . PHP_EOL . $command, LOG_ERR);
}
}
/**
* Updates the specified setting in the JSON configuration file.
* @param string $path The JSON path where the setting is stored.
Expand Down Expand Up @@ -264,7 +293,7 @@ private function updateDBSetting(string $key, string $newValue): void
$command = "$sqlite3 $dbPath \"UPDATE m_PbxSettings SET value='$newValue' WHERE key='$key'\"";
$res = Processes::mwExec($command, $out);
if ($res === 0) {
SystemMessages::sysLogMsg(__METHOD__, " - Update $key to '$newValue' in DB", LOG_INFO);
SystemMessages::sysLogMsg(__METHOD__, " - Update $key to '$newValue' in m_PbxSettings", LOG_INFO);
} else {
SystemMessages::sysLogMsg(__METHOD__, " - Update $key failed: " . implode($out) . PHP_EOL . 'Command:' . PHP_EOL . $command, LOG_ERR);
}
Expand Down
7 changes: 6 additions & 1 deletion src/Core/System/Network.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
namespace MikoPBX\Core\System;

use MikoPBX\Common\Models\LanInterfaces;
use MikoPBX\Common\Models\PbxSettings;
use MikoPBX\Common\Models\PbxSettingsConstants;
use MikoPBX\Core\Utilities\SubnetCalculator;
use MikoPBX\PBXCoreREST\Lib\Sysinfo\GetExternalIpInfoAction;
use Phalcon\Di\Injectable;
Expand Down Expand Up @@ -200,7 +202,10 @@ public function getGeneralNetSettings(): array
/** @var LanInterfaces $eth_settings */
$eth_settings = LanInterfaces::findFirst("disabled='0'");
if ($eth_settings !== null) {
$eth_settings->internet = 1;
$eth_settings->internet = '1';
if (PbxSettings::getValueByKey(PbxSettingsConstants::ENABLE_USE_NAT)==='1'){
$eth_settings->topology=LanInterfaces::TOPOLOGY_PRIVATE;
}
$eth_settings->save();
}
}
Expand Down

0 comments on commit 016911d

Please sign in to comment.