nsh improvements, now supports closing of sessions

This commit is contained in:
Izaya 2017-09-05 19:09:07 +10:00
parent fa3338589f
commit 3ded060771
2 changed files with 12 additions and 6 deletions

View File

@ -4,8 +4,10 @@ local function nshcw(h,p)
while true do while true do
local sI = os.getenv("sI") local sI = os.getenv("sI")
local ev = {event.pull()} local ev = {event.pull()}
if ev[1] == "net_msg" and ev[2] == h and ev[3] == p then if ev[1] == "net_msg" and ev[2] == h and ev[3] == p and ev[4]:sub(1,1) == "d" then
write(ev[4]) write(ev[4]:sub(2))
elseif ev[1] == "net_msg" and ev[2] == h and ev[3] == p and ev[4]:sub(1,1) == "\27" then
break
elseif ev[1] == "key" and ev[2] == sI then elseif ev[1] == "key" and ev[2] == sI then
net.send(h,p,string.char(ev[3],ev[4])) net.send(h,p,string.char(ev[3],ev[4]))
end end

View File

@ -1,30 +1,34 @@
local tA = {...} local tA = {...}
local nport = tA[1] or "23" local nport = tA[1] or "23"
function nshdw(h,p,s) function nshdw(h,p,s)
spawn("nshdw["..tostring(s).."]",function() print(pcall(function() spawn("nshdw["..tostring(s).."]",function() log(xpcall(function()
while true do while true do
local sI = os.getenv("sI") local sI = os.getenv("sI")
local ev = {event.pull()} local ev = {event.pull()}
if ev[1] == "net_msg" and ev[2] == h and ev[3] == p then 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))) event.push("key",sI,string.byte(ev[4]:sub(1,1)),string.byte(ev[4]:sub(2,2)))
elseif ev[1] == "key" and ev[2] == sI and ev[3] == 3 and ev[4] == 46 then
break
elseif ev[1] == "display" and ev[2] == sI then elseif ev[1] == "display" and ev[2] == sI then
if ev[3]:len() < 1024 then if ev[3]:len() < 1024 then
net.send(h,p,ev[3]) net.send(h,p,"d"..ev[3])
else else
for i = 1, ev[3]:len(), 1024 do for i = 1, ev[3]:len(), 1024 do
net.send(h,p,ev[3]:sub(i,i+1023)) net.send(h,p,"d"..ev[3]:sub(i,i+1023))
end end
end end
end end
end end
net.send(h,p,"\27")
end)) end,{sI=s}) end)) end,{sI=s})
end end
spawn("nshd["..tostring(nport).."]",function() print(pcall(function() spawn("nshd["..tostring(nport).."]",function() log(pcall(function()
while true do while true do
local _, src, port, msg = event.pull("net_msg") local _, src, port, msg = event.pull("net_msg")
if port == nport and msg == "initnsh2" then if port == nport and msg == "initnsh2" then
local cport = math.random(65535-1024,65535) local cport = math.random(65535-1024,65535)
local ns = "nsh-"..tostring(cport) local ns = "nsh-"..tostring(cport)
log("[nshd] Starting nsh session "..tostring(cport).." for "..src)
net.send(src,port,tostring(cport)) net.send(src,port,tostring(cport))
luash(ns) luash(ns)
nshdw(src,cport,ns) nshdw(src,cport,ns)