Advertisement
Guest User

WS.lua

a guest
Apr 18th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.40 KB | None | 0 0
  1. --OpenNet Server
  2. --Internet for OpenComputers
  3.  
  4. local component = require("component")
  5. local event = require("event")
  6. local keyboard = require("keyboard")
  7. local fs = require("filesystem")
  8.  
  9. --Config (DNS registration, webpages)
  10.  
  11. local dns_addr = "2578c0ec-94b2-4d52-8a31-65379978d0ad"
  12. local dns_name = "t-mobile.cz"
  13. local main_page = "index.onml"
  14.  
  15. -- Objects (various values needed)
  16.  
  17. local modem = component.modem
  18. local gpu = component.gpu
  19.  
  20. function def()
  21.   fldr = main_page
  22.   file = io.open(main_page, "r")
  23.   cont = file:read("*a")
  24.   file:close()
  25. end
  26.  
  27. function init()
  28.   modem.open(55)
  29.   modem.send(dns_addr, 55, "INIT " .. dns_name)
  30.   modem.close(55)
  31.   print("Reg on "..dns_addr)
  32. end
  33.  
  34. function respondToRequest(req)
  35.   if not modem.isOpen(80) then
  36.     modem.open(80)
  37.   end
  38.   print("User with network card address " .. req.remoteAddr .. " requested -"..fldr.."/")
  39.   modem.send(req.remoteAddr, 80, cont)
  40. end
  41.  
  42. init()
  43. running = true
  44. while running do
  45.  
  46.   modem.open(80)
  47.  
  48.   local _, _, from, _, _, request = event.pull("modem_message")
  49.  
  50. if string.match(request, "/") then
  51.  
  52.   url, fldr = string.match(request, "(.*)%/(.*)")
  53.   fldr = fldr..".onml"
  54.  
  55.   if fs.exists(fldr) then
  56.     file = io.open(fldr, "r")
  57.     cont = file:read("*a")
  58.     file:close()
  59.   else
  60.     cont = "Not found"
  61.   end
  62.  
  63.     respondToRequest({remoteAddr = from, request = request})
  64.  
  65. else
  66.  
  67.   def()
  68.   respondToRequest({remoteAddr = from, request = request})
  69.  
  70. end
  71.  
  72.  
  73. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement