levix-bot 4.7.0 → 4.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -7
- package/SETUP.md +21 -2
- package/app.cjs +69 -23
- package/package.json +3 -2
- package/public/dashboard/assets/index-B1sOW2VX.js +561 -0
- package/public/dashboard/assets/index-CK8osB4o.css +2 -0
- package/public/dashboard/index.html +2 -2
- package/public/dashboard.css +50 -0
- package/public/dashboard.js +25 -2
- package/public/gateway-i18n.js +26 -0
- package/src/bootstrap/session-startup-policy.js +6 -6
- package/src/commands/group/antilink.cjs +82 -59
- package/src/commands/group/antispam.cjs +112 -0
- package/src/commands/group/clearwarns.cjs +6 -6
- package/src/commands/group/demote.cjs +11 -13
- package/src/commands/group/flagged.cjs +52 -0
- package/src/commands/group/kick.cjs +25 -15
- package/src/commands/group/media.cjs +65 -29
- package/src/commands/group/policy.cjs +215 -0
- package/src/commands/group/promote.cjs +11 -12
- package/src/commands/group/rules.cjs +9 -6
- package/src/commands/group/setwarn.cjs +17 -27
- package/src/commands/group/warn.cjs +13 -63
- package/src/commands/group.cjs +5 -2
- package/src/commands/md.cjs +44 -0
- package/src/commands/mod.cjs +101 -26
- package/src/commands/translate.cjs +585 -0
- package/src/config/ai-identity.cjs +53 -1
- package/src/config/ai-persona.md +32 -73
- package/src/config/defaults.cjs +3 -0
- package/src/config/secrets.cjs +71 -3
- package/src/config/settings.cjs +22 -1
- package/src/core/socket.js +27 -5
- package/src/db/db.cjs +66 -0
- package/src/db/store.cjs +264 -16
- package/src/diagnostics/crashReports.cjs +35 -9
- package/src/diagnostics/debugLog.cjs +405 -0
- package/src/handlers/command.handler.js +3 -1
- package/src/handlers/group.handler.js +32 -1
- package/src/handlers/message.handler.js +53 -17
- package/src/middleware/antispam.middleware.js +95 -48
- package/src/panel/feedback.cjs +32 -5
- package/src/panel/one-time-code.cjs +156 -0
- package/src/panel/password-reset.cjs +187 -91
- package/src/panel/recovery-email.cjs +333 -0
- package/src/routes/dashboard.api.esm.js +341 -29
- package/src/services/aiAgent.cjs +25 -3
- package/src/services/moderationPolicy.cjs +895 -0
- package/src/services/settingsTransfer.cjs +4 -2
- package/src/utils/linkedCreds.cjs +22 -0
- package/src/utils/logger.cjs +93 -34
- package/src/utils/markdownParser.cjs +208 -136
- package/src/utils/moderationAction.cjs +637 -0
- package/src/utils/moderationSummary.cjs +225 -0
- package/src/utils/permissions.cjs +70 -0
- package/src/utils/permissions.esm.js +48 -8
- package/src/utils/storage.cjs +8 -0
- package/src/utils/storage.esm.js +4 -0
- package/views/login.ejs +100 -28
- package/public/dashboard/assets/index-RnT7DAde.js +0 -540
- package/public/dashboard/assets/index-UcqCdCvG.css +0 -2
- package/src/commands/group/antiSpam.cjs +0 -97
|
@@ -1,26 +1,33 @@
|
|
|
1
|
-
// Password reset over WhatsApp.
|
|
1
|
+
// Password reset over WhatsApp or a verified recovery email.
|
|
2
2
|
//
|
|
3
3
|
// The panel password is a one-way hash (src/config/secrets.cjs) — there is
|
|
4
|
-
// nothing to "recover"
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
// the
|
|
4
|
+
// nothing to "recover" from the hash. The operator proves they still hold a
|
|
5
|
+
// channel they set up: the linked WhatsApp account's own chat, or the recovery
|
|
6
|
+
// email saved in Settings. Both channels share one pending code, one throttle,
|
|
7
|
+
// and one `completeReset` (src/panel/one-time-code.cjs). A second request
|
|
8
|
+
// before the cooldown replaces nothing — it is refused, and the live code
|
|
9
|
+
// stays valid. An email code is bound to the address it was sent to. Removing
|
|
10
|
+
// or replacing that address drops it, and redeeming it is refused when the
|
|
11
|
+
// saved address is no longer that one. A WhatsApp code is not.
|
|
9
12
|
//
|
|
10
|
-
// Why a
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
// WhatsApp itself is down.
|
|
13
|
+
// Why a code rather than passkeys / biometrics (WebAuthn)? WebAuthn only
|
|
14
|
+
// exists in a secure context: HTTPS or localhost. A Levix panel is very often
|
|
15
|
+
// opened at plain http://<lan-ip>:3001, where no browser will offer a passkey
|
|
16
|
+
// at all. The code works on every shape of install, and `levix reset-password`
|
|
17
|
+
// on the server stays the offline fallback for when both channels are down.
|
|
16
18
|
//
|
|
17
19
|
// The code lives only in this process's memory — hashed, 10 minutes, five
|
|
18
20
|
// verification attempts, single use. A restart clears it, which is fine:
|
|
19
|
-
// requesting a new one costs
|
|
20
|
-
// (never `req.ip`, same discipline as login-throttle) and globally,
|
|
21
|
-
//
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
// requesting a new one costs one message. Requests are throttled per TCP peer
|
|
22
|
+
// (never `req.ip`, same discipline as login-throttle) and globally, and the
|
|
23
|
+
// throttle is the same whether the message goes to WhatsApp or to the relay.
|
|
24
|
+
//
|
|
25
|
+
// Five guesses per code and one code a minute still add up to 7,200 guesses
|
|
26
|
+
// a day against a 6-digit code. So wrong codes also count against one budget
|
|
27
|
+
// for the whole process: after MAX_FAILED_CODES_PER_DAY of them in 24 hours,
|
|
28
|
+
// reset is locked (requests and redemptions) until the oldest one ages out.
|
|
29
|
+
// That caps an attacker at about 1 in 100,000 per day. `levix reset-password`
|
|
30
|
+
// on the server is not affected.
|
|
24
31
|
|
|
25
32
|
const logger = require("../utils/logger.cjs");
|
|
26
33
|
const secrets = require("../config/secrets.cjs");
|
|
@@ -28,36 +35,69 @@ const brand = require("../config/brand.cjs");
|
|
|
28
35
|
const { sendBotMessage } = require("../utils/sendBotMessage.cjs");
|
|
29
36
|
const { withLang, detectLang, tr } = require("../utils/i18n.cjs");
|
|
30
37
|
const normalizeJid = require("../utils/normalizeJid.esm.js").default;
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
38
|
+
const recoveryEmail = require("./recovery-email.cjs");
|
|
39
|
+
const {
|
|
40
|
+
CODE_TTL_MS,
|
|
41
|
+
MAX_CODE_ATTEMPTS,
|
|
42
|
+
generateCode,
|
|
43
|
+
checkAttempt,
|
|
44
|
+
createThrottle,
|
|
45
|
+
} = require("./one-time-code.cjs");
|
|
39
46
|
|
|
40
47
|
// The backend's WhatsApp session manager, handed over by bootstrapPanel()
|
|
41
48
|
// next to the dashboard API's setSession(). Re-read the socket every time —
|
|
42
49
|
// a reconnect replaces it.
|
|
43
50
|
let session = null;
|
|
44
|
-
let pending = null; // { salt, hash, expiresAt, attempts }
|
|
45
|
-
|
|
46
|
-
|
|
51
|
+
let pending = null; // { salt, hash, expiresAt, attempts, channel, email }
|
|
52
|
+
const throttle = createThrottle();
|
|
53
|
+
// Bumped by every reset request and by an address change. The commit after
|
|
54
|
+
// `await` stores a code only when it still owns this number, so a later
|
|
55
|
+
// request (or a removed address) cannot lose to an older in-flight send.
|
|
56
|
+
let resetGeneration = 0;
|
|
57
|
+
let emailGeneration = 0;
|
|
58
|
+
|
|
59
|
+
const FAILURE_WINDOW_MS = 24 * 60 * 60 * 1000;
|
|
60
|
+
const MAX_FAILED_CODES_PER_DAY = 10;
|
|
61
|
+
/** Times of the wrong codes inside the window, oldest first. */
|
|
62
|
+
let failures = [];
|
|
63
|
+
|
|
64
|
+
/** Seconds until reset unlocks, or 0. */
|
|
65
|
+
function lockedFor(now = Date.now()) {
|
|
66
|
+
failures = failures.filter((at) => now - at < FAILURE_WINDOW_MS);
|
|
67
|
+
if (failures.length < MAX_FAILED_CODES_PER_DAY) return 0;
|
|
68
|
+
return Math.max(1, Math.ceil((failures[0] + FAILURE_WINDOW_MS - now) / 1000));
|
|
69
|
+
}
|
|
47
70
|
|
|
48
71
|
function setSession(manager) {
|
|
49
72
|
session = manager;
|
|
50
73
|
}
|
|
51
74
|
|
|
52
|
-
/** Tests: drop the in-memory code and throttle
|
|
75
|
+
/** Tests: drop the in-memory reset code and the shared reset throttle. */
|
|
53
76
|
function _resetForTests() {
|
|
54
77
|
pending = null;
|
|
55
|
-
|
|
56
|
-
|
|
78
|
+
failures = [];
|
|
79
|
+
throttle.reset();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Tests: drop only the send throttle, keeping codes and the failure budget. */
|
|
83
|
+
function _resetThrottleForTests() {
|
|
84
|
+
throttle.reset();
|
|
57
85
|
}
|
|
58
86
|
|
|
59
|
-
function
|
|
60
|
-
|
|
87
|
+
function _expireForTests() {
|
|
88
|
+
if (pending) pending.expiresAt = Date.now() - 1;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The verified recovery address changed. Drop an email reset bound to the
|
|
93
|
+
* previous one. A WhatsApp code is left alone. An email send still in flight
|
|
94
|
+
* sees `emailGeneration` move and does not become the pending code.
|
|
95
|
+
*
|
|
96
|
+
* @param {string | null} address the address now on file, or null
|
|
97
|
+
*/
|
|
98
|
+
function noteRecoveryAddress(address) {
|
|
99
|
+
if (pending?.channel === "email" && pending.email !== address) pending = null;
|
|
100
|
+
emailGeneration += 1;
|
|
61
101
|
}
|
|
62
102
|
|
|
63
103
|
function codeMessage(code) {
|
|
@@ -73,65 +113,106 @@ function codeMessage(code) {
|
|
|
73
113
|
);
|
|
74
114
|
}
|
|
75
115
|
|
|
116
|
+
function resolveChannel(channel) {
|
|
117
|
+
if (channel == null || channel === "" || channel === "whatsapp") return "whatsapp";
|
|
118
|
+
if (channel === "email") return "email";
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
|
|
76
122
|
/**
|
|
77
|
-
* Generate a reset code and
|
|
123
|
+
* Generate a reset code and deliver it.
|
|
124
|
+
*
|
|
125
|
+
* `options.channel` is `"whatsapp"` (the default, including a missing value)
|
|
126
|
+
* or `"email"`. `options.lang` is `"en"` or `"ar"` and is sent to the relay;
|
|
127
|
+
* anything else is English. The WhatsApp text keeps the bot's reply language.
|
|
78
128
|
*
|
|
129
|
+
* @param {string} peer TCP peer, from clientAddress() — never req.ip
|
|
130
|
+
* @param {{ channel?: string, lang?: string }} [options]
|
|
79
131
|
* @returns {Promise<object>} { ok: true } or { ok: false, reason, ... }
|
|
80
132
|
*/
|
|
81
|
-
async function requestResetCode(peer) {
|
|
133
|
+
async function requestResetCode(peer, options = {}) {
|
|
134
|
+
const opts = options && typeof options === "object" ? options : {};
|
|
135
|
+
const channel = resolveChannel(opts.channel);
|
|
136
|
+
if (!channel) return { ok: false, reason: "invalid" };
|
|
137
|
+
|
|
138
|
+
const lang = opts.lang === "ar" ? "ar" : "en";
|
|
139
|
+
|
|
82
140
|
const now = Date.now();
|
|
141
|
+
const locked = lockedFor(now);
|
|
142
|
+
if (locked) return { ok: false, reason: "locked", retryAfter: locked };
|
|
83
143
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const
|
|
88
|
-
if (
|
|
89
|
-
return {
|
|
90
|
-
ok: false,
|
|
91
|
-
reason: "throttled",
|
|
92
|
-
retryAfter: Math.ceil((REQUEST_WINDOW_MS - (now - entry.firstAt)) / 1000),
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
const sinceLast = now - lastRequestAt;
|
|
96
|
-
if (lastRequestAt && sinceLast < GLOBAL_COOLDOWN_MS) {
|
|
97
|
-
return {
|
|
98
|
-
ok: false,
|
|
99
|
-
reason: "throttled",
|
|
100
|
-
retryAfter: Math.ceil((GLOBAL_COOLDOWN_MS - sinceLast) / 1000),
|
|
101
|
-
};
|
|
102
|
-
}
|
|
144
|
+
const key = String(peer ?? "");
|
|
145
|
+
// An open cooldown answers first, including when the session or address is
|
|
146
|
+
// already gone. The check does not take a slot.
|
|
147
|
+
const early = throttle.check(key, now);
|
|
148
|
+
if (!early.ok) return { ok: false, reason: early.reason, retryAfter: early.retryAfter };
|
|
103
149
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
150
|
+
// Preconditions before a code exists, so a missing channel does not replace
|
|
151
|
+
// a live code and does not spend the cooldown.
|
|
152
|
+
let record = null;
|
|
153
|
+
let sock = null;
|
|
154
|
+
if (channel === "email") {
|
|
155
|
+
record = recoveryEmail.getRecord();
|
|
156
|
+
if (!record) {
|
|
157
|
+
return {
|
|
158
|
+
ok: false,
|
|
159
|
+
reason: recoveryEmail.publicStatus().needsVerification ? "unverified" : "no_email",
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
} else {
|
|
163
|
+
sock = session?.socket ?? null;
|
|
164
|
+
if (!sock?.user?.id) return { ok: false, reason: "no_session" };
|
|
107
165
|
}
|
|
166
|
+
const address = record?.email ?? null;
|
|
108
167
|
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
pending = { salt, hash: hashCode(code, salt), expiresAt: now + CODE_TTL_MS, attempts: 0 };
|
|
168
|
+
const gate = throttle.reserve(key, now);
|
|
169
|
+
if (!gate.ok) return { ok: false, reason: gate.reason, retryAfter: gate.retryAfter };
|
|
112
170
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
normalizeJid(sock.user.id),
|
|
117
|
-
{ text: codeMessage(code) },
|
|
118
|
-
{
|
|
119
|
-
typing: false,
|
|
120
|
-
delayMs: 0,
|
|
121
|
-
},
|
|
122
|
-
);
|
|
123
|
-
} catch (error) {
|
|
124
|
-
pending = null;
|
|
125
|
-
logger.error({ err: error }, "[reset] failed to send the reset code over WhatsApp");
|
|
126
|
-
return { ok: false, reason: "send_failed" };
|
|
127
|
-
}
|
|
171
|
+
const generated = generateCode(now);
|
|
172
|
+
const generation = ++resetGeneration;
|
|
173
|
+
const emailGen = channel === "email" ? ++emailGeneration : 0;
|
|
128
174
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
175
|
+
if (channel === "email") {
|
|
176
|
+
const sent = await recoveryEmail.sendResetCode({
|
|
177
|
+
to: record.email,
|
|
178
|
+
token: record.token,
|
|
179
|
+
code: generated.code,
|
|
180
|
+
lang,
|
|
181
|
+
});
|
|
182
|
+
if (!sent.ok) {
|
|
183
|
+
throttle.release(gate.reservation);
|
|
184
|
+
return sent;
|
|
185
|
+
}
|
|
186
|
+
logger.info({ channel }, "[reset] Reset code sent by email");
|
|
187
|
+
} else {
|
|
188
|
+
try {
|
|
189
|
+
await sendBotMessage(
|
|
190
|
+
sock,
|
|
191
|
+
normalizeJid(sock.user.id),
|
|
192
|
+
{ text: codeMessage(generated.code) },
|
|
193
|
+
{ typing: false, delayMs: 0 },
|
|
194
|
+
);
|
|
195
|
+
} catch (error) {
|
|
196
|
+
throttle.release(gate.reservation);
|
|
197
|
+
logger.error({ err: error }, "[reset] failed to send the reset code over WhatsApp");
|
|
198
|
+
return { ok: false, reason: "send_failed" };
|
|
199
|
+
}
|
|
200
|
+
logger.info({ ip: peer }, "[reset] Reset code sent over WhatsApp");
|
|
201
|
+
}
|
|
132
202
|
|
|
133
|
-
|
|
134
|
-
|
|
203
|
+
// Only a code the operator can actually have is stored. A failed send leaves
|
|
204
|
+
// any previous code in place. A newer request, or an address change while
|
|
205
|
+
// this send was in flight, owns the slot instead.
|
|
206
|
+
if (generation !== resetGeneration) return { ok: false, reason: "throttled" };
|
|
207
|
+
if (channel === "email" && (emailGen !== emailGeneration || recoveryEmail.getAddress() !== address)) {
|
|
208
|
+
return { ok: false, reason: "no_email" };
|
|
209
|
+
}
|
|
210
|
+
pending = {
|
|
211
|
+
...generated.pending,
|
|
212
|
+
channel,
|
|
213
|
+
email: channel === "email" ? address : null,
|
|
214
|
+
};
|
|
215
|
+
return { ok: true, channel, expiresInMinutes: Math.round(CODE_TTL_MS / 60000) };
|
|
135
216
|
}
|
|
136
217
|
|
|
137
218
|
/**
|
|
@@ -143,24 +224,32 @@ async function requestResetCode(peer) {
|
|
|
143
224
|
*/
|
|
144
225
|
function completeReset(code, password, confirm) {
|
|
145
226
|
if (!pending) return { ok: false, reason: "no_pending" };
|
|
227
|
+
const locked = lockedFor();
|
|
228
|
+
if (locked) {
|
|
229
|
+
pending = null;
|
|
230
|
+
return { ok: false, reason: "locked", retryAfter: locked };
|
|
231
|
+
}
|
|
146
232
|
if (Date.now() > pending.expiresAt) {
|
|
147
233
|
pending = null;
|
|
148
234
|
return { ok: false, reason: "expired" };
|
|
149
235
|
}
|
|
236
|
+
if (pending.channel === "email" && recoveryEmail.getAddress() !== pending.email) {
|
|
237
|
+
pending = null;
|
|
238
|
+
return { ok: false, reason: "address_changed" };
|
|
239
|
+
}
|
|
150
240
|
if (String(password ?? "") !== String(confirm ?? "")) {
|
|
151
241
|
return { ok: false, reason: "mismatch" };
|
|
152
242
|
}
|
|
153
243
|
|
|
154
|
-
pending
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
const attemptsLeft = MAX_CODE_ATTEMPTS - pending.attempts;
|
|
159
|
-
if (attemptsLeft <= 0) {
|
|
160
|
-
pending = null;
|
|
161
|
-
return { ok: false, reason: "too_many_attempts" };
|
|
244
|
+
const result = checkAttempt(pending, code);
|
|
245
|
+
if (!result.ok) {
|
|
246
|
+
if (result.reason === "wrong_code" || result.reason === "too_many_attempts") {
|
|
247
|
+
failures.push(Date.now());
|
|
162
248
|
}
|
|
163
|
-
|
|
249
|
+
if (result.clear) pending = null;
|
|
250
|
+
const failure = { ok: false, reason: result.reason };
|
|
251
|
+
if (result.attemptsLeft != null) failure.attemptsLeft = result.attemptsLeft;
|
|
252
|
+
return failure;
|
|
164
253
|
}
|
|
165
254
|
|
|
166
255
|
try {
|
|
@@ -168,20 +257,27 @@ function completeReset(code, password, confirm) {
|
|
|
168
257
|
// moment — including any an attacker might hold — stops being valid.
|
|
169
258
|
secrets.setDashboardPassword(password);
|
|
170
259
|
} catch (error) {
|
|
171
|
-
// The code
|
|
260
|
+
// The code matched, so checkAttempt already counted this guess. A rejected
|
|
261
|
+
// new password is the operator's typo, not a failed proof — give the
|
|
262
|
+
// attempt back. The code stays pending.
|
|
263
|
+
pending.attempts = Math.max(0, pending.attempts - 1);
|
|
172
264
|
return { ok: false, reason: "invalid_password", message: error.message };
|
|
173
265
|
}
|
|
174
266
|
|
|
175
267
|
pending = null;
|
|
176
|
-
logger.info("[reset] Panel password reset via
|
|
268
|
+
logger.info("[reset] Panel password reset via one-time code");
|
|
177
269
|
return { ok: true };
|
|
178
270
|
}
|
|
179
271
|
|
|
180
272
|
module.exports = {
|
|
181
273
|
CODE_TTL_MS,
|
|
182
274
|
MAX_CODE_ATTEMPTS,
|
|
275
|
+
MAX_FAILED_CODES_PER_DAY,
|
|
183
276
|
setSession,
|
|
184
277
|
requestResetCode,
|
|
185
278
|
completeReset,
|
|
279
|
+
noteRecoveryAddress,
|
|
186
280
|
_resetForTests,
|
|
281
|
+
_resetThrottleForTests,
|
|
282
|
+
_expireForTests,
|
|
187
283
|
};
|