Advertisement
Imgoodisher

multiserv_pkg

Jul 5th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.06 KB | None | 0 0
  1. local pkg={["servers"]={["googol.com"]={["asdf"]={},["logo"]="bbbbbb                          e                \
  2. b                               e                \
  3. b                               e                \
  4. b  bbb  eeeee 44444 bbbbb 55555 e                \
  5. b    b  e   e 4   4 b   b 5   5 e                \
  6. b    b  e   e 4   4 b   b 5   5 e                \
  7. bbbbbb  eeeee 44444 bbbbb 55555 eee              \
  8.                        b                        \
  9.                    bbbbb",["test"]={["asdf"]="print(\"hi\")",},["home"]="local w, h = term.getSize()\
  10. local img = loadImageFromServer(\"logo\")\
  11. \
  12. term.setBackgroundColor(colors.white)\
  13. term.clear()\
  14. paintutils.drawImage(img, w/2-35/2, 3)",},["freewebhost.com"]={["servapi"]="function urlenc(s)\
  15.  return s:gsub(\"([\\\"\\'%s%$;&%?])\", function(c) return \"$\"..string.byte(c)..\";\" end)\
  16. end\
  17. ",["api"]={["create"]="!SERVER\
  18. os.queueEvent(\"update_ids\")\
  19. if not _GET.name then\
  20.  return \"redirect('freewebhost.com/error?t=Name$32;required')\"\
  21. elseif _GET.name:find(\"%.%.\") or _GET.name:find(\"[/%s]\") then\
  22.  return \"redirect('freewebhost.com/error?t=Name$32;contains$32;invalid$32;characters!')\"\
  23. elseif fs.exists(\"/servers/\".._GET.name) then\
  24.  return \"redirect('freewebhost.com/error?t=Server$32;already$32;exists!')\"\
  25. elseif not _GET.pass then\
  26.  return \"redirect('freewebhost.com/error?t=Password$32;required')\"\
  27. else\
  28.  fs.makeDir(\"/servers/\".._GET.name)\
  29.  local f = io.open(\"/pass/\".._GET.name, \"w\")\
  30.  if f then\
  31.    f:write(_GET.pass)\
  32.    f:close()\
  33.  else\
  34.    return \"redirect('freewebhost.com/error?t=Server$32;Error')\"\
  35.  end\
  36.  return \"redirect('freewebhost.com/\"..(_GET.from or \"\")..\"')\"\
  37. end",},["help"]="term.setBackgroundColor(colors.white)\
  38. term.setTextColor(colors.black)\
  39. term.clear()\
  40. term.setCursorPos(1, 3)\
  41. cPrint(\"freewebhost Site Kit Documentation\")\
  42. print(\"\")\
  43. cPrint(\"Run the file downloaded to extract the files\")\
  44. cPrint(\"It contains 2 programs:\")\
  45. print(\"\")\
  46. cPrint(\"fwh/sync folder sitename\")\
  47. cPrint(\"Replaces all files on [sitename] with those\")\
  48. cPrint(\"in [folder]\")\
  49. print(\"\")\
  50. cPrint(\"fwh/download sitename folder\")\
  51. cPrint(\"Downloads all the files on [sitename] to [folder]\")",["create"]="term.setBackgroundColor(colors.white)\
  52. term.setTextColor(colors.black)\
  53. term.clear()\
  54. term.setCursorPos(1, 3)\
  55. cPrint(\"Create a new server\")\
  56. term.setCursorPos(3, 6)\
  57. print(\"Site Name:\")\
  58. term.setCursorPos(3, 8)\
  59. print(\"Password:\")\
  60. term.setCursorPos(14, 6)\
  61. local name = read()\
  62. term.setCursorPos(13, 8)\
  63. local pass = read(\"*\")\
  64. \
  65. function urlenc(str)\
  66.  return str:gsub(\"([&%?%s;%$\\\"\\'])\", function(c) return \"$\"..string.byte(c)..\";\" end)\
  67. end\
  68. \
  69. redirect(\"freewebhost.com/api/create?name=\"..urlenc(name)..\"&pass=\"..urlenc(pass)..\"&from=\"..urlenc(\"created?sitename=\"..urlenc(name)))",["error"]="!SERVER\
  70. return [[term.setTextColor(colors.red) print('') cPrint('Error:') cPrint(']].._GET.t:gsub(\"([\\\"\\'])\", \"\\\\%1\")..[[')]]",["download"]="term.setBackgroundColor(colors.white)\
  71. term.setTextColor(colors.black)\
  72. term.clear()\
  73. term.setCursorPos(1, 3)\
  74. cPrint(\"Downloading...\")\
  75. pastebinDownload(\"hxRGshGH\")\
  76. cPrint(\"Downloading Complete\")\
  77. sleep(1)\
  78. redirect(\"freewebhost.com/help\")\
  79. ",["created"]="!SERVER\
  80. return [[\
  81. term.setBackgroundColor(colors.white)\
  82. term.setTextColor(colors.black)\
  83. term.clear()\
  84. term.setCursorPos(1, 3)\
  85. cPrint(\"Your webiste (]].._GET.sitename..[[) has been created\")\
  86. cPrint(\"Click anywhere to continue\")\
  87. os.pullEvent(\"mouse_click\")\
  88. redirect(\"freewebhost.com\")\
  89. ]]",["home"]="term.setBackgroundColor(colors.white)\
  90. term.clear()\
  91. term.setCursorPos(1, 3)\
  92. \
  93. local w, h = term.getSize()\
  94. \
  95. term.setTextColor(colors.red)\
  96. cPrint(\"Free Firewolf Site Hosting\")\
  97. cPrint(\"By Googol\")\
  98. local result = prompt({\
  99.  {\"Create Server\", (w/2 - 13/2)-2, 7},\
  100.  {\"Download Site Kit\", (w/2 - 17/2)-2, 9},\
  101. }, \"vertical\")\
  102. \
  103. if result == \"Create Server\" then\
  104.  redirect(\"freewebhost.com/create\")\
  105. elseif result == \"Download Site Kit\" then\
  106.  redirect(\"freewebhost.com/download\")\
  107. else\
  108.  print(\"YOU BROKE FIREWOLF!11!!ELEVEN!!1ONE!!!\")\
  109. end",["test"]="!SERVER\
  110. return \"cPrint('\"..(_GET.text or \"???\")..\"')\"",},},["pass"]={["googol.com"]="googol",},["startup"]="local modem\
  111. for i,v in pairs(rs.getSides()) do  \
  112.     if peripheral.getType(v) == \"modem\" then\
  113.         modem = peripheral.wrap(v)\
  114.     end\
  115. end\
  116. --modem.open(os.computerID())\
  117. modem.open(rednet.CHANNEL_BROADCAST)\
  118. \
  119. local serverIDs = {}\
  120. \
  121. function sendAs(id, nRecipient, sMessage )\
  122.     id = (tonumber(id) or os.computerID())\
  123.     modem.transmit( nRecipient, id, sMessage )\
  124. end\
  125. \
  126. function send(nRecipient, sMessage)\
  127.     modem.transmit(nRecipient, os.computerID(), sMessage)\
  128. end\
  129. \
  130. local serverdir = \"/servers/\"\
  131. local notfounddata = [[print(\"\") cPrint(\"404\") cPrint(\"Page Not Found\")]]\
  132. \
  133. local ss_whitelist = {\
  134.     [\"googol.com\"] = true,\
  135.     [\"freewebhost.com\"] = true,\
  136. }\
  137. \
  138. local function updateIDs()\
  139.     serverIDs = {}\
  140.     local id = 62000\
  141.     local c = 0\
  142.     modem.open(id)\
  143.     print(\"opened \"..id)\
  144.     for i,v in pairs(fs.list(serverdir)) do\
  145.         serverIDs[v] = id\
  146.         c = c + 1\
  147.         if c == 3 then \
  148.             c = 0 \
  149.             id = id + 1 \
  150.             modem.open(id)\
  151.             print(\"opened \"..id)\
  152.         end\
  153.     end\
  154. end\
  155. updateIDs()\
  156. \
  157. local function firewolf_server()\
  158.     while true do\
  159.         local event = {os.pullEvent()}\
  160.         if event[1] == \"rednet_message\" then\
  161.             --print(event[3])\
  162.             if event[3] == \"firewolf.broadcast.dns.list\" then\
  163.                 for i,v in pairs(fs.list(serverdir)) do\
  164.                     --if fs.isDir(serverdir..v) then\
  165.                         sendAs(serverIDs[v], event[2], \"firewolf-site:\"..v)\
  166.                     --end\
  167.                 end\
  168.             else\
  169.                 local url = event[3]\
  170.                 _GET = {}\
  171.                 local site, path\
  172.                 if url:find(\"%?\") then\
  173.                     local getdata\
  174.                     url, getdata = url:match(\"(.-)(%?.+)\")\
  175.                     for key, val in getdata:gmatch(\"[%?&](.-)=([^&]+)\") do\
  176.                         _GET[key] = val:gsub(\"$(%d-);\", function(n) return string.char(tonumber(n)) end)\
  177.                         --print(key..\"=\"..val)\
  178.                     end\
  179.                 end\
  180.                 if url:find(\"/\") then\
  181.                     site, path = url:match(\"(.-)(/.*)\")\
  182.                     if path == \"/\" then path = \"/home\" end\
  183.                 else\
  184.                     site, path = url, \"/home\"\
  185.                 end\
  186.                 if fs.exists(serverdir..site) then\
  187.                     if fs.exists(serverdir..site..path) then\
  188.                         local f = io.open(serverdir..site..path, \"r\")\
  189.                         local data = f:read(\"*a\")\
  190.                         f:close()\
  191.         \
  192.                         if data:find(\"^!SERVER\") and ss_whitelist[site] then\
  193.                             local func, err = loadstring(data:sub(8))\
  194.                             local env = {_GET=_GET}\
  195.                             for i,v in pairs(_G) do\
  196.                                 if i~=\"os.pullEventRaw\" then\
  197.                                     env[i] = v\
  198.                                 end\
  199.                             end\
  200.                             setfenv(func, env)\
  201.                             if func then\
  202.                                 local ok, d = pcall(func)\
  203.                                 if not ok then print(\"ERROR: \"..d) end\
  204.                                 data = (ok and d) or \"cPrint('Server Error') cPrint('\"..d:gsub(\"([\\\"\\'])\", \"\\\\%1\")..\"')\"\
  205.                             else\
  206.                                 data = \"cPrint('Server Error') cPrint('\"..err:gsub(\"([\\\"\\'])\", \"\\\\%1\")..\"')\"\
  207.                             end\
  208.                         end\
  209.                         sendAs(serverIDs[site], event[2], data)\
  210.                     else\
  211.                         sendAs(serverIDs[site], event[2], notfounddata)\
  212.                     end\
  213.                 end\
  214.             end\
  215.         elseif event[1] == \"update_ids\" then\
  216.             updateIDs()\
  217.         end\
  218.     end\
  219. end\
  220. \
  221. local function urldec(str)\
  222.     return str:gsub(\"%$([^;]+);\", function(n) return tonumber(n) and string.char(tonumber(n)) end)\
  223. end\
  224. \
  225. local function file_server()\
  226.     while true do\
  227.         local event = {os.pullEvent()}\
  228.         if event[1] == \"rednet_message\" then\
  229.             if event[3]:find(\"^fwh:\") then\
  230.                 local msg = event[3]:match(\"fwh:(.+)\")\
  231.                 local arg = {}\
  232.                 for match in msg:gmatch(\"[^&]+\") do\
  233.                     local key, val = match:match(\"([^=]+)=(.+)\")\
  234.                     if key and val then\
  235.                         --print(key..\"=\"..val)\
  236.                         if key == \"name\" or key == \"dir\" then\
  237.                             val = urldec(val):match(\"^/?(.+)$\")\
  238.                         end\
  239.                         arg[urldec(key)] = urldec(val)\
  240.                         --print(urldec(key)..\"=\"..urldec(val))\
  241.                     end\
  242.                 end\
  243.                             \
  244.                 if (arg.name and arg.name:find(\"%.%.\")) or (arg.dir and arg.dir:find(\"%.%.\")) then\
  245.                     send(event[2], \"0Paths can not contain '..'\")\
  246. \
  247.                 elseif arg.cmd == \"upload\" then\
  248.                     if not (arg.site and arg.pass and arg.contents and arg.name) then\
  249.                         send(event[2], \"0Site name, password, file name, and file contents required\")\
  250.                     else\
  251.                         local f = io.open(\"/pass/\"..arg.site, \"r\")\
  252.                         if f then\
  253.                             local ok = f:read(\"*a\") == arg.pass\
  254.                             f:close()\
  255.             \
  256.                         \
  257.                             if not ok then\
  258.                                 send(event[2], \"0Invalid password\")\
  259.                             else\
  260.                                 --print(\"/servers/\"..arg.site..\"/\"..arg.name)\
  261.                                 local f = io.open(serverdir..arg.site..\"/\"..arg.name, \"w\")\
  262.                                 if f then\
  263.                                     --print(arg.contents)\
  264.                                     f:write(arg.contents)\
  265.                                     f:close()\
  266.                                     send(event[2], \"1Success\")\
  267.                                 else\
  268.                                     send(event[2], \"0Filesystem Error\")\
  269.                                 end\
  270.                             end\
  271.                         else\
  272.                             send(event[2], \"0Invalid Site\")\
  273.                         end\
  274.                     end\
  275.                 elseif arg.cmd == \"delete\" then\
  276.                     if not (arg.site and arg.pass and arg.name) then\
  277.                         send(event[2], \"0Site name, password, and file name required\")\
  278.                     else\
  279.                         local f = io.open(\"/pass/\"..arg.site, \"r\")\
  280.                         if f then\
  281.                             local ok = f:read(\"*a\") == arg.pass\
  282.                             f:close()\
  283.                             if ok then\
  284.                                 fs.delete(serverdir..arg.site..\"/\"..arg.name)\
  285.                                 send(event[2], \"1Success\")\
  286.                             else\
  287.                                 send(event[2], \"0Invalid password\")\
  288.                             end\
  289.                         else\
  290.                             send(event[2], \"0Invalid site\")\
  291.                         end\
  292.                     end\
  293.                 elseif arg.cmd == \"get\" then\
  294.                     if not (arg.site and arg.pass and arg.name) then\
  295.                         send(event[2], \"0Site name, password, and file name required\")\
  296.                     else\
  297.                         local f = io.open(\"/pass/\"..arg.site, \"r\")\
  298.                         if f then\
  299.                             local ok = f:read(\"*a\") == arg.pass\
  300.                             f:close()\
  301.                             if ok then\
  302.                                 local f = io.open(serverdir..arg.site..\"/\"..arg.name, \"r\")\
  303.                                 if f then\
  304.                                     send(event[2], \"1\"..f:read(\"*a\"))\
  305.                                     f:close()\
  306.                                 else\
  307.                                     send(event[2], \"0File does not exist\")\
  308.                                 end\
  309.                             else\
  310.                                 send(event[2], \"0Invalid password\")\
  311.                             end\
  312.                         else\
  313.                             send(event[2], \"0Invalid site\")\
  314.                         end\
  315.                     end\
  316.                 elseif arg.cmd == \"list\" then\
  317.                     arg.dir = arg.dir or \"\"\
  318.                     if not (arg.site and arg.pass) then\
  319.                         send(event[2], \"0Site name and password required\")\
  320.                     else\
  321.                         local f = io.open(\"/pass/\"..arg.site, \"r\")\
  322.                         if f then\
  323.                             local ok = f:read(\"*a\") == arg.pass\
  324.                             f:close()\
  325.                             if ok then\
  326.                                 if fs.exists(serverdir..arg.site..\"/\"..arg.dir) and fs.isDir(serverdir..arg.site..\"/\"..arg.dir) then\
  327.                                     send(event[2], \"1\"..textutils.serialize(fs.list(serverdir..arg.site..\"/\"..arg.dir)))\
  328.                                 else\
  329.                                     send(event[2], \"0Invalid directory\")\
  330.                                 end\
  331.                             else\
  332.                                 send(event[2], \"0Invalid password\")\
  333.                             end\
  334.                         else\
  335.                             send(event[2], \"0Invalid site\")\
  336.                         end\
  337.                     end\
  338.                 elseif arg.cmd == \"mkdir\" then\
  339.                     if not (arg.site and arg.pass and arg.name) then\
  340.                         send(event[2], \"0Site name, password, and directory name required\")\
  341.                     else\
  342.                         local f = io.open(\"/pass/\"..arg.site, \"r\")\
  343.                         if f then\
  344.                             local ok = f:read(\"*a\") == arg.pass\
  345.                             f:close()\
  346.                             if ok then\
  347.                                 if not fs.exists(serverdir..arg.site..\"/\"..arg.name) then\
  348.                                     fs.makeDir(serverdir..arg.site..\"/\"..arg.name)\
  349.                                     send(event[2], \"1Success\")\
  350.                                 else\
  351.                                     send(event[2], \"0File exists\")\
  352.                                 end\
  353.                             else\
  354.                                 send(event[2], \"0Invalid password\")\
  355.                             end\
  356.                         else\
  357.                             send(event[2], \"0Invalid site\")\
  358.                         end\
  359.                     end\
  360.                 elseif arg.cmd == \"verify\" then\
  361.                     local f = io.open(\"/pass/\"..arg.site, \"r\")\
  362.                     if f then\
  363.                         local ok = f:read(\"*a\") == arg.pass\
  364.                         f:close()\
  365.                         if ok then\
  366.                             send(event[2], \"1Valid\")\
  367.                         else\
  368.                             send(event[2], \"0Invalid password\")\
  369.                         end\
  370.                     else\
  371.                         send(event[2], \"0Invalid site\")\
  372.                     end\
  373.                 elseif arg.cmd == \"isdir\" then\
  374.                     if not (arg.site and arg.name and arg.pass) then\
  375.                         send(event[2], \"0Site name, password, and name required\")\
  376.                     else\
  377.                         local f = io.open(\"/pass/\"..arg.site, \"r\")\
  378.                         if f then\
  379.                             local ok = f:read(\"*a\") == arg.pass\
  380.                             f:close()\
  381.                             if ok then\
  382.                                 send(event[2], \"1\"..tostring(fs.isDir(serverdir..arg.site..\"/\"..arg.name)))\
  383.                             else\
  384.                                 send(event[2], \"0Invalid password\")\
  385.                             end\
  386.                         else\
  387.                             send(event[2], \"0Invalid site\")\
  388.                         end\
  389.                     end\
  390.                 end\
  391.             end\
  392.         end\
  393.     end\
  394. end\
  395. \
  396. function sh()\
  397.     shell.run(\"rom/programs/shell\")\
  398. end\
  399. \
  400. function rednet_modrun()\
  401.     while true do\
  402.         local sEvent, sSide, sChannel, sReplyChannel, sMessage, nDistance = os.pullEventRaw( \"modem_message\" )\
  403.         os.queueEvent( \"rednet_message\", sReplyChannel, sMessage, nDistance )\
  404.     end\
  405. end\
  406. \
  407. parallel.waitForAny(sh, firewolf_server, file_server, rednet_modrun)\
  408. modem.close(rednet.CHANNEL_BROADCAST)\
  409. --modem.close(os.computerID())\
  410. for i,v in pairs(serverIDs) do\
  411.     modem.close(v)\
  412. end",["PkgMake"]="sPackage=[[local pkg=%@1\
  413. local function makeFile(_path,_content)\
  414. local file=fs.open(_path,\"w\")\
  415. file.write(_content)\
  416. file.close()\
  417. end\
  418. local function makeFolder(_path,_content)\
  419. fs.makeDir(_path)\
  420. for k,v in pairs(_content) do\
  421.  if type(v)==\"table\" then\
  422.   makeFolder(_path..\"/\"..k,v)\
  423.  else\
  424.   makeFile(_path..\"/\"..k,v)\
  425.  end\
  426. end\
  427. end\
  428. local sDest=shell.resolve(\"%@2\") or \"/\"\
  429. if sDest==\"root\" then\
  430. sDest=\"/\"\
  431. end\
  432. local tPackage=pkg\
  433. makeFolder(sDest,tPackage)\
  434. print(\"Package Extracted to '\"..sDest..\"'!\")\
  435. ]]\
  436. \
  437. function addFile(_package,_path)\
  438.     if fs.getName(_path)==\".DS_Store\" then\
  439.         return _package\
  440.     end\
  441.     local file,err=fs.open(_path,\"r\")\
  442.     local content=file.readAll()\
  443.     content=content:gsub(\"%%\",\"%%%%\")\
  444.     _package[fs.getName(_path)]=content\
  445.     file.close()\
  446.     print(\"Added file '\".._path..\"'\")\
  447.     return _package\
  448. end\
  449. \
  450. function addFolder(_package,_path)\
  451.     if string.sub(_path,1,string.len(\"rom\"))==\"rom\" or string.sub(_path,1,string.len(\"/rom\"))==\"/rom\" then\
  452.         print(\"Ignored 'rom' folder. (\".._path..\")\")\
  453.         return\
  454.     end\
  455.     _package=_package or {}\
  456.     for _,f in ipairs(fs.list(_path)) do\
  457.         local path=_path..\"/\"..f\
  458.         if fs.isDir(path) then\
  459.             _package[fs.getName(f)]=addFolder(_package[fs.getName(f)],path)\
  460.         else\
  461.             _package=addFile(_package,path)\
  462.         end\
  463.     end\
  464.     return _package\
  465. end\
  466. \
  467. local tArgs={...}\
  468. if #tArgs<2 then\
  469.     print(\"Usage: PkgMake <source> <destination>\")\
  470.     return\
  471. end\
  472. \
  473. local sSource=shell.resolve(tArgs[1])\
  474. local sDest=shell.resolve(tArgs[2])\
  475. \
  476. if fs.isDir(sDest) then\
  477.     error(\"Destination must not be a folder.\")\
  478. end\
  479. \
  480. if sSource==sDest then\
  481.     error(\"Source can not be equal to destination.\")\
  482. end\
  483. \
  484. if fs.exists(sSource) and fs.isDir(sSource) then\
  485.     tPackage={}\
  486.     tPackage=addFolder(tPackage,sSource)\
  487.     fPackage=fs.open(sDest,\"w\")\
  488. \
  489.     sPackage=string.gsub(sPackage,\"%%@2\",fs.getName(sSource))\
  490.     sPackage=string.gsub(sPackage,\"%%@1\",textutils.serialize(tPackage))\
  491.     fPackage.write(sPackage)\
  492.     fPackage.close()\
  493.     print(\"Package Done! ('\"..sDest..\"')\")\
  494.     print(\"Type '\"..sDest..\"' to run it.\")\
  495. else\
  496.     error(\"Source does not exist or is not a folder.\")\
  497. end",}
  498. local function makeFile(_path,_content)
  499.  local file=fs.open(_path,"w")
  500.  file.write(_content)
  501.  file.close()
  502. end
  503. local function makeFolder(_path,_content)
  504.  fs.makeDir(_path)
  505.  for k,v in pairs(_content) do
  506.   if type(v)=="table" then
  507.    makeFolder(_path.."/"..k,v)
  508.   else
  509.    makeFile(_path.."/"..k,v)
  510.   end
  511.  end
  512. end
  513. local sDest=shell.resolve("root") or "/"
  514. if sDest=="root" then
  515.  sDest="/"
  516. end
  517. local tPackage=pkg
  518. makeFolder(sDest,tPackage)
  519. print("Package Extracted to '"..sDest.."'!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement