Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filebrowser dev #184

Merged
merged 38 commits into from
May 11, 2016
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
a5fbfeb
Add get_users_list provider
mauricioschneider Oct 2, 2015
769baf8
Refactor as_logged_user to allow to specify custom user.
mauricioschneider Nov 12, 2015
e2210c2
Fix files provider to walk a dir. Add walkdir dependency.
mauricioschneider Nov 13, 2015
06e27a5
Add runner script to walk paths.
mauricioschneider Nov 13, 2015
2a5391c
Handle user and path options in get_tree.
mauricioschneider Nov 17, 2015
aa7643f
Add mime package and remove walkdir.
mauricioschneider Nov 18, 2015
d951852
Modify providers.get to handle either options args, callback args and…
mauricioschneider Nov 18, 2015
846b42d
Implement correct dirtree generation.
mauricioschneider Nov 18, 2015
01da418
Small refactor to as_user.
mauricioschneider Nov 18, 2015
ef5dce4
Fix copyright comment in users provider.
mauricioschneider Nov 19, 2015
1acc3d1
Fix indentation in tree.js
mauricioschneider Nov 19, 2015
cd62ef6
Small refactor to providers.get callback mess.
mauricioschneider Nov 19, 2015
9cf7dd2
Update © comment in mac script for users provider.
mauricioschneider Nov 19, 2015
4654414
Remove unnecessary comment.
mauricioschneider Nov 19, 2015
6e57df5
Remove filebrowser action.
mauricioschneider Nov 19, 2015
2516a83
Add fileretrieval action.
mauricioschneider Nov 19, 2015
32b167b
Refactor api/push url generation.
mauricioschneider Nov 19, 2015
2a058db
Fix shebangs for files/tree.js.
mauricioschneider Nov 19, 2015
677efe6
Mavri way
javo Dec 16, 2015
cd4f134
Mavris changes
javo Jan 7, 2016
3e9ff2c
Fix files depth
javo Mar 1, 2016
2be5e52
Empty folders case fixed
javo Mar 1, 2016
d54940d
Folders before files and hidden field added
javo Mar 2, 2016
ddff15b
Folder parameters fixed
javo Mar 2, 2016
9914029
Folders names with spaces case fixed
javo Mar 2, 2016
b60a1f3
Add get_users_list handler for linux
javo Mar 7, 2016
1b2f03a
Spaces in path case solved for all OS
javo Mar 15, 2016
078429d
Merge branch 'filebrowser-dev' of https://github.com/prey/prey-node-c…
javo Mar 15, 2016
bcafcd7
Check hidden files and upload service working
javo Mar 18, 2016
8c888f3
Upload retry added
javo Apr 22, 2016
f9a34d5
Check if id exists in DB and upload logic moved to index
javo May 2, 2016
6a15488
Resumable upload
javo May 3, 2016
bc3673a
Fix FR navigation on windows
javo May 5, 2016
e220473
Deleted console.log and comments
javo May 5, 2016
533fd3a
FR fixes
javo May 10, 2016
ba7b23c
FR final fixes
javo May 11, 2016
774273b
Fix variables groups and else's
javo May 11, 2016
869eda3
Remove extra lines
javo May 11, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 0 additions & 111 deletions lib/agent/actions/filebrowser/index.js

This file was deleted.

69 changes: 69 additions & 0 deletions lib/agent/actions/fileretrieval/files_storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
var join = require('path').join,
base_path = join(__dirname, '..', '..'),
common = require('./../../common'),
hooks = require('./../../hooks'),
triggers = require('./../../triggers'),
storage = require('./../../utils/storage'),
fileretrieval = require(join(base_path, 'actions', 'fileretrieval'));

var logger = common.logger;
watching = false;

var NAMESPACE = "fr/";

var exist = function(id, cb) {
var key = NAMESPACE + id;
storage.all(function(err, files) {
if (err)
return logger.error(err.message);
if (files[key])
return cb(true);
return cb(false);
})
}

exports.store = function(id, path, size, user) {
exist(id, function(cb) {
if (cb == false) {
logger.debug('Storing file_id in DB: ' + id);
var opts = {
path: path,
size: size,
user: user
}
storage.set(NAMESPACE + id, opts);
}
});
}

exports.del = function(id) {
logger.debug('Removing file_id from DB: ' + id);
storage.del(NAMESPACE + id);
}

exports.run_stored = function(cb) {
storage.all(function(err, files) {
if (err)
return logger.error(err.message);

var count = Object.keys(files).length;
if (count <= 0)
return;

logger.warn('Re-uploading ' + count + ' pending files.');

for (key in files) {
if(key.indexOf(NAMESPACE) != 0) {
continue;
}
var opts = {
file_id: key.substring(3, key.length),
path: files[key].path,
user: files[key].user,
size: files[key].size,
resumable: true
}
fileretrieval.start(opts, cb);
}
})
}
124 changes: 124 additions & 0 deletions lib/agent/actions/fileretrieval/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
"use strict";

//////////////////////////////////////////
// Prey JS FileRetrieval
// (C) 2016 Prey, Inc.
// by Mauricio Schneider and Javier Acuña - http://preyproject.com
// GPLv3 Licensed
//////////////////////////////////////////

