2019-03-30 00:03:53 +08:00
|
|
|
/* jshint esversion: 6 */
|
|
|
|
|
|
|
|
const nThen = require('nthen');
|
2020-02-04 03:20:05 +08:00
|
|
|
const RPC = require("./rpc");
|
2020-02-08 07:58:57 +08:00
|
|
|
const HK = require("./hk-util.js");
|
2020-02-17 22:01:10 +08:00
|
|
|
const Store = require("./storage/file");
|
|
|
|
const BlobStore = require("./storage/blob");
|
2020-03-28 01:38:27 +08:00
|
|
|
const Workers = require("./workers/index");
|
2020-10-12 20:09:53 +08:00
|
|
|
const Core = require("./commands/core");
|
2020-02-17 22:01:10 +08:00
|
|
|
|
2020-10-12 20:09:53 +08:00
|
|
|
module.exports.create = function (Env, cb) {
|
|
|
|
const Log = Env.Log;
|
2019-04-18 16:36:49 +08:00
|
|
|
Log.silly('HK_LOADING', 'LOADING HISTORY_KEEPER MODULE');
|
2019-03-30 00:03:53 +08:00
|
|
|
|
2020-10-12 20:09:53 +08:00
|
|
|
Env.historyKeeper = {
|
2020-02-08 07:58:57 +08:00
|
|
|
metadata_cache: Env.metadata_cache,
|
|
|
|
channel_cache: Env.channel_cache,
|
2019-09-04 17:51:33 +08:00
|
|
|
|
2020-02-08 07:58:57 +08:00
|
|
|
id: Env.id,
|
2020-02-04 03:20:05 +08:00
|
|
|
|
2021-01-19 21:53:38 +08:00
|
|
|
channelMessage: function (Server, channel, msgStruct, cb) {
|
2020-02-04 03:20:05 +08:00
|
|
|
// netflux-server emits 'channelMessage' events whenever someone broadcasts to a channel
|
|
|
|
// historyKeeper stores these messages if the channel id indicates that they are
|
|
|
|
// a channel type with permanent history
|
2021-01-19 21:53:38 +08:00
|
|
|
HK.onChannelMessage(Env, Server, channel, msgStruct, cb);
|
2020-02-03 23:03:43 +08:00
|
|
|
},
|
|
|
|
channelClose: function (channelName) {
|
2020-02-04 03:20:05 +08:00
|
|
|
// netflux-server emits 'channelClose' events whenever everyone leaves a channel
|
|
|
|
// we drop cached metadata and indexes at the same time
|
2020-02-08 07:58:57 +08:00
|
|
|
HK.dropChannel(Env, channelName);
|
2020-02-03 23:03:43 +08:00
|
|
|
},
|
2020-02-25 22:09:16 +08:00
|
|
|
channelOpen: function (Server, channelName, userId, wait) {
|
2020-02-18 00:31:00 +08:00
|
|
|
Env.channel_cache[channelName] = Env.channel_cache[channelName] || {};
|
2020-02-25 22:09:16 +08:00
|
|
|
|
|
|
|
var sendHKJoinMessage = function () {
|
|
|
|
Server.send(userId, [
|
|
|
|
0,
|
|
|
|
Env.id,
|
|
|
|
'JOIN',
|
|
|
|
channelName
|
|
|
|
]);
|
|
|
|
};
|
|
|
|
|
|
|
|
// a little backwards compatibility in case you don't have the latest server
|
|
|
|
// allow lists won't work unless you update, though
|
|
|
|
if (typeof(wait) !== 'function') { return void sendHKJoinMessage(); }
|
|
|
|
|
|
|
|
var next = wait();
|
|
|
|
var cb = function (err, info) {
|
|
|
|
next(err, info, sendHKJoinMessage);
|
|
|
|
};
|
|
|
|
|
|
|
|
// only conventional channels can be restricted
|
|
|
|
if ((channelName || "").length !== HK.STANDARD_CHANNEL_LENGTH) {
|
|
|
|
return void cb();
|
|
|
|
}
|
|
|
|
|
|
|
|
// gets and caches the metadata...
|
|
|
|
HK.getMetadata(Env, channelName, function (err, metadata) {
|
|
|
|
if (err) {
|
|
|
|
Log.error('HK_METADATA_ERR', {
|
|
|
|
channel: channelName,
|
|
|
|
error: err,
|
|
|
|
});
|
|
|
|
}
|
2023-01-17 04:22:57 +08:00
|
|
|
|
2023-01-24 23:58:23 +08:00
|
|
|
if (metadata && metadata.selfdestruct && metadata.selfdestruct !== Env.id) {
|
|
|
|
HK.expireChannel(Env, channelName);
|
|
|
|
return void cb('ESELFDESTRUCT');
|
|
|
|
}
|
|
|
|
|
2023-01-17 04:22:57 +08:00
|
|
|
if (Env.selfDestructTo && Env.selfDestructTo[channelName]) {
|
|
|
|
clearTimeout(Env.selfDestructTo[channelName]);
|
|
|
|
}
|
|
|
|
|
2020-02-25 22:09:16 +08:00
|
|
|
if (!metadata || (metadata && !metadata.restricted)) {
|
|
|
|
// the channel doesn't have metadata, or it does and it's not restricted
|
|
|
|
// either way, let them join.
|
|
|
|
return void cb();
|
|
|
|
}
|
|
|
|
|
|
|
|
// this channel is restricted. verify that the user in question is in the allow list
|
|
|
|
|
|
|
|
// construct a definitive list (owners + allowed)
|
|
|
|
var allowed = HK.listAllowedUsers(metadata);
|
|
|
|
// and get the list of keys for which this user has already authenticated
|
|
|
|
var session = HK.getNetfluxSession(Env, userId);
|
|
|
|
|
|
|
|
if (HK.isUserSessionAllowed(allowed, session)) {
|
|
|
|
return void cb();
|
|
|
|
}
|
|
|
|
|
2020-10-23 21:44:47 +08:00
|
|
|
// If the channel is restricted, send the history keeper ID so that they
|
|
|
|
// can try to authenticate
|
|
|
|
allowed.unshift(Env.id);
|
|
|
|
|
2020-02-25 22:09:16 +08:00
|
|
|
// otherwise they're not allowed.
|
|
|
|
// respond with a special error that includes the list of keys
|
|
|
|
// which would be allowed...
|
2020-02-25 22:48:41 +08:00
|
|
|
// FIXME RESTRICT bonus points if you hash the keys to limit data exposure
|
2020-02-25 22:09:16 +08:00
|
|
|
cb("ERESTRICTED", allowed);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
sessionClose: function (userId, reason) {
|
|
|
|
HK.closeNetfluxSession(Env, userId);
|
2020-03-27 05:11:43 +08:00
|
|
|
if (['BAD_MESSAGE', 'SEND_MESSAGE_FAIL_2'].indexOf(reason) !== -1) {
|
2020-02-25 22:09:16 +08:00
|
|
|
if (reason && reason.code === 'ECONNRESET') { return; }
|
|
|
|
return void Log.error('SESSION_CLOSE_WITH_ERROR', {
|
|
|
|
userId: userId,
|
|
|
|
reason: reason,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-25 22:48:41 +08:00
|
|
|
if (['SOCKET_CLOSED', 'SOCKET_ERROR'].indexOf(reason)) { return; }
|
2020-02-25 22:09:16 +08:00
|
|
|
Log.verbose('SESSION_CLOSE_ROUTINE', {
|
|
|
|
userId: userId,
|
|
|
|
reason: reason,
|
|
|
|
});
|
2020-02-03 23:03:43 +08:00
|
|
|
},
|
2020-02-04 04:47:41 +08:00
|
|
|
directMessage: function (Server, seq, userId, json) {
|
2020-02-04 03:20:05 +08:00
|
|
|
// netflux-server allows you to register an id with a handler
|
|
|
|
// this handler is invoked every time someone sends a message to that id
|
2020-02-08 07:58:57 +08:00
|
|
|
HK.onDirectMessage(Env, Server, seq, userId, json);
|
2020-02-03 23:03:43 +08:00
|
|
|
},
|
2019-03-30 00:03:53 +08:00
|
|
|
};
|
2020-02-04 03:20:05 +08:00
|
|
|
|
2020-02-08 07:58:57 +08:00
|
|
|
Log.verbose('HK_ID', 'History keeper ID: ' + Env.id);
|
|
|
|
|
2020-10-12 20:09:53 +08:00
|
|
|
var pinPath = Env.paths.pin;
|
|
|
|
|
2020-02-08 07:58:57 +08:00
|
|
|
nThen(function (w) {
|
2020-02-17 22:01:10 +08:00
|
|
|
// create a pin store
|
|
|
|
Store.create({
|
|
|
|
filePath: pinPath,
|
2020-12-01 08:33:32 +08:00
|
|
|
archivePath: Env.paths.archive,
|
2021-06-25 22:52:16 +08:00
|
|
|
// indicate that archives should be put in a 'pins' archvie folder
|
|
|
|
volumeId: 'pins',
|
2020-03-25 01:31:03 +08:00
|
|
|
}, w(function (err, s) {
|
|
|
|
if (err) { throw err; }
|
2020-02-17 22:01:10 +08:00
|
|
|
Env.pinStore = s;
|
|
|
|
}));
|
|
|
|
|
|
|
|
// create a channel store
|
2020-10-12 20:09:53 +08:00
|
|
|
Store.create({
|
|
|
|
filePath: Env.paths.data,
|
2020-12-01 08:33:32 +08:00
|
|
|
archivePath: Env.paths.archive,
|
2020-10-12 20:09:53 +08:00
|
|
|
}, w(function (err, _store) {
|
2020-03-25 01:31:03 +08:00
|
|
|
if (err) { throw err; }
|
2020-02-17 22:01:10 +08:00
|
|
|
Env.msgStore = _store; // API used by rpc
|
|
|
|
Env.store = _store; // API used by historyKeeper
|
|
|
|
}));
|
|
|
|
|
|
|
|
// create a blob store
|
|
|
|
BlobStore.create({
|
2020-10-12 20:09:53 +08:00
|
|
|
blobPath: Env.paths.blob,
|
|
|
|
blobStagingPath: Env.paths.staging,
|
|
|
|
archivePath: Env.paths.archive,
|
2020-02-17 22:01:10 +08:00
|
|
|
getSession: function (safeKey) {
|
2020-02-17 23:30:44 +08:00
|
|
|
return Core.getSession(Env.Sessions, safeKey);
|
2020-02-17 22:01:10 +08:00
|
|
|
},
|
|
|
|
}, w(function (err, blob) {
|
|
|
|
if (err) { throw new Error(err); }
|
|
|
|
Env.blobStore = blob;
|
2020-02-08 07:58:57 +08:00
|
|
|
}));
|
2020-03-19 22:46:18 +08:00
|
|
|
}).nThen(function (w) {
|
2020-03-28 01:38:27 +08:00
|
|
|
Workers.initialize(Env, {
|
2020-10-12 20:09:53 +08:00
|
|
|
blobPath: Env.paths.blob,
|
|
|
|
blobStagingPath: Env.paths.staging,
|
|
|
|
taskPath: Env.paths.task,
|
|
|
|
pinPath: Env.paths.pin,
|
|
|
|
filePath: Env.paths.data,
|
|
|
|
archivePath: Env.paths.archive,
|
2021-04-29 16:02:19 +08:00
|
|
|
blockPath: Env.paths.block,
|
2020-10-12 20:09:53 +08:00
|
|
|
|
|
|
|
inactiveTime: Env.inactiveTime,
|
|
|
|
archiveRetentionTime: Env.archiveRetentionTime,
|
|
|
|
accountRetentionTime: Env.accountRetentionTime,
|
|
|
|
|
|
|
|
maxWorkers: Env.maxWorkers,
|
2020-03-25 05:43:15 +08:00
|
|
|
}, w(function (err) {
|
2020-03-19 22:46:18 +08:00
|
|
|
if (err) {
|
|
|
|
throw new Error(err);
|
|
|
|
}
|
|
|
|
}));
|
2020-04-17 23:43:54 +08:00
|
|
|
}).nThen(function () {
|
2020-03-28 02:38:58 +08:00
|
|
|
var tasks_running;
|
2020-10-12 20:09:53 +08:00
|
|
|
Env.intervals.taskExpiration = setInterval(function () {
|
2020-10-13 13:26:40 +08:00
|
|
|
if (Env.disableIntegratedTasks) { return; }
|
2020-03-28 02:38:58 +08:00
|
|
|
if (tasks_running) { return; }
|
|
|
|
tasks_running = true;
|
|
|
|
Env.runTasks(function (err) {
|
|
|
|
if (err) {
|
|
|
|
Log.error('TASK_RUNNER_ERR', err);
|
|
|
|
}
|
|
|
|
tasks_running = false;
|
|
|
|
});
|
|
|
|
}, 1000 * 60 * 5); // run every five minutes
|
2020-09-30 17:12:14 +08:00
|
|
|
}).nThen(function () {
|
|
|
|
const ONE_DAY = 24 * 1000 * 60 * 60;
|
|
|
|
// setting the time of the last eviction to "now"
|
|
|
|
// effectively makes it so that we'll start evicting after the server
|
|
|
|
// has been up for at least one day
|
|
|
|
|
|
|
|
var active = false;
|
2020-10-12 20:09:53 +08:00
|
|
|
Env.intervals.eviction = setInterval(function () {
|
2020-10-13 13:26:40 +08:00
|
|
|
if (Env.disableIntegratedEviction) { return; }
|
2020-09-30 17:12:14 +08:00
|
|
|
if (active) { return; }
|
|
|
|
var now = +new Date();
|
|
|
|
// evict inactive data once per day
|
2020-10-12 20:09:53 +08:00
|
|
|
if ((now - ONE_DAY) < Env.lastEviction) { return; }
|
2020-09-30 17:12:14 +08:00
|
|
|
active = true;
|
2020-10-15 15:41:09 +08:00
|
|
|
Env.evictInactive(function (err, report) {
|
2020-09-30 17:12:14 +08:00
|
|
|
if (err) {
|
|
|
|
// NO_INACTIVE_TIME
|
|
|
|
Log.error('EVICT_INACTIVE_MAIN_ERROR', err);
|
|
|
|
}
|
|
|
|
active = false;
|
2020-10-12 20:09:53 +08:00
|
|
|
Env.lastEviction = now;
|
2020-10-15 15:41:09 +08:00
|
|
|
if (report) {
|
|
|
|
Log.info('EVICT_INACTIVE_REPORT', report);
|
|
|
|
}
|
|
|
|
Env.evictionReport = report || {};
|
2020-09-30 17:12:14 +08:00
|
|
|
});
|
|
|
|
}, 60 * 1000);
|
2020-02-08 07:58:57 +08:00
|
|
|
}).nThen(function () {
|
2020-10-02 15:24:17 +08:00
|
|
|
|
2020-02-17 22:01:10 +08:00
|
|
|
RPC.create(Env, function (err, _rpc) {
|
2020-02-08 07:58:57 +08:00
|
|
|
if (err) { throw err; }
|
|
|
|
|
|
|
|
Env.rpc = _rpc;
|
2020-10-12 20:09:53 +08:00
|
|
|
cb(void 0, Env.historyKeeper);
|
2020-02-08 07:58:57 +08:00
|
|
|
});
|
2020-02-04 03:20:05 +08:00
|
|
|
});
|
2019-03-30 00:03:53 +08:00
|
|
|
};
|