diff --git a/dhcp-hosts.dpto2 b/dhcp-hosts.dpto2 index eea81d5..b980259 100644 --- a/dhcp-hosts.dpto2 +++ b/dhcp-hosts.dpto2 @@ -95,12 +95,13 @@ ff:ff:ff:ff:ff:fe,10.6.122.93 ff:ff:ff:ff:ff:fe,10.6.122.94 ff:ff:ff:ff:ff:fe,10.6.122.95 # { "reserved-by": "Andy" } -00:23:54:8c:65:13,10.6.122.96 +00:23:54:8c:65:13,10.6.122.96,set:client0023548c6513 ff:ff:ff:ff:ff:fe,10.6.122.97 ff:ff:ff:ff:ff:fe,10.6.122.98 ff:ff:ff:ff:ff:fe,10.6.122.99 ff:ff:ff:ff:ff:fe,10.6.122.100 -ff:ff:ff:ff:ff:fe,10.6.122.101 +# { "reserved-by": "Andy" } +aa:bb:cc:dd:ee:ff,10.6.122.101,set:clientaabbccddeeff ff:ff:ff:ff:ff:fe,10.6.122.102 ff:ff:ff:ff:ff:fe,10.6.122.103 ff:ff:ff:ff:ff:fe,10.6.122.104 diff --git a/dhcp-opts.dpto2 b/dhcp-opts.dpto2 index 6fbf936..cad2290 100644 --- a/dhcp-opts.dpto2 +++ b/dhcp-opts.dpto2 @@ -1 +1,3 @@ tag:client5cf9ffffffff,option:router,10.0.0.1 +tag:clientaabbccddeeff,option:router,10.6.122.96 +tag:client0023548c6513,option:router,10.6.122.2 diff --git a/main.py b/main.py index 10c6fd1..fcc1093 100755 --- a/main.py +++ b/main.py @@ -23,6 +23,7 @@ app.secret_key = "6ab77f3c45447429c2ae163c260a626029519a66450e474c" users_file = "/etc/freeradius/users.dpto2" dhcp_hosts_file = "/etc/dnsmasq.d/dpto2/dhcp-hosts" dhcp_opts_file = "/etc/dnsmasq.d/dpto2/dhcp-opts" +impossible_mac = "ff:ff:ff:ff:ff:fe" class Lock: def __init__(self, lockfile): @@ -191,19 +192,21 @@ def list_users(): return render_template("users.html", users=lines) mac_re = re.compile("(?:[0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}") -ip_re = re.compile("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}") +ip_re_str = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" +ip_re = re.compile(ip_re_str) DhcpHostLine = namedtuple("DhcpHostLine","macs ip tag") def parse_dchp_host(line): macs = [] ip = "" - tag = "" + tag = None parts = line.split(",") for p in parts: p = p.strip() m = mac_re.match(p) if m: - macs.append(m.group(0)) + if m.group(0) != impossible_mac: + macs.append(m.group(0)) continue m = ip_re.match(p) if m: @@ -213,14 +216,40 @@ def parse_dchp_host(line): tag = p[4:] return DhcpHostLine(macs, ip, tag) -Ip = namedtuple("Ip","reserved_by dhcp locked") +Ip = namedtuple("Ip","reserved_by dhcp_pool locked macs gw") def load_ips(): ipmap = {} for i in range(1,255): - ipmap[i] = Ip("",True,False) - ipmap[0] = ipmap[255] = Ip("", False, False) + ipmap[i] = Ip( + reserved_by="", + dhcp_pool=True, + locked=False, + macs=[], + gw=None + ) + ipmap[0] = ipmap[255] = Ip( + reserved_by="", + dhcp_pool=False, + locked=False, + macs=[], + gw=None + ) prefix = None + + # Read custom gateways + gws = {} + with open(dhcp_opts_file) as f: + for line in f: + line = line.strip() + if line.startswith("#"): continue + m = re.match( + "tag:(client[0-9a-f]{12}),option:router,(" + ip_re_str + ")", + line + ) + if m: + gws[m.group(1)] = m.group(2) + with open(dhcp_hosts_file) as f: meta = {} for line in f: @@ -241,8 +270,10 @@ def load_ips(): prefix = ".".join(r.ip.split(".")[:-1]) ipmap[ip] = Ip( reserved_by=meta.get("reserved-by",""), - dhcp=False, - locked=meta.get("locked",False) + dhcp_pool=False, + locked=meta.get("locked",False), + macs=r.macs, + gw=gws.get(r.tag, None) ) meta = {} ipmap["prefix"] = prefix @@ -253,7 +284,7 @@ def ips(): ipmap = load_ips() return render_template("ips.html", ipmap=ipmap) -@app.route("/ip/") +@app.route("/ip/", methods=['GET', 'POST']) def ip(last_byte): ipmap = load_ips() addr = "{}.{}".format(ipmap["prefix"], last_byte) diff --git a/templates/ip.html b/templates/ip.html index 9fd84ff..a849cfe 100644 --- a/templates/ip.html +++ b/templates/ip.html @@ -11,45 +11,54 @@

Asignación de {{ addr }}

+{{ meta }} + {% if meta.locked %}
Esta dirección está reservada por Ilúvatar
{% endif %}
+
{% if meta.locked %}
{% endif %} -
-
- - - +
+ -
+ checked="checked" + {%- endif -%}> + + +
+
+
- +
- +
- +
@@ -57,7 +66,7 @@
+ value="{{ meta.macs|join(",") }}"/>
@@ -66,7 +75,7 @@
+ value="{{ meta.gw if meta.gw }}"/>
diff --git a/templates/ips.html b/templates/ips.html index 4c512de..8bfc9f1 100644 --- a/templates/ips.html +++ b/templates/ips.html @@ -23,11 +23,11 @@ {%- if col == 7 %} ips-ver-half{% endif %} {%- if row == 7 %} ips-hor-half{% endif %} {% if unusable %} unusable{% endif %} - {%- if ipmap[ip].dhcp %} dhcp-pool{% endif -%} + {%- if ipmap[ip].dhcp_pool %} dhcp-pool{% endif -%} ">
- {% if ipmap[ip].dhcp %} + {% if ipmap[ip].dhcp_pool %} DHCP {% else %} {{ ipmap[ip].reserved_by }}