2019-06-28 17:50:02 +00:00
|
|
|
local Event = require('opus.event')
|
|
|
|
local Socket = require('opus.socket')
|
|
|
|
local Terminal = require('opus.terminal')
|
|
|
|
local Util = require('opus.util')
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2018-01-10 21:46:37 +00:00
|
|
|
local multishell = _ENV.multishell
|
|
|
|
local os = _G.os
|
|
|
|
local read = _G.read
|
2019-04-11 12:41:23 +00:00
|
|
|
local shell = _ENV.shell
|
2018-01-10 21:46:37 +00:00
|
|
|
local term = _G.term
|
|
|
|
|
2019-06-30 03:43:21 +00:00
|
|
|
local args, options = Util.parse(...)
|
2018-01-10 21:46:37 +00:00
|
|
|
|
2018-01-13 20:17:26 +00:00
|
|
|
local remoteId = tonumber(table.remove(args, 1) or '')
|
2018-01-10 21:46:37 +00:00
|
|
|
if not remoteId then
|
2018-01-24 22:39:38 +00:00
|
|
|
print('Enter host ID')
|
|
|
|
remoteId = tonumber(read())
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if not remoteId then
|
2018-10-15 20:05:43 +00:00
|
|
|
error('Syntax: telnet ID [PROGRAM] [ARGS]')
|
2018-01-10 21:46:37 +00:00
|
|
|
end
|
|
|
|
|
2018-10-15 20:05:43 +00:00
|
|
|
if multishell then
|
2019-06-30 03:43:21 +00:00
|
|
|
multishell.setTitle(multishell.getCurrent(),
|
|
|
|
(options.s and 'Secure ' or 'Telnet ') .. remoteId)
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
2019-04-08 13:30:47 +00:00
|
|
|
local socket, msg, reason
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2019-04-08 13:30:47 +00:00
|
|
|
while true do
|
2019-06-30 03:43:21 +00:00
|
|
|
socket, msg, reason = Socket.connect(remoteId, options.s and 22 or 23)
|
2019-04-08 13:30:47 +00:00
|
|
|
|
|
|
|
if socket then
|
|
|
|
break
|
|
|
|
elseif reason ~= 'NOTRUST' then
|
|
|
|
error(msg)
|
|
|
|
end
|
|
|
|
|
|
|
|
local s, m = shell.run('trust ' .. remoteId)
|
|
|
|
if not s then
|
|
|
|
error(m)
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local ct = Util.shallowCopy(term.current())
|
|
|
|
if not ct.isColor() then
|
2018-01-24 22:39:38 +00:00
|
|
|
Terminal.toGrayscale(ct)
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local w, h = ct.getSize()
|
|
|
|
socket:write({
|
2018-01-24 22:39:38 +00:00
|
|
|
width = w,
|
|
|
|
height = h,
|
|
|
|
isColor = ct.isColor(),
|
|
|
|
program = args,
|
|
|
|
pos = { ct.getCursorPos() },
|
2016-12-11 19:24:52 +00:00
|
|
|
})
|
|
|
|
|
2017-07-24 02:37:07 +00:00
|
|
|
Event.addRoutine(function()
|
2018-01-24 22:39:38 +00:00
|
|
|
while true do
|
|
|
|
local data = socket:read()
|
|
|
|
if not data then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
for _,v in ipairs(data) do
|
|
|
|
ct[v.f](table.unpack(v.args))
|
|
|
|
end
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
end)
|
|
|
|
|
2018-01-14 04:40:53 +00:00
|
|
|
--ct.clear()
|
|
|
|
--ct.setCursorPos(1, 1)
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2018-01-13 20:17:26 +00:00
|
|
|
local filter = Util.transpose {
|
2018-01-24 22:39:38 +00:00
|
|
|
'char', 'paste', 'key', 'key_up', 'terminate',
|
|
|
|
'mouse_scroll', 'mouse_click', 'mouse_drag', 'mouse_up',
|
2018-01-13 20:17:26 +00:00
|
|
|
}
|
2017-05-10 10:11:25 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
while true do
|
2018-01-24 22:39:38 +00:00
|
|
|
local e = { os.pullEventRaw() }
|
|
|
|
local event = e[1]
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
if filter[event] then
|
|
|
|
socket:write(e)
|
|
|
|
else
|
|
|
|
Event.processEvent(e)
|
|
|
|
end
|
2017-08-09 14:19:00 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
if not socket.connected then
|
2018-01-14 04:40:53 +00:00
|
|
|
-- print()
|
|
|
|
-- print('Connection lost')
|
|
|
|
-- print('Press enter to exit')
|
|
|
|
-- pcall(read)
|
2018-01-24 22:39:38 +00:00
|
|
|
break
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|