Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- CC-NFS: Computer Craft Networked File System
- description - Authentication-based File Server allowing for
- storing files across multiple computers from a
- single computer, securely.
- author - Maldaris
- version - v0.5 Beta
- ]]
- os.loadAPI('./lib/persistence');
- local directoryFormattedPaths = function(parent, children)
- local ret = {}
- for k,v in children do
- if fs.isDir(parent .. k) then
- ret[k] = v .. '/';
- end
- end
- return ret;
- end
- nfs = {
- timeout = 300, --Reauth Required every 5 Minutes
- sendAs = os.getComputerID(),
- registeredUsers = {},
- userSessions = {},
- }
- nfs.__index = nfs;
- function nfs:createUserSession(user)
- if(self.registeredUsers[user] == nil) then
- return nil
- end
- local session = {};
- session.crypt = {};
- --TODO: load crypto here
- session.lastRecieved = os.clock();
- return session;
- end -- function
- function nfs:auth(user, encoded)
- local challenge = "Hello World!";
- self.userSessions[user] = createUserSession(user);
- if self.userSessions[user] == nil then
- return false
- else
- local msg = self.userSessions.crypto.dec(encoded);
- if msg == challenge then
- return true
- else
- return false
- end
- end
- end -- function
- function nfs:isAuthenticated(user)
- if(userSessions and userSessions[user] ~= nil)
- local dt = os.clock();
- if(userSessions[user].lastRecieved - dt > self.timeout)
- return false
- else
- return userSessions[user].authenticated
- end
- else
- return false
- end
- end
- --[[
- ]]
- function nfs:list(user, path)
- if(self.isAuthenticated(user)) then
- --ignore private directories
- if path.sub(0,0) == '.' then
- return false, 'invalid directory'
- end
- --check if the path exists
- if fs.exists(path) then
- local directory = fs.list(path)
- directory = directoryFormattedPaths(directory);
- return true, directory;
- else
- return false, 'invalid directory';
- end -- exists
- else
- return false, 'Unauthenticated';
- end -- auth
- end -- list function
- function nfs:get(user, file)
- if(self.isAuthenticated(user)) then
- if fs.exists(path .. file) then
- return true, fs.open(path, 'r').readAll();
- else
- return false, 'invalid file path';
- end
- else
- return false, 'Unauthenticated';
- end
- end -- function
- function nfs:put(user, path, file, data, overwrite)
- if(self.isAuthenticated(user)) then
- if fs.exists(path) == false then
- fs.makeDir(path)
- end
- local flag = '';
- if overwrite == true then
- flag = 'w';
- else
- flag = 'a';
- end
- local file = fs.open(path .. file, flag);
- file.write(data);
- file.close();
- return true, nil;
- else
- return false, 'Unauthenticated';
- end
- end
- function nfs:setup(path, hostname)
- if fs.exists('./.&/users.tbl') then
- self.registeredUsers = persistence.load('./.&/users.tbl');
- else
- return nil;
- end
- if( fs.isDir(path) == false ) then
- return nil;
- else
- self.path = path;
- shell.path(self.path); -- set cwd
- end
- --last step, register the hostname
- self.hostname = hostname;
- rednet.host('nfs', hostname);
- self.setup = true;
- return self;
- end -- function
- function nfs:teardown():
- rednet.unhost('nfs', self.hostname);
- end
- function nfs:run()
- if(self.setup ~= true)
- return nil;
- else
- -- begin the event loop
- while true do
- id , msg = rednet.receive('nfs');
- if type(msg) == 'table' then
- local user = msg.user;
- local op = msg.op;
- if op == 'auth' then
- -- do auth
- local challenge = msg.challenge;
- rednet.send(id, self.auth(user, challenge), 'nfs');
- else if op == 'list' then
- -- do list
- local dir = msg.path;
- rednet.send(id, self.list(user, path), 'nfs');
- else if op == 'get' then
- -- do get
- local success, ret = self.get(user, msg.file);
- if success then
- rednet.send(id, ret, 'nfs');
- else
- rednet.send(id, 'Error:' .. ret, 'nfs');
- else if op == 'put' then
- -- do put
- local overwrite = msg.overwrite;
- local path = msg.path;
- local file = msg.file;
- local data = msg.data;
- local success, res = self.put(user, path, file, data, overwrite);
- if success then
- rednet.send(id, true, 'nfs');
- else
- rednet.send(id, 'Error:' .. res, 'nfs');
- else
- rednet.send(id, 'Malformed Command', 'nfs');
- end -- op
- end -- typeof
- end -- while
- end -- setup check
- end -- function
Advertisement
Add Comment
Please, Sign In to add comment