This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathlib.config.js
151 lines (134 loc) · 4.02 KB
/
lib.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const fs = require('fs');
const ini = require('loki-launcher/ini');
let disk_config = {};
let cache, storage;
// phase 1
const updateFromDisk = () => {
if (!fs.existsSync('loki.ini')) {
return false;
}
const ini_bytes = fs.readFileSync('loki.ini');
disk_config = ini.iniToJSON(ini_bytes.toString());
if (process.env.api__url) {
// console.log('setting api.api_url to', process.env.api__url, 'from environment');
disk_config.api.api_url = process.env.api__url;
}
if (process.env.admin__url) {
// console.log('setting api.admin_url to', process.env.api__url, 'from environment');
disk_config.api.admin_url = process.env.admin__url;
}
if (disk_config.api && disk_config.api.public_url) {
// strip any trailing slashes
if (disk_config.api.public_url.match(/\/$/)) {
console.log('Your loki.ini api.public_url has a trailing slash! Do not do that!');
disk_config.api.public_url = disk_config.api.public_url.replace(/\/$/, '');
}
}
return true;
}
// make sure we have some config loaded
// to configure cache
updateFromDisk();
// phase 2
const setup = (configObject) => {
// start setting things up
({ cache, storage } = configObject);
// now that we have cache
updateUserAccess();
// keep disk_config fresh-ish
setInterval(updateUserAccess, 15 * 60 * 1000); // every 15 mins
}
let user_access = {};
let whitelist_access = {};
const updateUserAccess = () => {
const configUtil = require('./server/lib/lib.config.js')
if (!updateFromDisk()) {
if (!configUtil.isQuiet()) {
console.log('overlay:::config.js - no loki.ini config file');
}
return;
}
const visualConfig = {...disk_config};
// don't put password in logs...
if (visualConfig.database) delete visualConfig.database.password;
console.log('config', visualConfig);
// reset permissions to purge any deletions
user_access = {};
// load globals pubkeys from file and set their access level
// if not array...
if (!disk_config.globals) {
if (!configUtil.isQuiet()) {
console.log('overlay:::config.js - no globals defined in loki.ini')
}
}
for(const pubKey in disk_config.globals) {
const access = disk_config.globals[pubKey];
// translate pubKey to id of user
cache.getUserID(pubKey, (err, user) => {
// only if user has registered
if (user) {
user_access[user.id] = access;
} else {
if (!configUtil.isQuiet()) {
console.log('global', pubKey, 'has not registered yet');
}
}
})
}
// optimal as long as requests outnumber number of entries...
if (disk_config.whitelist) {
whitelist_access = {};
for(const pubKey in disk_config.whitelist) {
// translate pubKey to id of user
cache.getUserID(pubKey, (err, user) => {
if (err) console.error('lib.config::updateUserAccess - getUserID err', err)
if (user) {
whitelist_access[user.id] = true;
} else {
if (!configUtil.isQuiet()) {
console.log('whitelist entry', pubKey, 'has not registered yet');
}
}
});
}
}
// user_access will always be empty here because async
};
const inWhiteListMode = () => {
return !!(disk_config.whitelist && Object.keys(disk_config.whitelist));
}
// FIXME: move out
const addTempModerator = async (userid) => {
console.log('Temporarily upgrading', userid, 'to global moderator');
await storage.addServerModerator(userid);
}
const getConfigGlobals = async() => {
const globals = [];
for(var uid in user_access) {
var access = user_access[uid]
if (access === true) {
globals.push(uid);
}
}
return globals;
}
// accessors:
const whitelistAllow = (userid) => {
return (!disk_config.whitelist) || whitelist_access[userid];
}
const globalAllow = (userid) => {
return user_access[userid];
}
module.exports = {
setup,
addTempModerator,
getConfigGlobals,
whitelistAllow,
globalAllow,
updateUserAccess,
inWhiteListMode,
getDiskConfig: () => {
// console.log('disk_config', disk_config);
return disk_config
},
};