Personal Telegram developer console for GitHub

feat(hardening): add owner kill switch controls

davoodmehraban89 committed Oct 5, 2026 at 15:29 UTC fd0be73df38b5628d4a8882af300429d519fc766
1 file changed +43
src/kill-switch-bot.js
+43
@@ -2,10 +2,53 @@ import { GuidedNexaDevBot } from './guide-bot.js';
2 import { DurableKillSwitchStoreClient } from './core/kill-switch-store.js';
3 import { KillSwitch } from './core/kill-switch.js';
4
5 +const lockMarkup = mode => ({ inline_keyboard: [[
6 + mode === 'LOCKED'
7 + ? { text: '🔓 بازکردن اضطراری', callback_data: 'killswitch:unlock' }
8 + : { text: '🔒 قفل اضطراری', callback_data: 'killswitch:lock' }
9 +]] });
10 +
11 export class KillSwitchGuidedNexaDevBot extends GuidedNexaDevBot {
12 constructor(env = {}) {
13 super(env);
14 this.killSwitch = new KillSwitch({ store: new DurableKillSwitchStoreClient({ namespace: env.KILL_SWITCH_STORE }) });
15 this.router.killSwitch = this.killSwitch;
16 }
17 +
18 + async handleMessage(message) {
19 + const text = typeof message?.text === 'string' ? message.text.trim() : '';
20 + if (text === '/killswitch' || text === '/lockdown') {
21 + const state = await this.killSwitch.status();
22 + const locked = state.mode === 'LOCKED';
23 + const res = await this.telegram.sendMessage(message.chat.id,
24 + locked ? '🔒 <b>قفل اضطراری فعال است</b>\nWRITE، DESTRUCTIVE و ادامه Automation متوقف هستند؛ READ مجاز است.' : '🔓 <b>وضعیت عادی</b>\nقفل اضطراری غیرفعال است.',
25 + lockMarkup(state.mode));
26 + return { ok: true, killSwitch: true, mode: state.mode, res };
27 + }
28 + return super.handleMessage(message);
29 + }
30 +
31 + async handleCallbackQuery(query) {
32 + const data = query?.data || '';
33 + if (data !== 'killswitch:lock' && data !== 'killswitch:unlock' && data !== 'killswitch:confirm-lock' && data !== 'killswitch:confirm-unlock') return super.handleCallbackQuery(query);
34 + const chatId = query?.message?.chat?.id;
35 + const messageId = query?.message?.message_id;
36 + try { await this.telegram.answerCallbackQuery(query.id); } catch {}
37 + if (data === 'killswitch:lock' || data === 'killswitch:unlock') {
38 + const target = data.endsWith('lock') ? 'lock' : 'unlock';
39 + const label = target === 'lock' ? 'فعال‌کردن قفل اضطراری' : 'بازکردن قفل اضطراری';
40 + const res = await this.telegram.editMessageText(chatId, messageId,
41 + `⚠️ <b>${label}</b>\nاین تغییر فقط با تأیید صریح مالک اجرا می‌شود.`,
42 + { inline_keyboard: [[{ text: '✅ تأیید', callback_data: `killswitch:confirm-${target}` }],[{ text: '↩️ انصراف', callback_data: 'menu:main' }]] });
43 + return { ok: true, killSwitchApprovalRequired: true, res };
44 + }
45 + const target = data.endsWith('lock') ? 'lock' : 'unlock';
46 + const context = { isOwner: true, approved: true };
47 + const state = target === 'lock' ? await this.killSwitch.lock(context) : await this.killSwitch.unlock(context);
48 + this.audit.log({ event: 'kill_switch_control', outcome: 'success', mode: state.mode });
49 + const res = await this.telegram.editMessageText(chatId, messageId,
50 + state.mode === 'LOCKED' ? '🔒 <b>قفل اضطراری فعال شد</b>\nWRITE، DESTRUCTIVE و Automation متوقف شدند.' : '🔓 <b>قفل اضطراری باز شد</b>\nاجرای عملیات طبق Policy عادی ادامه دارد.',
51 + lockMarkup(state.mode));
52 + return { ok: true, killSwitch: true, mode: state.mode, res };
53 + }
54 }