From e5dd91e1673efaeed021278ae6f6ad6acf45821b Mon Sep 17 00:00:00 2001 From: XeonSquared Date: Wed, 15 Apr 2020 13:33:27 +1000 Subject: [PATCH] updated termsrv to basically work with the new rc system, added nc to netutil --- lib/netutil.lua | 22 ++++++++++++++++ service/termsrv.lua | 63 ++++++++++++++++++++++++--------------------- 2 files changed, 55 insertions(+), 30 deletions(-) diff --git a/lib/netutil.lua b/lib/netutil.lua index b572255..7d623fd 100644 --- a/lib/netutil.lua +++ b/lib/netutil.lua @@ -46,4 +46,26 @@ function netutil.ping(addr,times,timeout, silent) -- Request acknowledgment from return success > 0, success, fail, avg end +function netutil.nc(host,port) + port = port or 22 + local socket = minitel.open(host,port) + if not socket then return false end + local b = "" + os.spawn(function() + repeat + local b = socket:read("*a") + if b and b:len() > 0 then + io.write(b) + end + coroutine.yield() + until socket.state ~= "open" + end) + repeat + local b = io.read() + if b and b:len() > 0 then + socket:write(b.."\n") + end + until socket.state ~= "open" +end + return netutil diff --git a/service/termsrv.lua b/service/termsrv.lua index 59d5613..2799c27 100644 --- a/service/termsrv.lua +++ b/service/termsrv.lua @@ -1,10 +1,7 @@ -print(pcall(function() local minitel = require "minitel" +local shell = require "shell" local port = 22 ---local logfile = "/boot/termsrv.log" -local oout = io.output() -local pname = os.taskInfo(os.pid()).name local function sread(self, len) while true do local d=self.sock:read(len) @@ -31,30 +28,36 @@ local function sflush(self) self.wb = "" self.flushing = false end -while true do - local sock = minitel.listen(port) - print(string.format("Connection from %s:%d",sock.addr,sock.port)) - os.spawn(function() _G.worked = {pcall(function() - local fh = {} - fh.sock = sock - fh.read = sread - fh.write = swrite - fh.close = sclose - fh.flush = sflush - fh.wb = "" - io.input(fh) - io.output(fh) - fh:write(string.format("Connected to %s on port %d\n",os.getenv("HOSTNAME"),sock.port)) - local pid = os.spawnfile("/boot/exec/shell.lua") - repeat - coroutine.yield() - if fh.wb:len() > 0 then - fh:flush() - end - until sock.state ~= "open" or not os.taskInfo(pid) - sock:close() - os.kill(pid) - oout:write(string.format("Session %s:%d ended",sock.addr,sock.port)) - end)} end,string.format(pname.." [%s:%d]",sock.addr,sock.port)) + +function start() + return os.spawn(function() + local oout = io.output() + local pname = os.taskInfo(os.pid()).name + while true do + local sock = minitel.listen(port) + print(string.format("Connection from %s:%d",sock.addr,sock.port)) + os.spawn(function() _G.worked = {pcall(function() + local fh = {} + fh.sock = sock + fh.read = sread + fh.write = swrite + fh.close = sclose + fh.flush = sflush + fh.wb = "" + io.input(fh) + io.output(fh) + print(_OSVERSION.." - "..tostring(math.floor(computer.totalMemory()/1024)).."K RAM") + local pid = os.spawn(shell.interactive,string.format(pname.." shell [%s:%d]",sock.addr,sock.port)) + repeat + coroutine.yield() + if fh.wb:len() > 0 then + fh:flush() + end + until sock.state ~= "open" or not os.taskInfo(pid) + sock:close() + os.kill(pid) + oout:write(string.format("Session %s:%d ended",sock.addr,sock.port)) + end)} end,string.format(pname.." [%s:%d]",sock.addr,sock.port)) + end + end,"termsrv") end -end))