Advertisement
DOGGYWOOF

Untitled

Feb 14th, 2025
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. -- Network Daemon Service for Doggy OS with UI and status tracking
  2. -- Save this as /disk/daemons/network_daemon.lua
  3.  
  4. -- Function to detect and open the rednet modem if available
  5. local function open_rednet_modem()
  6. local sides = { "left", "right", "top", "bottom", "front", "back" }
  7. for _, side in ipairs(sides) do
  8. if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  9. rednet.open(side)
  10. print("Rednet modem detected and opened on " .. side)
  11. return true
  12. end
  13. end
  14. print("No rednet modem found!")
  15. return false
  16. end
  17.  
  18. -- Function to check if a device is authorized
  19. local function is_device_authorized(sender_id)
  20. local file_path = "/disk/os/daemon/systemIDs/" .. sender_id .. ".txt"
  21. local file = fs.open(file_path, "r")
  22. if file then
  23. -- Device is authorized if the file exists
  24. file.close()
  25. return true
  26. else
  27. -- Device is not authorized if no file is found
  28. return false
  29. end
  30. end
  31.  
  32. -- Function to load network services from the configuration file
  33. local function load_network_services()
  34. local services = {}
  35.  
  36. local file_path = "/disk/os/daemon/networkservices.sys"
  37. if fs.exists(file_path) then
  38. local file = fs.open(file_path, "r")
  39. -- Read the entire file content
  40. local content = file.readAll()
  41. file.close()
  42.  
  43. -- Attempt to load the content as a Lua table
  44. local func, err = load("return " .. content)
  45. if func then
  46. services = func() -- Execute the function to return the table
  47. else
  48. print("Error loading network services: " .. err)
  49. end
  50. else
  51. print("Network services configuration file not found.")
  52. end
  53.  
  54. return services
  55. end
  56.  
  57. -- UI to show status of the Doggy OS Network Daemon
  58. local function display_status_ui(status)
  59. term.clear()
  60. term.setCursorPos(1, 1)
  61. print("Doggy OS Network Service Daemon - Status")
  62.  
  63. print("===================================")
  64. print("Daemon Status: " .. (status.running and "Online" or "Offline"))
  65. print("Commands Received: " .. status.commands_received)
  66. print("Authorized Devices: " .. status.authorized_devices)
  67. print("Unauthorized Devices: " .. status.unauthorized_devices)
  68. print("===================================")
  69. print("Press 'q' to quit or 'r' to refresh.")
  70. print("===================================")
  71. end
  72.  
  73. -- Function to execute a command or run a script based on the loaded services
  74. local function execute_command(command, sender_id, status)
  75. local services = load_network_services()
  76. local action = services[command]
  77.  
  78. if action then
  79. if action:match("^/") then
  80. -- If the action is a file path, run the script
  81. print("Running script: " .. action)
  82. os.run(action)
  83. elseif action == "status_response" then
  84. -- If the action is a predefined response, send the status
  85. rednet.send(sender_id, "Network Daemon is online")
  86. elseif action == "pong_response" then
  87. -- If the action is a predefined response, send a pong reply
  88. rednet.send(sender_id, "Pong")
  89. else
  90. print("Unknown action: " .. action)
  91. end
  92. status.commands_received = status.commands_received + 1
  93. else
  94. print("Unknown command: " .. command)
  95. rednet.send(sender_id, "Unknown command")
  96. end
  97. end
  98.  
  99. -- Function to handle network activities and commands
  100. local function handle_network_activity(status)
  101. while true do
  102. local sender_id, message = rednet.receive()
  103.  
  104. -- Only proceed if the sender is authorized
  105. if is_device_authorized(sender_id) then
  106. status.authorized_devices = status.authorized_devices + 1
  107. -- Execute the command if it's valid
  108. execute_command(message, sender_id, status)
  109. else
  110. status.unauthorized_devices = status.unauthorized_devices + 1
  111. -- Unauthorized device attempt
  112. print("Unauthorized device attempt: " .. sender_id)
  113. rednet.send(sender_id, "Unauthorized access")
  114. end
  115. end
  116. end
  117.  
  118. -- Start the network daemon
  119. local function start_daemon()
  120. local status = {
  121. running = false,
  122. commands_received = 0,
  123. authorized_devices = 0,
  124. unauthorized_devices = 0
  125. }
  126.  
  127. if open_rednet_modem() then
  128. status.running = true
  129. print("Network Daemon started. Listening for network activity...")
  130. handle_network_activity(status)
  131. else
  132. status.running = false
  133. print("Unable to start Network Daemon: No rednet modem detected.")
  134. end
  135.  
  136. return status
  137. end
  138.  
  139. -- Main program to show the UI
  140. local function main()
  141. local status = start_daemon()
  142.  
  143. -- Periodically show the status UI
  144. while true do
  145. display_status_ui(status)
  146.  
  147. local event, key = os.pullEvent("key")
  148.  
  149. if key == keys.q then
  150. print("Exiting the status UI...")
  151. break
  152. elseif key == keys.r then
  153. -- Refresh the UI if 'r' is pressed
  154. display_status_ui(status)
  155. end
  156. end
  157. end
  158.  
  159. -- Run the program
  160. main()
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement