Advertisement
Guest User

server

a guest
Mar 2nd, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.53 KB | None | 0 0
  1. -- |------------------------------------|
  2. -- |MADE BY SAPPHIRE                    |
  3. -- |WHEN USEING THIS SERVER, YOU AGREE  |
  4. -- |NOT TO MODIFY THE CODE IN ANY WAY   |
  5. -- |YOU ALSO AGREE THAT A VIRUS WEBSITE |
  6. -- |WILL BE SHUTDOWN!                   |
  7. -- |------------------------------------|
  8.  
  9. local version = "12.6"
  10. local error_404 = "term.setBackgroundColor(colors.white) term.clear() term.setCursorPos(1,1) term.setTextColor(colors.lightGray) print('404') print('Sorry, the page was not found on the server!')"
  11. local error_400 = "term.setBackgroundColor(colors.white) term.clear() term.setCursorPos(1,1) term.setTextColor(colors.lightGray) print('400') print('Send bad reqest')"
  12. local error_403 = "term.setBackgroundColor(colors.white) term.clear() term.setCursorPos(1,1) term.setTextColor(colors.lightGray) print('403') print('Forbidden page request!')"
  13. local size = { term.getSize() }
  14. local sides = {
  15.     "top",
  16.     "bottom",
  17.     "back",
  18.     "left",
  19.     "right"
  20. }
  21. if not fs.exists("www") then
  22.     fs.makeDir("www")
  23. else
  24.     if not fs.isDir("www") then
  25.         fs.delete("www")
  26.         fs.makeDir("www")
  27.     end
  28. end
  29. if not fs.exists("domain_name") then
  30.     term.setBackgroundColor(colors.white)
  31.     term.clear()
  32.     paintutils.drawFilledBox(1,1,size[1],4,colors.gray)
  33.     term.setCursorPos(1,1)
  34.     term.setTextColor(colors.lightGray)
  35.     term.setBackgroundColor(colors.gray)
  36.     print("Setup server domain name")
  37.     term.setBackgroundColor(colors.white)
  38.     term.setCursorPos(1,6)
  39.     print("(adds .cc) Enter domain name: ")
  40.     local dmn = read()
  41.     local dmn = (dmn..".cc")
  42.     local domainf = fs.open("domain_name","w")
  43.     domainf.write(dmn)
  44.     domainf.close()
  45.     term.clear()
  46.     paintutils.drawFilledBox(1,1,size[1],4,colors.gray)
  47.     term.setCursorPos(1,1)
  48.     print("Setup server owner")
  49.     term.setCursorPos(1,6)
  50.     term.setBackgroundColor(colors.white)
  51.     write("Enter server owner: ")
  52.     local owner = read()
  53.     local own = fs.open("server_owner","w")
  54.     own.write(owner)
  55.     own.close()
  56. end
  57. for i = 1, #sides do
  58.     if peripheral.isPresent(sides[i]) then
  59.         if peripheral.getType(sides[i]) == "modem" then
  60.             local modem = peripheral.find("modem")
  61.             rednet.open(sides[i])
  62.         end
  63.     end
  64. end
  65. local domainf = fs.open("domain_name","r")
  66. local domain = domainf.readAll()
  67. domainf.close()
  68. local function webServerMain()
  69.     local evnt = { rednet.receive() }
  70.     local str2 = ("www/"..tostring(evnt[2]))
  71.     local str3 = tostring(evnt[3])
  72.     if str2 and str3 then
  73.         local dt = string.sub(str2,1,1)
  74.         if dt == "." then
  75.             rednet.send(evnt[1],error_403)
  76.         else
  77.             if #str2 < 50 then
  78.                 if evnt[3] == domain then
  79.                     if fs.exists("advsettings") then
  80.                         local advf = fs.open("advsettings","r")
  81.                         local adv = advf.readAll()
  82.                         advf.close()
  83.                         if fs.exists(str2) then
  84.                             local codef = fs.open(str2,"r")
  85.                             local code = codef.readAll()
  86.                             codef.close()
  87.                             local fn = (adv.." "..code)
  88.                             rednet.send(evnt[1],fn)
  89.                         else
  90.                             rednet.send(evnt[1],error_404)
  91.                         end
  92.                     else
  93.                         if fs.exists(str2) then
  94.                             local codef = fs.open(str2,"r")
  95.                             local code = codef.readAll()
  96.                             codef.close()
  97.                             rednet.send(evnt[1],code)
  98.                         else
  99.                             rednet.send(evnt[1],error_404)
  100.                         end
  101.                     end
  102.                 end
  103.             end
  104.         end
  105.     else
  106.         rednet.send(evnt[1],error_400)
  107.     end
  108. end
  109. if not fs.exists("www/index") then
  110.     term.setBackgroundColor(colors.white)
  111.     term.clear()
  112.     paintutils.drawFilledBox(1,1,size[1],4,colors.gray)
  113.     term.setCursorPos(1,1)
  114.     term.setTextColor(colors.lightGray)
  115.     print("Uh oh,")
  116.     term.setBackgroundColor(colors.white)
  117.     term.setCursorPos(1,6)
  118.     print("The main file, index is missing! Please create it then restart the server!")
  119.     term.setTextColor(colors.cyan)
  120.     term.setCursorPos(1,9)
  121.     print("Click anywhere to shutdown")
  122.     os.pullEvent("mouse_click")
  123.     os.shutdown()
  124. end
  125. local function drawMainPage()
  126.     term.setBackgroundColor(colors.white)
  127.     term.clear()
  128.     term.setTextColor(colors.lightGray)
  129.     paintutils.drawFilledBox(1,1,size[1],4,colors.gray)
  130.     term.setCursorPos(1,1)
  131.     print("Server Version: "..version)
  132.     paintutils.drawFilledBox(1,6,21,9,colors.cyan)
  133.     term.setCursorPos(3,7)
  134.     term.setTextColor(colors.white)
  135.     print("Enter Domain Name")
  136.     paintutils.drawFilledBox(31,6,51,9,colors.yellow)
  137.     term.setCursorPos(36,7)
  138.     print("View Details")
  139.     paintutils.drawFilledBox(1,11,size[1],14,colors.orange)
  140.     term.setCursorPos(24,12)
  141.     print("Restart")
  142.     paintutils.drawFilledBox(1,16,size[1],19,colors.red)
  143.     term.setCursorPos(23,17)
  144.     print("Shutdown")
  145. end
  146. local function drawDetailsPage()
  147.     term.setBackgroundColor(colors.white)
  148.     term.clear()
  149.     paintutils.drawFilledBox(1,1,size[1],4,colors.gray)
  150.     term.setCursorPos(1,1)
  151.     term.setTextColor(colors.lightGray)
  152.     print("Details")
  153.     term.setBackgroundColor(colors.white)
  154.     local domainf = fs.open("domain_name","r")
  155.     local domain = domainf.readAll()
  156.     domainf.close()
  157.     local ownf = fs.open("server_owner","r")
  158.     local owner = ownf.readAll()
  159.     ownf.close()
  160.     term.setCursorPos(1,6)
  161.     print("Domain name: "..domain)
  162.     term.setCursorPos(1,8)
  163.     print("Server owner: "..owner)
  164.     term.setCursorPos(1,10)
  165.     term.setTextColor(colors.cyan)
  166.     print("Press any key to exit")
  167.     os.pullEvent("char")
  168.     drawMainPage()
  169. end
  170. local function drawDomainPage()
  171.     term.setBackgroundColor(colors.white)
  172.     term.clear()
  173.     paintutils.drawFilledBox(1,1,size[1],4,colors.gray)
  174.     term.setCursorPos(1,1)
  175.     term.setTextColor(colors.lightGray)
  176.     print("Domain name editor")
  177.     term.setCursorPos(1,6)
  178.     term.setBackgroundColor(colors.white)
  179.     write("Enter new domain name: ")
  180.     local new_name = read()
  181.     local dmn = fs.open("domain_name","w")
  182.     local new_name =(new_name..".cc")
  183.     dmn.write(new_name)
  184.     dmn.close()
  185.     drawMainPage()
  186. end
  187. drawMainPage()
  188. os.pullEvent = os.pullEventRaw
  189. local function guiHandle()
  190.     local evnt = { os.pullEvent("mouse_click") }
  191.     if evnt[3] >= 1 and evnt[3] <= 21 and evnt[4] >= 6 and evnt[4] <= 9 then
  192.         drawDomainPage()
  193.     elseif evnt[3] >= 31 and evnt[3] <= 51 and evnt[4] >= 6 and evnt[4] <= 9 then
  194.         drawDetailsPage()
  195.     elseif evnt[3] >= 1 and evnt[3] <= size[1] and evnt[4] >= 11 and evnt[4] <= 14 then
  196.         term.setBackgroundColor(colors.white)
  197.         term.clear()
  198.         term.setTextColor(colors.cyan)
  199.         term.setCursorPos(1,1)
  200.         print("Restarting...")
  201.         sleep(0.5)
  202.         os.reboot()
  203.     elseif evnt[3] >= 1 and evnt[3] <= size[1] and evnt[4] >= 16 and evnt[4] <= 19 then
  204.         term.setBackgroundColor(colors.white)
  205.         term.clear()
  206.         term.setTextColor(colors.cyan)
  207.         term.setCursorPos(1,1)
  208.         print("Shutting down...")
  209.         sleep(1)
  210.         os.shutdown()
  211.     end
  212. end
  213.     while true do
  214.         if br then
  215.             break
  216.         end
  217.         parallel.waitForAny(guiHandle,webServerMain)
  218.     end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement