pepeknamornik

ServerDNS

Feb 22nd, 2022 (edited)
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.27 KB | None | 0 0
  1. verze = "22.0222.R3"
  2. logs = false
  3. term.clear()
  4. term.setCursorPos(1,1)
  5. local idPC = os.getComputerID()
  6. local ADmode = false
  7. local IsAdmin = false
  8.  
  9. if fs.exists("/C/system/server/rules/AD/") and fs.exists("/C/service/AD.lua") then
  10. ADmode = true
  11. end
  12.  
  13. if fs.exists("/C/system/.core/defmodem") then
  14.         local data = fs.open ("/C/system/.core/defmodem","r")
  15.         modemPos = data.readLine()
  16.         data.close()
  17.         if not modemPos then
  18.         modemPos = "offline"
  19.         end
  20.     else
  21.         modemPos = "offline"
  22.     end
  23.    
  24. if not (modemPos == "offline") then
  25. modem = peripheral.wrap(modemPos)
  26. modem.open(idPC)
  27. print("DNS server, ID serveru: "..idPC)
  28. if ADmode then print"AD is active!" end
  29. else
  30. term.setTextColor(colors.red)
  31. print("Error, you must configure the modem first")
  32. modem.open("Error")
  33. end
  34.  
  35. function logy(Zlog)
  36.     cas = (textutils.formatTime(os.time(), true))
  37.     local l = fs.open("/C/system/server/rules/AD/log","a")
  38.     l.writeLine(cas..": "..Zlog)
  39.     l.close()
  40. end
  41.  
  42. function bar(styl, text)
  43.     if (styl == "OK") then                  -- Uspech
  44.         term.setTextColor(colors.green)
  45.     elseif (styl == "FAIL") then            -- Neuspesne
  46.         term.setTextColor(colors.red)
  47.     elseif (styl == "INFO") then            -- Informativni
  48.         term.setTextColor(colors.lightBlue)
  49.     elseif (styl == "def") then             -- defaultni
  50.         term.setTextColor(colors.white)
  51.     elseif (styl == "PRO") then             -- Progress
  52.         term.setTextColor(colors.yellow)
  53.     else
  54.         styl = "def"
  55.         term.setTextColor(colors.white)
  56.     end
  57.    
  58.     if not (styl == "def") then
  59.         zprava = ("["..textutils.formatTime(os.time(), true).."] ["..styl.."] "..text)
  60.         else
  61.         zprava = text
  62.     end
  63.    
  64.     if logs then
  65.     logy(zprava)
  66.     end
  67.     print (zprava)
  68. end
  69.  
  70. local function defaultV()
  71.      idComp = nil
  72.      d2p = ""
  73.      domain2 = ""
  74.      domain3 = ""
  75.      SrvID = nil
  76.      
  77.      bar("OK", "Default values set.")
  78. end
  79.  
  80. function prijmout ()
  81.     local _, side, freq, rfreq, message = os.pullEvent('modem_message')
  82.  
  83.     if (rfreq == 53) then
  84.         bar("INFO", "Data processing request.")
  85.         idComp = message
  86.         bar("PRO", "Computer ID: "..idComp)
  87.     end
  88.    
  89.      if (rfreq == 54) then
  90.         vyhledat(message)  
  91.     end
  92.    
  93.     prijmout()
  94. end
  95.  
  96. function vyhledat(domain)
  97.         bar("PRO", "I'm looking for a domain: "..domain)
  98.        
  99.             local q = #domain
  100.                 repeat
  101.                 q = q-1
  102.                 hledej = string.sub(domain,q,q);
  103.                 if (hledej==".") then
  104.                 domain2 = string.sub(domain,0,q-1);
  105.                 domain = string.sub(domain,q+1,#domain)
  106.                
  107.                 q = 0
  108.                 end
  109.                 until q == 0
  110. if domain2 ~= "" then              
  111.              q = #domain2
  112.                 repeat
  113.                 q = q-1
  114.                 hledej = string.sub(domain2,q,q);
  115.                 if (hledej==".") then
  116.                 domain3 = string.sub(domain2,0,q-1);
  117.                 domain2 = string.sub(domain2,q+1,#domain2)
  118.                
  119.                 q = 0
  120.                 end
  121.                 until q == 0
  122. end            
  123.  
  124.     local d1p = ("/C/system/server/rules/DNS/List/"..domain.."/"..domain..".A")
  125.        
  126.         if fs.exists ("/C/system/server/rules/DNS/List/"..domain.."/") then
  127.            
  128.             if fs.exists (d1p) then
  129.                 local data= fs.open(d1p, "r")
  130.                 SrvID = data.readLine()
  131.                 data.close()
  132.             else
  133.                 SrvID = os.getComputerID()
  134.             end
  135.            
  136.             bar("INFO", "1st order domain: "..domain.." ID: "..SrvID)
  137.                 local d2p = ("/C/system/server/rules/DNS/List/"..domain.."/"..domain2.."/"..domain2..".A")
  138.                                  
  139.                             if domain2 ~= "" then    
  140.                                  if fs.exists (d2p) then
  141.                                     local data= fs.open(d2p, "r")
  142.                                     SrvID = data.readLine()
  143.                                     data.close()
  144.                                    
  145.                                     bar("INFO", "2nd order domain:  "..domain2.." ID: "..SrvID)
  146.                                    
  147.                                     local d3p = ("/C/system/server/rules/DNS/List/"..domain.."/"..domain2.."/"..domain3.."/"..domain3..".A")
  148.                                    
  149.                                     if domain3 ~= "" then    
  150.                                          if fs.exists (d3p) then
  151.                                             local data= fs.open(d3p, "r")
  152.                                             SrvID = data.readLine()
  153.                                             data.close()
  154.                                             bar("INFO", "3rd order domain:  "..domain3.." ID: "..SrvID)
  155.                                         else
  156.                                             SrvID = os.getComputerID()
  157.                                         end
  158.                                     end
  159.                                 else SrvID = os.getComputerID()
  160.                                 end
  161.                             end
  162.         end
  163.        
  164.         if (SrvID == nil) then bar("FAIL", "Domain not found!") end
  165.         modem.transmit(tonumber(idComp),55,SrvID)
  166.         defaultV()
  167. end
  168.  
  169.  
  170. defaultV()
  171. prijmout()
Add Comment
Please, Sign In to add comment