Advertisement
Rodin172

Server

May 29th, 2023 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.46 KB | None | 0 0
  1. serverID = os.getComputerID()
  2. modem = peripheral.wrap("back")
  3. fs.delete("TimerKey.lua")
  4. modem.open(2410)
  5.  
  6. term.clear()
  7. term.setCursorPos(1,1)
  8.  
  9.  
  10. function str2tbl (str)
  11.     file = fs.open("temp.lua","w")
  12.     file.write("return "..str)
  13.     file.close()
  14.     local data = dofile("temp.lua")
  15.     fs.delete("temp.lua")
  16.     return data
  17. end
  18.  
  19. function findItemID (file, item, position)
  20.     local data = dofile(file)
  21.     for i = 1, table.maxn(data) do
  22.         checkItem = str2tbl(data[i])
  23.         if tostring(checkItem[position]) == tostring(item) then
  24.             return i
  25.         end
  26.     end
  27.     return nil
  28. end
  29.  
  30. function returnTableItem (file, tbl, position)
  31.     local data = dofile(file)
  32.     checkTable = str2tbl(data[tbl])
  33.     if tostring(checkTable[position]) then
  34.         return checkTable
  35.     else
  36.         return nil
  37.     end
  38. end
  39.  
  40. function removeTableLine (file, position)
  41.     itemID = findItemID("Users.lua", Username, 1)
  42.     if itemID ~= nil then
  43.         local tbl = dofile(file)
  44.         table.remove(tbl, itemID)
  45.         data = 'return {\n"'..table.concat(tbl,'",\n"')..'"\n}'
  46.         fs.delete(file)
  47.         file = fs.open(file,"a")
  48.         file.write(data)
  49.         file.close()
  50.         return itemID
  51.     else
  52.         return nil
  53.     end
  54. end
  55.  
  56. function newTableLine (file, data)                                  -- Func 'newTableLine' expects the 'data' variable to be a string in the following format: "{ 'var1', 'var2', 'etc'}"
  57.     if fs.exists(file) then
  58.         local tbl = dofile(file)
  59.     else
  60.         tbl = {}
  61.     end
  62.     table.insert(tbl, (table.maxn(tbl) + 1), data)
  63.     data = 'return {\n"'..table.concat(tbl,'",\n"')..'"\n}'
  64.     fs.delete(file)
  65.     file = fs.open(file,"a")
  66.     file.write(data)
  67.     file.close()
  68.     return table.maxn(tbl)
  69. end
  70.  
  71. function refreshTimer (channel,senderID,message)
  72.     fileID = findItemID("TimerKey.lua", tostring(senderID), 1)
  73.     if fileID ~= nil then
  74.         savedID         = returnTableItem("TimerKey.lua", fileID, 1)
  75.         savedChannel    = returnTableItem("TimerKey.lua", fileID, 2)
  76.         savedKey        = returnTableItem("TimerKey.lua", fileID, 3)
  77.                
  78.         if senderID == savedID and channel == savedChannel and message[1] == savedKey then
  79.             os.cancelTimer(key)
  80.             removeTableLine("TimerKey.lua", fileID)
  81.             newKey          = os.startTimer(10)
  82.             secureChannel   = math.random(4096,65536)
  83.             modem.open(secureChannel)
  84.             modem.transmit(channel, secureChannel, newKey)
  85.             if findItemID("TimerKey.lua", channel, 2) == nil then
  86.                 modem.close(channel)
  87.             end
  88.             newTableLine("TimerKey.lua","{'"..senderID.."','"..secureChannel.."','"..newKey.."'}")
  89.                    
  90.         else
  91.             modem.close(channel)
  92.             removeTableLine("TimerKey.lua", fileID)
  93.             modem.transmit(2410, senderID, -1)
  94.             modem.transmit(2410, savedID, -1)
  95.             modem.transmit(2410, savedChannel, -1)
  96.             modem.transmit(2410, savedKey, -1)
  97.                    
  98.         end
  99.     end
  100. end
  101.  
  102. while true do
  103.  
  104.     event, var1, var2, var3, var4, var5 = os.pullEventRaw()
  105.    
  106.     if event == "terminate" then
  107.         print("Rebooting")
  108.         RebootKey = os.startTimer(3)
  109.         modem.transmit(2410,0,-1)
  110.    
  111.     elseif event == "modem_message" then
  112.         side = var1; channel = var2; senderID = var3; message = var4; distance = var5
  113.         if message == -1 then
  114.             secureChannel = math.random(4096,65536)
  115.             modem.open(secureChannel)
  116.             newKey = os.startTimer(10)
  117.             modem.transmit(2410, secureChannel, newKey)
  118.             newTableLine("TimerKey.lua","{'"..senderID.."','"..secureChannel.."','"..newKey.."'}")
  119.            
  120.         else
  121.             refreshTimer(channel,senderID,message)
  122.         end
  123.    
  124.     elseif event == "timer" then
  125.         key = var1
  126.         if key == RebootKey then
  127.             os.reboot()
  128.         end
  129.  
  130.     elseif event == "key" then
  131.         print(var1)
  132.         if var1 == 211 then
  133.             return
  134.         end
  135.        
  136.     end
  137. end
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement