From 57ee7de7d40d0f27c1a137167f67f57959baa4f4 Mon Sep 17 00:00:00 2001 From: yflory Date: Thu, 11 May 2017 16:12:44 +0200 Subject: [PATCH 1/4] Update and return the storage limit --- config.example.js | 6 ++++ rpc.js | 62 ++++++++++++++++++++++++++++++++--- www/common/cryptpad-common.js | 9 ++++- www/common/pinpad.js | 23 +++++++++++++ 4 files changed, 94 insertions(+), 6 deletions(-) diff --git a/config.example.js b/config.example.js index 76bba6eae..1be17e510 100644 --- a/config.example.js +++ b/config.example.js @@ -116,6 +116,12 @@ module.exports = { 'contact', ], + /* Domain + * If you want to have enable payments on your CryptPad instance, it has to be able to tell + * our account server what is your domain + */ + // domain: 'https://cryptpad.fr', + /* You have the option of specifying an alternative storage adaptor. These status of these alternatives are specified in their READMEs, diff --git a/rpc.js b/rpc.js index ec6e516d0..dd84b6b29 100644 --- a/rpc.js +++ b/rpc.js @@ -7,10 +7,14 @@ var Nacl = require("tweetnacl"); var Fs = require("fs"); var Path = require("path"); +var Https = require("https"); var RPC = module.exports; var Store = require("./storage/file"); +var config = require('./config'); + +var DEFAULT_LIMIT = 100; var isValidChannel = function (chan) { return /^[a-fA-F0-9]/.test(chan) || @@ -454,8 +458,42 @@ var isPrivilegedUser = function (publicKey, cb) { }); }; -var getLimit = function (cb) { - cb = cb; // TODO +var limits = {}; +var updateLimits = function (publicKey, cb) { + if (typeof cb !== "function") { cb = function () {}; } + var domain = config.domain; + var options = { + host: 'accounts.cryptpad.fr', + path: '/api/getAuthorized?domain=' + encodeURIComponent(domain) + }; + var callback = function (response) { + var str = ''; + + response.on('data', function (chunk) { + str += chunk; + }); + + response.on('end', function () { + try { + var json = JSON.parse(str); + limits = json; + var l; + if (publicKey) { + l = typeof limits[publicKey] === "number" ? limits[publicKey] : DEFAULT_LIMIT; + } + cb(void 0, l); + } catch (e) { + cb(e); + } + }); + }; + Https.get(options, callback).on('error', function (e) { + console.error(e); + cb(e); + }); +}; +var getLimit = function (publicKey, cb) { + return void cb(null, typeof limits[publicKey] === "number" ? limits[publicKey] : DEFAULT_LIMIT); }; var safeMkdir = function (path, cb) { @@ -714,10 +752,16 @@ RPC.create = function (config /*:typeof(ConfigType)*/, cb /*:(?Error, ?Function) }); case 'GET_FILE_SIZE': return void getFileSize(ctx.store, msg[1], Respond); - case 'GET_LIMIT': // TODO implement this and cache it per-user - return void getLimit(function (e, limit) { + case 'UPDATE_LIMITS': + return void updateLimits(safeKey, function (e, limit) { + if (e) { return void Respond(e); } + Respond(void 0, limit); + }); + case 'GET_LIMIT': + return void getLimit(safeKey, function (e, limit) { + if (e) { return void Respond(e); } limit = limit; - Respond('NOT_IMPLEMENTED'); + Respond(void 0, limit); }); case 'GET_MULTIPLE_FILE_SIZE': return void getMultipleFileSize(ctx.store, msg[1], function (e, dict) { @@ -775,6 +819,14 @@ RPC.create = function (config /*:typeof(ConfigType)*/, cb /*:(?Error, ?Function) handleMessage(session.privilege); }; + var updateLimitDaily = function () { + updateLimits(function (e) { + if (e) { console.error('Error updating the storage limits', e); } + }); + }; + updateLimitDaily(); + setInterval(updateLimitDaily, 24*3600*1000); + Store.create({ filePath: pinPath, }, function (s) { diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 73b51e8b3..e63399ec1 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -744,8 +744,15 @@ define([ }); }; + common.updatePinLimit = function (cb) { + if (!pinsReady()) { return void cb('[RPC_NOT_READY]'); } + rpc.getFileListSize(cb); + }; + common.getPinLimit = function (cb) { - cb(void 0, typeof(AppConfig.pinLimit) === 'number'? AppConfig.pinLimit: 1000); + if (!pinsReady()) { return void cb('[RPC_NOT_READY]'); } + rpc.getFileListSize(cb); + //cb(void 0, typeof(AppConfig.pinLimit) === 'number'? AppConfig.pinLimit: 1000); }; common.isOverPinLimit = function (cb) { diff --git a/www/common/pinpad.js b/www/common/pinpad.js index 3470db34e..efa915ec7 100644 --- a/www/common/pinpad.js +++ b/www/common/pinpad.js @@ -121,6 +121,29 @@ define([ }); }; + // Update the limit value for all the users and return the limit for your publicKey + exp.updatePinLimits = function (cb) { + rpc.send('UPDATE_LIMITS', undefined, function (e, response) { + if (e) { return void cb(e); } + if (response && typeof response === "number") { + cb (void 0, response); + } else { + cb('INVALID_RESPONSE'); + } + }); + }; + // Get the storage limit associated with your publicKey + exp.getLimit = function (cb) { + rpc.send('GET_LIMIT', undefined, function (e, response) { + if (e) { return void cb(e); } + if (response && typeof response === "number") { + cb (void 0, response); + } else { + cb('INVALID_RESPONSE'); + } + }); + }; + cb(e, exp); }); }; From e123ad0333787e91d63589553e199e11e9c2a5c3 Mon Sep 17 00:00:00 2001 From: yflory Date: Thu, 11 May 2017 16:31:14 +0200 Subject: [PATCH 2/4] Use a POST request to get the storage limits --- rpc.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/rpc.js b/rpc.js index dd84b6b29..2e603d7fb 100644 --- a/rpc.js +++ b/rpc.js @@ -461,12 +461,23 @@ var isPrivilegedUser = function (publicKey, cb) { var limits = {}; var updateLimits = function (publicKey, cb) { if (typeof cb !== "function") { cb = function () {}; } - var domain = config.domain; + var body = JSON.stringify({ + domain: config.domain, + subdomain: config.subdomain + }); var options = { host: 'accounts.cryptpad.fr', - path: '/api/getAuthorized?domain=' + encodeURIComponent(domain) + path: '/api/getauthorized', + method: 'POST', + headers: { + "Content-Type": "application/json", + "Content-Length": Buffer.byteLength(body) + } }; - var callback = function (response) { + var req = Https.request(options, function (response) { + if (!('' + req.statusCode).match(/^2\d\d$/)) { + return void cb('SERVER ERROR ' + req.statusCode); + } var str = ''; response.on('data', function (chunk) { @@ -486,11 +497,14 @@ var updateLimits = function (publicKey, cb) { cb(e); } }); - }; - Https.get(options, callback).on('error', function (e) { + }); + + req.on('error', function (e) { console.error(e); cb(e); }); + + req.end(body); }; var getLimit = function (publicKey, cb) { return void cb(null, typeof limits[publicKey] === "number" ? limits[publicKey] : DEFAULT_LIMIT); From d2ba8f1c27d244e0595017aa6cbac506e8ea6400 Mon Sep 17 00:00:00 2001 From: yflory Date: Thu, 11 May 2017 18:07:29 +0200 Subject: [PATCH 3/4] Use the new format for the storage limits --- rpc.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/rpc.js b/rpc.js index 2e603d7fb..d9a619823 100644 --- a/rpc.js +++ b/rpc.js @@ -458,6 +458,8 @@ var isPrivilegedUser = function (publicKey, cb) { }); }; +// The limits object contains storage limits for all the publicKey that have paid +// To each key is associated an object containing the 'limit' value and a 'note' explaining that limit var limits = {}; var updateLimits = function (publicKey, cb) { if (typeof cb !== "function") { cb = function () {}; } @@ -490,7 +492,8 @@ var updateLimits = function (publicKey, cb) { limits = json; var l; if (publicKey) { - l = typeof limits[publicKey] === "number" ? limits[publicKey] : DEFAULT_LIMIT; + var limit = limits[publicKey]; + l = limit && typeof limit.limit === "number" ? limit.limit : DEFAULT_LIMIT; } cb(void 0, l); } catch (e) { @@ -507,7 +510,8 @@ var updateLimits = function (publicKey, cb) { req.end(body); }; var getLimit = function (publicKey, cb) { - return void cb(null, typeof limits[publicKey] === "number" ? limits[publicKey] : DEFAULT_LIMIT); + var limit = limits[publicKey]; + return limit && typeof limit.limit === "number" ? limit.limit : DEFAULT_LIMIT; }; var safeMkdir = function (path, cb) { From a249435003ac2e1af44beb977c03d17ca8f360ad Mon Sep 17 00:00:00 2001 From: ansuz Date: Fri, 12 May 2017 14:19:36 +0200 Subject: [PATCH 4/4] refactor limit handling a bit --- rpc.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rpc.js b/rpc.js index d9a619823..6e437e385 100644 --- a/rpc.js +++ b/rpc.js @@ -12,7 +12,6 @@ var Https = require("https"); var RPC = module.exports; var Store = require("./storage/file"); -var config = require('./config'); var DEFAULT_LIMIT = 100; @@ -461,8 +460,9 @@ var isPrivilegedUser = function (publicKey, cb) { // The limits object contains storage limits for all the publicKey that have paid // To each key is associated an object containing the 'limit' value and a 'note' explaining that limit var limits = {}; -var updateLimits = function (publicKey, cb) { +var updateLimits = function (config, publicKey, cb) { if (typeof cb !== "function") { cb = function () {}; } + var body = JSON.stringify({ domain: config.domain, subdomain: config.subdomain @@ -503,7 +503,7 @@ var updateLimits = function (publicKey, cb) { }); req.on('error', function (e) { - console.error(e); + if (!config.domain) { return cb(); } cb(e); }); @@ -771,7 +771,7 @@ RPC.create = function (config /*:typeof(ConfigType)*/, cb /*:(?Error, ?Function) case 'GET_FILE_SIZE': return void getFileSize(ctx.store, msg[1], Respond); case 'UPDATE_LIMITS': - return void updateLimits(safeKey, function (e, limit) { + return void updateLimits(config, safeKey, function (e, limit) { if (e) { return void Respond(e); } Respond(void 0, limit); }); @@ -838,7 +838,7 @@ RPC.create = function (config /*:typeof(ConfigType)*/, cb /*:(?Error, ?Function) }; var updateLimitDaily = function () { - updateLimits(function (e) { + updateLimits(config, undefined, function (e) { if (e) { console.error('Error updating the storage limits', e); } }); };