From 1223f1f94a110eb0ec50b9f35cf03c5757cc0603 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 5 Nov 2025 22:00:42 +0100 Subject: [PATCH] =?UTF-8?q?ocsense.lua=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ocsense.lua | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 ocsense.lua diff --git a/ocsense.lua b/ocsense.lua new file mode 100644 index 0000000..f18ab34 --- /dev/null +++ b/ocsense.lua @@ -0,0 +1,60 @@ +local event = require("event") +local minitel = require("minitel") +local serialization = require("serialization") + +local PORT = 5353 +local LOCAL_DOMAIN = "home1" -- oder "home1.net" +local records = {} +local running = true + +print("[ocsense] DNS/UUID-Server gestartet auf port " .. PORT) +print("[ocsense] Strg+C zum Beenden") + +local function stripLocalDomain(name) + if name:sub(-#LOCAL_DOMAIN) == LOCAL_DOMAIN then + local dot = name:sub(-( #LOCAL_DOMAIN + 1 ), -( #LOCAL_DOMAIN + 1 )) + if dot == "." then + return name:sub(1, -( #LOCAL_DOMAIN + 2 )) + else + return name:sub(1, -( #LOCAL_DOMAIN + 1 )) + end + end + return name +end + +while running do + local ev, fromName, port, data, fromAddr = event.pull() + if ev == "interrupted" then + print("[ocsense] beendet durch Strg+C") + running = false + + elseif ev == "net_msg" and port == PORT and type(data) == "string" then + local cmd, a, b = data:match("^(%S+)%s*(%S*)%s*(%S*)") + + if cmd == "REG" and a ~= "" and b ~= "" then + -- a = hostname, b = hardware UUID + records[a] = b + print("[ocsense] REG", a, "=>", b) + + elseif cmd == "Q" and a ~= "" then + local replyto = (b ~= "" and b) or fromName + local addr = records[a] + + -- lokale Domain abstreifen, falls nötig + if not addr then + local short = stripLocalDomain(a) + if short ~= a then + addr = records[short] + end + end + + if addr then + minitel.usend(replyto, PORT, "A " .. a .. " " .. addr) + print("[ocsense] A", a, "->", addr) + else + minitel.usend(replyto, PORT, "NX " .. a) + print("[ocsense] NX", a) + end + end + end +end