Skip to content

Commit 767de12

Browse files
committed
ENHANCEMENTS:
- /doc can generate firewall rules now
1 parent 248e545 commit 767de12

File tree

14 files changed

+126
-25
lines changed

14 files changed

+126
-25
lines changed

core/data/const.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ class PortType(Enum):
5656

5757
class Port:
5858

59-
def __init__(self, port: int, typ: PortType):
59+
def __init__(self, port: int, typ: PortType, *, public: bool = False):
6060
self.port = port
6161
self.typ = typ
62+
self.public = public
6263

6364
def __repr__(self):
6465
return f'{self.port}/{self.typ.value}'

core/data/impl/nodeimpl.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,7 +1692,7 @@ async def info(self) -> dict:
16921692

16931693
node_dict = {
16941694
"Node": self.node.name,
1695-
"Listen Port": repr(self.node.listen_port),
1695+
"Listen Port": self.node.listen_port,
16961696
"Public IP": self.node.public_ip,
16971697
"Bot Version": f"{self.node.bot_version}.{self.node.sub_version}",
16981698
"DCS Branch": self.dcs_branch,
@@ -1711,6 +1711,6 @@ async def info(self) -> dict:
17111711
if not service:
17121712
continue
17131713
for key, value in service.get_ports().items():
1714-
node_dict[key] = repr(value)
1714+
node_dict[key] = value
17151715

17161716
return node_dict

core/data/instance.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def dcs_port(self) -> Port:
4949
f"You need to run this server as Administrator!")
5050
elif port > 65535:
5151
raise FatalException(f"The DCS port of instance {self.name} is > 65535!")
52-
return Port(port, PortType.BOTH)
52+
return Port(port, PortType.BOTH, public=True)
5353

5454
@property
5555
def webgui_port(self) -> Port:
@@ -59,7 +59,7 @@ def webgui_port(self) -> Port:
5959
f"You need to run this server as Administrator!")
6060
elif webgui_port > 65535:
6161
raise FatalException(f"The WebGUI-port of instance {self.name} is > 65535!")
62-
return Port(webgui_port, PortType.TCP)
62+
return Port(webgui_port, PortType.TCP, public=True)
6363

6464
@property
6565
def bot_port(self) -> Port:

core/utils/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from core.utils.discord import *
99
from core.utils.helper import *
1010
from core.utils.mizedit import *
11+
from core.utils.network import *
1112
from core.utils.os import *
1213
from core.utils.performance import *
1314
from core.utils.squadrons import *

core/utils/network.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from core import Port, PortType
2+
from typing import Iterable
3+
4+
__all__ = ["generate_firewall_rules"]
5+
6+
7+
def fw_rule(port: int, protocol: str, name: str, description: str) -> str:
8+
"""
9+
Returns a single New-NetFirewallRule command string.
10+
"""
11+
cmd = (
12+
f'New-NetFirewallRule '
13+
f'-DisplayName "{name}" '
14+
f'-Direction Inbound '
15+
f'-Action Allow '
16+
f'-Protocol {protocol} '
17+
f'-LocalPort {port} '
18+
f'-Profile Any '
19+
f'-Description "{description}"'
20+
)
21+
return cmd
22+
23+
def generate_firewall_rules(ports: Iterable[Port]) -> str:
24+
"""
25+
Write a PowerShell script that adds inbound rules for the given ports.
26+
"""
27+
lines = [
28+
"# ------------------------------------------------------------",
29+
"# Auto‑generated PowerShell script to add inbound firewall rules",
30+
"# Run this script **as Administrator** in PowerShell.",
31+
"# ------------------------------------------------------------",
32+
"",
33+
"Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force",
34+
""
35+
]
36+
37+
for p in ports:
38+
if p.typ is PortType.BOTH:
39+
# Create two separate rules
40+
for proto in (PortType.TCP, PortType.UDP):
41+
name = f"Allow {p.port}/{proto.value.lower()}"
42+
desc = f"Auto‑generated rule for inbound {p.port}/{proto.value.lower()}"
43+
lines.append(fw_rule(p.port, proto.value, name, desc))
44+
lines.append("") # blank line for readability
45+
else:
46+
name = f"Allow {p.port}/{p.typ.value.lower()}"
47+
desc = f"Auto‑generated rule for inbound {p.port}/{p.typ.value.lower()}"
48+
lines.append(fw_rule(p.port, p.typ.value, name, desc))
49+
lines.append("")
50+
51+
return "\n".join(lines)

extensions/lardoon/extension.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,5 +226,5 @@ async def schedule(self):
226226
@override
227227
def get_ports(self) -> dict[str, Port]:
228228
return {
229-
"Lardoon": Port(self.config['bind'].split(':')[1], PortType.TCP)
229+
"Lardoon": Port(self.config['bind'].split(':')[1], PortType.TCP, public=True)
230230
} if self.enabled else {}

extensions/lotatc/extension.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,6 @@ async def schedule(self):
375375
@override
376376
def get_ports(self) -> dict[str, Port]:
377377
return {
378-
"LotAtc": Port(self.locals.get('port', 10310), PortType.TCP),
378+
"LotAtc": Port(self.locals.get('port', 10310), PortType.TCP, public=True),
379379
"LotAtc JSON Server Port": Port(self.locals.get('lotatc_inst.options', {}).get('jsonserver_port', 8081), PortType.TCP)
380380
} if self.enabled else {}

extensions/olympus/extension.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,6 @@ def shutdown(self, *, quiet: bool = False) -> bool:
340340
def get_ports(self) -> dict[str, Port]:
341341
return {
342342
"Olympus " + self.backend_tag.capitalize(): Port(self.config.get(self.backend_tag, {}).get('port', 4512), PortType.TCP),
343-
"Olympus " + self.frontend_tag.capitalize(): Port(self.config.get(self.frontend_tag, {}).get('port', 3000), PortType.TCP),
343+
"Olympus " + self.frontend_tag.capitalize(): Port(self.config.get(self.frontend_tag, {}).get('port', 3000), PortType.TCP, public=True),
344344
"Olympus WSPort": Port(self.config.get('audio', {}).get('WSPort', 4000), PortType.TCP)
345345
} if self.enabled else {}

extensions/sneaker/extension.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,5 +186,5 @@ async def render(self, param: dict | None = None) -> dict:
186186
@override
187187
def get_ports(self) -> dict[str, Port]:
188188
return {
189-
"Sneaker": Port(self.config['bind'].split(':')[1], PortType.TCP)
189+
"Sneaker": Port(self.config['bind'].split(':')[1], PortType.TCP, public=True)
190190
} if self.enabled else {}

extensions/srs/extension.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ async def schedule(self):
624624
def get_ports(self) -> dict[str, Port]:
625625
if self.enabled:
626626
rc: dict[str, Port] = {
627-
"SRS Port": Port(self.locals['Server Settings']['SERVER_PORT'], PortType.BOTH)
627+
"SRS Port": Port(self.locals['Server Settings']['SERVER_PORT'], PortType.BOTH, public=True)
628628
}
629629
if self.locals['General Settings'].get('LOTATC_EXPORT_ENABLED', False):
630630
rc["LotAtc Export Port"] = Port(self.locals['General Settings'].get('LOTATC_EXPORT_PORT', 10712), PortType.UDP)

0 commit comments

Comments
 (0)