updated nc and termsrv

This commit is contained in:
Izaya 2019-12-16 17:09:56 +11:00
parent b408cfe27b
commit 1cb85bd5e8
2 changed files with 56 additions and 38 deletions

View File

@ -1,16 +1,23 @@
local minitel = require "minitel" local minitel = require "minitel"
local tA = {...} local tA = {...}
host, port = tA[1], tA[2] host, port = tA[1], tA[2] or 22
local socket = minitel.open(host,port) local socket = minitel.open(host,port)
if not socket then return false end if not socket then return false end
local b = "" local b = ""
os.spawn(function()
repeat repeat
io.write(socket:read("*a")) local b = socket:read("*a")
if b and b:len() > 0 then
io.write(b)
end
coroutine.yield() coroutine.yield()
b = io.read(nil,true) or "" until socket.state ~= "open"
if b:len() > 0 then end)
repeat
local b = io.read()
if b and b:len() > 0 then
socket:write(b.."\n") socket:write(b.."\n")
end end
until socket.state ~= "open" until socket.state ~= "open"

View File

@ -3,47 +3,58 @@ local minitel = require "minitel"
local port = 22 local port = 22
--local logfile = "/boot/termsrv.log" --local logfile = "/boot/termsrv.log"
if logfile then local oout = io.output()
local log = io.open(logfile,"a") local pname = os.taskInfo(os.pid()).name
os.setenv("t",log.fd) local function sread(self, len)
while true do
local d=self.sock:read(len)
if d then
return d
end end
local function nextvty() coroutine.yield()
local vtyn = -1 end
repeat end
vtyn = vtyn + 1 local function swrite(self, data)
until not fs.exists("/iofs/vty"..tostring(vtyn)) while self.flushing do
return "vty"..tostring(vtyn) coroutine.yield()
end
if data and data:len() > 0 then
self.wb = self.wb .. (data or "")
end
end
local function sclose(self)
self.sock:close()
end
local function sflush(self)
self.flushing = true
self.sock:write(self.wb)
self.wb = ""
self.flushing = false
end end
while true do while true do
local sock = minitel.listen(port) local sock = minitel.listen(port)
print(string.format("[%s] Connection from %s:%d",os.date("%Y-%m-%d %H:%M"),sock.addr,sock.port)) print(string.format("Connection from %s:%d",sock.addr,sock.port))
os.spawn(function() _G.worked = {pcall(function() os.spawn(function() _G.worked = {pcall(function()
local vtyf = nextvty() local fh = {}
local fdo = {} fh.sock = sock
function fdo.read(d) fh.read = sread
return sock:read(d) fh.write = swrite
end fh.close = sclose
function fdo.write(d) fh.flush = sflush
return sock:write(d) fh.wb = ""
end io.input(fh)
function fdo.close() io.output(fh)
sock:close() fh:write(string.format("Connected to %s on port %d\n",os.getenv("HOSTNAME"),sock.port))
end local pid = os.spawnfile("/boot/exec/shell.lua")
iofs.register(vtyf,function() return fdo.read, fdo.write, fdo.close end)
local f = io.open("/iofs/"..vtyf,"rw")
print(vtyf, f.fd)
local ot = os.getenv("t")
os.setenv("t",f.fd)
sock:write(string.format("Connected to %s on port %d\n",computer.address():sub(1,8),sock.port))
local pid = spawnfile("/boot/exec/shell.lua",string.format("shell [%s:%d]",sock.addr,sock.port))
repeat repeat
coroutine.yield() coroutine.yield()
until sock.state ~= "open" or not tTasks[pid] if fh.wb:len() > 0 then
f:close() fh:flush()
end
until sock.state ~= "open" or not os.taskInfo(pid)
sock:close() sock:close()
os.kill(pid) os.kill(pid)
os.setenv("t",ot) oout:write(string.format("Session %s:%d ended",sock.addr,sock.port))
print(string.format("Session %s:%d ended",sock.addr,sock.port)) end)} end,string.format(pname.." [%s:%d]",sock.addr,sock.port))
end)} end,string.format("remote login [%s:%d]",sock.addr,sock.port))
end end
end)) end))