| 1 |
"""VPN endpoint module.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import re |
| 6 |
from typing import Any |
| 7 |
|
| 8 |
from asusrouter.modules.data import AsusData |
| 9 |
from asusrouter.modules.openvpn import AsusOVPNClient, AsusOVPNServer |
| 10 |
from asusrouter.tools.cleaners import clean_content |
| 11 |
from asusrouter.tools.converters import safe_int |
| 12 |
from asusrouter.tools.readers import read_as_snake_case, read_js_variables |
| 13 |
|
| 14 |
from .vpn_const import MAP_OVPN_CLIENT, MAP_OVPN_SERVER |
| 15 |
|
| 16 |
_TO_IGNORE = ("", "None", "0.0.0.0") # noqa: S104 |
| 17 |
|
| 18 |
|
| 19 |
def read(content: str, **kwargs: Any) -> dict[str, Any]: |
| 20 |
"""Read VPN data.""" |
| 21 |
|
| 22 |
vpn: dict[str, Any] = { |
| 23 |
"client": {}, |
| 24 |
"server": {}, |
| 25 |
} |
| 26 |
|
| 27 |
# Read the js variables |
| 28 |
values: dict[str, Any] = read_js_variables(clean_content(content)) |
| 29 |
|
| 30 |
# Values to ignore |
| 31 |
|
| 32 |
# Read the OpenVPN data |
| 33 |
for party in ("client", "server"): |
| 34 |
# Select proper method and state |
| 35 |
method = read_ovpn_client if party == "client" else read_ovpn_server |
| 36 |
state = AsusOVPNClient if party == "client" else AsusOVPNServer |
| 37 |
|
| 38 |
for num in range(1, 11): |
| 39 |
key = f"vpn_{party}{num}_status" |
| 40 |
if key not in values: |
| 41 |
break |
| 42 |
|
| 43 |
# Read the data |
| 44 |
if values[key] not in _TO_IGNORE: |
| 45 |
vpn[party][num] = method(values[key]) |
| 46 |
vpn[party][num]["state"] = state.CONNECTED |
| 47 |
else: |
| 48 |
vpn[party][num] = {"state": state.DISCONNECTED} |
| 49 |
|
| 50 |
# Read additional data |
| 51 |
for add_key in ("ip", "rip"): |
| 52 |
search_value = values.get(f"vpn_{party}{num}_{add_key}") |
| 53 |
if search_value and search_value not in _TO_IGNORE: |
| 54 |
vpn[party][num][add_key] = search_value |
| 55 |
|
| 56 |
# Read remote |
| 57 |
search_value = vpn[party][num].get("remote") |
| 58 |
if search_value: |
| 59 |
vpn[party][num]["remote"] = read_ovpn_remote(search_value) |
| 60 |
|
| 61 |
return vpn |
| 62 |
|
| 63 |
|
| 64 |
def read_ovpn_client(content: str) -> dict[str, Any]: |
| 65 |
"""Read OpenVPN client data.""" |
| 66 |
|
| 67 |
ovpn_client: dict[str, Any] = {} |
| 68 |
|
| 69 |
# Strip the content and make sure it's not an empty string |
| 70 |
content = content.strip() |
| 71 |
if content in ("", "None"): |
| 72 |
return ovpn_client |
| 73 |
|
| 74 |
# -------------------- |
| 75 |
# THIS PART IS LEGACY AND CAN BE REMOVED IN THE FUTURE --> |
| 76 |
# -------------------- |
| 77 |
|
| 78 |
for value in MAP_OVPN_CLIENT: |
| 79 |
key_old, key_new, method = value |
| 80 |
match = re.search(f"{key_old},(.*?)(?=>)", content) |
| 81 |
if match: |
| 82 |
ovpn_client[key_new] = method(match[1]) if method else match[1] |
| 83 |
|
| 84 |
# -------------------- |
| 85 |
# <-- THIS PART IS LEGACY AND CAN BE REMOVED IN THE FUTURE |
| 86 |
# -------------------- |
| 87 |
|
| 88 |
return ovpn_client |
| 89 |
|
| 90 |
|
| 91 |
def read_ovpn_remote(content: str) -> dict[str, Any]: |
| 92 |
"""Read OpenVPN remote data.""" |
| 93 |
|
| 94 |
try: |
| 95 |
parts = content.split(",") |
| 96 |
auth = parts[1] |
| 97 |
parts = parts[0].split(":") |
| 98 |
ip_address = parts[0] |
| 99 |
port = safe_int(parts[1]) |
| 100 |
except IndexError: |
| 101 |
return {} |
| 102 |
|
| 103 |
return { |
| 104 |
"ip": ip_address, |
| 105 |
"port": port, |
| 106 |
"auth": auth, |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
def read_ovpn_server(content: str) -> dict[str, Any]: |
| 111 |
"""Read OpenVPN server data.""" |
| 112 |
|
| 113 |
ovpn_server: dict[str, Any] = {} |
| 114 |
|
| 115 |
# Strip the content and make sure it's not an empty string |
| 116 |
content = content.strip() |
| 117 |
if content in ("", "None"): |
| 118 |
return ovpn_server |
| 119 |
|
| 120 |
# -------------------- |
| 121 |
# THIS PART IS LEGACY AND CAN BE REMOVED IN THE FUTURE --> |
| 122 |
# -------------------- |
| 123 |
|
| 124 |
# Map the values |
| 125 |
for value in MAP_OVPN_SERVER: |
| 126 |
key_old, key_new = value |
| 127 |
if key_old in content: |
| 128 |
flag = False |
| 129 |
keys = [] |
| 130 |
# Find headers |
| 131 |
header = re.search(f"HEADER,{key_old},(.*?)(?=>)", content) |
| 132 |
if header: |
| 133 |
keys_raw = header[1].split(",") |
| 134 |
keys = [read_as_snake_case(element) for element in keys_raw] |
| 135 |
flag = True |
| 136 |
|
| 137 |
# Hide header |
| 138 |
content = content.replace(f"HEADER,{key_old},", "USED-") |
| 139 |
|
| 140 |
ovpn_server[key_new] = [] |
| 141 |
# Find values |
| 142 |
while value := re.search(f"{key_old},(.*?)(?=>)", content): |
| 143 |
if not value: |
| 144 |
break |
| 145 |
if flag: |
| 146 |
cut = value[1].split(",") |
| 147 |
array = dict(zip(keys, cut)) |
| 148 |
ovpn_server[key_new].append(array) |
| 149 |
else: |
| 150 |
ovpn_server[key_new].append(value[1]) |
| 151 |
content = content.replace(f"{key_old},{value[1]}", "USED-") |
| 152 |
# -------------------- |
| 153 |
# <-- THIS PART IS LEGACY AND CAN BE REMOVED IN THE FUTURE |
| 154 |
# -------------------- |
| 155 |
|
| 156 |
return ovpn_server |
| 157 |
|
| 158 |
|
| 159 |
def process(data: dict[str, Any]) -> dict[AsusData, Any]: |
| 160 |
"""Process VPN data.""" |
| 161 |
|
| 162 |
vpn: dict[AsusData, Any] = { |
| 163 |
AsusData.OPENVPN_CLIENT: data.get("client"), |
| 164 |
AsusData.OPENVPN_SERVER: data.get("server"), |
| 165 |
} |
| 166 |
|
| 167 |
return vpn |