1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-05 08:26:16 +00:00
opus/sys/network/samba.lua

84 lines
1.7 KiB
Lua
Raw Normal View History

local Event = require('event')
2016-12-11 19:24:52 +00:00
local Socket = require('socket')
2018-01-13 20:17:26 +00:00
local fs = _G.fs
2016-12-11 19:24:52 +00:00
local fileUid = 0
local fileHandles = { }
local function remoteOpen(fn, fl)
2018-01-24 22:39:38 +00:00
local fh = fs.open(fn, fl)
if fh then
local methods = { 'close', 'write', 'writeLine', 'flush', 'read', 'readLine', 'readAll', }
fileUid = fileUid + 1
fileHandles[fileUid] = fh
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
local vfh = {
methods = { },
fileUid = fileUid,
}
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
for _,m in ipairs(methods) do
if fh[m] then
table.insert(vfh.methods, m)
end
end
return vfh
end
2016-12-11 19:24:52 +00:00
end
local function remoteFileOperation(fileId, op, ...)
2018-01-24 22:39:38 +00:00
local fh = fileHandles[fileId]
if fh then
return fh[op](...)
end
2016-12-11 19:24:52 +00:00
end
local function sambaConnection(socket)
2018-01-24 22:39:38 +00:00
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
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
print('samba: Connection closed')
2016-12-11 19:24:52 +00:00
end
2017-08-03 05:46:39 +00:00
Event.addRoutine(function()
2018-01-24 22:39:38 +00:00
print('samba: listening on port 139')
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
while true do
local socket = Socket.server(139)
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
Event.addRoutine(function()
print('samba: connection from ' .. socket.dhost)
sambaConnection(socket)
print('samba: closing connection to ' .. socket.dhost)
end)
end
2016-12-11 19:24:52 +00:00
end)
2018-01-13 20:17:26 +00:00
Event.on('network_attach', function(_, computer)
2018-01-24 22:39:38 +00:00
fs.mount(fs.combine('network', computer.label), 'netfs', computer.id)
2017-08-03 05:46:39 +00:00
end)
2016-12-11 19:24:52 +00:00
2018-01-13 20:17:26 +00:00
Event.on('network_detach', function(_, computer)
2018-01-24 22:39:38 +00:00
print('samba: detaching ' .. computer.label)
fs.unmount(fs.combine('network', computer.label))
2016-12-11 19:24:52 +00:00
end)