2016-12-11 19:24:52 +00:00
|
|
|
local Socket = require('socket')
|
2017-08-03 05:46:39 +00:00
|
|
|
local Event = require('event')
|
2016-12-11 19:24:52 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-08-03 05:46:39 +00:00
|
|
|
Event.addRoutine(function()
|
2016-12-11 19:24:52 +00:00
|
|
|
|
|
|
|
print('samba: listening on port 139')
|
|
|
|
|
|
|
|
while true do
|
|
|
|
local socket = Socket.server(139)
|
|
|
|
|
2017-08-05 08:17:06 +00:00
|
|
|
Event.addRoutine(function()
|
2017-08-03 05:46:39 +00:00
|
|
|
print('samba: connection from ' .. socket.dhost)
|
2016-12-11 19:24:52 +00:00
|
|
|
sambaConnection(socket)
|
|
|
|
print('samba: closing connection to ' .. socket.dhost)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2017-08-03 05:46:39 +00:00
|
|
|
Event.on('network_attach', function(e, computer)
|
|
|
|
fs.mount(fs.combine('network', computer.label), 'netfs', computer.id)
|
|
|
|
end)
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2017-08-03 05:46:39 +00:00
|
|
|
Event.on('network_detach', function(e, computer)
|
|
|
|
print('samba: detaching ' .. computer.label)
|
|
|
|
fs.unmount(fs.combine('network', computer.label))
|
2016-12-11 19:24:52 +00:00
|
|
|
end)
|