melzneni

TURTI_MainFrame

Oct 1st, 2021 (edited)
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. -- pastebin run 0YB9PsQV startup={files={mainFrame=<pb:PaSGLHfJ>,WP=<pb:30b8Erjx>},cmds={{'mainFrame'}}} reboot=true label=<input:'Label'>
  2. os.loadAPI("WP")
  3. for k, v in pairs(WP) do
  4. _G[k] = v
  5. end
  6.  
  7. function split(s, delimiter)
  8. local result = {};
  9. for match in (s .. delimiter):gmatch("(.-)" .. delimiter) do
  10. table.insert(result, match);
  11. end
  12. return result;
  13. end
  14.  
  15. local CMD_INIT_MAIN_FRAME = "initMainFrame"
  16. local CMD_WRITE_FILE = "writeFile"
  17. local CMD_EXECUTE = "execute"
  18. local CMD_LOG = "log"
  19.  
  20. local protocol = ActionProtocol:new()
  21.  
  22. local websocket
  23.  
  24. function startWebsocket()
  25. websocket = ProtocolWebsocket:new("wss://nmserver.ddns.net/api", protocol)
  26.  
  27. protocol:setOnOpen(function()
  28. print("requesting MFOS")
  29. protocol:execute(CMD_INIT_MAIN_FRAME)
  30. end)
  31. protocol:setOnAction(function(cmd)
  32. if cmd[1] == CMD_WRITE_FILE then
  33. write("downloading file '" .. cmd[2] .. "'... ")
  34. local answer = http.get("https://nmserver.ddns.net/api/getData?id=" .. cmd[3])
  35. local f = fs.open(cmd[2], "w")
  36. f.write(answer.readAll())
  37. f.close()
  38. answer.close()
  39. print("done")
  40. elseif cmd[1] == CMD_EXECUTE then
  41. local close = cmd[3] == "true"
  42. if close then
  43. write("closing websocket... ")
  44. websocket:close()
  45. print("done")
  46. end
  47. print("executing '" .. cmd[2] .. "':")
  48. print()
  49. shell.run(cmd[2])
  50. if close then
  51. rednet.send(os.getComputerID(), { })
  52. end
  53. elseif cmd[1] == CMD_LOG then
  54. print("[server]: " .. cmd[2])
  55. elseif cmd[1] == "getComputerID" then
  56. return { "initializer" }
  57. else
  58. print("unknown command: " .. cmd[1])
  59. end
  60. end)
  61. protocol:setOnClose(function(code, msg)
  62. if not code and msg then
  63. print("onClose: " .. msg)
  64. return
  65. end
  66. if msg then
  67. print("onClose: " .. code .. ", " .. msg)
  68. elseif code then
  69. print("onClose:" .. code)
  70. end
  71. end)
  72. websocket:start()
  73. end
  74.  
  75. print(" ")
  76. while true do
  77.  
  78. local status, err = pcall(startWebsocket)
  79. if status then
  80. break
  81. elseif err == "Terminated" then
  82. break
  83. else
  84. print(err)
  85. sleep(3)
  86. end
  87. end
  88.  
Add Comment
Please, Sign In to add comment