RiseAboveHate

dns_server.lua

Jul 17th, 2025
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.48 KB | None | 0 0
  1. -- dns_server.lua
  2. -- שרת ראשי של ה־DNS, רץ על מחשב עם מודם
  3.  
  4. local modemSide = "back"
  5. rednet.open(modemSide)
  6.  
  7. -- קונפיגורציה של A ו‑B
  8. local A = 5
  9. local B = 29
  10.  
  11. -- אתחול מספור X
  12. local nextID = 2
  13.  
  14. -- טבלת DNS (hostname → IP)
  15. local dnsTable = {}
  16.  
  17. -- אתחול מחולל אקראיות
  18. math.randomseed(os.time())
  19.  
  20. print("DNS server listening…")
  21. while true do
  22.   -- מקבלים הודעה מסוג טבלה בפרוטוקול "DNS"
  23.   local senderID, msg = rednet.receive("DNS")
  24.   if type(msg) == "table" and msg.cmd then
  25.  
  26.     if msg.cmd == "requestIP" then
  27.       local host = msg.name
  28.       if not dnsTable[host] then
  29.         -- בוחרים Y רנדומלי בין 100 ל־250
  30.         local zone = math.random(100, 250)
  31.         -- בונים IP בפורמט 5.29.Y.X
  32.         local ip = string.format("%d.%d.%d.%d", A, B, zone, nextID)
  33.         nextID = nextID + 1
  34.         dnsTable[host] = ip
  35.         print("Registered", host, "→", ip)
  36.       end
  37.       -- שולחים בחזרה את ה‑IP (חדש או קיים)
  38.       rednet.send(senderID, { ip = dnsTable[host] }, "DNS")
  39.  
  40.     elseif msg.cmd == "resolve" then
  41.       -- מחזירים IP של hostname קיים (או nil אם לא נמצא)
  42.       rednet.send(senderID, { ip = dnsTable[msg.name] }, "DNS")
  43.  
  44.     elseif msg.cmd == "list" then
  45.       -- שולחים את כל הטבלה
  46.       rednet.send(senderID, { table = dnsTable }, "DNS")
  47.     end
  48.   end
  49. end
  50.  
Advertisement
Add Comment
Please, Sign In to add comment