Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Network Daemon Service for Doggy OS with UI and status tracking
- -- Save this as /disk/daemons/network_daemon.lua
- -- Function to detect and open the rednet modem if available
- local function open_rednet_modem()
- local sides = { "left", "right", "top", "bottom", "front", "back" }
- for _, side in ipairs(sides) do
- if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
- rednet.open(side)
- print("Rednet modem detected and opened on " .. side)
- return true
- end
- end
- print("No rednet modem found!")
- return false
- end
- -- Function to check if a device is authorized
- local function is_device_authorized(sender_id)
- local file_path = "/disk/os/daemon/systemIDs/" .. sender_id .. ".txt"
- local file = fs.open(file_path, "r")
- if file then
- -- Device is authorized if the file exists
- file.close()
- return true
- else
- -- Device is not authorized if no file is found
- return false
- end
- end
- -- Function to load network services from the configuration file
- local function load_network_services()
- local services = {}
- local file_path = "/disk/os/daemon/networkservices.sys"
- if fs.exists(file_path) then
- local file = fs.open(file_path, "r")
- -- Read the entire file content
- local content = file.readAll()
- file.close()
- -- Attempt to load the content as a Lua table
- local func, err = load("return " .. content)
- if func then
- services = func() -- Execute the function to return the table
- else
- print("Error loading network services: " .. err)
- end
- else
- print("Network services configuration file not found.")
- end
- return services
- end
- -- UI to show status of the Doggy OS Network Daemon
- local function display_status_ui(status)
- term.clear()
- term.setCursorPos(1, 1)
- print("Doggy OS Network Service Daemon - Status")
- print("===================================")
- print("Daemon Status: " .. (status.running and "Online" or "Offline"))
- print("Commands Received: " .. status.commands_received)
- print("Authorized Devices: " .. status.authorized_devices)
- print("Unauthorized Devices: " .. status.unauthorized_devices)
- print("===================================")
- print("Press 'q' to quit or 'r' to refresh.")
- print("===================================")
- end
- -- Function to execute a command or run a script based on the loaded services
- local function execute_command(command, sender_id, status)
- local services = load_network_services()
- local action = services[command]
- if action then
- if action:match("^/") then
- -- If the action is a file path, run the script
- print("Running script: " .. action)
- os.run(action)
- elseif action == "status_response" then
- -- If the action is a predefined response, send the status
- rednet.send(sender_id, "Network Daemon is online")
- elseif action == "pong_response" then
- -- If the action is a predefined response, send a pong reply
- rednet.send(sender_id, "Pong")
- else
- print("Unknown action: " .. action)
- end
- status.commands_received = status.commands_received + 1
- else
- print("Unknown command: " .. command)
- rednet.send(sender_id, "Unknown command")
- end
- end
- -- Function to handle network activities and commands
- local function handle_network_activity(status)
- while true do
- local sender_id, message = rednet.receive()
- -- Only proceed if the sender is authorized
- if is_device_authorized(sender_id) then
- status.authorized_devices = status.authorized_devices + 1
- -- Execute the command if it's valid
- execute_command(message, sender_id, status)
- else
- status.unauthorized_devices = status.unauthorized_devices + 1
- -- Unauthorized device attempt
- print("Unauthorized device attempt: " .. sender_id)
- rednet.send(sender_id, "Unauthorized access")
- end
- end
- end
- -- Start the network daemon
- local function start_daemon()
- local status = {
- running = false,
- commands_received = 0,
- authorized_devices = 0,
- unauthorized_devices = 0
- }
- if open_rednet_modem() then
- status.running = true
- print("Network Daemon started. Listening for network activity...")
- handle_network_activity(status)
- else
- status.running = false
- print("Unable to start Network Daemon: No rednet modem detected.")
- end
- return status
- end
- -- Main program to show the UI
- local function main()
- local status = start_daemon()
- -- Periodically show the status UI
- while true do
- display_status_ui(status)
- local event, key = os.pullEvent("key")
- if key == keys.q then
- print("Exiting the status UI...")
- break
- elseif key == keys.r then
- -- Refresh the UI if 'r' is pressed
- display_status_ui(status)
- end
- end
- end
- -- Run the program
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement