Skip to content

Commit

Permalink
Merge pull request #2982 from Mips2648/alpha
Browse files Browse the repository at this point in the history
different default port by protocol
  • Loading branch information
Mips2648 authored Nov 22, 2024
2 parents 69661dc + 78c3d32 commit 2a34f2b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 31 deletions.
71 changes: 42 additions & 29 deletions core/class/network.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -191,42 +188,58 @@ 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') {
if (config::byKey($_mode . 'Protocol') == '') {
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;
Expand Down Expand Up @@ -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') != '') {
Expand Down
2 changes: 0 additions & 2 deletions core/config/default.config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2a34f2b

Please sign in to comment.