From 6e6a3cbd485c616cd50b73dbbe4ba6d82538df61 Mon Sep 17 00:00:00 2001 From: boffart <> Date: Wed, 28 Aug 2024 16:21:06 +0300 Subject: [PATCH] =?UTF-8?q?#782=20=D0=9F=D0=BE=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20=D0=B3=D0=B5=D0=BD=D0=B5=D1=80=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D1=8E=20iax2.conf.=20=D0=9F=D0=BE=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20=D0=BD=D0=B0=D1=81=D1=82=D1=80=D0=BE=D0=B9=D0=BA?= =?UTF-8?q?=D0=B8=20=D1=81=D0=B5=D1=82=D0=B5=D0=B2=D0=BE=D0=B3=D0=BE=20?= =?UTF-8?q?=D1=8D=D0=BA=D1=80=D0=B0=D0=BD=D0=B0=20=D0=B4=D0=BB=D1=8F=20IAX?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common/Messages/ru.php | 1 + src/Common/Models/FirewallRules.php | 14 +++ src/Core/Asterisk/Configs/IAXConf.php | 87 +++++++++---------- .../UpdateConfigsUpToVer2024v2v10.php | 73 ++++++++++++++++ src/Core/System/Util.php | 49 ++++++----- 5 files changed, 154 insertions(+), 70 deletions(-) create mode 100644 src/Core/System/Upgrade/Releases/UpdateConfigsUpToVer2024v2v10.php diff --git a/src/Common/Messages/ru.php b/src/Common/Messages/ru.php index fc0bc506f..90b5424a1 100644 --- a/src/Common/Messages/ru.php +++ b/src/Common/Messages/ru.php @@ -543,6 +543,7 @@ 'fw_Permit' => 'Подсеть', 'fw_Rules' => 'Доступные сервисы', 'fw_sipDescription' => 'SIP & RTP - регистрация телефонов и голосовой трафик', + 'fw_iaxDescription' => 'IAX - регистрация телефонов и голосовой трафик', 'fw_webDescription' => 'WEB - доступ к административному интерфейсу', 'fw_amiDescription' => 'AMI - доступ к asterisk manager api через telnet', 'fw_ctiDescription' => 'CTI - подключение панели телефонии', diff --git a/src/Common/Models/FirewallRules.php b/src/Common/Models/FirewallRules.php index 3f1489a6b..b24574c5d 100644 --- a/src/Common/Models/FirewallRules.php +++ b/src/Common/Models/FirewallRules.php @@ -146,6 +146,19 @@ public static function getDefaultRules(): array 'action' => 'allow', 'shortName' => 'SIP & RTP', ], + 'IAX' => [ + 'rules' => [ + [ + 'portfrom' => $protectedPortSet[PbxSettingsConstants::IAX_PORT], + 'portto' => $protectedPortSet[PbxSettingsConstants::IAX_PORT], + 'protocol' => 'udp', + 'portFromKey' => PbxSettingsConstants::IAX_PORT, + 'portToKey' => PbxSettingsConstants::IAX_PORT, + ] + ], + 'action' => 'allow', + 'shortName' => 'IAX', + ], 'WEB' => [ 'rules' => [ [ @@ -255,6 +268,7 @@ public static function getProtectedPortSet(): array PbxSettingsConstants::WEB_PORT, PbxSettingsConstants::WEB_HTTPS_PORT, PbxSettingsConstants::SSH_PORT, + PbxSettingsConstants::IAX_PORT, ]; $result = []; foreach ($portSet as $portName) { diff --git a/src/Core/Asterisk/Configs/IAXConf.php b/src/Core/Asterisk/Configs/IAXConf.php index c475d2d95..52a1b3bb9 100644 --- a/src/Core/Asterisk/Configs/IAXConf.php +++ b/src/Core/Asterisk/Configs/IAXConf.php @@ -36,9 +36,7 @@ class IAXConf extends AsteriskConfigClass { // The module hook applying priority public int $priority = 600; - public const TYPE_IAX2 = 'IAX2'; - protected string $description = 'iax.conf'; /** @@ -64,9 +62,8 @@ public function extensionGenContexts(): string */ protected function generateConfigProtected(): void { - $conf = ''; - $conf .= $this->generateGeneral(); - $conf .= $this->generateProviders(); + $conf = $this->generateGeneral(); + $conf.= $this->generateProviders(); // Write the configuration content to the file Util::fileWriteContent($this->config->path('asterisk.astetcdir') . '/iax.conf', $conf); @@ -80,17 +77,15 @@ protected function generateConfigProtected(): void */ private function generateGeneral(): string { - $iax_port = (trim($this->generalSettings[PbxSettingsConstants::IAX_PORT]) !== '') ? $this->generalSettings[PbxSettingsConstants::IAX_PORT] : '4569'; - $conf = '[general]' . "\n"; - // $conf .= "context=public-direct-dial \n"; - $conf .= "bindport={$iax_port}\n"; - $conf .= "bindaddr=0.0.0.0\n"; - $conf .= "delayreject=yes\n"; - $conf .= "iaxthreadcount=100\n"; - $conf .= "iaxmaxthreadcount=200\n"; - $conf .= "jitterbuffer=no\n"; - $conf .= "forcejitterbuffer=no\n\n"; - + $iax_port = $this->generalSettings[PbxSettingsConstants::IAX_PORT]; + $conf = '[general]'.PHP_EOL; + $conf .= "bindport=$iax_port".PHP_EOL; + $conf .= "bindaddr=0.0.0.0".PHP_EOL; + $conf .= "delayreject=yes".PHP_EOL; + $conf .= "iaxthreadcount=100".PHP_EOL; + $conf .= "iaxmaxthreadcount=200".PHP_EOL; + $conf .= "jitterbuffer=no".PHP_EOL; + $conf .= "forcejitterbuffer=no".PHP_EOL.PHP_EOL; return $conf; } @@ -107,37 +102,42 @@ private function generateProviders(): string $lang = str_replace('_', '-', strtolower($this->generalSettings[PbxSettingsConstants::PBX_LANGUAGE])); $providers = $this->getProviders(); foreach ($providers as $provider) { - $prov_config .= "[{$provider['uniqid']}];\n"; - $prov_config .= "type=friend\n"; - $prov_config .= "auth=md5\n"; - $prov_config .= "context={$provider['uniqid']}-incoming \n"; - $prov_config .= "language={$lang}\n"; - $prov_config .= "qualify=2000\n"; - $prov_config .= "transfer=mediaonly\n"; - $prov_config .= "disallow=all\n"; - $prov_config .= ";username={$provider['username']}\n"; - $prov_config .= "host=dynamic\n"; - $prov_config .= "trunk=yes\n"; - - $prov_config .= "secret={$provider['secret']}\n"; + $manual_attributes = Util::parseIniSettings(base64_decode($provider['manualattributes'])); + $options = [ + 'type' => 'friend', + 'auth' => 'plaintext', + 'context' => "{$provider['uniqid']}-incoming", + 'language' => $lang, + 'qualify' => 2000, + 'transfer' => 'mediaonly', + 'disallow' => 'all', + 'username' => $provider['username'], + 'trunk' => 'yes', + 'secret' => $provider['secret'], + 'host' => 'dynamic' + ]; + $prov_config .= "[{$provider['uniqid']}];".PHP_EOL; foreach ($provider['codecs'] as $codec) { - $prov_config .= "allow={$codec}\n"; + $prov_config .= "allow=$codec".PHP_EOL; } $prov_config .= "setvar=contextID={$provider['uniqid']}-incoming".PHP_EOL; - $prov_config .= "\n"; + $prov_config .= Util::overrideConfigurationArray($options, $manual_attributes, ' '); + $prov_config .= PHP_EOL; // Formulate the registration string - if ($provider['noregister'] == 0) { + if ($provider['noregister'] === '0') { // Registration is only required if the current host has a dynamic IP - $user = $provider['username']; - $secret = (trim($provider['secret']) == '') ? '' : ":{$provider['secret']}"; - $host = $provider['host']; - $port = ''; - $reg_strings .= "register => {$user}{$secret}@{$host}{$port} \n"; + $user = $options['username']; + $secret = (trim($options['secret']) === '') ? '' : ":{$options['secret']}"; + [$host, $port] = explode( ':',$provider['host']); + if(!empty($port)){ + $port = ":$port"; + } + $reg_strings .= "register => $user$secret@$host$port ".PHP_EOL; } } - return $reg_strings . "\n" . $prov_config; + return $reg_strings .PHP_EOL . $prov_config; } /** @@ -151,26 +151,19 @@ private function getProviders(): array // Получим настройки всех аккаунтов. $arrIaxProviders = Iax::find("disabled IS NULL OR disabled = '0'"); foreach ($arrIaxProviders as $peer) { - /** @var \MikoPBX\Common\Models\Iax $peer */ + /** @var Iax $peer */ $arr_data = $peer->toArray(); - - // $network_filter = NetworkFilters::findFirst($peer->networkfilterid); - // $arr_data['permit'] = ($network_filter==null)?'':$network_filter->permit; - // $arr_data['deny'] = ($network_filter==null)?'':$network_filter->deny; - $arr_data['codecs'] = []; $filter = [ 'conditions' => 'disabled="0"', 'order' => 'type, priority', ]; - $codecs = Codecs::find($filter); + $codecs = Codecs::find($filter); foreach ($codecs as $ob_codec) { $arr_data['codecs'][] = $ob_codec->name; } $data_providers[] = $arr_data; } - return $data_providers; } - } diff --git a/src/Core/System/Upgrade/Releases/UpdateConfigsUpToVer2024v2v10.php b/src/Core/System/Upgrade/Releases/UpdateConfigsUpToVer2024v2v10.php new file mode 100644 index 000000000..8af0480f3 --- /dev/null +++ b/src/Core/System/Upgrade/Releases/UpdateConfigsUpToVer2024v2v10.php @@ -0,0 +1,73 @@ +. + */ + +namespace MikoPBX\Core\System\Upgrade\Releases; + +use MikoPBX\Common\Models\FirewallRules; +use MikoPBX\Common\Models\NetworkFilters; +use MikoPBX\Common\Models\PbxSettings; +use MikoPBX\Common\Models\PbxSettingsConstants; +use MikoPBX\Core\System\Upgrade\UpgradeSystemConfigInterface; +use Phalcon\Di\Injectable; + +class UpdateConfigsUpToVer2024v2v10 extends Injectable implements UpgradeSystemConfigInterface +{ + public const PBX_VERSION = '2024.2.10'; + + /** + * Class constructor. + */ + public function __construct() + { + } + + /** + * https://github.com/mikopbx/Core/issues/782 + */ + public function processUpdate(): void + { + $colName = PbxSettingsConstants::IAX_PORT; + $iax_port = PbxSettings::getValueByKey(PbxSettingsConstants::IAX_PORT); + $nets = NetworkFilters::find(['columns' => 'id']); + foreach ($nets as $net){ + $filter = [ + "portFromKey='$colName' AND networkfilterid='$net->id'", + 'columns' => 'id' + ]; + $rule = FirewallRules::findFirst($filter); + if($rule){ + continue; + } + $rule = new FirewallRules(); + foreach ($rule->toArray() as $key => $value){ + $rule->$key = $value; + } + $rule->networkfilterid = $net->id; + $rule->action = 'block'; + $rule->portfrom = $iax_port; + $rule->portto = $iax_port; + $rule->protocol = 'udp'; + $rule->portFromKey = $colName; + $rule->portToKey = $colName; + $rule->category = 'IAX'; + $rule->save(); + } + } + +} \ No newline at end of file diff --git a/src/Core/System/Util.php b/src/Core/System/Util.php index cd534c1db..7e29d93b1 100644 --- a/src/Core/System/Util.php +++ b/src/Core/System/Util.php @@ -53,7 +53,7 @@ class Util * * @return string The resulting configuration string. */ - public static function overrideConfigurationArray($options, $manual_attributes, $section): string + public static function overrideConfigurationArray(&$options, $manual_attributes, $section): string { $result_config = ''; if ($manual_attributes !== null && isset($manual_attributes[$section])) { @@ -70,9 +70,9 @@ public static function overrideConfigurationArray($options, $manual_attributes, } if (is_array($value)) { array_unshift($value, ' '); - $result_config .= trim(implode("\n{$key} = ", $value)) . "\n"; + $result_config .= trim(implode("\n$key = ", $value)) . "\n"; } else { - $result_config .= "{$key} = {$value}\n"; + $result_config .= "$key = $value\n"; } } @@ -744,30 +744,33 @@ public static function parseIniSettings(string $manual_attributes): array foreach ($sections as $section) { $data_rows = explode("\n", trim($section)); $section_name = trim($data_rows[0] ?? ''); - if (!empty($section_name)) { + if(empty($section_name) || strpos($section_name, '[') === false){ + // Noname section + $section_name = ' '; + }else{ unset($data_rows[0]); - $manual_data[$section_name] = []; - foreach ($data_rows as $row) { - $value = ''; + } + $manual_data[$section_name] = []; + foreach ($data_rows as $row) { + $value = ''; - // Skip rows without an equal sign - if (strpos($row, '=') === false) { - continue; - } - $key = ''; - $arr_value = explode('=', $row); - if (count($arr_value) > 1) { - $key = trim($arr_value[0]); - unset($arr_value[0]); - $value = trim(implode('=', $arr_value)); - } + // Skip rows without an equal sign + if (strpos($row, '=') === false) { + continue; + } + $key = ''; + $arr_value = explode('=', $row); + if (count($arr_value) > 1) { + $key = trim($arr_value[0]); + unset($arr_value[0]); + $value = trim(implode('=', $arr_value)); + } - // Skip rows with empty key or value not equal to '0' - if (($value !== '0' && empty($value)) || empty($key)) { - continue; - } - $manual_data[$section_name][$key] = $value; + // Skip rows with empty key or value not equal to '0' + if (($value !== '0' && empty($value)) || empty($key)) { + continue; } + $manual_data[$section_name][$key] = $value; } }