nonogamer9

t

Feb 24th, 2026 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.89 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local term = require("term")
  4. local internet = component.internet
  5. local keyboard = require("keyboard")
  6. local gpu = component.gpu
  7.  
  8. -- Telnet Constants (Handshake protocol)
  9. local IAC, DONT, DO, WONT, WILL = "\255", "\254", "\253", "\252", "\251"
  10. local SB, SE, IS, SEND, AYT = "\250", "\240", "\0", "\1", "\246"
  11. local BINARY, ECHO, TTYPE, NAWS, TSPEED, ENVIRON = "\0", "\1", "\24", "\31", "\32", "\36"
  12.  
  13. -- CP437 Mapping (Extended set 127-255)
  14. local cp437 = {[127]="⌂", [128]="Ç", [129]="ü", [130]="é", [131]="â", [132]="ä", [133]="à", [134]="å", [135]="ç", [136]="ê", [137]="ë", [138]="è", [139]="ï", [140]="î", [141]="ì", [142]="Ä", [143]="Å", [144]="É", [145]="æ", [146]="Æ", [147]="ô", [148]="ö", [149]="ò", [150]="û", [151]="ù", [152]="ÿ", [153]="Ö", [154]="Ü", [155]="¢", [156]="£", [157]="¥", [158]="₧", [159]="ƒ", [160]="á", [161]="í", [162]="ó", [163]="ú", [164]="ñ", [165]="Ñ", [166]="ª", [167]="º", [168]="¿", [169]="⌐", [170]="¬", [171]="½", [172]="¼", [173]="¡", [174]="«", [175]="»", [176]="░", [177]="▒", [178]="▓", [179]="│", [180]="┤", [181]="╡", [182]="╢", [183]="╖", [184]="╕", [185]="╣", [186]="║", [187]="╗", [188]="╝", [189]="╜", [190]="╛", [191]="┐", [192]="└", [193]="┴", [194]="┬", [195]="├", [196]="─", [197]="┼", [198]="╞", [199]="╟", [200]="╚", [201]="╔", [202]="╩", [203]="╦", [204]="╠", [205]="═", [206]="╬", [207]="╧", [208]="╨", [209]="╤", [210]="╥", [211]="╙", [212]="╘", [213]="╒", [214]="₧", [215]="╫", [216]="╪", [217]="┘", [218]="┌", [219]="█", [220]="▄", [221]="▌", [222]="▐", [223]="▀", [224]="α", [225]="ß", [226]="Γ", [227]="π", [228]="Σ", [229]="σ", [230]="µ", [231]="τ", [232]="Φ", [233]="Θ", [234]="Ω", [235]="δ", [236]="∞", [237]="φ", [238]="ε", [239]="∩", [240]="≡", [241]="±", [242]="≥", [243]="≤", [244]="⌠", [245]="⌡", [246]="÷", [247]="≈", [248]="°", [249]="∙", [250]="·", [251]="√", [252]="ⁿ", [253]="²", [254]="■", [255]=" "}
  15.  
  16. -- Palette Generation
  17. local palette = {[0]=0x000000, 0xAA0000, 0x00AA00, 0xAA5500, 0x0000AA, 0xAA00AA, 0x00AAAA, 0xAAAAAA, 0x555555, 0xFF5555, 0x55FF55, 0xFFFF55, 0x5555FF, 0xFF55FF, 0x55FFFF, 0xFFFFFF}
  18. for r=0,5 do for g=0,5 do for b=0,5 do table.insert(palette, (r*51*65536) + (g*51*256) + (b*51)) end end end
  19.  
  20. -- State and Formatting
  21. local state = { mode = "TEXT", params = {}, curr = "", private = false }
  22. local style = { fg = 7, bg = 0, bold = false, inv = false }
  23. local lastFG, lastBG = -1, -1
  24. local buf = ""
  25.  
  26. --- RENDERING CORE ---
  27.  
  28. local function apply()
  29. local f, b = style.fg, style.bg
  30. local fg = (style.bold and f < 8) and palette[f+8] or palette[f]
  31. local bg = palette[b]
  32. if style.inv then fg, bg = bg, fg end
  33. if fg ~= lastFG then gpu.setForeground(fg or 0xFFFFFF); lastFG = fg end
  34. if bg ~= lastBG then gpu.setBackground(bg or 0x000000); lastBG = bg end
  35. end
  36.  
  37. local function flush() if #buf > 0 then io.write(buf); buf = "" end end
  38.  
  39. local function handleSGR()
  40. flush()
  41. if #state.params == 0 then table.insert(state.params, 0) end
  42. local i = 1
  43. while i <= #state.params do
  44. local p = state.params[i]
  45. if p == 0 then style.bold, style.inv, style.fg, style.bg = false, false, 7, 0
  46. elseif p == 1 then style.bold = true
  47. elseif p == 7 then style.inv = true
  48. elseif p == 38 or p == 48 then
  49. local val = state.params[i+2]; if p == 38 then style.fg = val else style.bg = val end
  50. i = i + 2
  51. elseif p >= 30 and p <= 37 then style.fg = p - 30
  52. elseif p >= 40 and p <= 47 then style.bg = p - 40
  53. elseif p >= 90 and p <= 97 then style.fg = p - 82
  54. end
  55. i = i + 1
  56. end
  57. apply()
  58. end
  59.  
  60. local function parse(char)
  61. if state.mode == "TEXT" then
  62. if char == "\27" then flush(); state.mode = "ESC"
  63. elseif char == "\r" then flush(); local _, y = term.getCursor(); term.setCursor(1, y)
  64. elseif char == "\n" then flush(); io.write("\n")
  65. elseif char == "\b" then flush(); local x, y = term.getCursor(); term.setCursor(math.max(1, x-1), y)
  66. else local b = char:byte(); buf = buf .. (cp437[b] or (b >= 32 and char or "")) end
  67. elseif state.mode == "ESC" then
  68. if char == "[" then state.mode = "CSI"; state.params = {}; state.curr = ""; state.private = false
  69. else state.mode = "TEXT" end
  70. elseif state.mode == "CSI" then
  71. if char == "?" then state.private = true
  72. elseif char:match("%d") then state.curr = state.curr .. char
  73. elseif char == ";" then table.insert(state.params, tonumber(state.curr) or 0); state.curr = ""
  74. else
  75. table.insert(state.params, tonumber(state.curr) or 0)
  76. local p1, p2 = state.params[1] or 1, state.params[2] or 1
  77. if char == "m" then handleSGR()
  78. elseif char == "H" or char == "f" then term.setCursor(p2, p1)
  79. elseif char == "A" then term.setCursor(term.getCursor(), math.max(1, select(2, term.getCursor()) - p1))
  80. elseif char == "B" then term.setCursor(term.getCursor(), select(2, term.getCursor()) + p1)
  81. elseif char == "C" then term.setCursor(term.getCursor() + p1, select(2, term.getCursor()))
  82. elseif char == "D" then term.setCursor(math.max(1, term.getCursor() - p1), select(2, term.getCursor()))
  83. elseif char == "J" then
  84. local w, h = gpu.getResolution(); local x, y = term.getCursor()
  85. if p1 == 2 then term.clear()
  86. elseif p1 == 0 then gpu.fill(x, y, w-x+1, 1, " "); if y < h then gpu.fill(1, y+1, w, h-y, " ") end end
  87. elseif char == "K" then
  88. local x, y = term.getCursor(); local w, _ = gpu.getResolution()
  89. if p1 == 0 then gpu.fill(x, y, w-x+1, 1, " ")
  90. elseif p1 == 2 then gpu.fill(1, y, w, 1, " ") end
  91. end
  92. state.mode = "TEXT"
  93. end
  94. end
  95. end
  96.  
  97. --- TELNET NEGOTIATION ---
  98.  
  99. local function handleTelnet(conn, cmd)
  100. local command = cmd:sub(2,2)
  101. local option = cmd:sub(3,3)
  102.  
  103. if command == DO then
  104. if option == TTYPE then conn.write(IAC .. WILL .. TTYPE)
  105. elseif option == BINARY then conn.write(IAC .. WILL .. BINARY)
  106. elseif option == NAWS then
  107. local w, h = gpu.getResolution()
  108. conn.write(IAC .. WILL .. NAWS .. IAC .. SB .. NAWS .. string.char(math.floor(w/256), w%256, math.floor(h/256), h%256) .. IAC .. SE)
  109. elseif option == TSPEED then conn.write(IAC .. WILL .. TSPEED)
  110. elseif option == ENVIRON then conn.write(IAC .. WILL .. ENVIRON)
  111. else conn.write(IAC .. WONT .. option) end
  112. elseif command == WILL then
  113. if option == BINARY or option == ECHO then conn.write(IAC .. DO .. option) end
  114. elseif command == AYT then
  115. conn.write(" ") -- Reply to AYT probe
  116. elseif command == SB then
  117. if option == TTYPE then
  118. conn.write(IAC .. SB .. TTYPE .. IS .. "xterm-256color" .. IAC .. SE)
  119. elseif option == TSPEED then
  120. conn.write(IAC .. SB .. TSPEED .. IS .. "38400,38400" .. IAC .. SE)
  121. end
  122. end
  123. end
  124.  
  125. --- MAIN CONNECTION ---
  126.  
  127. local function connect(host, port)
  128. local conn = internet.connect(host, port)
  129. if not conn then return end
  130. while not conn.finishConnect() do os.sleep(0.05) end
  131.  
  132. -- Force start the handshake
  133. conn.write(IAC .. WILL .. BINARY)
  134. conn.write(IAC .. WILL .. TTYPE)
  135. conn.write(IAC .. WILL .. NAWS)
  136. conn.write(IAC .. WILL .. TSPEED)
  137.  
  138. local telnetBuf = ""
  139. while true do
  140. local data = conn.read(8192)
  141. if data then
  142. telnetBuf = telnetBuf .. data
  143. while true do
  144. local s, e = telnetBuf:find(IAC .. "[^" .. IAC .. "][^" .. IAC .. "]")
  145. if not s then s, e = telnetBuf:find(IAC .. IAC) end
  146. if s then
  147. handleTelnet(conn, telnetBuf:sub(s, e))
  148. telnetBuf = telnetBuf:sub(1, s-1) .. telnetBuf:sub(e+1)
  149. else break end
  150. end
  151. for i=1, #telnetBuf do parse(telnetBuf:sub(i,i)) end
  152. flush(); io.flush(); telnetBuf = ""
  153. end
  154.  
  155. local ev, _, char, code = event.pull(0)
  156. if ev == "key_down" then
  157. if char == 0 and code == 46 then break -- Ctrl+C fallback
  158. elseif code == 28 then conn.write("\r\n")
  159. elseif code == 14 then conn.write("\b")
  160. elseif char > 0 then conn.write(string.char(char)) end
  161. elseif ev == "interrupted" then break end
  162. end
  163. conn.close()
  164. end
  165.  
  166. -- Start Script
  167. term.clear()
  168. print("OC-TELNET 3 | ULTRA-HANDSHAKE MODE")
  169. print("Target Host/Port:")
  170. local inp = io.read()
  171. if inp then
  172. local h, p = inp:match("(%S+)%s+(%d+)")
  173. if h and p then connect(h, tonumber(p)) end
  174. end
Advertisement
Add Comment
Please, Sign In to add comment