Skip to content

Commit

Permalink
app resources: Fix firewall, exposed can be a bool too
Browse files Browse the repository at this point in the history
  • Loading branch information
Salamandar committed Dec 20, 2024
1 parent 6802b91 commit a1cfdf1
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions src/utils/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,13 @@ def _port_is_used(self, port):

return used_by_process or used_by_app or used_by_self_provisioning

def _exposed_to_protos(self, exposed: str | bool) -> list[str]:
if isinstance(exposed, bool):
return ["tcp"] if exposed else []
if exposed.lower() == "both":
return ["tcp", "udp"]
return [exposed.lower()]

def provision_or_update(self, context: Dict = {}):
from yunohost.firewall import YunoFirewall
firewall = YunoFirewall()
Expand Down Expand Up @@ -1408,16 +1415,9 @@ def provision_or_update(self, context: Dict = {}):
self.ports_used_by_self.append(port_value)
self.set_setting(setting_name, port_value)

if infos["exposed"]:
if infos["exposed"].lower() == "both":
protos = ["tcp", "udp"]
else:
protos = [infos["exposed"].lower()]

comment = f"{self.app} {name}"
for proto in protos:
firewall.open_port(proto, port_value, comment, infos["upnp"])

comment = f"{self.app} {name}"
for proto in self._exposed_to_protos(infos["exposed"]):

Check warning

Code scanning / CodeQL

Unnecessary 'else' clause in loop Warning

This 'for' statement has a redundant 'else' as no 'break' is present in the body.
firewall.open_port(proto, port_value, comment, infos["upnp"])
else:
for proto in ["tcp", "udp"]:
firewall.close_port(proto, port_value)
Expand All @@ -1434,11 +1434,7 @@ def deprovision(self, context: Dict = {}):
value = self.get_setting(setting_name)
self.delete_setting(setting_name)
if value and str(value).strip():
if infos["exposed"].lower() == "both":
protos = ["tcp", "udp"]
else:
protos = [infos["exposed"].lower()]
for proto in protos:
for proto in self._exposed_to_protos(infos["exposed"]):
firewall.delete_port(proto, value)

if firewall.need_reload:
Expand Down

0 comments on commit a1cfdf1

Please sign in to comment.