OC-PsychOS/exec/nshd.lua

38 lines
1.1 KiB
Lua

local tA = {...}
local nport = tA[1] or "23"
function nshdw(h,p,s)
spawn("nshdw["..tostring(s).."]",function() log(xpcall(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] == "key" and ev[2] == sI and ev[3] == 3 and ev[4] == 46 then
break
elseif ev[1] == "display" and ev[2] == sI then
if ev[3]:len() < 1024 then
net.send(h,p,"d"..ev[3])
else
for i = 1, ev[3]:len(), 1024 do
net.send(h,p,"d"..ev[3]:sub(i,i+1023))
end
end
end
end
net.send(h,p,"\27")
end)) end,{sI=s})
end
spawn("nshd["..tostring(nport).."]",function() log(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)
log("[nshd] Starting nsh session "..tostring(cport).." for "..src)
net.send(src,port,tostring(cport))
luash(ns)
nshdw(src,cport,ns)
end
end
end)) end)