Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further OpenVPN Server Refinements #297

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 49 additions & 6 deletions custom_components/opnsense/pyopnsense/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,10 @@ async def _get_telemetry_filesystems(self) -> list:

@_log_errors
async def get_openvpn(self) -> Mapping[str, Any]:
# https://docs.opnsense.org/development/api/core/openvpn.html
# https://github.com/opnsense/core/blob/master/src/opnsense/www/js/widgets/OpenVPNClients.js
# https://github.com/opnsense/core/blob/master/src/opnsense/www/js/widgets/OpenVPNServers.js

sessions_info: Mapping[str, Any] = await self._get(
"/api/openvpn/service/searchSessions"
)
Expand Down Expand Up @@ -1396,20 +1400,28 @@ async def get_openvpn(self) -> Mapping[str, Any]:
openvpn["servers"][server_id].update(
{
"name": session.get("description", ""),
"status": (
"up"
if session.get("status", None) == "ok"
else session.get("status", "down")
),
"clients": [],
}
)

if not session.get("is_client", False):
if openvpn["servers"][server_id].get("enabled", True) is False:
openvpn["servers"][server_id].update({"status": "disabled"})
elif session.get("status", None) in ["connected", "ok"]:
openvpn["servers"][server_id].update({"status": "up"})
elif session.get("status", None) in ["failed"]:
openvpn["servers"][server_id].update({"status": "failed"})
elif isinstance(session.get("status", None), str):
openvpn["servers"][server_id].update(
{"status": session.get("status")}
)
else:
openvpn["servers"][server_id].update({"status": "down"})
openvpn["servers"][server_id]["clients"] = []
else:
openvpn["servers"][server_id].update({"status": "up"})
openvpn["servers"][server_id]["connected_clients"] += 1
client = {
client: Mapping[str, Any] = {
"common_name": session.get("common_name", None),
"endpoint": session.get("real_address", None),
"bytes_recv": self._try_to_int(session.get("bytes_received", 0), 0),
Expand All @@ -1429,6 +1441,14 @@ async def get_openvpn(self) -> Mapping[str, Any]:
)
}
)
if session.get("status", None) in ["connected", "ok"]:
client.update({"status": "connected"})
elif session.get("status", None) in ["failed"]:
client.update({"status": "failed"})
elif isinstance(session.get("status", None), str):
client.update({"status": session.get("status")})
else:
client.update({"status": "disabled"})
openvpn["servers"][server_id]["clients"].append(client)

for uuid, server in openvpn["servers"].items():
Expand Down Expand Up @@ -1466,6 +1486,29 @@ async def get_openvpn(self) -> Mapping[str, Any]:
"uuid": instance.get("uuid", None),
"enabled": bool(instance.get("enabled", "0") == "1"),
}
if (
openvpn["clients"][instance.get("uuid")].get("enabled", True)
is False
):
openvpn["clients"][instance.get("uuid")].update(
{"status": "disabled"}
)
elif instance.get("status", None) in ["connected", "ok"]:
openvpn["clients"][instance.get("uuid")].update(
{"status": "connected"}
)
elif instance.get("status", None) in ["failed"]:
openvpn["clients"][instance.get("uuid")].update(
{"status": "failed"}
)
elif isinstance(instance.get("status", None), str):
openvpn["clients"][instance.get("uuid")].update(
{"status": instance.get("status")}
)
else:
openvpn["clients"][instance.get("uuid")].update(
{"status": "stopped"}
)

# for connect in sessions_info.get("rows", []):
# if not isinstance(connect, Mapping) or not connect:
Expand Down
23 changes: 21 additions & 2 deletions custom_components/opnsense/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,6 @@ def _handle_coordinator_update(self) -> None:
"tunnel_addresses",
"dns_servers",
"latest_handshake",
"clients",
]
elif prop_name == "connected_clients":
properties: list = [
Expand All @@ -823,7 +822,6 @@ def _handle_coordinator_update(self) -> None:
"tunnel_addresses",
"dns_servers",
"latest_handshake",
"clients",
]
elif prop_name == "connected_servers":
properties: list = [
Expand All @@ -843,6 +841,27 @@ def _handle_coordinator_update(self) -> None:
for attr in properties:
if instance.get(attr, None) is not None:
self._attr_extra_state_attributes[attr] = instance.get(attr)

if (
isinstance(instance.get("clients", None), list)
and clients_servers == "servers"
and prop_name in ["connected_clients", "status"]
):
self._attr_extra_state_attributes["clients"] = []
for clnt in instance.get("clients"):
client: Mapping[str, Any] = {}
for client_attr in [
"common_name",
"status",
"endpoint",
"tunnel_addresses",
"latest_handshake",
"byes_sent",
"bytes_recv",
]:
if clnt.get(client_attr, None) is not None:
client[client_attr] = clnt.get(client_attr)
self._attr_extra_state_attributes["clients"].append(client)
self.async_write_ha_state()

@property
Expand Down
Loading