var fs = require('fs'),
path = require('path'),
mime = require('mime'),
needle = require('needle'),
join = require('path').join,
api = require('./../../plugins/control-panel/api'),
files = require('./files_storage'),
EventEmitter = require('events').EventEmitter;

var common = require('./../../common'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all require calls should be in one var group and non require calls should be in a different var group

// First group
var a = require('a'),
  b = require('b');

// Second group
var something_with_a = a.foo(),
  something_with_b = b.bar();

system = common.system,
config = common.config,
protocol = config._values['control-panel'].protocol,
host = config._values['control-panel'].host,
url = protocol + '://' + host,
node_bin = path.join(system.paths.current, 'bin', 'node'),
logger = common.logger,
os_name = process.platform.replace('darwin', 'mac').replace('win32', 'windows');

var em,
cp;

var UPLOAD_SERVER = url + '/upload/upload',
RESUMABLE_HEADER = 'X-Prey-Upload-Resumable',
OPEN_TIMEOUT = 180000,
READ_TIMEOUT = 5000;

exports.check_pending_files = function() {
files.run_stored();
}

exports.start = function(options, cb) {
var file_path = options.path,
file_id = options.file_id,
file_size = parseInt(options.size),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value should already be a int, so callers of filetrieval.start should provide a numeric value for file_size

user = options.user;

em = em || new EventEmitter();
if (cb) cb(null, em);

if (!options.resumable)
files.store(file_id, file_path, file_size, user);

var file = {
total: 0,
path: file_path,
user: user,
id: file_id,
size: file_size,
resumable: options.resumable
}

retrieve_file(file, function() {
files.del(file.id);
});
}

function retrieve_file(file, cb) {
if (file.resumable) {
var url = UPLOAD_SERVER + '?uploadID=' + file.id;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a comment describing the protocol (for the community), saying that this call basically gets the last byte processed by the upload server in order to resume the upload from that position

// Make a call to get the last byte processed by the upload server
// before the disconnection in order to resume the upload from that position
needle.request('get', url, null, function(err, res) {
if (err) return em.emit(err);
var data = JSON.parse(res.body);
file.total = data.Total;
get_file(file, cb);
})
return;
}
get_file(file, cb);
}

function get_file(file, cb) {
var buffsize = (file.size == 0) ? 1 : (file.size - file.total);
var buf = new Buffer(buffsize);
var fd = fs.openSync(file.path, "r");

fs.read(fd, buf, 0, file.size - file.total, file.total, function(err, read, buf) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

process the err

if (err) do_something()

if (err) return em.emit(err);
upload_file(file, buf, cb);
})
}

function upload_file(file, buf, cb) {
var options = {
open_timeout: OPEN_TIMEOUT,
read_timeout: READ_TIMEOUT
};

if (file.total > 0) {
RESUMABLE_HEADER = file.total;
}
var url = UPLOAD_SERVER + '?uploadID='+file.id;
needle.post(url, buf, options, function(err, res) {
if (err) {
logger.error(err)
return em.emit(err);
}
var out = res.statusCode;
if (out !== 200 && out !== 201) {
var err = new Error('There was an error communicating with the server');
logger.error(err)
em.emit(err);
}
cb();
})
em.emit('end');
}

exports.stop = function() {
if (cp && !cp.exitCode) {
cp.kill();
}
}
2 changes: 1 addition & 1 deletion lib/agent/actions/wipe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ exports.start = function(opts, cb) {
})

child.on('exit', function(code) {
if (code != 0)
if (code !== 0)
last_err = new Error('Wipe command failed.');

finished(last_err);
Expand Down
1 change: 0 additions & 1 deletion lib/agent/actions/wipe/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ process.on('SIGTERM', function() {

process.on('exit', function(code) {
console.log('Wipe finished. Last error: ' + (last_err || 'none.'));
// process.exit(last_err ? 1 : 0);
})
15 changes: 9 additions & 6 deletions lib/agent/plugins/control-panel/api/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ var keys = require('./keys'),
errors = require('./errors'),
request = require('./request');

var format = '.json';

var check_keys = function() {
if (!keys.present())
throw errors.get('MISSING_KEY');
Expand All @@ -22,24 +20,29 @@ var post = function(url, data, opts, cb) {

exports.response = function(data, opts, cb) {
check_keys();
var url = '/devices/' + keys.get().device + '/response' + format;
var url = format_url('response');
post(url, data, opts, cb);
}

exports.event = function(data, opts, cb) {
check_keys();
var url = '/devices/' + keys.get().device + '/events' + format;
var url = format_url('events');
post(url, data, opts, cb);
}

exports.data = function(data, opts, cb) {
check_keys();
var url = '/devices/' + keys.get().device + '/data' + format;
var url = format_url('data');
post(url, data, opts, cb);
}

exports.report = function(data, opts, cb) {
check_keys();
var url = '/devices/' + keys.get().device + '/reports' + format;
var url = format_url(reports);
post(url, data, opts, cb);
}

function format_url(endpoint) {
var format = '.json';
return '/devices/' + keys.get().device + '/' + endpoint + format;
}
Loading