1
0
mirror of https://github.com/kepler155c/opus synced 2025-10-19 01:37:39 +00:00

Initial commit

This commit is contained in:
kepler155c@gmail.com
2016-12-11 14:24:52 -05:00
commit fc243a9c12
110 changed files with 25577 additions and 0 deletions

87
sys/network/samba.lua Normal file
View File

@@ -0,0 +1,87 @@
local Socket = require('socket')
local process = require('process')
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
printError('samba: ' .. m)
end
socket:write({ response = ret })
end
print('samba: Connection closed')
end
process:newThread('samba_server', function()
print('samba: listening on port 139')
while true do
local socket = Socket.server(139)
print('samba: connection from ' .. socket.dhost)
process:newThread('samba_connection', function()
sambaConnection(socket)
print('samba: closing connection to ' .. socket.dhost)
end)
end
end)
process:newThread('samba_manager', function()
while true do
local e, computer = os.pullEvent()
if e == 'network_attach' then
fs.mount(fs.combine('network', computer.label), 'netfs', computer.id)
elseif e == 'network_detach' then
print('samba: detaching ' .. computer.label)
fs.unmount(fs.combine('network', computer.label))
end
end
end)

166
sys/network/snmp.lua Normal file
View File

@@ -0,0 +1,166 @@
local Socket = require('socket')
local GPS = require('gps')
local process = require('process')
-- 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 fn, msg = loadstring(msg.args, 'script')
if fn then
multishell.openTab({
fn = fn,
env = getfenv(1),
title = 'script',
})
else
printError(msg)
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.status
end
socket:write(info)
end
end
end
process:newThread('snmp_server', function()
print('snmp: listening on port 161')
while true do
local socket = Socket.server(161)
print('snmp: connection from ' .. socket.dhost)
process:newThread('snmp_connection', function()
snmpConnection(socket)
print('snmp: closing connection to ' .. socket.dhost)
end)
end
end)
process:newThread('discovery_server', function()
device.wireless_modem.open(999)
os.sleep(1) -- allow services a chance to startup
print('discovery: listening on port 999')
while true do
local e, s, sport, id, info, distance = os.pullEvent('modem_message')
if sport == 999 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
end)
local function sendInfo()
local info = {
id = os.getComputerID(),
label = os.getComputerLabel(),
uptime = math.floor(os.clock()),
}
if turtle then
info.fuel = turtle.getFuelLevel()
info.status = turtle.status
end
device.wireless_modem.transmit(999, os.getComputerID(), info)
end
-- every 10 seconds, send out this computer's info
process:newThread('discovery_heartbeat', function()
os.sleep(1)
while true do
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
os.sleep(10)
end
end)
if os.isTurtle() then
process:newThread('turtle_heartbeat', function()
local lastUpdate = os.clock()
os.sleep(1)
while true do
os.pullEvent('turtle_response')
if os.clock() - lastUpdate >= 1 then
lastUpdate = os.clock()
sendInfo()
end
end
end)
end

96
sys/network/telnet.lua Normal file
View File

@@ -0,0 +1,96 @@
local Socket = require('socket')
local process = require('process')
local function wrapTerm(socket, termInfo)
local methods = { 'clear', 'clearLine', 'setCursorPos', 'write', 'blit',
'setTextColor', 'setTextColour', 'setBackgroundColor',
'setBackgroundColour', 'scroll', 'setCursorBlink', }
socket.term = term.current()
local oldWindow = Util.shallowCopy(socket.term)
for _,k in pairs(methods) do
socket.term[k] = function(...)
if not socket.queue then
socket.queue = { }
os.startTimer(0)
end
table.insert(socket.queue, {
f = k,
args = { ... },
})
oldWindow[k](...)
end
end
socket.term.getSize = function()
return termInfo.width, termInfo.height
end
end
local function telnetHost(socket, termInfo)
require = requireInjector(getfenv(1))
local process = require('process')
wrapTerm(socket, termInfo)
local shellThread = process:newThread('shell_wrapper', function()
os.run(getfenv(1), '/apps/shell')
socket:close()
end)
local queueThread = process:newThread('telnet_read', function()
while true do
local data = socket:read()
if not data then
break
end
if data.type == 'shellRemote' then
local event = table.remove(data.event, 1)
shellThread:resume(event, unpack(data.event))
end
end
end)
while true do
local e = process:pullEvent('timer')
if e == 'terminate' then
break
end
if not socket.connected then
break
end
if socket.queue then
socket:write(socket.queue)
socket.queue = nil
end
end
socket:close()
process:threadEvent('terminate')
end
process:newThread('telnet_server', function()
print('telnet: listening on port 23')
while true do
local socket = Socket.server(23)
print('telnet: connection from ' .. socket.dhost)
local termInfo = socket:read(5)
if termInfo then
multishell.openTab({
fn = telnetHost,
args = { socket, termInfo },
env = getfenv(1),
title = 'Telnet Client',
hidden = true,
})
end
end
end)

85
sys/network/vnc.lua Normal file
View File

@@ -0,0 +1,85 @@
local Socket = require('socket')
local process = require('process')
local function wrapTerm(socket, termInfo)
local methods = { 'blit', 'clear', 'clearLine', 'setCursorPos', 'write',
'setTextColor', 'setTextColour', 'setBackgroundColor',
'setBackgroundColour', 'scroll', 'setCursorBlink', }
socket.term = multishell.term
socket.oldTerm = Util.shallowCopy(socket.term)
for _,k in pairs(methods) do
socket.term[k] = function(...)
if not socket.queue then
socket.queue = { }
os.queueEvent('vnc_flush')
end
table.insert(socket.queue, {
f = k,
args = { ... },
})
socket.oldTerm[k](...)
end
end
socket.term.getSize = function()
return termInfo.width, termInfo.height
end
end
local function vncHost(socket, termInfo)
wrapTerm(socket, termInfo)
local queueThread = process:newThread('queue_writer', function()
while true do
os.pullEvent('vnc_flush')
if socket.queue then
socket:write(socket.queue)
socket.queue = nil
end
end
end)
os.queueEvent('term_resize')
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(unpack(data.event))
end
end
queueThread:terminate()
for k,v in pairs(socket.oldTerm) do
socket.term[k] = v
end
os.queueEvent('term_resize')
end
process:newThread('vnc_server', function()
print('vnc: listening on port 5900')
while true do
local socket = Socket.server(5900)
print('vnc: connection from ' .. socket.dhost)
local termInfo = socket:read(5)
if termInfo then
-- no new process - only 1 connection allowed
-- due to term size issues
vncHost(socket, termInfo)
else
socket:close()
end
end
end)