Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Utility functions
- local function loadFile(filePath)
- if not fs.exists(filePath) then
- return nil
- end
- local file = fs.open(filePath, "r")
- local content = file.readAll()
- file.close()
- return content
- end
- local function saveFile(filePath, content)
- local file = fs.open(filePath, "w")
- file.write(content)
- file.close()
- end
- local function logEvent(event)
- local logFile = "/logs/server.log"
- local log = loadFile(logFile) or ""
- log = log .. os.date("%Y-%m-%d %H:%M:%S") .. " - " .. event .. "\n"
- saveFile(logFile, log)
- end
- -- Main server function
- local function startServer()
- local blockedIDs = {} -- List of blocked IDs
- local offlineMode = fs.exists("OFFLINE.TXT")
- rednet.open("top") -- Assuming the modem is on the top
- print("Webserver running...")
- while true do
- local senderID, message = rednet.receive()
- if offlineMode then
- local offlineContent = loadFile("offline.code")
- if offlineContent then
- rednet.send(senderID, offlineContent)
- else
- rednet.send(senderID, "Offline code not found.")
- end
- logEvent("Connection attempt while offline")
- elseif blockedIDs[senderID] then
- local blockContent = loadFile("block.code")
- if blockContent then
- rednet.send(senderID, blockContent)
- else
- rednet.send(senderID, "Blocked content not found.")
- end
- logEvent("Blocked ID attempted to connect: " .. senderID)
- else
- if message == "GET" then
- local webCode = loadFile("web.code")
- if webCode then
- rednet.send(senderID, webCode)
- else
- local missingContent = loadFile("missing.code")
- if missingContent then
- rednet.send(senderID, missingContent)
- else
- rednet.send(senderID, "Missing code not found.")
- end
- end
- logEvent("Request fulfilled for ID: " .. senderID)
- end
- end
- end
- end
- -- UI Functions
- local function editFile(fileName)
- local content = loadFile(fileName) or ""
- print("Current content of " .. fileName .. ":")
- print(content)
- print("\nEnter new content (end with a blank line):")
- local newContent = ""
- while true do
- local line = read()
- if line == "" then break end
- newContent = newContent .. line .. "\n"
- end
- saveFile(fileName, newContent)
- logEvent("Edited file: " .. fileName)
- end
- local function manageBlockedIDs()
- print("Current blocked IDs:")
- for id in pairs(blockedIDs) do
- print(id)
- end
- print("\n1. Add blocked ID")
- print("2. Remove blocked ID")
- local choice = tonumber(read())
- if choice == 1 then
- print("Enter ID to block:")
- local id = tonumber(read())
- blockedIDs[id] = true
- logEvent("Added blocked ID: " .. id)
- elseif choice == 2 then
- print("Enter ID to unblock:")
- local id = tonumber(read())
- blockedIDs[id] = nil
- logEvent("Removed blocked ID: " .. id)
- end
- end
- local function viewLogs()
- local logs = loadFile("/logs/server.log")
- print("Server Logs:")
- print(logs or "No logs available.")
- end
- local function clearLogs()
- saveFile("/logs/server.log", "")
- logEvent("Logs cleared")
- print("Logs cleared.")
- end
- local function backupLogs()
- local logs = loadFile("/logs/server.log")
- saveFile("/Backup/backup-logs.txt", logs or "")
- logEvent("Logs backed up")
- print("Logs backed up.")
- end
- -- Main UI loop
- local function ui()
- while true do
- print("Choose an option:")
- print("1. Edit Web content")
- print("2. Access and Security")
- print("3. Logs")
- print("4. Exit")
- local choice = tonumber(read())
- if choice == 1 then
- print("Which file to edit?")
- print("1. web.code")
- print("2. offline.code")
- print("3. missing.code")
- print("4. block.code")
- local fileChoice = tonumber(read())
- if fileChoice == 1 then
- editFile("web.code")
- elseif fileChoice == 2 then
- editFile("offline.code")
- elseif fileChoice == 3 then
- editFile("missing.code")
- elseif fileChoice == 4 then
- editFile("block.code")
- end
- elseif choice == 2 then
- manageBlockedIDs()
- elseif choice == 3 then
- print("1. View logs")
- print("2. Clear logs")
- print("3. Backup logs")
- local logChoice = tonumber(read())
- if logChoice == 1 then
- viewLogs()
- elseif logChoice == 2 then
- clearLogs()
- elseif logChoice == 3 then
- backupLogs()
- end
- elseif choice == 4 then
- break
- end
- end
- end
- -- Run UI and server
- parallel.waitForAny(ui, startServer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement