Skip to content

Commit

Permalink
Add possibility to change external hostname and IP by environment set…
Browse files Browse the repository at this point in the history
…tings in Docker
  • Loading branch information
jorikfon committed May 8, 2024
1 parent 9fc4444 commit 9a5a720
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/Common/Models/PbxSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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=>'',
];
}

Expand Down
6 changes: 3 additions & 3 deletions src/Common/Models/PbxSettingsConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';

}
32 changes: 21 additions & 11 deletions src/Core/System/DockerEntrypoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
/**
Expand Down Expand Up @@ -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);
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/Core/System/Network.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down

0 comments on commit 9a5a720

Please sign in to comment.