Sirshark10

rednoot

Sep 11th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. if not socket or not socket.websocket then
  2. error("You do not have CCTweaks installed or are not on the latest version.")
  3. end
  4.  
  5. if not fs.exists(".rednoot") then
  6. fs.makeDir(".rednoot")
  7. end
  8.  
  9. if not fs.exists(".rednoot/json") then
  10. print("Downloading json...")
  11. shell.run("pastebin get 4nRg9CHU .rednoot/json")
  12. end
  13.  
  14. os.loadAPI(".rednoot/json")
  15. local json = json
  16. local defaultEndpoint = "ws://rednoot.krist.club"
  17. local defaultSide = "front"
  18.  
  19. local args = {...}
  20.  
  21. if args[1] and (args[1].sub(1,1) == "-" or #args[1] == 4 or #args[1] == 1) and (args[1]:find("help") or args[1]:find("%?") or args[1]:find("/")) then
  22. error("Usage: "..shell.getRunningProgram().." [endpoint ("..defaultEndpoint..")] [mountPoint ("..defaultSide..")]")
  23. end
  24.  
  25. local endpoint = args[1] or defaultEndpoint
  26.  
  27. if endpoint:sub(1,5) ~= "ws://" then
  28. endpoint = "ws://"..endpoint
  29. end
  30.  
  31. local mountPoint = args[2] or defaultSide
  32.  
  33. local oldPeripheral = peripheral
  34. local oldGetID = os.getComputerID
  35. _G.peripheral = {}
  36.  
  37. local virtualModem = {}
  38. local ws
  39. local userID = 0
  40. local openChannels = {}
  41. local connected = false
  42. local timer
  43. local timeout = 10
  44.  
  45. os.getComputerID = function()
  46. return userID
  47. end
  48.  
  49. peripheral.isPresent = function(side)
  50. if side == mountPoint then return true end
  51. return oldPeripheral.isPresent(side)
  52. end
  53.  
  54. peripheral.getType = function(side)
  55. if side == mountPoint then return "modem" end
  56. return oldPeripheral.getType(side)
  57. end
  58.  
  59. peripheral.getMethods = function(side)
  60. if side == mountPoint then
  61. return {
  62. "open", "isOpen", "close", "closeAll", "transmit", "isWireless"
  63. }
  64. end
  65. return oldPeripheral.getMethods(side)
  66. end
  67.  
  68. peripheral.call = function(side, method, ...)
  69. local args = {...}
  70. if side == mountPoint then
  71. return virtualModem[method](unpack(args))
  72. end
  73. return oldPeripheral.call(side, method, unpack(args))
  74. end
  75.  
  76. peripheral.wrap = function(side)
  77. if side == mountPoint then
  78. return virtualModem
  79. end
  80. return oldPeripheral.wrap(side)
  81. end
  82.  
  83. peripheral.find = function(pType, fnFilter)
  84. if pType == "modem" then
  85. if not fnFilter then
  86. return peripheral.wrap(mountPoint)
  87. elseif fnFilter(mountPoint, peripheral.wrap(mountPoint)) then
  88. return peripheral.wrap(mountPoint)
  89. end
  90. end
  91. return oldPeripheral.find(pType, fnFilter)
  92. end
  93.  
  94. peripheral.getNames = function()
  95. local names = oldPeripheral.getNames()
  96. local found = false
  97. for k, v in pairs(names) do
  98. if v == mountPoint then found = true end
  99. end
  100. if not found then
  101. table.insert(names, mountPoint)
  102. end
  103. return names
  104. end
  105.  
  106. virtualModem.open = function(channel)
  107. table.insert(openChannels, channel)
  108. os.queueEvent("rednoot_open", channel)
  109. end
  110.  
  111. virtualModem.close = function(channel)
  112. local newChannels = {}
  113. for k, v in pairs(openChannels) do
  114. if v ~= channel then
  115. table.insert(newChannels, v)
  116. end
  117. end
  118.  
  119. openChannels = newChannels
  120. os.queueEvent("rednoot_close", channel)
  121. end
  122.  
  123. virtualModem.isWireless = function()
  124. return true
  125. end
  126.  
  127. virtualModem.transmit = function(channel, replyChannel, message)
  128. os.queueEvent("rednoot_transmit", channel, replyChannel, message)
  129. end
  130.  
  131. virtualModem.closeAll = function()
  132. openChannels = {}
  133. os.queueEvent("rednoot_close_all")
  134. end
  135.  
  136. virtualModem.isOpen = function(channel)
  137. for _, v in pairs(openChannels) do
  138. if v == channel then
  139. return true
  140. end
  141. end
  142. return false
  143. end
  144.  
  145. local function daemon()
  146. ws = socket.websocket(endpoint)
  147. timer = os.startTimer(timeout)
  148. while true do
  149. local ev = {coroutine.yield()}
  150. if ev[1] == "socket_connect" and ev[2] == ws.id() then
  151. connected = true
  152. elseif ev[1] == "socket_message" and ev[2] == ws.id() then
  153. local mess = ws.read()
  154. if mess then
  155. local msg = json.decode(mess)
  156. if msg.type == "id" then
  157. userID = msg.value
  158. elseif msg.type == "receive" then
  159. os.queueEvent("modem_message", mountPoint, msg.channel, msg.reply_channel, msg.message, 0, msg.id)
  160. elseif msg.type == "error" then
  161. os.queueEvent("rednoot_error", msg.message)
  162. end
  163. end
  164. elseif ev[1] == "rednoot_open" then
  165. ws.write(json.encode({
  166. type = "open",
  167. channel = ev[2]
  168. }))
  169. elseif ev[1] == "rednoot_close" then
  170. ws.write(json.encode({
  171. type = "close",
  172. channel = ev[2]
  173. }))
  174. elseif ev[1] == "rednoot_close_all" then
  175. ws.write(json.encode({
  176. type = "close_all"
  177. }))
  178. elseif ev[1] == "rednoot_transmit" then
  179. ws.write(json.encode({
  180. type = "transmit",
  181. channel = ev[2],
  182. reply_channel = ev[3],
  183. message_type = type(ev[4]),
  184. message = ev[4]
  185. }))
  186. elseif ev[1] == "timer" and ev[2] == timer then
  187. if not connected then
  188. return
  189. end
  190. end
  191. end
  192. end
  193.  
  194. term.clear()
  195. term.setCursorPos(1,1)
  196.  
  197. parallel.waitForAny(daemon, function()
  198. while not connected and not ws.checkConnected() do
  199. write(".")
  200. sleep(0)
  201. end
  202. term.clear()
  203. term.setCursorPos(1,1)
  204. print("Connected to network!")
  205. shell.run("/rom/programs/shell")
  206. end)
  207.  
  208. _G.peripheral = oldPeripheral
  209. _G.os.getComputerID = oldGetID
  210. if ws.checkConnected() then
  211. ws.close()
  212. print("Disconnected from the network.")
  213. else
  214. print("Timed out.")
  215. end
Advertisement
Add Comment
Please, Sign In to add comment