diff --git a/core/class/network.class.php b/core/class/network.class.php index 0573c82b32..7958b1da78 100644 --- a/core/class/network.class.php +++ b/core/class/network.class.php @@ -77,36 +77,33 @@ public static function getNetworkAccess($_mode = 'auto', $_protocol = '', $_defa return config::byKey('internalAddr', 'core', $_default); } if ($_protocol == 'ip:port' || $_protocol == 'dns:port') { - return config::byKey('internalAddr') . ':' . config::byKey('internalPort', 'core', 80); + return config::byKey('internalAddr') . ':' . self::getPort($_mode); } if ($_protocol == 'proto:ip' || $_protocol == 'proto:dns') { return config::byKey('internalProtocol') . config::byKey('internalAddr'); } if ($_protocol == 'proto:ip:port' || $_protocol == 'proto:dns:port') { - return config::byKey('internalProtocol') . config::byKey('internalAddr') . ':' . config::byKey('internalPort', 'core', 80); + return config::byKey('internalProtocol') . config::byKey('internalAddr') . ':' . self::getPort($_mode); } if ($_protocol == 'proto:127.0.0.1:port:comp') { if (jeedom::getHardwareName() == 'docker') { - return trim(config::byKey('internalProtocol') . config::byKey('internalAddr') . ':' . config::byKey('internalPort', 'core', 80) . '/' . trim(config::byKey('internalComplement'), '/'), '/'); + return trim(config::byKey('internalProtocol') . config::byKey('internalAddr') . ':' . self::getPort($_mode) . '/' . trim(config::byKey('internalComplement'), '/'), '/'); } - return trim(config::byKey('internalProtocol') . '127.0.0.1:' . config::byKey('internalPort', 'core', 80) . '/' . trim(config::byKey('internalComplement'), '/'), '/'); + return trim(config::byKey('internalProtocol') . '127.0.0.1:' . self::getPort($_mode) . '/' . trim(config::byKey('internalComplement'), '/'), '/'); } if ($_protocol == 'http:127.0.0.1:port:comp') { if (jeedom::getHardwareName() == 'docker') { - return trim('http://'. config::byKey('internalAddr') . ':' . config::byKey('internalPort', 'core', 80) . '/' . trim(config::byKey('internalComplement'), '/'), '/'); + return trim('http://' . config::byKey('internalAddr') . ':' . self::getPort($_mode, 'http') . '/' . trim(config::byKey('internalComplement'), '/'), '/'); } - return trim('http://127.0.0.1:' . config::byKey('internalPort', 'core', 80) . '/' . trim(config::byKey('internalComplement'), '/'), '/'); + return trim('http://127.0.0.1:' . self::getPort($_mode, 'http') . '/' . trim(config::byKey('internalComplement'), '/'), '/'); } - if (config::byKey('internalPort', 'core', '') == '') { - return trim(config::byKey('internalProtocol') . config::byKey('internalAddr') . '/' . trim(config::byKey('internalComplement'), '/'), '/'); + if (config::byKey('internalProtocol') == 'http://' && self::getPort($_mode) == 80) { + return trim(config::byKey('internalProtocol') . config::byKey('internalAddr') . '/' . trim(config::byKey('internalComplement'), '/'), '/'); } - if (config::byKey('internalProtocol') == 'http://' && config::byKey('internalPort', 'core', 80) == 80) { - return trim(config::byKey('internalProtocol') . config::byKey('internalAddr') . '/' . trim(config::byKey('internalComplement'), '/'), '/'); + if (config::byKey('internalProtocol') == 'https://' && self::getPort($_mode) == 443) { + return trim(config::byKey('internalProtocol') . config::byKey('internalAddr') . '/' . trim(config::byKey('internalComplement'), '/'), '/'); } - if (config::byKey('internalProtocol') == 'https://' && config::byKey('internalPort', 'core', 443) == 443) { - return trim(config::byKey('internalProtocol') . config::byKey('internalAddr') . '/' . trim(config::byKey('internalComplement'), '/'), '/'); - } - return trim(config::byKey('internalProtocol') . config::byKey('internalAddr') . ':' . config::byKey('internalPort', 'core', 80) . '/' . trim(config::byKey('internalComplement'), '/'), '/'); + return trim(config::byKey('internalProtocol') . config::byKey('internalAddr') . ':' . self::getPort($_mode) . '/' . trim(config::byKey('internalComplement'), '/'), '/'); } if ($_mode == 'dnsjeedom') { return config::byKey('jeedom::url'); @@ -129,7 +126,7 @@ public static function getNetworkAccess($_mode = 'auto', $_protocol = '', $_defa } } } - return config::byKey('externalAddr') . ':' . config::byKey('externalPort', 'core', 80); + return config::byKey('externalAddr') . ':' . self::getPort($_mode); } if ($_protocol == 'proto:dns:port' || $_protocol == 'proto:ip:port') { if (config::byKey('market::allowDNS') == 1 && config::byKey('jeedom::url') != '' && config::byKey('network::disableMangement') == 0) { @@ -146,7 +143,7 @@ public static function getNetworkAccess($_mode = 'auto', $_protocol = '', $_defa } } } - return config::byKey('externalProtocol') . config::byKey('externalAddr') . ':' . config::byKey('externalPort', 'core', 80); + return config::byKey('externalProtocol') . config::byKey('externalAddr') . ':' . self::getPort($_mode); } if ($_protocol == 'proto:dns' || $_protocol == 'proto:ip') { if (config::byKey('market::allowDNS') == 1 && config::byKey('jeedom::url') != '' && config::byKey('network::disableMangement') == 0) { @@ -176,7 +173,7 @@ public static function getNetworkAccess($_mode = 'auto', $_protocol = '', $_defa } } } - return config::byKey('externalAddr') . ':' . config::byKey('externalPort', 'core', 80); + return config::byKey('externalAddr') . ':' . self::getPort($_mode); } if ($_protocol == 'proto') { if (config::byKey('market::allowDNS') == 1 && config::byKey('jeedom::url') != '' && config::byKey('network::disableMangement') == 0) { @@ -191,17 +188,33 @@ public static function getNetworkAccess($_mode = 'auto', $_protocol = '', $_defa if (config::byKey('dns::token') != '' && config::byKey('market::allowDNS') == 1 && config::byKey('jeedom::url') != '' && config::byKey('network::disableMangement') == 0) { return trim(config::byKey('jeedom::url') . '/' . trim(config::byKey('externalComplement', 'core', ''), '/'), '/'); } - if (config::byKey('externalPort', 'core', '') == '') { - return trim(config::byKey('externalProtocol') . config::byKey('externalAddr') . '/' . trim(config::byKey('externalComplement'), '/'), '/'); - } - if (config::byKey('externalProtocol') == 'http://' && config::byKey('externalPort', 'core', 80) == 80) { + if (config::byKey('externalProtocol') == 'http://' && self::getPort($_mode, 'http') == 80) { return trim(config::byKey('externalProtocol') . config::byKey('externalAddr') . '/' . trim(config::byKey('externalComplement'), '/'), '/'); } - if (config::byKey('externalProtocol') == 'https://' && config::byKey('externalPort', 'core', 443) == 443) { + if (config::byKey('externalProtocol') == 'https://' && self::getPort($_mode, 'https') == 443) { return trim(config::byKey('externalProtocol') . config::byKey('externalAddr') . '/' . trim(config::byKey('externalComplement'), '/'), '/'); } - return trim(config::byKey('externalProtocol') . config::byKey('externalAddr') . ':' . config::byKey('externalPort', 'core', 80) . '/' . trim(config::byKey('externalComplement'), '/'), '/'); + return trim(config::byKey('externalProtocol') . config::byKey('externalAddr') . ':' . self::getPort($_mode) . '/' . trim(config::byKey('externalComplement'), '/'), '/'); + } + } + + private static function getPort(string $_mode = 'internal', string $_protocol = '') { + if ($_mode != 'internal' && $_mode != 'external') { + throw new InvalidArgumentException("Mode must be 'internal' or 'external'. Invalid mode received: {$_mode}"); } + if ($_protocol == '') { + $_protocol = strtolower(trim(config::byKey("{$_mode}Protocol", 'core'), ':/ ')); + } + if ($_protocol != 'http' && $_protocol != 'https') { + throw new RuntimeException("Protocol must be 'http' or 'https'. Invalid protocol: {$_protocol}"); + } + + if ($_protocol == 'http') { + $default_port = 80; + } else { + $default_port = 443; + } + return config::byKey("{$_mode}Port", 'core', $default_port); } public static function checkConf($_mode = 'external') { @@ -209,24 +222,24 @@ public static function checkConf($_mode = 'external') { config::save($_mode . 'Protocol', 'http://'); } if (config::byKey($_mode . 'Port') == '') { - config::save($_mode . 'Port', 80); + config::save($_mode . 'Port', self::getPort($_mode)); } - if (config::byKey($_mode . 'Protocol') == 'https://' && config::byKey($_mode . 'Port') == 80) { + if (config::byKey($_mode . 'Protocol') == 'https://' && self::getPort($_mode) == 80) { config::save($_mode . 'Port', 443); } - if (config::byKey($_mode . 'Protocol') == 'http://' && config::byKey($_mode . 'Port') == 443) { + if (config::byKey($_mode . 'Protocol') == 'http://' && self::getPort($_mode) == 443) { config::save($_mode . 'Port', 80); } if (trim(config::byKey($_mode . 'Complement')) == '/') { config::save($_mode . 'Complement', ''); } if ($_mode == 'internal') { - if (config::byKey('network::disableInternalAuto','core',0) == 0) { + if (config::byKey('network::disableInternalAuto', 'core', 0) == 0) { foreach ((self::getInterfacesInfo()) as $interface) { if ($interface['ifname'] == 'lo' || !isset($interface['addr_info']) || strpos($interface['ifname'], 'docker') !== false || strpos($interface['ifname'], 'tun') !== false || strpos($interface['ifname'], 'br') !== false) { continue; } - if (config::byKey('network::internalAutoInterface','core','auto') != 'auto' && $interface['ifname'] != config::byKey('network::internalAutoInterface','core','auto')){ + if (config::byKey('network::internalAutoInterface', 'core', 'auto') != 'auto' && $interface['ifname'] != config::byKey('network::internalAutoInterface', 'core', 'auto')) { continue; } $ip = null; @@ -337,7 +350,7 @@ public static function dns_create() { $openvpn->setEqType_name('openvpn'); $openvpn->setConfiguration('dev', 'tun'); $openvpn->setConfiguration('proto', 'udp'); - if(config::byKey('dns::preferProtocol') != '' && strpos(config::byKey('dns::protocol'),config::byKey('dns::preferProtocol')) !== false){ + if (config::byKey('dns::preferProtocol') != '' && strpos(config::byKey('dns::protocol'), config::byKey('dns::preferProtocol')) !== false) { $openvpn->setConfiguration('proto', config::byKey('dns::preferProtocol')); } if (config::byKey('dns::vpnurl') != '') { diff --git a/core/config/default.config.ini b/core/config/default.config.ini index 51a797af50..1d54bc82d2 100644 --- a/core/config/default.config.ini +++ b/core/config/default.config.ini @@ -150,9 +150,7 @@ interact::warnme::enable = 1 ;Network internalProtocol = http:// -internalPort = 80 externalProtocol = http:// -externalPort = 80 network::localip = "127.0.0.1;192.168.*.*;10.*.*.*;172.*.*.*" ;Cache