anfbckdo

server.lua

Nov 12th, 2024 (edited)
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.85 KB | None | 0 0
  1. -- Server.lua
  2.  
  3. -- SPR Class (Special Request Processor)
  4. local SPR = {}
  5. SPR.__index = SPR
  6.  
  7. -- SPR Constructor
  8. function SPR:new(modem, serverID, drive)
  9.     local instance = setmetatable({}, SPR)
  10.     instance.modem = modem
  11.     instance.serverID = serverID
  12.     instance.drive = drive
  13.     instance.Blacklist = {}
  14.     return instance
  15. end
  16.  
  17. -- Method: Start Server
  18. function SPR:STRT()
  19.     self.modem.open()
  20.     self.modem.host("Server", "Server")
  21.     self.modem.broadcast("Server is online!")
  22.     print("Server started.")
  23. end
  24.  
  25. -- Method: Shutdown Server
  26. function SPR:SHUT()
  27.     self.modem.broadcast("Server powering down...")
  28.     self.modem.unhost("Server", "Server")
  29.     self.modem.close()
  30.     print("Server shut down.")
  31. end
  32.  
  33. -- Method: Add to Blacklist
  34. function SPR:BLST_ADD(computerIDs)
  35.     for _, id in ipairs(computerIDs) do
  36.         table.insert(self.Blacklist, id)
  37.         print("Added to Blacklist:", id)
  38.     end
  39. end
  40.  
  41. -- Method: Remove from Blacklist
  42. function SPR:BLST_RMV(computerIDs)
  43.     for _, id in ipairs(computerIDs) do
  44.         for i, existingID in ipairs(self.Blacklist) do
  45.             if existingID == id then
  46.                 table.remove(self.Blacklist, i)
  47.                 print("Removed from Blacklist:", id)
  48.                 break
  49.             end
  50.         end
  51.     end
  52. end
  53.  
  54. -- Method: Show Blacklist
  55. function SPR:BLST_SHO()
  56.     print("Blacklist:")
  57.     for _, id in ipairs(self.Blacklist) do
  58.         print(id)
  59.     end
  60. end
  61.  
  62. -- Method: Dump Globals
  63. function SPR:MDMP()
  64.     print("Dumping global variables:")
  65.     for variable, value in pairs(_G) do
  66.         print("Global key:", variable, "value:", value)
  67.     end
  68. end
  69.  
  70. -- Method: Dump Disk Data
  71. function SPR:DDMP()
  72.     if self.drive and self.drive.isDiskPresent() then
  73.         print("Drive mount path:", self.drive.getMountPath())
  74.     else
  75.         print("No disk present.")
  76.     end
  77. end
  78.  
  79. -- Method: Listen on Modem
  80. function SPR:LISN(time, echo)
  81.     if type(echo) ~= "boolean" then
  82.         error("echo must be a boolean", 0)
  83.     end
  84.  
  85.     if echo then
  86.         local startTime = os.clock()
  87.         print("Listening with echo for " .. time .. " seconds.")
  88.         while os.clock() - startTime < time do
  89.             local id, message = self.modem.receive(1)
  90.             if id then
  91.                 self.modem.send(id, "ECHO: " .. message)
  92.                 print("Echoed message to " .. id .. ": " .. message)
  93.             end
  94.         end
  95.     else
  96.         print("Listening without echo for " .. time .. " seconds.")
  97.         local id, message = self.modem.receive(time)
  98.         if id then
  99.             print(id .. " sent message: " .. message)
  100.         else
  101.             print("No messages received.")
  102.         end
  103.     end
  104. end
  105.  
  106. -- Server Class
  107. local Server = {}
  108. Server.__index = Server
  109.  
  110. -- Server Constructor
  111. function Server:new()
  112.     local modem = peripheral.find("modem")
  113.     if not modem then
  114.         error("Could not find modem peripheral.", 0)
  115.     end
  116.  
  117.     local drive = peripheral.find("drive") or nil
  118.     local serverID = os.getComputerID()
  119.     local modem = peripheral.find('modem') or error("could not find modem",0)
  120.     local instance = setmetatable({}, Server)
  121.     instance.keys = { "keyA", "keyB" }
  122.     instance.modem = modem
  123.     instance.drive = drive
  124.     instance.serverID = serverID
  125.     instance.spr = nil  -- Will hold the SPR instance
  126.     return instance
  127. end
  128.  
  129. -- Method: Decode Request
  130. function Server:DecodeRequest(message)
  131.     local parts = {}
  132.     for part in string.gmatch(message, "[^;]+") do
  133.         table.insert(parts, part:match("^%s*(.-)%s*$"))
  134.     end
  135.     return parts
  136. end
  137.  
  138. -- Method: Authorize Key
  139. function Server:authorize(key)
  140.     for _, validKey in ipairs(self.keys) do
  141.         if key == validKey then
  142.             return true
  143.         end
  144.     end
  145.     return false
  146. end
  147.  
  148. -- Method: Handle Command
  149. function Server:handleCommand(parts)
  150.     local KEY = parts[1]
  151.     local OPERATION = parts[2]
  152.     local DATA = parts[3]
  153.     local STORE_METHOD = parts[4]
  154.     local END = parts[5]  -- Not used in current implementation
  155.  
  156.     if not self:authorize(KEY) then
  157.         print("Unauthorized access attempt detected with key:", KEY)
  158.         return
  159.     end
  160.  
  161.     if not self.spr then
  162.         self.spr = SPR:new(self.modem, self.serverID, self.drive)
  163.     end
  164.  
  165.     -- Check if the command exists in SPR
  166.     if self.spr[OPERATION] then
  167.         -- Handle different command data
  168.         if OPERATION == "WLST_ADD" or OPERATION == "WLST_RMV" then
  169.             -- Assuming data is a comma-separated list of IDs
  170.             local computerIDs = {}
  171.             for id in string.gmatch(DATA, "[^,]+") do
  172.                 table.insert(computerIDs, id)
  173.             end
  174.             self.spr[OPERATION](self.spr, computerIDs)
  175.         elseif OPERATION == "LISN" then
  176.             -- Assuming data contains time and echo separated by comma
  177.             local time, echoStr = DATA:match("([^,]+),([^,]+)")
  178.             local timeNum = tonumber(time)
  179.             local echo = echoStr == "true"
  180.             if timeNum then
  181.                 self.spr:LISN(timeNum, echo)
  182.             else
  183.                 print("Invalid LISN parameters.")
  184.             end
  185.         else
  186.             self.spr[OPERATION](self.spr)
  187.         end
  188.     else
  189.         print("Invalid command received:", OPERATION)
  190.     end
  191. end
  192.  
  193. -- Main Program Execution
  194. local function main()
  195.     -- Initialize Server
  196.     local server = Server:new()
  197.     print("Server initialized with ID:", server.serverID)
  198.  
  199.     -- Starting the server manually (optional)
  200.     server.spr = SPR:new(server.modem, server.serverID, server.drive)
  201.     server.spr:STRT()
  202.  
  203.  
  204.     while true do
  205.         print("Waiting for messages...")
  206.  
  207.         local id, message = modem.receive()
  208.  
  209.         local parts = server:DecodeRequest(message)
  210.        
  211.  
  212.         server:handleCommand(parts)
  213.     end
  214. end
  215.  
  216. -- Run the main function
  217. main()
Advertisement
Add Comment
Please, Sign In to add comment