diff --git a/build.cfg b/build.cfg index 40b27bd..2f294b5 100755 --- a/build.cfg +++ b/build.cfg @@ -18,6 +18,8 @@ modules/net/net-ext.lua modules/applications/luash.lua modules/applications/genkernel.lua fwrap skex exec/skex2.lua +fwrap nshd exec/nshd.lua +fwrap nsh exec/nsh.lua modules/util/tape.lua modules/util/fs-automount.lua modules/setup.lua diff --git a/exec/nsh.lua b/exec/nsh.lua new file mode 100644 index 0000000..4981405 --- /dev/null +++ b/exec/nsh.lua @@ -0,0 +1,24 @@ +tA = {...} +local h,p = tA[1], tonumber(tA[2]) +local function nshcw(h,p) + while true do + local sI = os.getenv("sI") + local ev = {event.pull()} + if ev[1] == "net_msg" and ev[2] == h and ev[3] == p then + write(ev[4]) + elseif ev[1] == "key" and ev[2] == sI then + net.send(h,p,string.char(ev[3],ev[4])) + end + end +end +net.send(h,p or 23,"initnsh2") +local bt = os.time() +local tp,src,port,msg +repeat + tp, src, port, msg = event.pull() +until (tp == "net_msg" and src == h and port == p) or os.time() > bt+16 +if tp == "net_msg" then + nshcw(h,tonumber(msg)) +else + print("timeout.") +end diff --git a/exec/nshd.lua b/exec/nshd.lua new file mode 100644 index 0000000..0dad329 --- /dev/null +++ b/exec/nshd.lua @@ -0,0 +1,33 @@ +local tA = {...} +local nport = tA[1] or "23" +function nshdw(h,p,s) + spawn("nshdw["..tostring(s).."]",function() print(pcall(function() + while true do + local sI = os.getenv("sI") + local ev = {event.pull()} + if ev[1] == "net_msg" and ev[2] == h and ev[3] == p then + event.push("key",sI,string.byte(ev[4]:sub(1,1)),string.byte(ev[4]:sub(2,2))) + elseif ev[1] == "display" and ev[2] == sI then + if ev[3]:len() < 1024 then + net.send(h,p,ev[3]) + else + for i = 1, ev[3]:len(), 1024 do + net.send(h,p,ev[3]:sub(i,i+1023)) + end + end + end + end + end)) end,{sI=s}) +end +spawn("nshd["..tostring(nport).."]",function() print(pcall(function() + while true do + local _, src, port, msg = event.pull("net_msg") + if port == nport and msg == "initnsh2" then + local cport = math.random(65535-1024,65535) + local ns = "nsh-"..tostring(cport) + net.send(src,port,tostring(cport)) + luash(ns) + nshdw(src,cport,ns) + end + end +end)) end) diff --git a/modules/applications/luash.lua b/modules/applications/luash.lua index 80412c6..60dcbd7 100644 --- a/modules/applications/luash.lua +++ b/modules/applications/luash.lua @@ -41,5 +41,5 @@ spawn("lua shell",function() print(table.unpack(r)) end end -end,si) +end,{sI=si}) end