MtnMCG

sethmc ndisk node

Dec 20th, 2025 (edited)
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. -- store_node.lua
  2.  
  3. local modem = peripheral.find("modem") or error("No modem found")
  4. local drive = peripheral.find("drive") or error("No disk drive found")
  5.  
  6. -- ============================================
  7. -- SECURITY SETUP
  8. -- ============================================
  9.  
  10. local STORAGE_SECRET = nil
  11.  
  12. if not fs.exists(".ndisk_secret") then
  13. term.clear()
  14. term.setCursorPos(1, 1)
  15. print("NDISK NODE SETUP")
  16. print("================")
  17. print("No security secret found.")
  18. write("Enter shared secret for cluster: ")
  19. local secret = read("*")
  20.  
  21. local f = fs.open(".ndisk_secret", "w")
  22. f.write(secret)
  23. f.close()
  24. print("\nSecret saved.")
  25. sleep(1)
  26. end
  27.  
  28. local f = fs.open(".ndisk_secret", "r")
  29. STORAGE_SECRET = f.readAll()
  30. f.close()
  31.  
  32. -- ============================================
  33. -- NODE LOGIC
  34. -- ============================================
  35.  
  36. modem.open(5) -- Open channel 5 for NDisk communication
  37.  
  38. local function sendResponse(id, response)
  39. modem.transmit(5, 5, {id = id, response = response})
  40. end
  41.  
  42. local function handleRequest(sender, message)
  43. -- AUTHENTICATION CHECK
  44. if message.secret ~= STORAGE_SECRET then
  45. -- Silent drop for security (prevent scanning)
  46. return
  47. end
  48.  
  49. if message.type == "discovery" then
  50. modem.transmit(5, 5, {type = "discovery_response", id = os.getComputerID()})
  51. elseif message.targetNode == os.getComputerID() then
  52. -- PATH SANITIZATION (Defense in Depth)
  53. if message.path and (message.path:find("%.%.") or message.path:find(":")) then
  54. sendResponse(message.id, {success = false, error = "Invalid path security"})
  55. return
  56. end
  57.  
  58. if message.action == "write" then
  59. if not drive.isDiskPresent() then
  60. sendResponse(message.id, {success = false, error = "No disk present"})
  61. return
  62. end
  63. local path = fs.combine(drive.getMountPath(), message.path .. "_chunk_" .. message.chunk)
  64. local file = fs.open(path, "w")
  65. file.write(message.content)
  66. file.close()
  67. sendResponse(message.id, {success = true})
  68. elseif message.action == "read" then
  69. if not drive.isDiskPresent() then
  70. sendResponse(message.id, {success = false, error = "No disk present"})
  71. return
  72. end
  73. local path = fs.combine(drive.getMountPath(), message.path .. "_chunk_" .. message.chunk)
  74. if fs.exists(path) then
  75. local file = fs.open(path, "r")
  76. local content = file.readAll()
  77. file.close()
  78. sendResponse(message.id, {success = true, content = content})
  79. else
  80. sendResponse(message.id, {success = false, error = "File not found"})
  81. end
  82. elseif message.action == "delete" then
  83. if not drive.isDiskPresent() then
  84. sendResponse(message.id, {success = false, error = "No disk present"})
  85. return
  86. end
  87. local path = fs.combine(drive.getMountPath(), message.path .. "_chunk_" .. message.chunk)
  88. if fs.exists(path) then
  89. fs.delete(path)
  90. sendResponse(message.id, {success = true})
  91. else
  92. sendResponse(message.id, {success = false, error = "File not found"})
  93. end
  94. elseif message.action == "getSpace" then
  95. if not drive.isDiskPresent() then
  96. sendResponse(message.id, {success = false, error = "No disk present"})
  97. return
  98. end
  99. local freeSpace = fs.getFreeSpace(drive.getMountPath())
  100. sendResponse(message.id, {success = true, space = freeSpace})
  101. elseif message.action == "listFiles" then
  102. -- List all files on the disk (for orphan detection)
  103. if not drive.isDiskPresent() then
  104. sendResponse(message.id, {success = false, error = "No disk present"})
  105. return
  106. end
  107. local files = fs.list(drive.getMountPath())
  108. sendResponse(message.id, {success = true, files = files})
  109. elseif message.action == "deleteItem" then
  110. -- Delete a specific file or directory by name (for orphan cleanup)
  111. if not drive.isDiskPresent() then
  112. sendResponse(message.id, {success = false, error = "No disk present"})
  113. return
  114. end
  115. local path = fs.combine(drive.getMountPath(), message.itemName)
  116. if fs.exists(path) then
  117. fs.delete(path)
  118. sendResponse(message.id, {success = true})
  119. else
  120. sendResponse(message.id, {success = false, error = "Item not found"})
  121. end
  122. end
  123. end
  124. end
  125.  
  126. print("Storage node ready (SECURE). Waiting for requests...")
  127. print("Node ID: " .. os.getComputerID())
  128.  
  129. while true do
  130. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  131. if channel == 5 then
  132. handleRequest(side, message)
  133. end
  134. end
Advertisement
Add Comment
Please, Sign In to add comment