Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local timeout = 10
- local apis = {}
- local side = "top"
- local version = 1.0
- local bootFile = nil
- local bootFiles = {}
- local server, message, distance = nil
- local verbose = false
- local options_bootServer, options_default = nil
- rednet.open(side)
- function loadAPIs()
- if #apis == 0 then
- print("No APIs need to be loaded.")
- return
- end
- local apiList = fs.open("apiList", "w")
- print("Loading APIs...")
- for index, value in ipairs(apis) do
- io.write("Loading API: "..value.."...")
- os.sleep(fs.getSize(fs.combine("apis/", value)) / 500 + 0.5)
- io.write(tostring(os.loadAPI(fs.combine("apis/", value))).."\n")
- apiList.writeLine(value)
- os.sleep(0.5)
- end
- apiList.close()
- end
- function doBoot()
- rednet.send(server, "bootrequest:"..bootFile)
- while true do
- server, message, distance = rednet.receive(timeout)
- if verbose then
- print("DEBUG:"..server..":"..message)
- end
- if string.sub(message, 1, 5) == "boot:" then -- boot file code
- print("Boot file recieved!")
- local bootFile = string.sub(message, 6)
- local bootFileHandle = fs.open("shell", "w")
- bootFileHandle.write(bootFile)
- bootFileHandle.close()
- loadAPIs()
- print("Running shell...")
- os.sleep(2)
- rednet.close(side)
- term.clear()
- term.setCursorPos(1,1)
- os.sleep(1)
- shell.run("shell")
- return
- end
- if string.sub(message, 1,4) == "api:" then
- local apiName = string.sub(message,5,string.find(message,":",5)-1)
- local apiContent = string.sub(message,string.find(message,":",5)+1)
- print("Custom API recieved: "..apiName)
- table.insert(apis, apiName)
- if not fs.exists("apis") then
- fs.makeDir("apis")
- end
- local apiFileHandle = fs.open(fs.combine("apis/",apiName), "w")
- apiFileHandle.write(apiContent)
- apiFileHandle.close()
- end
- if string.sub(message, 1,8) == "program:" then
- local programName = string.sub(message,9,string.find(message,":",9)-1)
- local programContent = string.sub(message,string.find(message,":",9)+1)
- print("Program recieved: "..programName)
- local programFileHandle = fs.open(programName, "w")
- programFileHandle.write(programContent)
- programFileHandle.close()
- end
- end
- end
- function lastBootFiles()
- os.sleep(2)
- local apiFile = fs.open("apiList", "r")
- local apiLine = nil
- while true do
- apiLine = apiFile.readLine()
- if apiLine == nil then
- break
- else
- print("Found API: "..apiLine)
- table.insert(apis, apiLine)
- end
- end
- apiFile.close()
- loadAPIs()
- print("Running shell...")
- os.sleep(2)
- rednet.close(side)
- term.clear()
- term.setCursorPos(1,1)
- os.sleep(1)
- shell.run("shell")
- assert(1==2)
- end
- print("Network Boot Client V"..tostring(version))
- print("Options:")
- print("1. Default boot")
- print("2. Select a boot file")
- print("3. Use last boot file/APIs")
- print("4. Options")
- print("5. Shutdown")
- if fs.exists("bootClient.options") then
- local optionsHandle = fs.open("bootClient.options", "r")
- options_bootServer = tonumber(optionsHandle.readLine())
- options_default = optionsHandle.readLine()
- else
- optionsHandle = fs.open("bootClient.options", "w")
- optionsHandle.writeLine("-1")
- optionsHandle.writeLine("default")
- optionsHandle.close()
- end
- while true do
- event, key = os.pullEvent("key")
- if key == 2 then
- bootFile = options_default
- break
- elseif key == 3 then
- --[[
- rednet.broadcast("list")
- while true do
- server, message, distance = rednet.receive(timeout)
- if message == nil then
- print("Could not contact boot server.")
- os.sleep(1)
- os.shutdown()
- elseif string.sub(message,1,5) == "list:" then
- bootFiles = {}
- bootFiles = textutils.unserialize(string.sub(message,6))
- break
- end
- end
- print("BOOT FILES:")
- for index, value in ipairs(bootFiles) do
- print(index..". "..value)
- end
- ]]--
- print(" ")
- print("Type in a boot file: ")
- bootFile = io.read()
- break
- elseif key == 4 then
- print("Loading files...")
- lastBootFiles()
- elseif key == 5 then
- optionsHandle = fs.open("bootClient.options", "w")
- print("Configure this client:")
- print("Boot server (Set to -1 to automatically find a boot server on startup):")
- local options_bootServer = io.read()
- print("Default file (This is the file to load when you select option 1 in the menu):")
- local options_default = io.read()
- optionsHandle.writeLine(options_bootServer)
- optionsHandle.writeLine(options_default)
- optionsHandle.close()
- print("Configured. Rebooting...")
- os.sleep(2)
- os.reboot()
- elseif key == 6 then
- print("Shutting down...")
- os.sleep(2)
- os.shutdown()
- end
- end
- if options_bootServer < 0 then
- rednet.broadcast("authrequired")
- else
- rednet.send(options_bootServer, "authrequired")
- end
- server, message, distance = rednet.receive(timeout)
- if server ~= nil then
- print("Boot server found. ID:"..server)
- else
- print("CRITICAL ERROR: A boot server could be found!")
- os.sleep(2)
- os.shutdown()
- end
- if message == "authReply:true" then
- print("The boot server requires a password to boot.")
- print("Type it in now:")
- local password = io.read()
- rednet.send(server, "authenticate:"..password)
- server, message, distance = rednet.receive(timeout)
- if message == "authReply:true" then
- print("Password correct!")
- doBoot()
- else
- print("Incorrect password.")
- print("Shutting down...")
- os.sleep(2)
- os.shutdown()
- end
- else
- print("The boot server does not require a password to boot.")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement