Advertisement
CodeCrafter

Lightning Eddited

Mar 9th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.15 KB | None | 0 0
  1. -- Edit by gijsvdsande
  2. local version = "1.0"
  3. logs = {}
  4. logs[1] = "Press ` to exit the logs."
  5. gui = true
  6. compID = os.getComputerID()
  7. --Lightning webserver.
  8. for k, v in pairs(peripheral.getNames()) do
  9. if peripheral.getType(v) == "modem" then
  10. modem = peripheral.wrap(v)
  11. rednet.open(v)
  12. end
  13. end
  14. for k, v in pairs(peripheral.getNames()) do
  15. if peripheral.getType(v) == "modem" then
  16. if peripheral.call(v,"isWireless") then
  17. modem = peripheral.wrap(v)
  18. rednet.open(v)
  19. end
  20. end
  21. end
  22.  
  23. errors = {}
  24. errors[400] = "%sid% Sent a bad equest"
  25. errors[403] = "%sid% Tried to access %msg% on %sd% which is forbidden"
  26. errors[404] = "Page %msg% on/or %sd% didn't exist as requested by %sid%"
  27. errors[413] = "Request from %sid% was too long"
  28. errors[418] = "I'm a teapot"
  29. errors[500] = "Encountered a internal server error"
  30.  
  31. defErrors = {}
  32. defErrors[400] = "print('Error 400: Bad request.')"
  33. defErrors[403] = "print('Error 403: Forbidden.')"
  34. defErrors[404] = "print('Error 404: Page not found.')"
  35. defErrors[413] = "print('Error 413: Request Entity Too Large.')"
  36. defErrors[418] = "print('Error 418: I'm a teapot.')"
  37. defErrors[500] = "print('Error 500: Internal server error.')"
  38.  
  39. function errorCode(wError)
  40. if errors[wError] and defErrors[wError] then
  41. if fs.exists("/errors/"..tostring(wError)) then
  42. local page = fs.open("/errors/"..tostring(wError),"r")
  43. local code = extra..page.readAll()
  44. rednet.send(sid, code)
  45. page.close()
  46. tPrint = errors[wError]
  47. tPrint = string.gsub(tPrint,"%%msg%%", msg)
  48. tPrint = string.gsub(tPrint,"%%sid%%", sid)
  49. tPrint = string.gsub(tPrint,"%%sd%%",subDomain)
  50. logs[#logs+1] = tPrint
  51. else
  52. local page = defErrors[wError]
  53. local code = extra..page
  54. rednet.send(sid, code)
  55. tPrint = errors[wError]
  56. tPrint = string.gsub(tPrint,"%%msg%%", msg)
  57. tPrint = string.gsub(tPrint,"%%sid%%", sid)
  58. tPrint = string.gsub(tPrint,"%%sd%%",subDomain)
  59. logs[#logs+1] = tPrint
  60. end
  61. else
  62. if fs.exists("/errors/500") then
  63. local page = fs.open("/errors/500","r")
  64. local code = extra..page.readAll()
  65. rednet.send(sid, code)
  66. page.close()
  67. tPrint = errors[500]
  68. tPrint = string.gsub(tPrint,"%%msg%%", msg)
  69. tPrint = string.gsub(tPrint,"%%sid%%", sid)
  70. tPrint = string.gsub(tPrint,"%%sd%%",subDomain)
  71. logs[#logs+1] = tPrint
  72. else
  73. local page = defErrors[500]
  74. local code = extra..page
  75. rednet.send(sid, code)
  76. tPrint = errors[500]
  77. tPrint = string.gsub(tPrint,"%%msg%%", msg)
  78. tPrint = string.gsub(tPrint,"%%sid%%", sid)
  79. tPrint = string.gsub(tPrint,"%%sd%%",subDomain)
  80. logs[#logs+1] = tPrint
  81. end
  82. end
  83. end
  84.  
  85. function showLog()
  86. scrx, scry = term.getSize()
  87. term.setBackgroundColor(bgColor)
  88. term.setTextColor(txtColor)
  89. term.clear()
  90. term.setCursorPos(1,1)
  91. for i = 1, scry do
  92. if logs[#logs-scry+i] then
  93. print(logs[#logs-scry+i])
  94. end
  95. end
  96. end
  97.  
  98. function showGui()
  99. term.setBackgroundColor(bgColor)
  100. term.clear()
  101. term.setCursorPos(1,1)
  102. term.setBackgroundColor(colors.gray)
  103. print(fline)
  104. print(fline)
  105. term.setTextColor(txtColor)
  106. term.setCursorPos(1,1)
  107. write("Minedows.cc File Webserver v"..version)
  108. term.setCursorPos(1,2)
  109. write("Requests: "..requests)
  110. term.setCursorPos(scrx-#domainName,1)
  111. write(domainName)
  112. paintutils.drawFilledBox(2, 4, 12, 6, colors.red)
  113. paintutils.drawFilledBox(scrx-11, 4, scrx-1, 6, colors.blue)
  114. term.setTextColor(colors.white)
  115. term.setBackgroundColor(colors.red)
  116. b1txt = "Shutdown"
  117. term.setCursorPos(6-#b1txt/2,5)
  118. write(b1txt)
  119. b2txt = "Logs"
  120. term.setCursorPos(scrx-7-#b2txt/2,5)
  121. term.setBackgroundColor(colors.blue)
  122. write(b2txt)
  123. end
  124.  
  125. bgColor = colors.white
  126. txtColor = colors.lightGray
  127. term.setBackgroundColor(bgColor)
  128. term.setTextColor(txtColor)
  129. term.clear()
  130. scrx,scry = term.getSize()
  131. fline = string.rep(" ",scrx)
  132. term.setCursorPos(1,1)
  133. requests = 0
  134.  
  135. if fs.exists("settings") then
  136. f = fs.open("settings","r")
  137. settings = textutils.unserialize(f.readAll())
  138. f.close()
  139. domainName = settings[1]
  140. extra = ""
  141. if settings[2] and settings[3] then
  142. logs[#logs+1] = "Global settings detected!"
  143. bgc = settings[2]
  144. txtc = settings[3]
  145. if settings[4] then
  146. miscData = settings[4]
  147. end
  148. extra = "term.setBackgroundColor("..tostring(bgc)..")\n"
  149. extra = extra.."term.setTextColor("..tostring(txtc)..")\n"
  150. extra = extra.."term.clear()\n"
  151. extra = extra.."term.setCursorPos(1,1)\n"
  152. extra = extra.."defaultBG = "..tostring(bgc).."\n"
  153. extra = extra.."defaultText = "..tostring(txtc).."\n"
  154. if miscData then
  155. extra = extra..miscData.."\n"
  156. end
  157. end
  158. else
  159. gui = false
  160. logs[#logs+1] = "No settings data"
  161. showLog()
  162. write("Domain: ")
  163. local dmn = read()
  164. tSettings= {dmn}
  165. domainName = dmn
  166. extra = ""
  167. local swrite = fs.open("settings","w")
  168. swrite.write(textutils.serialize(tSettings))
  169. swrite.close()
  170. term.clear()
  171. term.setCursorPos(1,1)
  172. end
  173. if fs.exists("advsettings") then
  174. f = fs.open("advsettings","r")
  175. fc = f.readAll()
  176. f.close()
  177. extra = extra..fc.."\n"
  178. logs[#logs+1] = "Advanced global settings detected"
  179. end
  180.  
  181. doRedirs = false
  182. if fs.exists("sd_settings") then
  183. f = fs.open("sd_settings","r")
  184. sds = f.readAll()
  185. dRedirs = textutils.unserialize(sds)
  186. doRedirs = true
  187. f.close()
  188. else
  189. example = {}
  190. example["example."] = 1
  191. f = fs.open("sd_settings","w")
  192. f.write(textutils.serialize(example))
  193. f.close()
  194. end
  195.  
  196. if not fs.exists("webapi") then
  197. f = fs.open("webapi","w")
  198. f.writeLine("wArgs = {...}")
  199. f.writeLine("pageRequested = wArgs[1]")
  200. f.writeLine("sArgs = {}")
  201. f.writeLine("for w in wArgs[2]:gmatch('[^&]+') do")
  202. f.writeLine(" table.insert(sArgs,w)")
  203. f.writeLine("end")
  204. f.writeLine("return false, false, ''")
  205. end
  206.  
  207. logs[#logs+1] = "Lightning web server v"..version
  208. logs[#logs+1] = "Server is hosted at "..domainName
  209.  
  210. function serv()
  211. rquests = 0
  212. while true do
  213. if not gui then
  214. showLog()
  215. end
  216. sid, msg, proto = rednet.receive()
  217. uWA = false
  218. disablePage = false
  219. disableExtras = false
  220. extCode = ""
  221. if type(msg) == "string" and type(proto) == "string" then
  222. if string.sub(string.lower(proto),#proto+1-#tostring(compID),#proto) == string.lower(tostring(compID)) then
  223. proto = string.sub(string.lower(proto),1,#proto-#tostring(compID))..domainName
  224. if string.sub(msg,1,9) == "TLD-REDIR" then
  225. sid = tonumber(string.sub(msg,10,14))
  226. msg = string.sub(msg,15,#msg)
  227. end
  228. end
  229. if string.sub(string.lower(proto),#proto+1-#domainName,#proto) == string.lower(domainName) then
  230. if string.sub(proto,#proto-#domainName,#proto-#domainName) == "." or string.sub(proto, #proto+1-#domainName,#proto+1-#domainName) == "." or string.lower(proto) == string.lower(domainName) then
  231. if string.lower(proto) == string.lower(domainName) then
  232. if string.sub(proto,#proto+1-#domainName,#proto+1-#domainName) == "." then
  233. proto = "www"..proto
  234. else
  235. proto = "www."..proto
  236. end
  237. end
  238. subDomain = string.sub(proto,0,#proto-#domainName)
  239. requests = requests +1
  240. rquests = rquests +1
  241. if rquests >= 250 then
  242. sleep(0)
  243. rquests = 0
  244. end
  245. serveRequest = true
  246. if doRedirs then
  247. if dRedirs[subDomain] then
  248. sidl = #tostring(sid)
  249. sidts = string.rep("0",5-sidl)..tostring(sid)
  250. protots = string.sub(proto,1,#proto-#domainName)..dRedirs[subDomain]
  251. rednet.send(dRedirs[subDomain],"TLD-REDIR"..sidts..msg,protots)
  252. serveRequest = false
  253. end
  254. end
  255. if serveRequest then
  256. loq = msg:find("?")
  257. if loq and #msg > loq then
  258. webArgs = msg:sub(loq+1,#msg)
  259. msg = msg:sub(1,loq-1)
  260. uWA = true
  261. end
  262. if fs.exists("webapi") and uWA then
  263. f = fs.open("webapi","r")
  264. fc = f.readAll()
  265. f.close()
  266. webApi = loadstring(fc)
  267. setfenv(webApi,getfenv())
  268. disablePage, disableExtras, extCode = webApi(msg,subDomain,webArgs)
  269. if not extCode then
  270. extCode = ""
  271. elseif #extCode < 1 then
  272. extCode = ""
  273. end
  274. end
  275. if #msg <= 256 then
  276. if fs.exists(subDomain.."/"..msg) then
  277. if msg ~= "startup" and msg ~= "settings" then
  278. if not disablePage then
  279. page = fs.open(subDomain.."/"..msg, "r")
  280. pageI = page.readAll()
  281. page.close()
  282. else
  283. pageI = ""
  284. end
  285. if not disableExtras then
  286. code = extra..extCode.."\n"..pageI
  287. else
  288. code = extCode.."\n"..pageI
  289. end
  290. rednet.send(sid, code)
  291. logs[#logs+1] = "Sent page "..subDomain..domainName.."/"..msg.." to "..sid
  292. else
  293. errorCode(403)
  294. end
  295. else
  296. errorCode(404)
  297. end
  298. else
  299. errorCode(413)
  300. end
  301. else
  302. logs[#logs+1] = "Redirected "..sid.." to "..dRedirs[subDomain].." for page "..subDomain
  303. end
  304. end
  305. end
  306. else
  307. --errorCode(400)
  308. end
  309. end
  310. end
  311.  
  312. function fGui()
  313. showGui()
  314. TID = os.startTimer(0.1)
  315. while true do
  316. e,mb,clickx,clicky = os.pullEvent()
  317. if e == "timer" and mb == TID then
  318. TID = os.startTimer(0.1)
  319. if not gui then
  320. showLog()
  321. else
  322. showGui()
  323. end
  324. end
  325. if gui then
  326. if e == "mouse_click" then
  327. if clickx >= 2 and clickx <= 12 and clicky >= 4 and clicky <= 6 then
  328. sleep(1)
  329. os.shutdown()
  330. elseif clickx >= scrx-11 and clickx <= scrx-1 and clicky >= 4 and clicky <= 6 then
  331. gui = false
  332. showLog()
  333. end
  334. showGui()
  335. end
  336. end
  337. if e == "key" and mb == 41 then
  338. gui = true
  339. showGui()
  340. end
  341. end
  342. end
  343.  
  344. parallel.waitForAny(serv,fGui)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement