Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----Configuration----
- function config() --Don't modify this line
- newURL = "test" --Change this to the website URL, do not add rdnt://
- filePath = "." ..newURL --Default save path, not recommended to change, if you do change this, modify lines 33 and 34
- homePage = filePath.. "/index" --Page returned when requesting a unspecific path, not recommended to change.
- end --Don't modify this line
- ----Don't modify anything below----
- function stopBlinking( lolk )
- local x,y = term.getSize()
- if #lolk > y then
- oldLolk = lolk
- lolk = {}
- for i=#oldLolk-y,#oldLolk do
- lolk[#lolk+1] = oldLolk[i]
- end
- end
- return lolk
- end
- --How the installer works:
- --1) Looks for a file called DONTDELETE
- --2) Looks for a file called install, if found it skips to step 5
- --3) Scans /serverFiles and puts all the pages inside the host folder
- --4) Replaces the server with /serverSoftware
- --5) Reruns install/server
- function install( iPath )
- if fs.exists( iPath ) and fs.isDir( iPath ) and fs.exists( iPath.. "/DONTDELETE" ) then
- if fs.exists( iPath.. "/install" ) then
- shell.run( iPath.. "/install" )
- else
- if fs.exists( iPath.. "/serverFiles" ) and fs.isDir( iPath.. "/serverFiles" ) then
- local files = fs.list( iPath.. "/serverFiles" )
- for i=1,#files do
- if fs.exists( filePath.. "/" ..files[i] ) then
- messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> Failed to install " ..iPath.. "/" ..files[i].. " : Already Exists")
- else
- fs.copy( iPath.. "/serverFiles/" ..files[i] , filePath.. "/" ..files[i] )
- end
- end
- end
- if fs.exists( iPath.. "/serverSoftware" ) and fs.isDir( iPath.. "/serverSoftware" ) ~= true then
- if fs.isReadOnly( shell.getRunningProgram() ) then
- messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> Failed to install " ..iPath.. "/serverSoftware : Server is in ROM")
- else
- fs.delete(shell.getRunningProgram())
- fs.copy( iPath.. "/serverSoftware", shell.getRunningProgram() )
- shell.run( shell.getRunningProgram() )
- error()
- end
- end
- end
- return true
- else
- return false
- end
- end
- function newServer()
- messages = {}
- local x,y = term.getSize()
- local blocked = {}
- local sText = ""
- local downloads = 0
- config()
- function drawFooter()
- term.setCursorPos(1,1)
- term.setTextColour( colors.gray )
- term.clearLine()
- print("Console (rdnt://" ..newURL..")")
- for i=1,x do
- term.setCursorPos(i, 2)
- write("-")
- end
- end
- messages[1] = "Starting server..."
- rednet.broadcast( newURL )
- local id,message = rednet.receive( 0.5 )
- if message then
- messages[2] = "<WARNING> A server with this hostname has been found (" ..id.. ")"
- messages[3] = "<WARNING> type 'stop' to cancel."
- end
- if not fs.exists( filePath ) then
- fs.makeDir( filePath )
- fs.open( homePage, "w" ).close()
- end
- messages[#messages+1] = "Server started."
- while true do
- term.clear()
- term.setCursorPos(1,3)
- messages = stopBlinking( messages )
- term.setTextColour( colors.green )
- for i=1,#messages do
- print(messages[i])
- end
- print()
- print()
- drawFooter()
- for i=1,x do
- term.setCursorPos(i, y-1)
- write("-")
- end
- if messages[#messages] == ("[" ..textutils.formatTime( os.time() ).. "] <INFO> Stopped") then
- error()
- end
- term.setCursorPos(1,y)
- term.clearLine()
- term.setTextColour( colors.yellow )
- write("host> ")
- term.setTextColour( colors.white )
- write(sText)
- term.setCursorBlink( true )
- local event, key, message = os.pullEventRaw()
- if event == "rednet_message" then
- if blocked[tostring(key)] then else
- if message == newURL then
- downloads = downloads +1
- file = fs.open( homePage, "r" )
- rednet.send( key, file.readAll() )
- file.close()
- messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <" ..key.."> Index downloaded.")
- elseif fs.exists( "." ..message ) then
- downloads = downloads +1
- file = fs.open( "." ..message, "r" )
- if file then
- rednet.send( key, file.readAll() )
- file.close()
- end
- messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <" ..key.."> Subpage downloaded.")
- elseif message == "rednet.api.ping.searchengine" then
- rednet.send( key, newURL )
- messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <" ..key.."> Searched for server")
- else
- messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <" ..key.."> Junk received")
- end
- end
- elseif event == "terminate" then
- messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> Stopping...")
- messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> Stopped")
- elseif event == "char" then
- sText = sText..key
- elseif event == "key" then
- if key == keys.backspace then
- sText = string.sub(sText, 1, #sText-1)
- elseif key == keys.enter then
- local tWords = {}
- for match in string.gmatch( sText, "[^ \t]+" ) do
- table.insert( tWords, match )
- end
- local sCommand = tWords[1]
- if sCommand == "stop" then
- messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> Stopping...")
- messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> Stopped")
- elseif sCommand == "install" then
- if tWords[2] then
- messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> Installing " ..tWords[2])
- if install( tWords[2] ) then
- messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> " ..tWords[2].. " successfully installed!")
- else
- messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> " ..tWords[2].. " failed to install :c")
- end
- else
- messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> Missing 1 parameter(s)")
- end
- elseif sCommand == "help" or sCommand == "?" then
- messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> Commands: stop,install,help(?),stats")
- elseif sCommand == "stats" then
- messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> Downloads this session: " ..downloads)
- elseif sCommand == "time" then
- messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> <-- Over there")
- elseif sCommand == "block" then
- if tWords[2] then
- blocked[#tWords[2]] = true
- end
- elseif sCommand == "unblock" then
- if tWords[2] then
- blocked[#tWords[2]] = false
- end
- elseif sText ~= "" then
- messages[#messages+1] = ("[" ..textutils.formatTime( os.time() ).. "] <INFO> Unknown command.")
- end
- sText = ""
- end
- end
- term.setCursorBlink( false )
- end
- end
- for i,v in pairs( rs.getSides() ) do
- rednet.open( v )
- end
- newServer()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement