1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-16 21:54:49 +00:00
opus/sys/apps/network/telnet.lua
kepler155c@gmail.com 8279c1ae12 environments, error messages, and stack traces, oh my!
Changed the way processes are launched.
multishell.openTab and kernel.run now accept the current environment as a parameter.
That new process inherits a copy of the passed environment.
Reduces complexity as the calling process is not required to create a suitable env.
Stack traces have been greatly improved and now include the stack for coroutines that error.
2020-05-11 17:25:58 -06:00

104 lines
2.3 KiB
Lua

local Alt = require('opus.alternate')
local Event = require('opus.event')
local Socket = require('opus.socket')
local Util = require('opus.util')
local kernel = _G.kernel
local term = _G.term
local window = _G.window
local function telnetHost(socket, mode)
local methods = { 'clear', 'clearLine', 'setCursorPos', 'write', 'blit',
'setTextColor', 'setTextColour', 'setBackgroundColor',
'setBackgroundColour', 'scroll', 'setCursorBlink', }
local termInfo = socket:read(5)
if not termInfo then
_G.printError('read failed')
return
end
local win = window.create(_G.device.terminal, 1, 1, termInfo.width, termInfo.height, false)
win.setCursorPos(table.unpack(termInfo.pos))
for _,k in pairs(methods) do
local fn = win[k]
win[k] = function(...)
if not socket.queue then
socket.queue = { }
Event.onTimeout(0, function()
socket:write(socket.queue)
socket.queue = nil
end)
end
table.insert(socket.queue, {
f = k,
args = { ... },
})
fn(...)
end
end
local shellThread = kernel.run(_ENV, {
window = win,
title = mode .. ' client',
hidden = true,
fn = function()
Util.run(kernel.makeEnv(_ENV), Alt.get('shell'), table.unpack(termInfo.program))
if socket.queue then
socket:write(socket.queue)
end
socket:close()
end,
})
Event.addRoutine(function()
while true do
local data = socket:read()
if not data then
shellThread:resume('terminate')
break
end
local previousTerm = term.current()
shellThread:resume(table.unpack(data))
term.redirect(previousTerm)
end
end)
end
Event.addRoutine(function()
print('ssh: listening on port 22')
while true do
local socket = Socket.server(22, { ENCRYPT = true })
print('ssh: connection from ' .. socket.dhost)
Event.addRoutine(function()
local s, m = pcall(telnetHost, socket, 'SSH')
if not s and m then
print('ssh error')
_G.printError(m)
end
end)
end
end)
Event.addRoutine(function()
print('telnet: listening on port 23')
while true do
local socket = Socket.server(23)
print('telnet: connection from ' .. socket.dhost)
Event.addRoutine(function()
local s, m = pcall(telnetHost, socket, 'Telnet')
if not s and m then
print('Telnet error')
_G.printError(m)
end
end)
end
end)