Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local term = require("term")
- local unicode = require("unicode")
- local serialization = require("serialization")
- local shell = require("shell")
- local fs = require("filesystem")
- local gpu = component.gpu
- local keyboard = require("keyboard")
- local internet = component.list("internet")()
- local inet = component.proxy(internet)
- if not inet then
- error("Internet Card not found. Please install an Internet Card.")
- end
- local IAC, DONT, DO, WONT, WILL = string.char(255), string.char(254), string.char(253), string.char(252), string.char(251)
- local SB, SE = string.char(250), string.char(240)
- local NOP, DM, BRK, IP, AO, AYT, EC, EL, GA = string.char(241), string.char(242), string.char(243), string.char(244), string.char(245), string.char(246), string.char(247), string.char(248), string.char(249)
- local ECHO, SUPPRESS_GO_AHEAD, STATUS, TIMING_MARK = string.char(1), string.char(3), string.char(5), string.char(6)
- local TERMINAL_TYPE, WINDOW_SIZE, TERMINAL_SPEED = string.char(24), string.char(31), string.char(32)
- local REMOTE_FLOW_CONTROL, LINEMODE, ENVIRONMENT = string.char(33), string.char(34), string.char(36)
- local config = {
- default_host = "localhost",
- default_port = 23,
- log_file = "/home/telnet.log",
- color_enabled = true
- }
- local function load_config()
- local config_path = "/home/telnet_config.lua"
- if fs.exists(config_path) then
- local file = io.open(config_path, "r")
- local content = file:read("*all")
- file:close()
- local loaded_config = serialization.unserialize(content)
- for k, v in pairs(loaded_config) do
- config[k] = v
- end
- end
- end
- local function save_config()
- local config_path = "/home/telnet_config.lua"
- local file = io.open(config_path, "w")
- file:write(serialization.serialize(config))
- file:close()
- end
- local function log(message)
- local file = io.open(config.log_file, "a")
- file:write(os.date("[%Y-%m-%d %H:%M:%S] ") .. message .. "\n")
- file:close()
- end
- local function send_command(connection, cmd, option)
- connection.write(IAC .. cmd .. option)
- end
- local function handle_option(connection, cmd, option)
- if cmd == DO then
- if option == TERMINAL_TYPE then
- send_command(connection, WILL, TERMINAL_TYPE)
- connection.write(IAC .. SB .. TERMINAL_TYPE .. string.char(0) .. "ANSI" .. IAC .. SE)
- elseif option == WINDOW_SIZE then
- send_command(connection, WILL, WINDOW_SIZE)
- local width, height = term.getViewport()
- connection.write(IAC .. SB .. WINDOW_SIZE .. string.char(0) .. string.char(width) .. string.char(0) .. string.char(height) .. IAC .. SE)
- else
- send_command(connection, WONT, option)
- end
- elseif cmd == WILL then
- if option == ECHO or option == SUPPRESS_GO_AHEAD then
- send_command(connection, DO, option)
- else
- send_command(connection, DONT, option)
- end
- end
- end
- local function handleANSI(str)
- local i = 1
- while i <= #str do
- local c = str:sub(i, i)
- if c == "\27" then
- local seq, cmd = str:match("\27%[([%d;]*)([A-Za-z])", i)
- if seq and cmd then
- local params = {}
- for param in seq:gmatch("%d+") do
- table.insert(params, tonumber(param))
- end
- if #params == 0 then
- params = {1}
- end
- if cmd == "H" or cmd == "f" then
- local x, y = params[2] or 1, params[1] or 1
- term.setCursor(x, y)
- elseif cmd == "J" then
- if params[1] == 2 then
- term.clear()
- elseif params[1] == 0 then
- term.clearLine()
- end
- elseif cmd == "K" then
- term.clearLine()
- elseif cmd == "A" then
- local x, y = term.getCursor()
- term.setCursor(x, y - (params[1] or 1))
- elseif cmd == "B" then
- local x, y = term.getCursor()
- term.setCursor(x, y + (params[1] or 1))
- elseif cmd == "C" then
- local x, y = term.getCursor()
- term.setCursor(x + (params[1] or 1), y)
- elseif cmd == "D" then
- local x, y = term.getCursor()
- term.setCursor(x - (params[1] or 1), y)
- elseif cmd == "m" then
- for _, param in ipairs(params) do
- if param == 0 then
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0x000000)
- elseif param == 1 then
- elseif param >= 30 and param <= 37 then
- local colors = {
- 0x000000, 0xFF0000, 0x00FF00, 0xFFFF00,
- 0x0000FF, 0xFF00FF, 0x00FFFF, 0xFFFFFF
- }
- gpu.setForeground(colors[param - 29])
- elseif param >= 40 and param <= 47 then
- local colors = {
- 0x000000, 0xFF0000, 0x00FF00, 0xFFFF00,
- 0x0000FF, 0xFF00FF, 0x00FFFF, 0xFFFFFF
- }
- gpu.setBackground(colors[param - 39])
- end
- end
- end
- i = i + #seq + 3
- else
- io.write(c)
- i = i + 1
- end
- else
- io.write(c)
- i = i + 1
- end
- end
- end
- local function telnet(host, port)
- local connection = inet.connect(host, port)
- if not connection then
- error("Failed to connect to " .. host .. ":" .. port)
- end
- print("Connected to " .. host .. ":" .. port)
- log("Connected to " .. host .. ":" .. port)
- local buffer = ""
- local in_iac = false
- local cmd, option
- while true do
- local data = connection.read(1024)
- if data then
- for i = 1, #data do
- local char = data:sub(i, i)
- if in_iac then
- if not cmd then
- cmd = char
- elseif not option then
- option = char
- handle_option(connection, cmd, option)
- in_iac, cmd, option = false, nil, nil
- end
- elseif char == IAC then
- in_iac = true
- else
- buffer = buffer .. char
- end
- end
- if #buffer > 0 then
- if config.color_enabled then
- handleANSI(buffer)
- else
- io.write(buffer)
- end
- log(buffer)
- buffer = ""
- end
- end
- local _, _, char, code = event.pull(0.05, "key_down")
- if char then
- local utf8char = unicode.char(char)
- connection.write(utf8char)
- if not connection.finishConnect() then
- print("\nConnection closed")
- log("Connection closed")
- break
- end
- end
- end
- connection.close()
- end
- local function config_menu()
- print("Configuration Menu")
- io.write("Enter default host (current: " .. config.default_host .. "): ")
- local new_host = io.read()
- if new_host ~= "" then config.default_host = new_host end
- io.write("Enter default port (current: " .. config.default_port .. "): ")
- local new_port = tonumber(io.read())
- if new_port then config.default_port = new_port end
- io.write("Enable color? (y/n, current: " .. (config.color_enabled and "yes" or "no") .. "): ")
- local color_choice = io.read():lower()
- if color_choice == "y" or color_choice == "n" then
- config.color_enabled = (color_choice == "y")
- end
- save_config()
- print("Configuration saved")
- end
- local function connect_to_server()
- io.write("Enter host (or press Enter for default): ")
- local host = io.read()
- host = host ~= "" and host or config.default_host
- io.write("Enter port (or press Enter for default): ")
- local port_input = io.read()
- local port = tonumber(port_input) or config.default_port
- telnet(host, port)
- end
- local function display_logo()
- print([[
- U ___ u ____ _____ U _____ u _ _ _ U _____ u _____
- \/"_ \/U /"___| |_ " _| \| ___"|/ |"| | \ |"| \| ___"|/ |_ " _|
- | | | |\| | u U u | | | _|" U | | u <| \| |> | _|" | |
- .-,_| |_| | | |/__ /___\ /| |\ | |___ \| |/__U| |\ |u | |___ /| |\
- \_)-\___/ \____|__"__|u |_|U |_____| |_____||_| \_| |_____| u |_|U
- \\ _// \\ _// \\_ << >> // \\ || \\,-.<< >> _// \\_
- (__) (__)(__) (__) (__)(__) (__)(_")("_)(_") (_/(__) (__)(__) (__)
- ____
- |___"\
- U __) |
- \/ __/ \
- |_____|u
- << //
- (__)(__)
- ]])
- end
- local function main_menu()
- while true do
- print("\nOC-TELNET 2 Coded by nonogamer9!")
- print("1. Connect to server")
- print("2. Configure settings")
- print("3. Exit")
- io.write("Choose an option: ")
- local choice = io.read()
- if choice == "1" then
- connect_to_server()
- elseif choice == "2" then
- config_menu()
- elseif choice == "3" then
- break
- else
- print("Invalid option. Please try again.")
- end
- end
- end
- display_logo()
- load_config()
- main_menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement