1
0
mirror of https://github.com/kepler155c/opus synced 2025-10-25 20:57:39 +00:00

fix menu separators + sys dir restructure

This commit is contained in:
kepler155c@gmail.com
2019-03-31 15:16:45 -04:00
parent 19159730a4
commit 737ac095de
25 changed files with 16 additions and 13 deletions

View File

@@ -0,0 +1,60 @@
local Event = require('event')
local Socket = require('socket')
local Util = require('util')
local function getProxy(path)
local x = Util.split(path, '(.-)/')
local proxy = _G
for _, v in pairs(x) do
proxy = proxy[v]
if not proxy then
break
end
end
return proxy
end
Event.addRoutine(function()
while true do
print('proxy: listening on port 188')
local socket = Socket.server(188)
print('proxy: connection from ' .. socket.dhost)
Event.addRoutine(function()
local path = socket:read(2)
if path then
local api = getProxy(path)
if not api then
print('proxy: invalid API')
socket:close()
return
end
local methods = { }
for k,v in pairs(api) do
if type(v) == 'function' then
table.insert(methods, k)
end
end
socket:write(methods)
local s, m = pcall(function()
while true do
local data = socket:read()
if not data then
print('proxy: lost connection from ' .. socket.dhost)
break
end
socket:write({ api[data[1]](table.unpack(data, 2)) })
end
end)
if not s and m then
_G.printError(m)
end
end
socket:close()
end)
end
end)

View File

@@ -0,0 +1,115 @@
local Event = require('event')
local Util = require('util')
local fs = _G.fs
local modem = _G.device.wireless_modem
local os = _G.os
local computerId = os.getComputerID()
--modem.open(80)
-- https://github.com/golgote/neturl/blob/master/lib/net/url.lua
local function parseQuery(str)
local sep = '&'
local values = {}
for key,val in str:gmatch(string.format('([^%q=]+)(=*[^%q=]*)', sep, sep)) do
--local key = decode(key)
local keys = {}
key = key:gsub('%[([^%]]*)%]', function(v)
-- extract keys between balanced brackets
if string.find(v, "^-?%d+$") then
v = tonumber(v)
--else
--v = decode(v)
end
table.insert(keys, v)
return "="
end)
key = key:gsub('=+.*$', "")
key = key:gsub('%s', "_") -- remove spaces in parameter name
val = val:gsub('^=+', "")
if not values[key] then
values[key] = {}
end
if #keys > 0 and type(values[key]) ~= 'table' then
values[key] = {}
elseif #keys == 0 and type(values[key]) == 'table' then
values[key] = val --decode(val)
end
local t = values[key]
for i,k in ipairs(keys) do
if type(t) ~= 'table' then
t = {}
end
if k == "" then
k = #t+1
end
if not t[k] then
t[k] = {}
end
if i == #keys then
t[k] = val --decode(val)
end
t = t[k]
end
end
return values
end
local function getListing(path, recursive)
local list = { }
local function listing(p)
for _, f in pairs(fs.listEx(p)) do
local abs = fs.combine(p, f.name)
table.insert(list, {
isDir = f.isDir,
path = string.sub(abs, #path + 1),
size = f.size,
})
if recursive and f.isDir then
listing(abs)
end
end
end
listing(path)
return list
end
--[[
Event.on('modem_message', function(_, _, dport, dhost, request)
if dport == 80 and dhost == computerId and type(request) == 'table' then
if request.method == 'GET' then
local query
if not request.path or type(request.path) ~= 'string' then
return
end
local path = request.path:gsub('%?(.*)', function(v)
query = parseQuery(v)
return ''
end)
if fs.isDir(path) then
-- TODO: more validation
modem.transmit(request.replyPort, request.replyAddress, {
statusCode = 200,
contentType = 'table/directory',
data = getListing(path, query and query.recursive == 'true'),
})
elseif fs.exists(path) then
modem.transmit(request.replyPort, request.replyAddress, {
statusCode = 200,
contentType = 'table/file',
data = Util.readFile(path),
})
else
modem.transmit(request.replyPort, request.replyAddress, {
statusCode = 404,
})
end
end
end
end)
]]

View File

@@ -0,0 +1,83 @@
local Event = require('event')
local Socket = require('socket')
local fs = _G.fs
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 vfh = {
methods = { },
fileUid = fileUid,
}
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
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
print('samba: Connection closed')
end
Event.addRoutine(function()
print('samba: listening on port 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
end)
Event.on('network_attach', function(_, computer)
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))
end)

202
sys/apps/network/snmp.lua Normal file
View File

@@ -0,0 +1,202 @@
local Event = require('event')
local GPS = require('gps')
local Socket = require('socket')
local Util = require('util')
local device = _G.device
local kernel = _G.kernel
local network = _G.network
local os = _G.os
local turtle = _G.turtle
-- move this into gps api
local gpsRequested
local gpsLastPoint
local gpsLastRequestTime
local function snmpConnection(socket)
while true do
local msg = socket:read()
if not msg then
break
end
if msg.type == 'reboot' then
os.reboot()
elseif msg.type == 'shutdown' then
os.shutdown()
elseif msg.type == 'ping' then
socket:write('pong')
elseif msg.type == 'script' then
local env = setmetatable(Util.shallowCopy(_ENV), { __index = _G })
local fn, err = load(msg.args, 'script', nil, env)
if fn then
kernel.run({
fn = fn,
env = env,
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 == '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
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
end
Event.addRoutine(function()
print('snmp: listening on port 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
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 not network[id].active then
network[id].active = true
os.queueEvent('network_attach', network[id])
end
end
end)
local info = {
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
if device.neuralInterface then
info.status = device.neuralInterface.status
if not info.status and device.neuralInterface.getMetaOwner then
pcall(function()
local meta = device.neuralInterface.getMetaOwner()
if meta.isWet then
info.status = 'Swimming'
elseif meta.isElytraFlying then
info.status = 'Flying'
elseif meta.isBurning then
info.status = 'Burning'
elseif meta.isDead then
info.status = 'Deceased'
elseif meta.isOnLadder then
info.status = 'Climbing'
elseif meta.isRiding then
info.status = 'Riding'
elseif meta.isSneaking then
info.status = 'Sneaking'
elseif meta.isSprinting then
info.status = 'Running'
else
info.status = 'health: ' ..
math.floor(meta.health /
meta.maxHealth * 100)
end
end)
end
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
end)
Event.on('turtle_response', function()
if turtle.getStatus() ~= info.status or
turtle.fuel ~= info.fuel then
sendInfo()
end
end)

View File

@@ -0,0 +1,82 @@
local Event = require('event')
local Socket = require('socket')
local Util = require('util')
local kernel = _G.kernel
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 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))
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
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.lua', 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)
end
Event.addRoutine(function()
print('telnet: listening on port 23')
while true do
local socket = Socket.server(23)
print('telnet: connection from ' .. socket.dhost)
Event.addRoutine(function()
telnetHost(socket)
end)
end
end)

View File

@@ -0,0 +1,137 @@
--[[
Low level socket protocol implementation.
* sequencing
* background read buffering
]]--
local Event = require('event')
local os = _G.os
local computerId = os.getComputerID()
local transport = {
timers = { },
sockets = { },
UID = 0,
}
_G.transport = transport
function transport.open(socket)
transport.UID = transport.UID + 1
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
end
function transport.write(socket, data)
--_debug('>> ' .. Util.tostring({ type = 'DATA', seq = socket.wseq }))
socket.transmit(socket.dport, socket.dhost, data)
--local timerId = os.startTimer(3)
--transport.timers[timerId] = socket
--socket.timers[socket.wseq] = timerId
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,
})
local timerId = os.startTimer(5)
transport.timers[timerId] = socket
socket.timers[-1] = timerId
end
end
function transport.close(socket)
transport.sockets[socket.sport] = nil
end
Event.on('timer', function(_, 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
end)
Event.on('modem_message', function(_, _, dport, dhost, msg, distance)
if dhost == computerId and type(msg) == 'table' then
local socket = transport.sockets[dport]
if socket and socket.connected then
--if msg.type then _debug('<< ' .. Util.tostring(msg)) end
if socket.co and coroutine.status(socket.co) == 'dead' then
_G._debug('socket coroutine dead')
socket:close()
elseif 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 == '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)
_debug(msg.data)
_debug('current ' .. socket.rseq)
_debug('expected ' .. msg.seq)
-- socket:close()
-- os.queueEvent('transport_' .. socket.uid)
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
--_debug('>> ' .. Util.tostring({ type = 'ACK', seq = msg.seq }))
--socket.transmit(socket.dport, socket.dhost, {
-- type = 'ACK',
-- seq = msg.seq,
--})
end
end
end
end
end)

View File

@@ -0,0 +1,35 @@
local Crypto = require('crypto')
local Event = require('event')
local Security = require('security')
local Socket = require('socket')
local Util = require('util')
Event.addRoutine(function()
print('trust: listening on port 19')
while true do
local socket = Socket.server(19)
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)
socket:write({ success = true, msg = 'Trust accepted' })
else
socket:write({ msg = 'Invalid password' })
end
end
end
socket:close()
end
end)

69
sys/apps/network/vnc.lua Normal file
View File

@@ -0,0 +1,69 @@
local Event = require('event')
local Socket = require('socket')
local Util = require('util')
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 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
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
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')
while true do
local socket = Socket.server(5900)
print('vnc: connection from ' .. socket.dhost)
-- no new process - only 1 connection allowed
-- due to term size issues
vncHost(socket)
socket:close()
end
end)