| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- import { randomUUID, randomBytes, createHmac } from 'node:crypto';
- import { lookup } from 'node:dns/promises';
- import { isIP, BlockList } from 'node:net';
- import http from 'node:http';
- import https from 'node:https';
- import { classifyMessage } from './message-signals.js';
- const MODES = new Set(['all', 'mention', 'direct', 'mention_or_direct']);
- const UUID = /^[a-f\d]{8}(?:-[a-f\d]{4}){3}-[a-f\d]{12}$/i;
- const blocked = new BlockList();
- for (const [address, prefix] of [['0.0.0.0',8],['10.0.0.0',8],['100.64.0.0',10],['127.0.0.0',8],['169.254.0.0',16],['172.16.0.0',12],['192.0.0.0',24],['192.0.2.0',24],['192.168.0.0',16],['198.18.0.0',15],['198.51.100.0',24],['203.0.113.0',24],['224.0.0.0',4],['240.0.0.0',4]]) blocked.addSubnet(address,prefix,'ipv4');
- const globalV6 = new BlockList(); globalV6.addSubnet('2000::',3,'ipv6');
- for (const [address,prefix] of [['2001:db8::',32],['2001::',32],['2002::',16]]) blocked.addSubnet(address,prefix,'ipv6');
- const hostText = host => host.replace(/^\[|\]$/g, '');
- const loopback = address => address === '::1' || /^127\.\d+\.\d+\.\d+$/.test(address);
- export function isAllowedWebhookAddress(address, allowLocal = false) {
- const family = isIP(address);
- if (allowLocal && loopback(address)) return true;
- return family === 4 ? !blocked.check(address,'ipv4') : family === 6 && globalV6.check(address,'ipv6') && !blocked.check(address,'ipv6');
- }
- export function validateWebhookUrl(raw, allowLocal = false) {
- try {
- if (typeof raw !== 'string' || raw.length > 4096 || /[\u0000-\u0020\u007f]/.test(raw)) throw new Error();
- const url = new URL(raw), host = hostText(url.hostname);
- if (url.username || url.password || url.hash || !['http:','https:'].includes(url.protocol)) throw new Error();
- if (url.protocol === 'http:' && !(allowLocal && (host === 'localhost' || loopback(host)))) throw new Error();
- if (host === 'localhost' && !allowLocal || isIP(host) && !isAllowedWebhookAddress(host,allowLocal)) throw new Error();
- return url;
- } catch { throw new Error('WebHook URL 无效。默认仅支持公网 HTTPS;本机 HTTP 接收器需明确 allowLocal=true。'); }
- }
- function abortable(promise, signal) {
- return new Promise((resolve,reject) => {
- const abort = () => reject(new Error('cancelled'));
- if (signal.aborted) return abort();
- signal.addEventListener('abort',abort,{once:true});
- promise.then(resolve,reject).finally(() => signal.removeEventListener('abort',abort));
- });
- }
- /** DNS results are verified then pinned into the actual socket lookup. No redirects. */
- export async function sendWebhook({ url: raw, allowLocal, body, headers, signal }, { resolveHost = lookup } = {}) {
- const url = validateWebhookUrl(raw,allowLocal), host = hostText(url.hostname);
- const timeout = AbortSignal.timeout(10_000), combined = signal ? AbortSignal.any([signal,timeout]) : timeout;
- const addresses = isIP(host) ? [{address:host,family:isIP(host)}] : await abortable(resolveHost(host,{all:true,verbatim:true}),combined);
- if (!addresses.length || addresses.length > 32 || addresses.some(({address}) => !isAllowedWebhookAddress(address,allowLocal))) throw new Error('destination-blocked');
- return new Promise((resolve,reject) => {
- const request = (url.protocol === 'https:' ? https : http).request(url, { method:'POST',agent:false,signal:combined,
- headers:{...headers,'Content-Type':'application/json','Content-Length':Buffer.byteLength(body)},
- lookup: (_host,options,callback) => options?.all ? callback(null,addresses) : callback(null,addresses[0].address,addresses[0].family),
- }, response => {
- // Only status and Retry-After are useful; receiver bodies may contain secrets.
- resolve({ status:response.statusCode, retryAfter:response.headers['retry-after'] || null }); response.destroy();
- });
- request.on('error',() => reject(new Error('delivery-failed'))); request.end(body);
- });
- }
- const parseConfig = row => ({...row,config:JSON.parse(row.config)});
- function publicSubscription(row) {
- const {secret,url,...safe} = row, parsed = new URL(url);
- return {...safe,enabled:Boolean(row.enabled),endpointOrigin:parsed.origin,hasSecret:true};
- }
- function cleanIds(value, fallback = []) {
- if (value === undefined) return fallback;
- if (!Array.isArray(value) || value.length > 100 || value.some(id => typeof id !== 'string' || !id || id.length > 2000 || /[\u0000-\u001f]/.test(id))) throw new Error('账号/聊天过滤必须是最多 100 个标识的数组。');
- return [...new Set(value)];
- }
- function retryDelay(value, now) {
- if (typeof value !== 'string') return 0;
- const milliseconds = /^\d+$/.test(value) ? Number(value)*1000 : Date.parse(value)-now;
- // An unreasonable delay needs manual review, never an earlier-than-requested retry.
- return milliseconds > 7*86400_000 ? Infinity : Number.isFinite(milliseconds) && milliseconds > 0 ? milliseconds : 0;
- }
- export function createWebhookService({ store, classify = classifyMessage, send = sendWebhook, now = Date.now,
- schedule = setTimeout, unschedule = clearTimeout } = {}) {
- let active = null, running = false, timer = null, controller = null, generation = 0;
- const at = () => new Date(now()).toISOString();
- const latest = () => Number(store.readOne('SELECT COALESCE(MAX(seq),0) AS n FROM message_live_observations').n);
- const get = id => { const row = store.readOne('SELECT * FROM webhook_subscriptions WHERE id=?',[id]); return row ? parseConfig(row) : null; };
- function configure(input) {
- if (!input || typeof input !== 'object' || Array.isArray(input)) throw new Error('订阅参数无效。');
- const allowed = new Set(['id','name','url','enabled','mode','accountIds','chatIds','includeSelf','includeContent','allowLocal']);
- if (Object.keys(input).some(k => !allowed.has(k))) throw new Error('包含未知订阅参数。');
- if (input.id && !UUID.test(input.id)) throw new Error('订阅 ID 无效。');
- return store.transaction(() => {
- const old = input.id ? get(input.id) : null;
- if (input.id && !old) throw new Error('找不到订阅。');
- if (!old && Number(store.readOne('SELECT COUNT(*) n FROM webhook_subscriptions').n)>=100) throw new Error('订阅上限为 100 个。');
- for (const key of ['enabled','includeSelf','includeContent','allowLocal']) if (input[key] !== undefined && typeof input[key] !== 'boolean') throw new Error('订阅开关必须是布尔值。');
- const config = { mode:input.mode ?? old?.config.mode ?? 'mention_or_direct',
- accountIds:cleanIds(input.accountIds,old?.config.accountIds),chatIds:cleanIds(input.chatIds,old?.config.chatIds),
- includeSelf:input.includeSelf ?? old?.config.includeSelf ?? false, includeContent:input.includeContent ?? old?.config.includeContent ?? false,
- allowLocal:input.allowLocal ?? old?.config.allowLocal ?? false };
- if (!MODES.has(config.mode)) throw new Error('提醒模式必须为 all、mention、direct 或 mention_or_direct。');
- const url = validateWebhookUrl(input.url ?? old?.url,config.allowLocal).href;
- const name = input.name ?? old?.name ?? '消息提醒';
- if (typeof name !== 'string' || !name.trim() || name.length > 100) throw new Error('订阅名称必须为 1–100 个字符。');
- const id = old?.id || randomUUID(), secret = old?.secret || randomBytes(32).toString('base64url');
- const enabled = input.enabled ?? Boolean(old?.enabled), timestamp = at();
- config.activeSince=timestamp;
- // Updating scope/destination starts a new subscription epoch. Old queued
- // messages cannot be sent to a newly configured recipient or expanded scope.
- store.execute(`INSERT INTO webhook_subscriptions(id,name,url,secret,config,enabled,revision,cursor,createdAt,updatedAt)
- VALUES(?,?,?,?,?,?,?,?,?,?) ON CONFLICT(id) DO UPDATE SET name=excluded.name,url=excluded.url,config=excluded.config,
- enabled=excluded.enabled,revision=excluded.revision,cursor=excluded.cursor,updatedAt=excluded.updatedAt`,
- [id,name,url,secret,JSON.stringify(config),Number(enabled),(old?.revision || 0)+1,latest(),old?.createdAt || timestamp,timestamp]);
- store.execute("UPDATE webhook_deliveries SET status='cancelled',leaseToken=NULL,leaseUntil=NULL,updatedAt=? WHERE subscriptionId=? AND status IN ('pending','inflight','dead')",[timestamp,id]);
- return {subscription:publicSubscription(get(id)),...(!old ? {signingSecret:secret} : {})};
- });
- }
- const list = () => ({items:store.readRows('SELECT * FROM webhook_subscriptions ORDER BY createdAt,id').map(row=>publicSubscription(parseConfig(row)))});
- function remove({id}) {
- if (!UUID.test(id || '')) throw new Error('订阅 ID 无效。');
- return {removed:Number(store.execute('DELETE FROM webhook_subscriptions WHERE id=?',[id]).changes)>0};
- }
- function listDeliveries({subscriptionId,status,limit=50} = {}) {
- if (!Number.isInteger(limit) || limit<1 || limit>100) throw new Error('limit 必须为 1–100。');
- if (subscriptionId && !UUID.test(subscriptionId)) throw new Error('订阅 ID 无效。');
- if (status && !['pending','inflight','sent','dead','cancelled'].includes(status)) throw new Error('投递状态无效。');
- const where=[],params=[];
- if (subscriptionId) {where.push('subscriptionId=?');params.push(subscriptionId);}
- if (status) {where.push('status=?');params.push(status);}
- return {items:store.readRows(`SELECT id,subscriptionId,seq,status,attempts,nextAttemptAt,lastStatus,lastError,createdAt,updatedAt FROM webhook_deliveries ${where.length?'WHERE '+where.join(' AND '):''} ORDER BY createdAt DESC,id DESC LIMIT ?`,[...params,limit])};
- }
- function retry({deliveryId}) {
- if (!UUID.test(deliveryId || '')) throw new Error('投递 ID 无效。');
- return store.transaction(() => {
- const delivery=store.readOne('SELECT * FROM webhook_deliveries WHERE id=?',[deliveryId]);
- const sub=delivery && get(delivery.subscriptionId);
- if (!delivery || delivery.status !== 'dead' || !sub?.enabled || sub.revision !== delivery.subscriptionRevision) throw new Error('只能重试当前已启用订阅的失败投递。');
- store.execute("UPDATE webhook_deliveries SET status='pending',attempts=0,nextAttemptAt=?,updatedAt=?,lastError=NULL WHERE id=?",[at(),at(),deliveryId]);
- return {queued:true,deliveryId};
- });
- }
- function matches(sub,change,record,signals) {
- if (change.action !== 'created' || change.ingestion !== 'live' || record.kind !== 'message' || record.deleted
- || !['web-api','graph'].includes(record.source)) return false;
- // This second boundary prevents a freshly discovered chat's old first page
- // from generating a burst of notifications after subscription creation.
- const occurred=Date.parse(record.occurredAt);
- if (!Number.isFinite(occurred) || occurred < Date.parse(sub.config.activeSince || sub.createdAt) || occurred > now()+5*60_000) return false;
- const c=sub.config;
- if (c.accountIds.length && !c.accountIds.includes(record.accountId) || c.chatIds.length && !c.chatIds.includes(record.parentId)) return false;
- if (!c.includeSelf && signals.isFromSelf !== false) return false;
- return c.mode === 'all' || c.mode === 'mention' && signals.mentionsSelf === true
- || c.mode === 'direct' && signals.isDirect === true || c.mode === 'mention_or_direct' && (signals.mentionsSelf === true || signals.isDirect === true);
- }
- function plan() {
- return store.transaction(() => {
- let queued=0;
- for (const raw of store.readRows('SELECT * FROM webhook_subscriptions WHERE enabled=1 ORDER BY id')) {
- const sub=parseConfig(raw), observations=store.readRows('SELECT * FROM message_live_observations WHERE seq>? ORDER BY seq LIMIT 500',[sub.cursor]);
- for (const observation of observations) {
- // Notification eligibility follows the first live observation, while
- // public change polling and delivery identity retain the original change.
- const change={...observation,seq:observation.changeSeq,action:'created',ingestion:'live'};
- const record=JSON.parse(observation.payload);
- const chat=record.parentId ? store.getRecordByIdentity({source:record.source,accountId:record.accountId,kind:'chat',externalId:record.parentId}) : null;
- const signals=classify(record,chat);
- if (!matches(sub,change,record,signals)) continue;
- const id=randomUUID();
- const payload={id,type:'teams.message.created',sequence:change.seq,observedAt:change.at,contentTrust:'untrusted_teams_data',
- message:{id:change.recordId,source:record.source,accountId:record.accountId,chatId:record.parentId,occurredAt:record.occurredAt,
- senderId:signals.senderId,mentionsSelf:signals.mentionsSelf,isDirect:signals.isDirect,
- ...(sub.config.includeContent ? {title:record.title.slice(0,1000),text:record.text.slice(0,20_000),truncated:record.text.length>20_000} : {})}};
- const timestamp=at();
- queued+=Number(store.execute(`INSERT OR IGNORE INTO webhook_deliveries(id,subscriptionId,seq,subscriptionRevision,payload,status,nextAttemptAt,createdAt,updatedAt)
- VALUES(?,?,?,?,?,'pending',?,?,?)`,[id,sub.id,change.seq,sub.revision,JSON.stringify(payload),timestamp,timestamp,timestamp]).changes);
- }
- if (observations.length) store.execute('UPDATE webhook_subscriptions SET cursor=? WHERE id=?',[observations.at(-1).seq,sub.id]);
- }
- return queued;
- });
- }
- function claim() {
- return store.transaction(() => {
- const timestamp=at();
- store.execute("UPDATE webhook_deliveries SET status='dead',leaseToken=NULL,leaseUntil=NULL,updatedAt=? WHERE attempts>=6 AND (status='pending' OR (status='inflight' AND leaseUntil<=?))",[timestamp,timestamp]);
- const row=store.readOne(`SELECT d.* FROM webhook_deliveries d JOIN webhook_subscriptions s ON s.id=d.subscriptionId
- WHERE s.enabled=1 AND s.revision=d.subscriptionRevision AND ((d.status='pending' AND d.nextAttemptAt<=?) OR (d.status='inflight' AND d.leaseUntil<=?))
- ORDER BY d.nextAttemptAt,d.id LIMIT 1`,[timestamp,timestamp]);
- if (!row) return null;
- const leaseToken=randomUUID(), attempts=row.attempts+1;
- store.execute("UPDATE webhook_deliveries SET status='inflight',attempts=?,leaseUntil=?,leaseToken=?,updatedAt=? WHERE id=?",
- [attempts,new Date(now()+60_000).toISOString(),leaseToken,timestamp,row.id]);
- return {...row,attempts,leaseToken,subscription:get(row.subscriptionId)};
- });
- }
- async function dispatchOnce({signal} = {}) {
- if (active) return active;
- const task=(async()=>{
- const result={queued:plan(),attempted:0,sent:0,failed:0};
- for (let i=0;i<20 && !signal?.aborted;i++) {
- const delivery=claim(); if (!delivery) break;
- const sub=delivery.subscription, timestamp=String(Math.floor(now()/1000)); result.attempted++;
- const signature=createHmac('sha256',sub.secret).update(timestamp+'.'+delivery.payload).digest('hex');
- let response;
- try {response=await send({url:sub.url,allowLocal:sub.config.allowLocal,body:delivery.payload,signal,
- headers:{'X-Teams-Event-Id':delivery.id,'X-Teams-Timestamp':timestamp,'X-Teams-Signature':`sha256=${signature}`}});} catch {response={status:0};}
- const code=Number(response.status)||0,success=code>=200&&code<300;
- const retryable=code===0||[408,425,429].includes(code)||code>=500;
- const requestedDelay=retryDelay(response.retryAfter,now());
- const state=success?'sent':retryable&&delivery.attempts<6&&Number.isFinite(requestedDelay)?'pending':'dead';
- const delay=Math.max(Math.min(300_000,1000*2**delivery.attempts),Number.isFinite(requestedDelay)?requestedDelay:0);
- // Config changes/removal revoke an outstanding lease. A response from an
- // old attempt cannot revive a cancelled delivery or change a new epoch.
- store.execute('UPDATE webhook_deliveries SET status=?,nextAttemptAt=?,leaseUntil=NULL,leaseToken=NULL,lastStatus=?,lastError=?,updatedAt=? WHERE id=? AND leaseToken=?',
- [state,new Date(now()+delay).toISOString(),code||null,success?null:'接收端拒绝、超时或目标地址不被允许;响应正文未记录。',at(),delivery.id,delivery.leaseToken]);
- if(success)result.sent++;else result.failed++;
- }
- return result;
- })();
- active=task; try{return await task;}finally{if(active===task)active=null;}
- }
- async function tick(epoch,control) {
- if(!running||epoch!==generation)return;
- try {await dispatchOnce({signal:control.signal});} catch {store.addLog('warn','WebHook 投递轮次未完成,将稍后重试。');}
- if(running&&epoch===generation)timer=schedule(()=>{timer=null;void tick(epoch,control);},2000);
- }
- function start(){if(running)return;running=true;controller=new AbortController();void tick(++generation,controller);}
- async function stop(){running=false;generation++;if(timer!==null)unschedule(timer);timer=null;controller?.abort();if(active)await active;}
- const status=()=>({running,subscriptions:Number(store.readOne('SELECT COUNT(*) n FROM webhook_subscriptions').n),enabled:Number(store.readOne('SELECT COUNT(*) n FROM webhook_subscriptions WHERE enabled=1').n),
- deliveries:store.readRows('SELECT status,COUNT(*) count FROM webhook_deliveries GROUP BY status')});
- return {configure,list,remove,listDeliveries,retry,dispatchOnce,start,stop,status};
- }
|