opus/sys/network/telnet.lua

87 lines
1.8 KiB
Lua
Raw Normal View History

local Event = require('event')
2016-12-11 19:24:52 +00:00
local Socket = require('socket')
local Util = require('util')
2016-12-11 19:24:52 +00:00
2017-10-25 03:01:40 +00:00
local multishell = _ENV.multishell
local os = _G.os
local term = _G.term
2017-07-28 23:01:59 +00:00
2017-10-25 03:01:40 +00:00
local function telnetHost(socket)
_G.requireInjector()
2017-07-28 23:01:59 +00:00
local Event = require('event')
2016-12-11 19:24:52 +00:00
local methods = { 'clear', 'clearLine', 'setCursorPos', 'write', 'blit',
'setTextColor', 'setTextColour', 'setBackgroundColor',
'setBackgroundColour', 'scroll', 'setCursorBlink', }
2017-08-03 05:46:39 +00:00
local termInfo = socket:read(5)
if not termInfo then
2018-01-06 11:07:49 +00:00
_G.printError('read failed')
2017-08-03 05:46:39 +00:00
return
end
2016-12-11 19:24:52 +00:00
socket.term = term.current()
local oldWindow = Util.shallowCopy(socket.term)
for _,k in pairs(methods) do
socket.term[k] = function(...)
2017-07-28 23:01:59 +00:00
2016-12-11 19:24:52 +00:00
if not socket.queue then
socket.queue = { }
2017-07-28 23:01:59 +00:00
Event.onTimeout(0, function()
socket:write(socket.queue)
socket.queue = nil
end)
2016-12-11 19:24:52 +00:00
end
2017-07-28 23:01:59 +00:00
2016-12-11 19:24:52 +00:00
table.insert(socket.queue, {
f = k,
args = { ... },
})
oldWindow[k](...)
end
end
socket.term.getSize = function()
return termInfo.width, termInfo.height
end
2017-07-28 23:01:59 +00:00
local shellThread = Event.addRoutine(function()
2017-10-25 03:01:40 +00:00
os.run(_ENV, 'sys/apps/shell')
2017-07-28 23:01:59 +00:00
Event.exitPullEvents()
2016-12-11 19:24:52 +00:00
end)
2017-07-28 23:01:59 +00:00
Event.addRoutine(function()
2016-12-11 19:24:52 +00:00
while true do
local data = socket:read()
if not data then
2017-07-28 23:01:59 +00:00
Event.exitPullEvents()
2016-12-11 19:24:52 +00:00
break
end
2017-07-28 23:01:59 +00:00
shellThread:resume(table.unpack(data))
2016-12-11 19:24:52 +00:00
end
end)
2017-07-28 23:01:59 +00:00
Event.pullEvents()
2016-12-11 19:24:52 +00:00
socket:close()
2017-07-28 23:01:59 +00:00
shellThread:terminate()
2016-12-11 19:24:52 +00:00
end
2017-08-03 05:46:39 +00:00
Event.addRoutine(function()
2016-12-11 19:24:52 +00:00
print('telnet: listening on port 23')
while true do
local socket = Socket.server(23)
print('telnet: connection from ' .. socket.dhost)
2017-08-03 05:46:39 +00:00
multishell.openTab({
fn = telnetHost,
args = { socket },
title = 'Telnet Client',
hidden = true,
})
2016-12-11 19:24:52 +00:00
end
end)