Advertisement
DOGGYWOOF

Untitled

Sep 16th, 2024 (edited)
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. -- Utility functions
  2. local function loadFile(filePath)
  3. if not fs.exists(filePath) then
  4. return nil
  5. end
  6. local file = fs.open(filePath, "r")
  7. local content = file.readAll()
  8. file.close()
  9. return content
  10. end
  11.  
  12. local function saveFile(filePath, content)
  13. local file = fs.open(filePath, "w")
  14. file.write(content)
  15. file.close()
  16. end
  17.  
  18. local function logEvent(event)
  19. local logFile = "/logs/server.log"
  20. local log = loadFile(logFile) or ""
  21. log = log .. os.date("%Y-%m-%d %H:%M:%S") .. " - " .. event .. "\n"
  22. saveFile(logFile, log)
  23. end
  24.  
  25. -- Main server function
  26. local function startServer()
  27. local blockedIDs = {} -- List of blocked IDs
  28. local offlineMode = fs.exists("OFFLINE.TXT")
  29.  
  30. rednet.open("top") -- Assuming the modem is on the top
  31. print("Webserver running...")
  32.  
  33. while true do
  34. local senderID, message = rednet.receive()
  35. if offlineMode then
  36. local offlineContent = loadFile("offline.code")
  37. if offlineContent then
  38. rednet.send(senderID, offlineContent)
  39. else
  40. rednet.send(senderID, "Offline code not found.")
  41. end
  42. logEvent("Connection attempt while offline")
  43. elseif blockedIDs[senderID] then
  44. local blockContent = loadFile("block.code")
  45. if blockContent then
  46. rednet.send(senderID, blockContent)
  47. else
  48. rednet.send(senderID, "Blocked content not found.")
  49. end
  50. logEvent("Blocked ID attempted to connect: " .. senderID)
  51. else
  52. if message == "GET" then
  53. local webCode = loadFile("web.code")
  54. if webCode then
  55. rednet.send(senderID, webCode)
  56. else
  57. local missingContent = loadFile("missing.code")
  58. if missingContent then
  59. rednet.send(senderID, missingContent)
  60. else
  61. rednet.send(senderID, "Missing code not found.")
  62. end
  63. end
  64. logEvent("Request fulfilled for ID: " .. senderID)
  65. end
  66. end
  67. end
  68. end
  69.  
  70. -- UI Functions
  71. local function editFile(fileName)
  72. local content = loadFile(fileName) or ""
  73. print("Current content of " .. fileName .. ":")
  74. print(content)
  75. print("\nEnter new content (end with a blank line):")
  76. local newContent = ""
  77. while true do
  78. local line = read()
  79. if line == "" then break end
  80. newContent = newContent .. line .. "\n"
  81. end
  82. saveFile(fileName, newContent)
  83. logEvent("Edited file: " .. fileName)
  84. end
  85.  
  86. local function manageBlockedIDs()
  87. print("Current blocked IDs:")
  88. for id in pairs(blockedIDs) do
  89. print(id)
  90. end
  91. print("\n1. Add blocked ID")
  92. print("2. Remove blocked ID")
  93. local choice = tonumber(read())
  94. if choice == 1 then
  95. print("Enter ID to block:")
  96. local id = tonumber(read())
  97. blockedIDs[id] = true
  98. logEvent("Added blocked ID: " .. id)
  99. elseif choice == 2 then
  100. print("Enter ID to unblock:")
  101. local id = tonumber(read())
  102. blockedIDs[id] = nil
  103. logEvent("Removed blocked ID: " .. id)
  104. end
  105. end
  106.  
  107. local function viewLogs()
  108. local logs = loadFile("/logs/server.log")
  109. print("Server Logs:")
  110. print(logs or "No logs available.")
  111. end
  112.  
  113. local function clearLogs()
  114. saveFile("/logs/server.log", "")
  115. logEvent("Logs cleared")
  116. print("Logs cleared.")
  117. end
  118.  
  119. local function backupLogs()
  120. local logs = loadFile("/logs/server.log")
  121. saveFile("/Backup/backup-logs.txt", logs or "")
  122. logEvent("Logs backed up")
  123. print("Logs backed up.")
  124. end
  125.  
  126. -- Main UI loop
  127. local function ui()
  128. while true do
  129. print("Choose an option:")
  130. print("1. Edit Web content")
  131. print("2. Access and Security")
  132. print("3. Logs")
  133. print("4. Exit")
  134.  
  135. local choice = tonumber(read())
  136.  
  137. if choice == 1 then
  138. print("Which file to edit?")
  139. print("1. web.code")
  140. print("2. offline.code")
  141. print("3. missing.code")
  142. print("4. block.code")
  143. local fileChoice = tonumber(read())
  144. if fileChoice == 1 then
  145. editFile("web.code")
  146. elseif fileChoice == 2 then
  147. editFile("offline.code")
  148. elseif fileChoice == 3 then
  149. editFile("missing.code")
  150. elseif fileChoice == 4 then
  151. editFile("block.code")
  152. end
  153. elseif choice == 2 then
  154. manageBlockedIDs()
  155. elseif choice == 3 then
  156. print("1. View logs")
  157. print("2. Clear logs")
  158. print("3. Backup logs")
  159. local logChoice = tonumber(read())
  160. if logChoice == 1 then
  161. viewLogs()
  162. elseif logChoice == 2 then
  163. clearLogs()
  164. elseif logChoice == 3 then
  165. backupLogs()
  166. end
  167. elseif choice == 4 then
  168. break
  169. end
  170. end
  171. end
  172.  
  173. -- Run UI and server
  174. parallel.waitForAny(ui, startServer)
  175.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement