format and installer branches
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
--[[
|
||||
Allow sharing of local peripherals.
|
||||
Allow sharing of local peripherals.
|
||||
]]--
|
||||
|
||||
local Event = require('event')
|
||||
@@ -7,76 +7,76 @@ local Peripheral = require('peripheral')
|
||||
local Socket = require('socket')
|
||||
|
||||
Event.addRoutine(function()
|
||||
print('peripheral: listening on port 189')
|
||||
while true do
|
||||
local socket = Socket.server(189)
|
||||
print('peripheral: listening on port 189')
|
||||
while true do
|
||||
local socket = Socket.server(189)
|
||||
|
||||
print('peripheral: connection from ' .. socket.dhost)
|
||||
print('peripheral: connection from ' .. socket.dhost)
|
||||
|
||||
Event.addRoutine(function()
|
||||
local uri = socket:read(2)
|
||||
if uri then
|
||||
local peripheral = Peripheral.lookup(uri)
|
||||
Event.addRoutine(function()
|
||||
local uri = socket:read(2)
|
||||
if uri then
|
||||
local peripheral = Peripheral.lookup(uri)
|
||||
|
||||
-- only 1 proxy of this device can happen at one time
|
||||
-- need to prevent multiple shares
|
||||
if not peripheral then
|
||||
print('peripheral: invalid peripheral ' .. uri)
|
||||
socket:write('Invalid peripheral: ' .. uri)
|
||||
else
|
||||
print('peripheral: proxing ' .. uri)
|
||||
local proxy = {
|
||||
methods = { }
|
||||
}
|
||||
if not peripheral then
|
||||
print('peripheral: invalid peripheral ' .. uri)
|
||||
socket:write('Invalid peripheral: ' .. uri)
|
||||
else
|
||||
print('peripheral: proxing ' .. uri)
|
||||
local proxy = {
|
||||
methods = { }
|
||||
}
|
||||
|
||||
if peripheral.blit then
|
||||
--peripheral = Util.shallowCopy(peripheral)
|
||||
peripheral.fastBlit = function(data)
|
||||
for _,v in ipairs(data) do
|
||||
peripheral[v.fn](unpack(v.args))
|
||||
end
|
||||
end
|
||||
end
|
||||
if peripheral.blit then
|
||||
--peripheral = Util.shallowCopy(peripheral)
|
||||
peripheral.fastBlit = function(data)
|
||||
for _,v in ipairs(data) do
|
||||
peripheral[v.fn](unpack(v.args))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
for k,v in pairs(peripheral) do
|
||||
if type(v) == 'function' then
|
||||
table.insert(proxy.methods, k)
|
||||
else
|
||||
proxy[k] = v
|
||||
end
|
||||
end
|
||||
for k,v in pairs(peripheral) do
|
||||
if type(v) == 'function' then
|
||||
table.insert(proxy.methods, k)
|
||||
else
|
||||
proxy[k] = v
|
||||
end
|
||||
end
|
||||
|
||||
socket:write(proxy)
|
||||
socket:write(proxy)
|
||||
|
||||
if proxy.type == 'monitor' then
|
||||
peripheral.eventChannel = function(...)
|
||||
socket:write({
|
||||
fn = 'event',
|
||||
data = { ... }
|
||||
})
|
||||
end
|
||||
end
|
||||
if proxy.type == 'monitor' then
|
||||
peripheral.eventChannel = function(...)
|
||||
socket:write({
|
||||
fn = 'event',
|
||||
data = { ... }
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
while true do
|
||||
local data = socket:read()
|
||||
if not data then
|
||||
print('peripheral: lost connection from ' .. socket.dhost)
|
||||
break
|
||||
end
|
||||
if not _G.device[peripheral.name] then
|
||||
print('periperal: detached')
|
||||
socket:close()
|
||||
break
|
||||
end
|
||||
if peripheral[data.fn] then
|
||||
socket:write({ peripheral[data.fn](table.unpack(data.args)) })
|
||||
end
|
||||
end
|
||||
while true do
|
||||
local data = socket:read()
|
||||
if not data then
|
||||
print('peripheral: lost connection from ' .. socket.dhost)
|
||||
break
|
||||
end
|
||||
if not _G.device[peripheral.name] then
|
||||
print('periperal: detached')
|
||||
socket:close()
|
||||
break
|
||||
end
|
||||
if peripheral[data.fn] then
|
||||
socket:write({ peripheral[data.fn](table.unpack(data.args)) })
|
||||
end
|
||||
end
|
||||
|
||||
peripheral.eventChannel = nil
|
||||
peripheral.fastBlit = nil
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
peripheral.eventChannel = nil
|
||||
peripheral.fastBlit = nil
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
end)
|
||||
|
||||
@@ -2,18 +2,18 @@ local Event = require('event')
|
||||
local Socket = require('socket')
|
||||
|
||||
Event.addRoutine(function()
|
||||
while true do
|
||||
print('proxy: listening on port 188')
|
||||
local socket = Socket.server(188)
|
||||
while true do
|
||||
print('proxy: listening on port 188')
|
||||
local socket = Socket.server(188)
|
||||
|
||||
print('proxy: connection from ' .. socket.dhost)
|
||||
print('proxy: connection from ' .. socket.dhost)
|
||||
|
||||
Event.addRoutine(function()
|
||||
local api = socket:read(2)
|
||||
if api then
|
||||
Event.addRoutine(function()
|
||||
local api = socket:read(2)
|
||||
if api then
|
||||
local proxy = _G[api]
|
||||
|
||||
local methods = { }
|
||||
local methods = { }
|
||||
for k,v in pairs(proxy) do
|
||||
if type(v) == 'function' then
|
||||
table.insert(methods, k)
|
||||
@@ -21,14 +21,14 @@ Event.addRoutine(function()
|
||||
end
|
||||
socket:write(methods)
|
||||
|
||||
while true do
|
||||
local data = socket:read()
|
||||
if not data then
|
||||
while true do
|
||||
local data = socket:read()
|
||||
if not data then
|
||||
print('proxy: lost connection from ' .. socket.dhost)
|
||||
break
|
||||
end
|
||||
socket:write({ proxy[data.fn](table.unpack(data.args)) })
|
||||
end
|
||||
break
|
||||
end
|
||||
socket:write({ proxy[data.fn](table.unpack(data.args)) })
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
@@ -7,77 +7,77 @@ local fileUid = 0
|
||||
local fileHandles = { }
|
||||
|
||||
local function remoteOpen(fn, fl)
|
||||
local fh = fs.open(fn, fl)
|
||||
if fh then
|
||||
local methods = { 'close', 'write', 'writeLine', 'flush', 'read', 'readLine', 'readAll', }
|
||||
fileUid = fileUid + 1
|
||||
fileHandles[fileUid] = fh
|
||||
local fh = fs.open(fn, fl)
|
||||
if fh then
|
||||
local methods = { 'close', 'write', 'writeLine', 'flush', 'read', 'readLine', 'readAll', }
|
||||
fileUid = fileUid + 1
|
||||
fileHandles[fileUid] = fh
|
||||
|
||||
local vfh = {
|
||||
methods = { },
|
||||
fileUid = fileUid,
|
||||
}
|
||||
local vfh = {
|
||||
methods = { },
|
||||
fileUid = fileUid,
|
||||
}
|
||||
|
||||
for _,m in ipairs(methods) do
|
||||
if fh[m] then
|
||||
table.insert(vfh.methods, m)
|
||||
end
|
||||
end
|
||||
return vfh
|
||||
end
|
||||
for _,m in ipairs(methods) do
|
||||
if fh[m] then
|
||||
table.insert(vfh.methods, m)
|
||||
end
|
||||
end
|
||||
return vfh
|
||||
end
|
||||
end
|
||||
|
||||
local function remoteFileOperation(fileId, op, ...)
|
||||
local fh = fileHandles[fileId]
|
||||
if fh then
|
||||
return fh[op](...)
|
||||
end
|
||||
local fh = fileHandles[fileId]
|
||||
if fh then
|
||||
return fh[op](...)
|
||||
end
|
||||
end
|
||||
|
||||
local function sambaConnection(socket)
|
||||
while true do
|
||||
local msg = socket:read()
|
||||
if not msg then
|
||||
break
|
||||
end
|
||||
local fn = fs[msg.fn]
|
||||
if msg.fn == 'open' then
|
||||
fn = remoteOpen
|
||||
elseif msg.fn == 'fileOp' then
|
||||
fn = remoteFileOperation
|
||||
end
|
||||
local ret
|
||||
local s, m = pcall(function()
|
||||
ret = fn(unpack(msg.args))
|
||||
end)
|
||||
if not s and m then
|
||||
_G.printError('samba: ' .. m)
|
||||
end
|
||||
socket:write({ response = ret })
|
||||
end
|
||||
while true do
|
||||
local msg = socket:read()
|
||||
if not msg then
|
||||
break
|
||||
end
|
||||
local fn = fs[msg.fn]
|
||||
if msg.fn == 'open' then
|
||||
fn = remoteOpen
|
||||
elseif msg.fn == 'fileOp' then
|
||||
fn = remoteFileOperation
|
||||
end
|
||||
local ret
|
||||
local s, m = pcall(function()
|
||||
ret = fn(unpack(msg.args))
|
||||
end)
|
||||
if not s and m then
|
||||
_G.printError('samba: ' .. m)
|
||||
end
|
||||
socket:write({ response = ret })
|
||||
end
|
||||
|
||||
print('samba: Connection closed')
|
||||
print('samba: Connection closed')
|
||||
end
|
||||
|
||||
Event.addRoutine(function()
|
||||
print('samba: listening on port 139')
|
||||
print('samba: listening on port 139')
|
||||
|
||||
while true do
|
||||
local socket = Socket.server(139)
|
||||
while true do
|
||||
local socket = Socket.server(139)
|
||||
|
||||
Event.addRoutine(function()
|
||||
print('samba: connection from ' .. socket.dhost)
|
||||
sambaConnection(socket)
|
||||
print('samba: closing connection to ' .. socket.dhost)
|
||||
end)
|
||||
end
|
||||
Event.addRoutine(function()
|
||||
print('samba: connection from ' .. socket.dhost)
|
||||
sambaConnection(socket)
|
||||
print('samba: closing connection to ' .. socket.dhost)
|
||||
end)
|
||||
end
|
||||
end)
|
||||
|
||||
Event.on('network_attach', function(_, computer)
|
||||
fs.mount(fs.combine('network', computer.label), 'netfs', computer.id)
|
||||
fs.mount(fs.combine('network', computer.label), 'netfs', computer.id)
|
||||
end)
|
||||
|
||||
Event.on('network_detach', function(_, computer)
|
||||
print('samba: detaching ' .. computer.label)
|
||||
fs.unmount(fs.combine('network', computer.label))
|
||||
print('samba: detaching ' .. computer.label)
|
||||
fs.unmount(fs.combine('network', computer.label))
|
||||
end)
|
||||
|
||||
@@ -15,156 +15,156 @@ local gpsLastPoint
|
||||
local gpsLastRequestTime
|
||||
|
||||
local function snmpConnection(socket)
|
||||
while true do
|
||||
local msg = socket:read()
|
||||
if not msg then
|
||||
break
|
||||
end
|
||||
while true do
|
||||
local msg = socket:read()
|
||||
if not msg then
|
||||
break
|
||||
end
|
||||
|
||||
if msg.type == 'reboot' then
|
||||
os.reboot()
|
||||
if msg.type == 'reboot' then
|
||||
os.reboot()
|
||||
|
||||
elseif msg.type == 'shutdown' then
|
||||
os.shutdown()
|
||||
elseif msg.type == 'shutdown' then
|
||||
os.shutdown()
|
||||
|
||||
elseif msg.type == 'ping' then
|
||||
socket:write('pong')
|
||||
elseif msg.type == 'ping' then
|
||||
socket:write('pong')
|
||||
|
||||
elseif msg.type == 'script' then
|
||||
local fn, err = loadstring(msg.args, 'script')
|
||||
if fn then
|
||||
kernel.run({
|
||||
fn = fn,
|
||||
title = 'script',
|
||||
})
|
||||
else
|
||||
_G.printError(err)
|
||||
end
|
||||
elseif msg.type == 'script' then
|
||||
local fn, err = loadstring(msg.args, 'script')
|
||||
if fn then
|
||||
kernel.run({
|
||||
fn = fn,
|
||||
title = 'script',
|
||||
})
|
||||
else
|
||||
_G.printError(err)
|
||||
end
|
||||
|
||||
elseif msg.type == 'scriptEx' then
|
||||
local s, m = pcall(function()
|
||||
local env = setmetatable(Util.shallowCopy(_ENV), { __index = _G })
|
||||
local fn, m = load(msg.args, 'script', nil, env)
|
||||
if not fn then
|
||||
error(m)
|
||||
end
|
||||
return { fn() }
|
||||
end)
|
||||
if s then
|
||||
socket:write(m)
|
||||
else
|
||||
socket:write({ s, m })
|
||||
end
|
||||
elseif msg.type == 'scriptEx' then
|
||||
local s, m = pcall(function()
|
||||
local env = setmetatable(Util.shallowCopy(_ENV), { __index = _G })
|
||||
local fn, m = load(msg.args, 'script', nil, env)
|
||||
if not fn then
|
||||
error(m)
|
||||
end
|
||||
return { fn() }
|
||||
end)
|
||||
if s then
|
||||
socket:write(m)
|
||||
else
|
||||
socket:write({ s, m })
|
||||
end
|
||||
|
||||
elseif msg.type == 'gps' then
|
||||
if gpsRequested then
|
||||
repeat
|
||||
os.sleep(0)
|
||||
until not gpsRequested
|
||||
end
|
||||
elseif msg.type == 'gps' then
|
||||
if gpsRequested then
|
||||
repeat
|
||||
os.sleep(0)
|
||||
until not gpsRequested
|
||||
end
|
||||
|
||||
if gpsLastPoint and os.clock() - gpsLastRequestTime < .5 then
|
||||
socket:write(gpsLastPoint)
|
||||
else
|
||||
if gpsLastPoint and os.clock() - gpsLastRequestTime < .5 then
|
||||
socket:write(gpsLastPoint)
|
||||
else
|
||||
|
||||
gpsRequested = true
|
||||
local pt = GPS.getPoint(2)
|
||||
if pt then
|
||||
socket:write(pt)
|
||||
else
|
||||
print('snmp: Unable to get GPS point')
|
||||
end
|
||||
gpsRequested = false
|
||||
gpsLastPoint = pt
|
||||
if pt then
|
||||
gpsLastRequestTime = os.clock()
|
||||
end
|
||||
end
|
||||
gpsRequested = true
|
||||
local pt = GPS.getPoint(2)
|
||||
if pt then
|
||||
socket:write(pt)
|
||||
else
|
||||
print('snmp: Unable to get GPS point')
|
||||
end
|
||||
gpsRequested = false
|
||||
gpsLastPoint = pt
|
||||
if pt then
|
||||
gpsLastRequestTime = os.clock()
|
||||
end
|
||||
end
|
||||
|
||||
elseif msg.type == 'info' then
|
||||
local info = {
|
||||
id = os.getComputerID(),
|
||||
label = os.getComputerLabel(),
|
||||
uptime = math.floor(os.clock()),
|
||||
}
|
||||
if turtle then
|
||||
info.fuel = turtle.getFuelLevel()
|
||||
info.status = turtle.getStatus()
|
||||
end
|
||||
socket:write(info)
|
||||
end
|
||||
end
|
||||
elseif msg.type == 'info' then
|
||||
local info = {
|
||||
id = os.getComputerID(),
|
||||
label = os.getComputerLabel(),
|
||||
uptime = math.floor(os.clock()),
|
||||
}
|
||||
if turtle then
|
||||
info.fuel = turtle.getFuelLevel()
|
||||
info.status = turtle.getStatus()
|
||||
end
|
||||
socket:write(info)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Event.addRoutine(function()
|
||||
print('snmp: listening on port 161')
|
||||
print('snmp: listening on port 161')
|
||||
|
||||
while true do
|
||||
local socket = Socket.server(161)
|
||||
while true do
|
||||
local socket = Socket.server(161)
|
||||
|
||||
Event.addRoutine(function()
|
||||
print('snmp: connection from ' .. socket.dhost)
|
||||
snmpConnection(socket)
|
||||
print('snmp: closing connection to ' .. socket.dhost)
|
||||
end)
|
||||
end
|
||||
Event.addRoutine(function()
|
||||
print('snmp: connection from ' .. socket.dhost)
|
||||
snmpConnection(socket)
|
||||
print('snmp: closing connection to ' .. socket.dhost)
|
||||
end)
|
||||
end
|
||||
end)
|
||||
|
||||
device.wireless_modem.open(999)
|
||||
print('discovery: listening on port 999')
|
||||
|
||||
Event.on('modem_message', function(_, _, sport, id, info, distance)
|
||||
if sport == 999 and tonumber(id) and type(info) == 'table' then
|
||||
if not network[id] then
|
||||
network[id] = { }
|
||||
end
|
||||
Util.merge(network[id], info)
|
||||
network[id].distance = distance
|
||||
network[id].timestamp = os.clock()
|
||||
if sport == 999 and tonumber(id) and type(info) == 'table' then
|
||||
if not network[id] then
|
||||
network[id] = { }
|
||||
end
|
||||
Util.merge(network[id], info)
|
||||
network[id].distance = distance
|
||||
network[id].timestamp = os.clock()
|
||||
|
||||
if not network[id].active then
|
||||
network[id].active = true
|
||||
os.queueEvent('network_attach', network[id])
|
||||
end
|
||||
end
|
||||
if not network[id].active then
|
||||
network[id].active = true
|
||||
os.queueEvent('network_attach', network[id])
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
local info = {
|
||||
id = os.getComputerID()
|
||||
id = os.getComputerID()
|
||||
}
|
||||
local infoTimer = os.clock()
|
||||
|
||||
local function sendInfo()
|
||||
if os.clock() - infoTimer >= 1 then -- don't flood
|
||||
infoTimer = os.clock()
|
||||
info.label = os.getComputerLabel()
|
||||
info.uptime = math.floor(os.clock())
|
||||
if turtle then
|
||||
info.fuel = turtle.getFuelLevel()
|
||||
info.status = turtle.getStatus()
|
||||
info.point = turtle.point
|
||||
info.inventory = turtle.getInventory()
|
||||
info.slotIndex = turtle.getSelectedSlot()
|
||||
end
|
||||
device.wireless_modem.transmit(999, os.getComputerID(), info)
|
||||
end
|
||||
if os.clock() - infoTimer >= 1 then -- don't flood
|
||||
infoTimer = os.clock()
|
||||
info.label = os.getComputerLabel()
|
||||
info.uptime = math.floor(os.clock())
|
||||
if turtle then
|
||||
info.fuel = turtle.getFuelLevel()
|
||||
info.status = turtle.getStatus()
|
||||
info.point = turtle.point
|
||||
info.inventory = turtle.getInventory()
|
||||
info.slotIndex = turtle.getSelectedSlot()
|
||||
end
|
||||
device.wireless_modem.transmit(999, os.getComputerID(), info)
|
||||
end
|
||||
end
|
||||
|
||||
-- every 10 seconds, send out this computer's info
|
||||
Event.onInterval(10, function()
|
||||
sendInfo()
|
||||
for _,c in pairs(_G.network) do
|
||||
local elapsed = os.clock()-c.timestamp
|
||||
if c.active and elapsed > 15 then
|
||||
c.active = false
|
||||
os.queueEvent('network_detach', c)
|
||||
end
|
||||
end
|
||||
sendInfo()
|
||||
for _,c in pairs(_G.network) do
|
||||
local elapsed = os.clock()-c.timestamp
|
||||
if c.active and elapsed > 15 then
|
||||
c.active = false
|
||||
os.queueEvent('network_detach', c)
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
Event.on('turtle_response', function()
|
||||
if turtle.getStatus() ~= info.status or
|
||||
turtle.fuel ~= info.fuel then
|
||||
sendInfo()
|
||||
end
|
||||
if turtle.getStatus() ~= info.status or
|
||||
turtle.fuel ~= info.fuel then
|
||||
sendInfo()
|
||||
end
|
||||
end)
|
||||
|
||||
@@ -7,76 +7,76 @@ local term = _G.term
|
||||
local window = _G.window
|
||||
|
||||
local function telnetHost(socket)
|
||||
local methods = { 'clear', 'clearLine', 'setCursorPos', 'write', 'blit',
|
||||
'setTextColor', 'setTextColour', 'setBackgroundColor',
|
||||
'setBackgroundColour', 'scroll', 'setCursorBlink', }
|
||||
local methods = { 'clear', 'clearLine', 'setCursorPos', 'write', 'blit',
|
||||
'setTextColor', 'setTextColour', 'setBackgroundColor',
|
||||
'setBackgroundColour', 'scroll', 'setCursorBlink', }
|
||||
|
||||
local termInfo = socket:read(5)
|
||||
if not termInfo then
|
||||
_G.printError('read failed')
|
||||
return
|
||||
end
|
||||
local termInfo = socket:read(5)
|
||||
if not termInfo then
|
||||
_G.printError('read failed')
|
||||
return
|
||||
end
|
||||
|
||||
local win = window.create(_G.device.terminal, 1, 1, termInfo.width, termInfo.height, false)
|
||||
win.setCursorPos(table.unpack(termInfo.pos))
|
||||
local win = window.create(_G.device.terminal, 1, 1, termInfo.width, termInfo.height, false)
|
||||
win.setCursorPos(table.unpack(termInfo.pos))
|
||||
|
||||
for _,k in pairs(methods) do
|
||||
local fn = win[k]
|
||||
win[k] = function(...)
|
||||
for _,k in pairs(methods) do
|
||||
local fn = win[k]
|
||||
win[k] = function(...)
|
||||
|
||||
if not socket.queue then
|
||||
socket.queue = { }
|
||||
Event.onTimeout(0, function()
|
||||
socket:write(socket.queue)
|
||||
socket.queue = nil
|
||||
end)
|
||||
end
|
||||
if not socket.queue then
|
||||
socket.queue = { }
|
||||
Event.onTimeout(0, function()
|
||||
socket:write(socket.queue)
|
||||
socket.queue = nil
|
||||
end)
|
||||
end
|
||||
|
||||
table.insert(socket.queue, {
|
||||
f = k,
|
||||
args = { ... },
|
||||
})
|
||||
fn(...)
|
||||
end
|
||||
end
|
||||
table.insert(socket.queue, {
|
||||
f = k,
|
||||
args = { ... },
|
||||
})
|
||||
fn(...)
|
||||
end
|
||||
end
|
||||
|
||||
local shellThread = kernel.run({
|
||||
terminal = win,
|
||||
window = win,
|
||||
title = 'Telnet client',
|
||||
hidden = true,
|
||||
co = coroutine.create(function()
|
||||
Util.run(_ENV, 'sys/apps/shell', table.unpack(termInfo.program))
|
||||
if socket.queue then
|
||||
socket:write(socket.queue)
|
||||
end
|
||||
socket:close()
|
||||
end)
|
||||
})
|
||||
local shellThread = kernel.run({
|
||||
terminal = win,
|
||||
window = win,
|
||||
title = 'Telnet client',
|
||||
hidden = true,
|
||||
co = coroutine.create(function()
|
||||
Util.run(_ENV, 'sys/apps/shell', table.unpack(termInfo.program))
|
||||
if socket.queue then
|
||||
socket:write(socket.queue)
|
||||
end
|
||||
socket:close()
|
||||
end)
|
||||
})
|
||||
|
||||
Event.addRoutine(function()
|
||||
while true do
|
||||
local data = socket:read()
|
||||
if not data then
|
||||
shellThread:resume('terminate')
|
||||
break
|
||||
end
|
||||
local previousTerm = term.current()
|
||||
shellThread:resume(table.unpack(data))
|
||||
term.redirect(previousTerm)
|
||||
end
|
||||
end)
|
||||
Event.addRoutine(function()
|
||||
while true do
|
||||
local data = socket:read()
|
||||
if not data then
|
||||
shellThread:resume('terminate')
|
||||
break
|
||||
end
|
||||
local previousTerm = term.current()
|
||||
shellThread:resume(table.unpack(data))
|
||||
term.redirect(previousTerm)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
Event.addRoutine(function()
|
||||
print('telnet: listening on port 23')
|
||||
while true do
|
||||
local socket = Socket.server(23)
|
||||
print('telnet: listening on port 23')
|
||||
while true do
|
||||
local socket = Socket.server(23)
|
||||
|
||||
print('telnet: connection from ' .. socket.dhost)
|
||||
print('telnet: connection from ' .. socket.dhost)
|
||||
|
||||
Event.addRoutine(function()
|
||||
telnetHost(socket)
|
||||
end)
|
||||
end
|
||||
Event.addRoutine(function()
|
||||
telnetHost(socket)
|
||||
end)
|
||||
end
|
||||
end)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
--[[
|
||||
Low level socket protocol implementation.
|
||||
Low level socket protocol implementation.
|
||||
|
||||
* sequencing
|
||||
* background read buffering
|
||||
* sequencing
|
||||
* background read buffering
|
||||
]]--
|
||||
|
||||
local Event = require('event')
|
||||
@@ -11,120 +11,120 @@ local os = _G.os
|
||||
|
||||
local computerId = os.getComputerID()
|
||||
local transport = {
|
||||
timers = { },
|
||||
sockets = { },
|
||||
UID = 0,
|
||||
timers = { },
|
||||
sockets = { },
|
||||
UID = 0,
|
||||
}
|
||||
_G.transport = transport
|
||||
|
||||
function transport.open(socket)
|
||||
transport.UID = transport.UID + 1
|
||||
transport.UID = transport.UID + 1
|
||||
|
||||
transport.sockets[socket.sport] = socket
|
||||
socket.activityTimer = os.clock()
|
||||
socket.uid = transport.UID
|
||||
transport.sockets[socket.sport] = socket
|
||||
socket.activityTimer = os.clock()
|
||||
socket.uid = transport.UID
|
||||
end
|
||||
|
||||
function transport.read(socket)
|
||||
local data = table.remove(socket.messages, 1)
|
||||
if data then
|
||||
return unpack(data)
|
||||
end
|
||||
local data = table.remove(socket.messages, 1)
|
||||
if data then
|
||||
return unpack(data)
|
||||
end
|
||||
end
|
||||
|
||||
function transport.write(socket, data)
|
||||
--debug('>> ' .. Util.tostring({ type = 'DATA', seq = socket.wseq }))
|
||||
socket.transmit(socket.dport, socket.dhost, data)
|
||||
--debug('>> ' .. Util.tostring({ type = 'DATA', seq = socket.wseq }))
|
||||
socket.transmit(socket.dport, socket.dhost, data)
|
||||
|
||||
--local timerId = os.startTimer(3)
|
||||
--local timerId = os.startTimer(3)
|
||||
|
||||
--transport.timers[timerId] = socket
|
||||
--socket.timers[socket.wseq] = timerId
|
||||
--transport.timers[timerId] = socket
|
||||
--socket.timers[socket.wseq] = timerId
|
||||
|
||||
socket.wseq = socket.wseq + 1
|
||||
socket.wseq = socket.wseq + 1
|
||||
end
|
||||
|
||||
function transport.ping(socket)
|
||||
--debug('>> ' .. Util.tostring({ type = 'DATA', seq = socket.wseq }))
|
||||
if os.clock() - socket.activityTimer > 10 then
|
||||
socket.activityTimer = os.clock()
|
||||
socket.transmit(socket.dport, socket.dhost, {
|
||||
type = 'PING',
|
||||
seq = -1,
|
||||
})
|
||||
--debug('>> ' .. Util.tostring({ type = 'DATA', seq = socket.wseq }))
|
||||
if os.clock() - socket.activityTimer > 10 then
|
||||
socket.activityTimer = os.clock()
|
||||
socket.transmit(socket.dport, socket.dhost, {
|
||||
type = 'PING',
|
||||
seq = -1,
|
||||
})
|
||||
|
||||
local timerId = os.startTimer(3)
|
||||
transport.timers[timerId] = socket
|
||||
socket.timers[-1] = timerId
|
||||
end
|
||||
local timerId = os.startTimer(3)
|
||||
transport.timers[timerId] = socket
|
||||
socket.timers[-1] = timerId
|
||||
end
|
||||
end
|
||||
|
||||
function transport.close(socket)
|
||||
transport.sockets[socket.sport] = nil
|
||||
transport.sockets[socket.sport] = nil
|
||||
end
|
||||
|
||||
Event.on('timer', function(_, timerId)
|
||||
local socket = transport.timers[timerId]
|
||||
local socket = transport.timers[timerId]
|
||||
|
||||
if socket and socket.connected then
|
||||
print('transport timeout - closing socket ' .. socket.sport)
|
||||
socket:close()
|
||||
transport.timers[timerId] = nil
|
||||
end
|
||||
if socket and socket.connected then
|
||||
print('transport timeout - closing socket ' .. socket.sport)
|
||||
socket:close()
|
||||
transport.timers[timerId] = nil
|
||||
end
|
||||
end)
|
||||
|
||||
Event.on('modem_message', function(_, _, dport, dhost, msg, distance)
|
||||
if dhost == computerId and msg then
|
||||
local socket = transport.sockets[dport]
|
||||
if socket and socket.connected then
|
||||
if dhost == computerId and msg then
|
||||
local socket = transport.sockets[dport]
|
||||
if socket and socket.connected then
|
||||
|
||||
--if msg.type then debug('<< ' .. Util.tostring(msg)) end
|
||||
--if msg.type then debug('<< ' .. Util.tostring(msg)) end
|
||||
|
||||
if msg.type == 'DISC' then
|
||||
-- received disconnect from other end
|
||||
if socket.connected then
|
||||
os.queueEvent('transport_' .. socket.uid)
|
||||
end
|
||||
socket.connected = false
|
||||
socket:close()
|
||||
if msg.type == 'DISC' then
|
||||
-- received disconnect from other end
|
||||
if socket.connected then
|
||||
os.queueEvent('transport_' .. socket.uid)
|
||||
end
|
||||
socket.connected = false
|
||||
socket:close()
|
||||
|
||||
elseif msg.type == 'ACK' then
|
||||
local ackTimerId = socket.timers[msg.seq]
|
||||
if ackTimerId then
|
||||
os.cancelTimer(ackTimerId)
|
||||
socket.timers[msg.seq] = nil
|
||||
socket.activityTimer = os.clock()
|
||||
transport.timers[ackTimerId] = nil
|
||||
end
|
||||
elseif msg.type == 'ACK' then
|
||||
local ackTimerId = socket.timers[msg.seq]
|
||||
if ackTimerId then
|
||||
os.cancelTimer(ackTimerId)
|
||||
socket.timers[msg.seq] = nil
|
||||
socket.activityTimer = os.clock()
|
||||
transport.timers[ackTimerId] = nil
|
||||
end
|
||||
|
||||
elseif msg.type == 'PING' then
|
||||
socket.activityTimer = os.clock()
|
||||
socket.transmit(socket.dport, socket.dhost, {
|
||||
type = 'ACK',
|
||||
seq = msg.seq,
|
||||
})
|
||||
elseif msg.type == 'PING' then
|
||||
socket.activityTimer = os.clock()
|
||||
socket.transmit(socket.dport, socket.dhost, {
|
||||
type = 'ACK',
|
||||
seq = msg.seq,
|
||||
})
|
||||
|
||||
elseif msg.type == 'DATA' and msg.data then
|
||||
socket.activityTimer = os.clock()
|
||||
if msg.seq ~= socket.rseq then
|
||||
print('transport seq error - closing socket ' .. socket.sport)
|
||||
socket:close()
|
||||
else
|
||||
socket.rseq = socket.rseq + 1
|
||||
table.insert(socket.messages, { msg.data, distance })
|
||||
elseif msg.type == 'DATA' and msg.data then
|
||||
socket.activityTimer = os.clock()
|
||||
if msg.seq ~= socket.rseq then
|
||||
print('transport seq error - closing socket ' .. socket.sport)
|
||||
socket:close()
|
||||
else
|
||||
socket.rseq = socket.rseq + 1
|
||||
table.insert(socket.messages, { msg.data, distance })
|
||||
|
||||
-- use resume instead ??
|
||||
if not socket.messages[2] then -- table size is 1
|
||||
os.queueEvent('transport_' .. socket.uid)
|
||||
end
|
||||
-- use resume instead ??
|
||||
if not socket.messages[2] then -- table size is 1
|
||||
os.queueEvent('transport_' .. socket.uid)
|
||||
end
|
||||
|
||||
--debug('>> ' .. Util.tostring({ type = 'ACK', seq = msg.seq }))
|
||||
--socket.transmit(socket.dport, socket.dhost, {
|
||||
-- type = 'ACK',
|
||||
-- seq = msg.seq,
|
||||
--})
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
--debug('>> ' .. Util.tostring({ type = 'ACK', seq = msg.seq }))
|
||||
--socket.transmit(socket.dport, socket.dhost, {
|
||||
-- type = 'ACK',
|
||||
-- seq = msg.seq,
|
||||
--})
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
@@ -6,30 +6,30 @@ local Util = require('util')
|
||||
|
||||
Event.addRoutine(function()
|
||||
|
||||
print('trust: listening on port 19')
|
||||
while true do
|
||||
local socket = Socket.server(19)
|
||||
print('trust: listening on port 19')
|
||||
while true do
|
||||
local socket = Socket.server(19)
|
||||
|
||||
print('trust: connection from ' .. socket.dhost)
|
||||
print('trust: connection from ' .. socket.dhost)
|
||||
|
||||
local data = socket:read(2)
|
||||
if data then
|
||||
local password = Security.getPassword()
|
||||
if not password then
|
||||
socket:write({ msg = 'No password has been set' })
|
||||
else
|
||||
data = Crypto.decrypt(data, password)
|
||||
if data and data.pk and data.dh == socket.dhost then
|
||||
local trustList = Util.readTable('usr/.known_hosts') or { }
|
||||
trustList[data.dh] = data.pk
|
||||
Util.writeTable('usr/.known_hosts', trustList)
|
||||
local data = socket:read(2)
|
||||
if data then
|
||||
local password = Security.getPassword()
|
||||
if not password then
|
||||
socket:write({ msg = 'No password has been set' })
|
||||
else
|
||||
data = Crypto.decrypt(data, password)
|
||||
if data and data.pk and data.dh == socket.dhost then
|
||||
local trustList = Util.readTable('usr/.known_hosts') or { }
|
||||
trustList[data.dh] = data.pk
|
||||
Util.writeTable('usr/.known_hosts', trustList)
|
||||
|
||||
socket:write({ success = true, msg = 'Trust accepted' })
|
||||
else
|
||||
socket:write({ msg = 'Invalid password' })
|
||||
end
|
||||
end
|
||||
end
|
||||
socket:close()
|
||||
end
|
||||
socket:write({ success = true, msg = 'Trust accepted' })
|
||||
else
|
||||
socket:write({ msg = 'Invalid password' })
|
||||
end
|
||||
end
|
||||
end
|
||||
socket:close()
|
||||
end
|
||||
end)
|
||||
|
||||
@@ -6,64 +6,64 @@ local os = _G.os
|
||||
local terminal = _G.device.terminal
|
||||
|
||||
local function vncHost(socket)
|
||||
local methods = { 'blit', 'clear', 'clearLine', 'setCursorPos', 'write',
|
||||
'setTextColor', 'setTextColour', 'setBackgroundColor',
|
||||
'setBackgroundColour', 'scroll', 'setCursorBlink', }
|
||||
local methods = { 'blit', 'clear', 'clearLine', 'setCursorPos', 'write',
|
||||
'setTextColor', 'setTextColour', 'setBackgroundColor',
|
||||
'setBackgroundColour', 'scroll', 'setCursorBlink', }
|
||||
|
||||
local oldTerm = Util.shallowCopy(terminal)
|
||||
local oldTerm = Util.shallowCopy(terminal)
|
||||
|
||||
for _,k in pairs(methods) do
|
||||
terminal[k] = function(...)
|
||||
if not socket.queue then
|
||||
socket.queue = { }
|
||||
Event.onTimeout(0, function()
|
||||
socket:write(socket.queue)
|
||||
socket.queue = nil
|
||||
end)
|
||||
end
|
||||
table.insert(socket.queue, {
|
||||
f = k,
|
||||
args = { ... },
|
||||
})
|
||||
oldTerm[k](...)
|
||||
end
|
||||
end
|
||||
for _,k in pairs(methods) do
|
||||
terminal[k] = function(...)
|
||||
if not socket.queue then
|
||||
socket.queue = { }
|
||||
Event.onTimeout(0, function()
|
||||
socket:write(socket.queue)
|
||||
socket.queue = nil
|
||||
end)
|
||||
end
|
||||
table.insert(socket.queue, {
|
||||
f = k,
|
||||
args = { ... },
|
||||
})
|
||||
oldTerm[k](...)
|
||||
end
|
||||
end
|
||||
|
||||
while true do
|
||||
local data = socket:read()
|
||||
if not data then
|
||||
print('vnc: closing connection to ' .. socket.dhost)
|
||||
break
|
||||
end
|
||||
while true do
|
||||
local data = socket:read()
|
||||
if not data then
|
||||
print('vnc: closing connection to ' .. socket.dhost)
|
||||
break
|
||||
end
|
||||
|
||||
if data.type == 'shellRemote' then
|
||||
os.queueEvent(table.unpack(data.event))
|
||||
elseif data.type == 'termInfo' then
|
||||
terminal.getSize = function()
|
||||
return data.width, data.height
|
||||
end
|
||||
os.queueEvent('term_resize')
|
||||
end
|
||||
end
|
||||
if data.type == 'shellRemote' then
|
||||
os.queueEvent(table.unpack(data.event))
|
||||
elseif data.type == 'termInfo' then
|
||||
terminal.getSize = function()
|
||||
return data.width, data.height
|
||||
end
|
||||
os.queueEvent('term_resize')
|
||||
end
|
||||
end
|
||||
|
||||
for k,v in pairs(oldTerm) do
|
||||
terminal[k] = v
|
||||
end
|
||||
os.queueEvent('term_resize')
|
||||
for k,v in pairs(oldTerm) do
|
||||
terminal[k] = v
|
||||
end
|
||||
os.queueEvent('term_resize')
|
||||
end
|
||||
|
||||
Event.addRoutine(function()
|
||||
|
||||
print('vnc: listening on port 5900')
|
||||
print('vnc: listening on port 5900')
|
||||
|
||||
while true do
|
||||
local socket = Socket.server(5900)
|
||||
while true do
|
||||
local socket = Socket.server(5900)
|
||||
|
||||
print('vnc: connection from ' .. socket.dhost)
|
||||
print('vnc: connection from ' .. socket.dhost)
|
||||
|
||||
-- no new process - only 1 connection allowed
|
||||
-- due to term size issues
|
||||
vncHost(socket)
|
||||
socket:close()
|
||||
end
|
||||
-- no new process - only 1 connection allowed
|
||||
-- due to term size issues
|
||||
vncHost(socket)
|
||||
socket:close()
|
||||
end
|
||||
end)
|
||||
|
||||
Reference in New Issue
Block a user