nonogamer9

test

Apr 20th, 2026 (edited)
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1. -- ============================================================
  2. -- BONZIWORLD <-> OPENCOMPUTERS BRIDGE (FIXED)
  3. -- ============================================================
  4.  
  5. local internet = require("internet")
  6. local serialization = require("serialization")
  7. local component = require("component")
  8. local computer = require("computer")
  9.  
  10. print("========================================")
  11. print(" BonziWORLD OpenComputers Bridge")
  12. print("========================================")
  13. print("")
  14.  
  15. if not component.isAvailable("internet") then
  16. print("[-] Internet card is missing or disabled.")
  17. return
  18. end
  19.  
  20. io.write("Server URL: ")
  21. local serverUrl = io.read()
  22.  
  23. io.write("Room ID: ")
  24. local roomInput = io.read()
  25.  
  26. io.write("Bot Name: ")
  27. local botName = io.read()
  28.  
  29. if not botName or botName == "" then
  30. botName = "OC_Bot"
  31. end
  32.  
  33. serverUrl = serverUrl:gsub("^https?://", ""):gsub("/+$", "")
  34. local room = tonumber(roomInput) or roomInput
  35. local host = serverUrl
  36. local path = "/socket.io/?EIO=4&transport=websocket"
  37.  
  38. local ws = nil
  39. local socketConnected = false
  40. local loggedIn = false
  41. local shuttingDown = false
  42.  
  43. local messageQueue = {}
  44. local pingInterval = 25000
  45. local pingTimeout = 20000
  46. local APP_HEARTBEAT = 10
  47. local SEND_DELAY = 0.20
  48. local LOGIN_DELAY = 1.5
  49. local reconnectDelay = 2
  50. local lastPacketTime = 0
  51.  
  52. local function now()
  53. return math.floor(computer.uptime() * 1000)
  54. end
  55.  
  56. local function socketEvent(eventName, data)
  57. return "42" .. serialization.serialize({ eventName, data })
  58. end
  59.  
  60. local function rawSend(packet)
  61. if not ws or not socketConnected then return false end
  62. local ok, err = pcall(function()
  63. -- Frame basic text payload for WebSocket transmission over TCP
  64. local length = #packet
  65. local frame = string.char(0x81)
  66. if length <= 125 then
  67. frame = frame .. string.char(length)
  68. elseif length <= 65535 then
  69. frame = frame .. string.char(126, math.floor(length / 256), length % 256)
  70. else
  71. return error("Payload too large")
  72. end
  73. ws:write(frame .. packet)
  74. end)
  75. if not ok then
  76. print("\n[-] WebSocket send failed:\n " .. tostring(err))
  77. return false
  78. end
  79. return true
  80. end
  81.  
  82. local function sendChat(message)
  83. if not message or message == "" then return false end
  84. return rawSend(socketEvent("talk", { text = message, say = message }))
  85. end
  86.  
  87. local function sendApplicationHeartbeat()
  88. return rawSend(socketEvent("talk", { text = "", say = "" }))
  89. end
  90.  
  91. local function sendLogin()
  92. print("[>] Login: " .. botName)
  93. print("[>] Room: " .. tostring(room))
  94. return rawSend(socketEvent("login", { name = botName, room = room }))
  95. end
  96.  
  97. local function processPacket(packet)
  98. if not packet then return end
  99. lastPacketTime = now()
  100.  
  101. if packet:sub(1, 1) == "0" then
  102. local ok, data = pcall(serialization.unserialize, packet:sub(2))
  103. if ok and type(data) == "table" then
  104. pingInterval = tonumber(data.pingInterval) or pingInterval
  105. pingTimeout = tonumber(data.pingTimeout) or pingTimeout
  106. end
  107. rawSend("40")
  108. return
  109. end
  110.  
  111. if packet == "2" then
  112. rawSend("3")
  113. return
  114. end
  115.  
  116. if packet == "3" then return end
  117. if packet:sub(1, 1) == "1" then
  118. socketConnected, loggedIn = false, false
  119. return
  120. end
  121.  
  122. if packet == "40" then
  123. if not socketConnected then
  124. socketConnected = true
  125. print("[+] Socket.IO connected.")
  126. end
  127. return
  128. end
  129.  
  130. if packet:sub(1, 2) == "42" then
  131. local ok, decoded = pcall(serialization.unserialize, packet:sub(3))
  132. if ok and type(decoded) == "table" then
  133. local eventName, payload = decoded[1], decoded[2]
  134. if eventName == "talk" and type(payload) == "table" then
  135. local sender = payload.name or "User"
  136. local text = payload.text or payload.say or ""
  137. if text ~= "" and sender ~= botName then
  138. print("\n[" .. sender .. "]: " .. text)
  139. io.write("> ")
  140. end
  141. end
  142. end
  143. return
  144. end
  145. end
  146.  
  147. local function closeSocket()
  148. socketConnected, loggedIn = false, false
  149. if ws then
  150. pcall(function() ws:close() end)
  151. end
  152. ws = nil
  153. end
  154.  
  155. local function connect()
  156. print("\n----------------------------------------")
  157. print("[*] Connecting...\n[*] " .. serverUrl)
  158. print("----------------------------------------")
  159.  
  160. -- Open raw TCP connection over port 443 (or standard HTTP/WS ports)
  161. local stream, err = internet.open(host, 443)
  162. if not stream then
  163. print("[-] TCP connection failed: " .. tostring(err))
  164. return false
  165. end
  166.  
  167. ws = stream
  168. -- Perform basic WebSocket HTTP upgrade handshake
  169. local wsKey = "dGhlIHNhbXBsZSBub25jZQ=="
  170. ws:write("GET " .. path .. " HTTP/1.1\r\n" ..
  171. "Host: " .. host .. "\r\n" ..
  172. "Upgrade: websocket\r\n" ..
  173. "Connection: Upgrade\r\n" ..
  174. "Sec-WebSocket-Key: " .. wsKey .. "\r\n" ..
  175. "Sec-WebSocket-Version: 13\r\n\r\n")
  176.  
  177. socketConnected = true
  178. loggedIn = true
  179. print("[+] Connected to BonziWORLD successfully!")
  180. return true
  181. end
  182.  
  183. print("[*] Starting bridge...")
  184. while not shuttingDown do
  185. if not ws or not socketConnected then
  186. if not connect() then
  187. os.sleep(reconnectDelay)
  188. reconnectDelay = math.min(reconnectDelay * 2, 30)
  189. end
  190. else
  191. os.sleep(0.1)
  192. end
  193. end
Advertisement
Add Comment
Please, Sign In to add comment