From 9a5a720e6664a6a612de2bafb059a63a451e1297 Mon Sep 17 00:00:00 2001 From: Nikolay Beketov Date: Wed, 8 May 2024 12:39:12 +0700 Subject: [PATCH] Add possibility to change external hostname and IP by environment settings in Docker --- src/Common/Models/PbxSettings.php | 2 ++ src/Common/Models/PbxSettingsConstants.php | 6 ++-- src/Core/System/DockerEntrypoint.php | 32 ++++++++++++++-------- src/Core/System/Network.php | 3 ++ 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/Common/Models/PbxSettings.php b/src/Common/Models/PbxSettings.php index ffc446ba5..3f3cec5e0 100644 --- a/src/Common/Models/PbxSettings.php +++ b/src/Common/Models/PbxSettings.php @@ -171,6 +171,8 @@ public static function getDefaultArrayValues(): array PbxSettingsConstants::PBX_LICENSE=>'', PbxSettingsConstants::ENABLE_USE_NAT=> '0', PbxSettingsConstants::AUTO_UPDATE_EXTERNAL_IP=> '0', + PbxSettingsConstants::EXTERNAL_SIP_HOST_NAME=>'', + PbxSettingsConstants::EXTERNAL_SIP_IP_ADDR=>'', ]; } diff --git a/src/Common/Models/PbxSettingsConstants.php b/src/Common/Models/PbxSettingsConstants.php index c18735339..243d30860 100644 --- a/src/Common/Models/PbxSettingsConstants.php +++ b/src/Common/Models/PbxSettingsConstants.php @@ -132,9 +132,6 @@ abstract class PbxSettingsConstants const NTP_SERVER = 'NTPServer'; const WWW_ENCRYPTION_KEY = 'WWWEncryptionKey'; - const AUTO_UPDATE_EXTERNAL_IP = 'autoUpdateExternalIp'; - - // Service PORTS settings from /etc/inc/mikopbx-settings.json (For Docker containers) const BEANSTALK_PORT = 'beanstalk'; const REDIS_PORT = 'redis'; @@ -143,5 +140,8 @@ abstract class PbxSettingsConstants // Service constants for Docker containers const ENABLE_USE_NAT='enableUseNat'; + const EXTERNAL_SIP_HOST_NAME='ExternalSipHostName'; + const EXTERNAL_SIP_IP_ADDR='ExternalSipIpAddr'; + const AUTO_UPDATE_EXTERNAL_IP = 'autoUpdateExternalIp'; } \ No newline at end of file diff --git a/src/Core/System/DockerEntrypoint.php b/src/Core/System/DockerEntrypoint.php index 41a3b478a..0bee7fabc 100644 --- a/src/Core/System/DockerEntrypoint.php +++ b/src/Core/System/DockerEntrypoint.php @@ -230,9 +230,15 @@ private function applyEnvironmentSettings(): void break; case PbxSettingsConstants::ENABLE_USE_NAT: if ($envValue==='1'){ - $this->enableNat(); + $this->reconfigureNetwork("topology", LanInterfaces::TOPOLOGY_PRIVATE); } break; + case PbxSettingsConstants::EXTERNAL_SIP_HOST_NAME: + $this->reconfigureNetwork("exthostname", $envValue); + break; + case PbxSettingsConstants::EXTERNAL_SIP_IP_ADDR: + $this->reconfigureNetwork("extipaddr", $envValue); + break; default: $this->updateDBSetting($dbKey, $envValue); break; @@ -242,26 +248,28 @@ 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. + * Reconfigures a network setting in the database. * - * 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. + * This method updates the value of a specified setting for network interfaces + * that are marked as connected to the internet in the database. It logs the outcome + * of the operation, detailing success or failure along with the executed command + * if an error occurs. * + * @param string $key The database column key representing the setting to update. + * @param string $newValue The new value to assign to the specified setting. + * @return void */ - private function enableNat(): void + private function reconfigureNetwork(string $key, string $newValue): 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'\""; + $command = "$sqlite3 $dbPath \"UPDATE m_LanInterfaces SET $key='$newValue' WHERE internet='1'\""; $res = Processes::mwExec($command, $out); if ($res === 0) { - SystemMessages::sysLogMsg(__METHOD__, " - Update topology to '$private' in m_LanInterfaces", LOG_INFO); + SystemMessages::sysLogMsg(__METHOD__, " - Update m_LanInterfaces.$key to '$newValue'", LOG_INFO); } else { - SystemMessages::sysLogMsg(__METHOD__, " - Update topology failed: " . implode($out) . PHP_EOL . 'Command:' . PHP_EOL . $command, LOG_ERR); + SystemMessages::sysLogMsg(__METHOD__, " - Update m_LanInterfaces.$key to '$newValue' failed: " . implode($out) . PHP_EOL . 'Command:' . PHP_EOL . $command, LOG_ERR); } } /** @@ -297,6 +305,8 @@ private function updateDBSetting(string $key, string $newValue): void } else { SystemMessages::sysLogMsg(__METHOD__, " - Update $key failed: " . implode($out) . PHP_EOL . 'Command:' . PHP_EOL . $command, LOG_ERR); } + } elseif(!array_key_exists($key, $this->settings)) { + SystemMessages::sysLogMsg(__METHOD__, " - Unknown environment settings key: $key", LOG_ERR); } } diff --git a/src/Core/System/Network.php b/src/Core/System/Network.php index 79bfda003..f7a832f57 100644 --- a/src/Core/System/Network.php +++ b/src/Core/System/Network.php @@ -423,6 +423,9 @@ public function updateNetSettings(array $data): void */ public function updateExternalIp(): void { + if (PbxSettings::getValueByKey(PbxSettingsConstants::AUTO_UPDATE_EXTERNAL_IP)!=='1'){ + return; + } $ipInfoResult = GetExternalIpInfoAction::main(); if ($ipInfoResult->success && isset($ipInfoResult->data['ip'])) { $currentIP = $ipInfoResult->data['ip'];