Development
This commit is contained in:
parent
705c69abc4
commit
470cc39356
5 changed files with 244 additions and 245 deletions
|
|
@ -492,8 +492,9 @@ def build_vlan_dnsmasq_conf(vlan, data, iface):
|
|||
line(f"dhcp-host={s['ip']},{s['hostname']}")
|
||||
line()
|
||||
|
||||
active_res = [r for r in vlan.get("reservations", []) if r.get("enabled") is True]
|
||||
inactive_res = [r for r in vlan.get("reservations", []) if r.get("enabled") is not True]
|
||||
vlan_res = [r for r in data.get("dhcp_reservations", []) if r.get("vlan") == name]
|
||||
active_res = [r for r in vlan_res if r.get("enabled") is True]
|
||||
inactive_res = [r for r in vlan_res if r.get("enabled") is not True]
|
||||
|
||||
if active_res:
|
||||
line("# -- Reservations -----------------------------------------------")
|
||||
|
|
@ -1821,12 +1822,12 @@ RADIUS_USERS_FILE = Path("/etc/freeradius/3.0/users")
|
|||
|
||||
def radius_clients(data):
|
||||
"""Return list of (reservation, vlan) tuples where radius_client is True."""
|
||||
result = []
|
||||
for vlan in data["vlans"]:
|
||||
for r in vlan.get("reservations", []):
|
||||
if r.get("radius_client") is True:
|
||||
result.append((r, vlan))
|
||||
return result
|
||||
vlan_by_name = {v["name"]: v for v in data.get("vlans", [])}
|
||||
return [
|
||||
(r, vlan_by_name[r["vlan"]])
|
||||
for r in data.get("dhcp_reservations", [])
|
||||
if r.get("radius_client") is True and r.get("vlan") in vlan_by_name
|
||||
]
|
||||
|
||||
def radius_enabled(data):
|
||||
"""Return True if any reservation has radius_client: true."""
|
||||
|
|
@ -1889,22 +1890,25 @@ def build_radius_users(data):
|
|||
"",
|
||||
]
|
||||
|
||||
for vlan in data["vlans"]:
|
||||
vlan_by_name = {v["name"]: v for v in data.get("vlans", [])}
|
||||
for r in data.get("dhcp_reservations", []):
|
||||
if r.get("enabled") is not True:
|
||||
continue
|
||||
mac = r.get("mac", "").replace(":", "").lower()
|
||||
if not mac:
|
||||
continue
|
||||
vlan = vlan_by_name.get(r.get("vlan", ""))
|
||||
if not vlan:
|
||||
continue
|
||||
vlan_id = vlan.get('vlan_id')
|
||||
for r in vlan.get("reservations", []):
|
||||
if r.get("enabled") is not True:
|
||||
continue
|
||||
mac = r.get("mac", "").replace(":", "").lower()
|
||||
if not mac:
|
||||
continue
|
||||
lines += [
|
||||
f"# {r['description']} -> VLAN {vlan_id} ({vlan['name']})",
|
||||
f"{mac} Cleartext-Password := \"{mac}\"",
|
||||
f" Tunnel-Type = VLAN,",
|
||||
f" Tunnel-Medium-Type = IEEE-802,",
|
||||
f" Tunnel-Private-Group-Id = \"{vlan_id}\"",
|
||||
"",
|
||||
]
|
||||
lines += [
|
||||
f"# {r['description']} -> VLAN {vlan_id} ({vlan['name']})",
|
||||
f"{mac} Cleartext-Password := \"{mac}\"",
|
||||
f" Tunnel-Type = VLAN,",
|
||||
f" Tunnel-Medium-Type = IEEE-802,",
|
||||
f" Tunnel-Private-Group-Id = \"{vlan_id}\"",
|
||||
"",
|
||||
]
|
||||
|
||||
default_id = default_vlan.get('vlan_id')
|
||||
lines += [
|
||||
|
|
@ -2143,13 +2147,13 @@ def reset_leases(data, vlan_name=None):
|
|||
|
||||
def show_leases(data):
|
||||
# Build MAC -> reservation lookup across all VLANs
|
||||
vlan_by_name = {v["name"]: v for v in data.get("vlans", [])}
|
||||
res_by_mac = {}
|
||||
for vlan in data["vlans"]:
|
||||
for r in vlan.get("reservations", []):
|
||||
if r.get("enabled") is True:
|
||||
mac = r.get("mac", "").lower().strip()
|
||||
if mac:
|
||||
res_by_mac[mac] = (r, vlan)
|
||||
for r in data.get("dhcp_reservations", []):
|
||||
if r.get("enabled") is True:
|
||||
mac = r.get("mac", "").lower().strip()
|
||||
if mac:
|
||||
res_by_mac[mac] = (r, vlan_by_name.get(r.get("vlan", ""), {}))
|
||||
|
||||
now = int(datetime.now().timestamp())
|
||||
any_leases = False
|
||||
|
|
@ -2922,10 +2926,7 @@ def cmd_apply(data, dry_run=False):
|
|||
print("RADIUS (dry-run) ====================================================")
|
||||
num_clients = len(radius_clients(data))
|
||||
default_vlan = next((v for v in data["vlans"] if v.get("radius_default") is True), None)
|
||||
total_macs = sum(
|
||||
len([r for r in v.get("reservations", []) if r.get("enabled") is True])
|
||||
for v in data["vlans"]
|
||||
)
|
||||
total_macs = len([r for r in data.get("dhcp_reservations", []) if r.get("enabled") is True])
|
||||
print(f" Would write: {RADIUS_CLIENTS_CONF}")
|
||||
print(f" {num_clients} RADIUS client(s)")
|
||||
print(f" Would write: {RADIUS_USERS_FILE}")
|
||||
|
|
@ -2944,14 +2945,10 @@ def cmd_apply(data, dry_run=False):
|
|||
|
||||
check_root()
|
||||
|
||||
total_enabled = sum(
|
||||
len([r for r in v.get("reservations", []) if r.get("enabled") is True])
|
||||
for v in data["vlans"] if not is_wg(v)
|
||||
)
|
||||
total_disabled = sum(
|
||||
len([r for r in v.get("reservations", []) if r.get("enabled") is not True])
|
||||
for v in data["vlans"] if not is_wg(v)
|
||||
)
|
||||
wg_names = {v["name"] for v in data["vlans"] if is_wg(v)}
|
||||
non_wg_res = [r for r in data.get("dhcp_reservations", []) if r.get("vlan") not in wg_names]
|
||||
total_enabled = len([r for r in non_wg_res if r.get("enabled") is True])
|
||||
total_disabled = len([r for r in non_wg_res if r.get("enabled") is not True])
|
||||
total_wg_peers = sum(len(v.get("peers", [])) for v in data["vlans"] if is_wg(v))
|
||||
wg_part = f", {total_wg_peers} WG peer(s)" if total_wg_peers else ""
|
||||
print(f"Applying config: {len(data['vlans'])} VLAN(s), "
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue