turtle5204

Amethyst Server

Apr 22nd, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. -- Beware, horrible code lurks below, waiting to strike a carefree programmer.
  2.  
  3. local d = false
  4. local mod = nil
  5. for k, v in pairs(peripheral.getNames()) do
  6. if peripheral.getType(v) == "modem" then
  7. rednet.open(v)
  8. mod = peripheral.wrap(v)
  9. d = true
  10. end
  11. end
  12. if not d or not mod then
  13. error("A modem is required!")
  14. end
  15.  
  16. -- Some functions
  17.  
  18. local dns_channel = 331
  19. local args = {...}
  20. local hostname = args[1] or "test.net"
  21.  
  22. local function download_file(url, target)
  23. local ht = http.get(url)
  24. local data = ht.readAll()
  25. ht.close()
  26. local file = fs.open(target, "w")
  27. file.write(data)
  28. file.close()
  29. end
  30.  
  31. local function read_file(target)
  32. local file = fs.open(target, "r")
  33. local data = file.readAll()
  34. file.close()
  35. return data
  36. end
  37.  
  38. -- API Loading
  39. local function getAPI(name, url)
  40. local apiloc = "/" .. name
  41. if (not fs.exists(apiloc)) then
  42. local newapiloc = shell and shell.resolveProgram(name) or nil
  43. if type(newapiloc) ~= "string" then
  44. download_file(url, "/" .. name)
  45. else
  46. apiloc = newapiloc
  47. end
  48. end
  49. os.loadAPI(apiloc)
  50. if not _G[name] then
  51. error(name .. " failed to load!", 0)
  52. return
  53. end
  54. end
  55.  
  56. getAPI("ringnet", "https://pastebin.com/raw/bnDHMbNx")
  57. ringnet.openChannel(32031)
  58.  
  59. -- The actual coroutines
  60. parallel.waitForAll(
  61. function()
  62. ringnet.connectionHandler(true)
  63. end,
  64. ringnet.pingHandler,
  65. function()
  66. while true do
  67. local ev, from, data, dist = os.pullEvent("secure_receive")
  68. if (ev ~= nil) then
  69. if (type(data) ~= "table") then
  70. data = textutils.unserialize(tostring(data))
  71. end
  72. if not data then
  73. return
  74. end
  75. if (type(data.type) == "string") then
  76. local path = fs.combine("/www", fs.combine("", tostring(data.path)):gsub("%.", ""))
  77. if (data.type == "AMETHYSTGET" and type(data.path) == "string" and fs.exists(path)) then
  78. local file = fs.open(path, "r")
  79. local stuff = file.readAll()
  80. file.close()
  81. sendData(
  82. from,
  83. get_frq(from),
  84. textutils.serialize(
  85. {
  86. type = "AMETHYSTDATA",
  87. data = stuff
  88. }
  89. )
  90. )
  91. end
  92. end
  93. end
  94. sleep(0)
  95. end
  96. end
  97. )
Advertisement
Add Comment
Please, Sign In to add comment