Skip to content

Commit

Permalink
#782 Поправил генерацию iax2.conf. Поправил настройки сетевого экрана…
Browse files Browse the repository at this point in the history
… для IAX.
  • Loading branch information
boffart committed Aug 28, 2024
1 parent b8f91d9 commit 6e6a3cb
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 70 deletions.
1 change: 1 addition & 0 deletions src/Common/Messages/ru.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 - подключение панели телефонии',
Expand Down
14 changes: 14 additions & 0 deletions src/Common/Models/FirewallRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
[
Expand Down Expand Up @@ -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) {
Expand Down
87 changes: 40 additions & 47 deletions src/Core/Asterisk/Configs/IAXConf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand All @@ -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);
Expand All @@ -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;
}

Expand All @@ -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;
}

/**
Expand All @@ -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;
}

}
73 changes: 73 additions & 0 deletions src/Core/System/Upgrade/Releases/UpdateConfigsUpToVer2024v2v10.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/*
* MikoPBX - free phone system for small business
* Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*/

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();
}
}

}
49 changes: 26 additions & 23 deletions src/Core/System/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand All @@ -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";
}
}

Expand Down Expand Up @@ -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;
}
}

Expand Down

0 comments on commit 6e6a3cb

Please sign in to comment.