Advertisement
Guest User

main.lua

a guest
Feb 28th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.22 KB | None | 0 0
  1. -- Port by Fronun.
  2.  
  3. local GUI = require("GUI")
  4. local system = require("System")
  5. local component = require("Component")
  6. local event = require("Event")
  7. local server_password = "passw0rd"
  8. local version = "web-server mineWEB 1.1, powered by MineOS"
  9. local modem = component.modem
  10. local fs = require("Filesystem")
  11. local dirPage = "/web/"
  12. local computer = require("Computer")
  13.  
  14. local workspace = GUI.workspace()
  15.  
  16. workspace:addChild(GUI.panel(1, 1, workspace.width, workspace.height, 0x2D2D2D))
  17.  
  18. local layout = workspace:addChild(GUI.layout(1, 1, workspace.width, workspace.height, 1, 8))
  19.  
  20. local function addText(text)
  21.   layout:addChild(GUI.text(layout.width, layout.height, 0xE1E1E1, text))
  22. end
  23.  
  24. addText(version)
  25. addText("modem address: "..modem.address)
  26.  
  27. modem.open(80)
  28.  
  29.  workspace:draw()
  30. while true do
  31. -- e3 удаленный айпи, e6 команда, e7 страница, e8, дата для PUT, e9 пароль
  32. local _, _, remoteIP, _, _, command, page, data, pass = event.pull()
  33.   if command == "GET" then
  34.   addText(remoteIP.. " request file: " ..page)
  35.     local file = fs.read(dirPage..page)
  36.     if not file then
  37.     addText(remoteIP.. " redirect to 404")
  38.       notpage = fs.read(dirPage.. "404.onml")
  39.       modem.send(remoteIP, 80, notpage)
  40.     end
  41.     if file then
  42.       local cont = fs.read(dirPage..page)
  43.       modem.send(remoteIP, 80, cont)
  44.     workspace:draw()
  45.   end
  46.   end
  47.   if command == "PUT" then
  48.   addText(remoteIP.. " trying to send a file to the server. File: "..page)
  49.   if pass==server_password then
  50.     file = fs.write(dirPage..page, data)
  51.     modem.send(remoteIP, 80, "File_saved")
  52.       end
  53.       modem.send(remoteIP, 80, "invalid_password")
  54.       workspace:draw()
  55.   end
  56.   if command == "VERSION" then
  57.   addText(remoteIP.. " Request about version web-server")
  58.     modem.send(remoteIP, 80, version)
  59.     workspace:draw()
  60.   end
  61.   if command == "ping" then
  62.     addText(remoteIP.. " ping")
  63.     modem.send(remoteIP, 80, "pong")
  64.     workspace:draw()
  65.   end
  66.   if command == "reboot" then
  67.     addText(remoteIP.. " Trying reboot server")
  68.     if pass==server_password then
  69.       modem.send(remoteIP, 80, "server_rebooted")
  70.       modem.close(80)
  71.       computer.shutdown(true)
  72.       end
  73.       modem.send(remoteIP, 80, "invalid_password")
  74.       end
  75. end
  76.  
  77.  
  78. workspace:start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement