2017-09-05 06:09:31 +00:00
|
|
|
local Event = require('event')
|
2016-12-11 19:24:52 +00:00
|
|
|
local Socket = require('socket')
|
2017-09-05 06:09:31 +00:00
|
|
|
local Util = require('util')
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2018-01-14 04:40:53 +00:00
|
|
|
local kernel = _G.kernel
|
|
|
|
local term = _G.term
|
|
|
|
local window = _G.window
|
2017-07-28 23:01:59 +00:00
|
|
|
|
2017-10-25 03:01:40 +00:00
|
|
|
local function telnetHost(socket)
|
2018-01-24 22:39:38 +00:00
|
|
|
local methods = { 'clear', 'clearLine', 'setCursorPos', 'write', 'blit',
|
|
|
|
'setTextColor', 'setTextColour', 'setBackgroundColor',
|
|
|
|
'setBackgroundColour', 'scroll', 'setCursorBlink', }
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
local termInfo = socket:read(5)
|
|
|
|
if not termInfo then
|
|
|
|
_G.printError('read failed')
|
|
|
|
return
|
|
|
|
end
|
2017-08-03 05:46:39 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
local win = window.create(_G.device.terminal, 1, 1, termInfo.width, termInfo.height, false)
|
|
|
|
win.setCursorPos(table.unpack(termInfo.pos))
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
for _,k in pairs(methods) do
|
|
|
|
local fn = win[k]
|
|
|
|
win[k] = function(...)
|
2017-07-28 23:01:59 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
if not socket.queue then
|
|
|
|
socket.queue = { }
|
|
|
|
Event.onTimeout(0, function()
|
|
|
|
socket:write(socket.queue)
|
|
|
|
socket.queue = nil
|
|
|
|
end)
|
|
|
|
end
|
2017-07-28 23:01:59 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
table.insert(socket.queue, {
|
|
|
|
f = k,
|
|
|
|
args = { ... },
|
|
|
|
})
|
|
|
|
fn(...)
|
|
|
|
end
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
local shellThread = kernel.run({
|
|
|
|
terminal = win,
|
|
|
|
window = win,
|
|
|
|
title = 'Telnet client',
|
|
|
|
hidden = true,
|
|
|
|
co = coroutine.create(function()
|
2019-02-12 22:03:50 +00:00
|
|
|
Util.run(_ENV, 'sys/apps/shell.lua', table.unpack(termInfo.program))
|
2018-01-24 22:39:38 +00:00
|
|
|
if socket.queue then
|
|
|
|
socket:write(socket.queue)
|
|
|
|
end
|
|
|
|
socket:close()
|
|
|
|
end)
|
|
|
|
})
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
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)
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
2017-08-03 05:46:39 +00:00
|
|
|
Event.addRoutine(function()
|
2018-01-24 22:39:38 +00:00
|
|
|
print('telnet: listening on port 23')
|
|
|
|
while true do
|
|
|
|
local socket = Socket.server(23)
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
print('telnet: connection from ' .. socket.dhost)
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
Event.addRoutine(function()
|
|
|
|
telnetHost(socket)
|
|
|
|
end)
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
end)
|