webhook-service.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import { randomUUID, randomBytes, createHmac } from 'node:crypto';
  2. import { lookup } from 'node:dns/promises';
  3. import { isIP, BlockList } from 'node:net';
  4. import http from 'node:http';
  5. import https from 'node:https';
  6. import { classifyMessage } from './message-signals.js';
  7. const MODES = new Set(['all', 'mention', 'direct', 'mention_or_direct']);
  8. const UUID = /^[a-f\d]{8}(?:-[a-f\d]{4}){3}-[a-f\d]{12}$/i;
  9. const blocked = new BlockList();
  10. 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');
  11. const globalV6 = new BlockList(); globalV6.addSubnet('2000::',3,'ipv6');
  12. for (const [address,prefix] of [['2001:db8::',32],['2001::',32],['2002::',16]]) blocked.addSubnet(address,prefix,'ipv6');
  13. const hostText = host => host.replace(/^\[|\]$/g, '');
  14. const loopback = address => address === '::1' || /^127\.\d+\.\d+\.\d+$/.test(address);
  15. export function isAllowedWebhookAddress(address, allowLocal = false) {
  16. const family = isIP(address);
  17. if (allowLocal && loopback(address)) return true;
  18. return family === 4 ? !blocked.check(address,'ipv4') : family === 6 && globalV6.check(address,'ipv6') && !blocked.check(address,'ipv6');
  19. }
  20. export function validateWebhookUrl(raw, allowLocal = false) {
  21. try {
  22. if (typeof raw !== 'string' || raw.length > 4096 || /[\u0000-\u0020\u007f]/.test(raw)) throw new Error();
  23. const url = new URL(raw), host = hostText(url.hostname);
  24. if (url.username || url.password || url.hash || !['http:','https:'].includes(url.protocol)) throw new Error();
  25. if (url.protocol === 'http:' && !(allowLocal && (host === 'localhost' || loopback(host)))) throw new Error();
  26. if (host === 'localhost' && !allowLocal || isIP(host) && !isAllowedWebhookAddress(host,allowLocal)) throw new Error();
  27. return url;
  28. } catch { throw new Error('WebHook URL 无效。默认仅支持公网 HTTPS;本机 HTTP 接收器需明确 allowLocal=true。'); }
  29. }
  30. function abortable(promise, signal) {
  31. return new Promise((resolve,reject) => {
  32. const abort = () => reject(new Error('cancelled'));
  33. if (signal.aborted) return abort();
  34. signal.addEventListener('abort',abort,{once:true});
  35. promise.then(resolve,reject).finally(() => signal.removeEventListener('abort',abort));
  36. });
  37. }
  38. /** DNS results are verified then pinned into the actual socket lookup. No redirects. */
  39. export async function sendWebhook({ url: raw, allowLocal, body, headers, signal }, { resolveHost = lookup } = {}) {
  40. const url = validateWebhookUrl(raw,allowLocal), host = hostText(url.hostname);
  41. const timeout = AbortSignal.timeout(10_000), combined = signal ? AbortSignal.any([signal,timeout]) : timeout;
  42. const addresses = isIP(host) ? [{address:host,family:isIP(host)}] : await abortable(resolveHost(host,{all:true,verbatim:true}),combined);
  43. if (!addresses.length || addresses.length > 32 || addresses.some(({address}) => !isAllowedWebhookAddress(address,allowLocal))) throw new Error('destination-blocked');
  44. return new Promise((resolve,reject) => {
  45. const request = (url.protocol === 'https:' ? https : http).request(url, { method:'POST',agent:false,signal:combined,
  46. headers:{...headers,'Content-Type':'application/json','Content-Length':Buffer.byteLength(body)},
  47. lookup: (_host,options,callback) => options?.all ? callback(null,addresses) : callback(null,addresses[0].address,addresses[0].family),
  48. }, response => {
  49. // Only status and Retry-After are useful; receiver bodies may contain secrets.
  50. resolve({ status:response.statusCode, retryAfter:response.headers['retry-after'] || null }); response.destroy();
  51. });
  52. request.on('error',() => reject(new Error('delivery-failed'))); request.end(body);
  53. });
  54. }
  55. const parseConfig = row => ({...row,config:JSON.parse(row.config)});
  56. function publicSubscription(row) {
  57. const {secret,url,...safe} = row, parsed = new URL(url);
  58. return {...safe,enabled:Boolean(row.enabled),endpointOrigin:parsed.origin,hasSecret:true};
  59. }
  60. function cleanIds(value, fallback = []) {
  61. if (value === undefined) return fallback;
  62. 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 个标识的数组。');
  63. return [...new Set(value)];
  64. }
  65. function retryDelay(value, now) {
  66. if (typeof value !== 'string') return 0;
  67. const milliseconds = /^\d+$/.test(value) ? Number(value)*1000 : Date.parse(value)-now;
  68. // An unreasonable delay needs manual review, never an earlier-than-requested retry.
  69. return milliseconds > 7*86400_000 ? Infinity : Number.isFinite(milliseconds) && milliseconds > 0 ? milliseconds : 0;
  70. }
  71. export function createWebhookService({ store, classify = classifyMessage, send = sendWebhook, now = Date.now,
  72. schedule = setTimeout, unschedule = clearTimeout } = {}) {
  73. let active = null, running = false, timer = null, controller = null, generation = 0;
  74. const at = () => new Date(now()).toISOString();
  75. const latest = () => Number(store.readOne('SELECT COALESCE(MAX(seq),0) AS n FROM message_live_observations').n);
  76. const get = id => { const row = store.readOne('SELECT * FROM webhook_subscriptions WHERE id=?',[id]); return row ? parseConfig(row) : null; };
  77. function configure(input) {
  78. if (!input || typeof input !== 'object' || Array.isArray(input)) throw new Error('订阅参数无效。');
  79. const allowed = new Set(['id','name','url','enabled','mode','accountIds','chatIds','includeSelf','includeContent','allowLocal']);
  80. if (Object.keys(input).some(k => !allowed.has(k))) throw new Error('包含未知订阅参数。');
  81. if (input.id && !UUID.test(input.id)) throw new Error('订阅 ID 无效。');
  82. return store.transaction(() => {
  83. const old = input.id ? get(input.id) : null;
  84. if (input.id && !old) throw new Error('找不到订阅。');
  85. if (!old && Number(store.readOne('SELECT COUNT(*) n FROM webhook_subscriptions').n)>=100) throw new Error('订阅上限为 100 个。');
  86. for (const key of ['enabled','includeSelf','includeContent','allowLocal']) if (input[key] !== undefined && typeof input[key] !== 'boolean') throw new Error('订阅开关必须是布尔值。');
  87. const config = { mode:input.mode ?? old?.config.mode ?? 'mention_or_direct',
  88. accountIds:cleanIds(input.accountIds,old?.config.accountIds),chatIds:cleanIds(input.chatIds,old?.config.chatIds),
  89. includeSelf:input.includeSelf ?? old?.config.includeSelf ?? false, includeContent:input.includeContent ?? old?.config.includeContent ?? false,
  90. allowLocal:input.allowLocal ?? old?.config.allowLocal ?? false };
  91. if (!MODES.has(config.mode)) throw new Error('提醒模式必须为 all、mention、direct 或 mention_or_direct。');
  92. const url = validateWebhookUrl(input.url ?? old?.url,config.allowLocal).href;
  93. const name = input.name ?? old?.name ?? '消息提醒';
  94. if (typeof name !== 'string' || !name.trim() || name.length > 100) throw new Error('订阅名称必须为 1–100 个字符。');
  95. const id = old?.id || randomUUID(), secret = old?.secret || randomBytes(32).toString('base64url');
  96. const enabled = input.enabled ?? Boolean(old?.enabled), timestamp = at();
  97. config.activeSince=timestamp;
  98. // Updating scope/destination starts a new subscription epoch. Old queued
  99. // messages cannot be sent to a newly configured recipient or expanded scope.
  100. store.execute(`INSERT INTO webhook_subscriptions(id,name,url,secret,config,enabled,revision,cursor,createdAt,updatedAt)
  101. VALUES(?,?,?,?,?,?,?,?,?,?) ON CONFLICT(id) DO UPDATE SET name=excluded.name,url=excluded.url,config=excluded.config,
  102. enabled=excluded.enabled,revision=excluded.revision,cursor=excluded.cursor,updatedAt=excluded.updatedAt`,
  103. [id,name,url,secret,JSON.stringify(config),Number(enabled),(old?.revision || 0)+1,latest(),old?.createdAt || timestamp,timestamp]);
  104. store.execute("UPDATE webhook_deliveries SET status='cancelled',leaseToken=NULL,leaseUntil=NULL,updatedAt=? WHERE subscriptionId=? AND status IN ('pending','inflight','dead')",[timestamp,id]);
  105. return {subscription:publicSubscription(get(id)),...(!old ? {signingSecret:secret} : {})};
  106. });
  107. }
  108. const list = () => ({items:store.readRows('SELECT * FROM webhook_subscriptions ORDER BY createdAt,id').map(row=>publicSubscription(parseConfig(row)))});
  109. function remove({id}) {
  110. if (!UUID.test(id || '')) throw new Error('订阅 ID 无效。');
  111. return {removed:Number(store.execute('DELETE FROM webhook_subscriptions WHERE id=?',[id]).changes)>0};
  112. }
  113. function listDeliveries({subscriptionId,status,limit=50} = {}) {
  114. if (!Number.isInteger(limit) || limit<1 || limit>100) throw new Error('limit 必须为 1–100。');
  115. if (subscriptionId && !UUID.test(subscriptionId)) throw new Error('订阅 ID 无效。');
  116. if (status && !['pending','inflight','sent','dead','cancelled'].includes(status)) throw new Error('投递状态无效。');
  117. const where=[],params=[];
  118. if (subscriptionId) {where.push('subscriptionId=?');params.push(subscriptionId);}
  119. if (status) {where.push('status=?');params.push(status);}
  120. 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])};
  121. }
  122. function retry({deliveryId}) {
  123. if (!UUID.test(deliveryId || '')) throw new Error('投递 ID 无效。');
  124. return store.transaction(() => {
  125. const delivery=store.readOne('SELECT * FROM webhook_deliveries WHERE id=?',[deliveryId]);
  126. const sub=delivery && get(delivery.subscriptionId);
  127. if (!delivery || delivery.status !== 'dead' || !sub?.enabled || sub.revision !== delivery.subscriptionRevision) throw new Error('只能重试当前已启用订阅的失败投递。');
  128. store.execute("UPDATE webhook_deliveries SET status='pending',attempts=0,nextAttemptAt=?,updatedAt=?,lastError=NULL WHERE id=?",[at(),at(),deliveryId]);
  129. return {queued:true,deliveryId};
  130. });
  131. }
  132. function matches(sub,change,record,signals) {
  133. if (change.action !== 'created' || change.ingestion !== 'live' || record.kind !== 'message' || record.deleted
  134. || !['web-api','graph'].includes(record.source)) return false;
  135. // This second boundary prevents a freshly discovered chat's old first page
  136. // from generating a burst of notifications after subscription creation.
  137. const occurred=Date.parse(record.occurredAt);
  138. if (!Number.isFinite(occurred) || occurred < Date.parse(sub.config.activeSince || sub.createdAt) || occurred > now()+5*60_000) return false;
  139. const c=sub.config;
  140. if (c.accountIds.length && !c.accountIds.includes(record.accountId) || c.chatIds.length && !c.chatIds.includes(record.parentId)) return false;
  141. if (!c.includeSelf && signals.isFromSelf !== false) return false;
  142. return c.mode === 'all' || c.mode === 'mention' && signals.mentionsSelf === true
  143. || c.mode === 'direct' && signals.isDirect === true || c.mode === 'mention_or_direct' && (signals.mentionsSelf === true || signals.isDirect === true);
  144. }
  145. function plan() {
  146. return store.transaction(() => {
  147. let queued=0;
  148. for (const raw of store.readRows('SELECT * FROM webhook_subscriptions WHERE enabled=1 ORDER BY id')) {
  149. const sub=parseConfig(raw), observations=store.readRows('SELECT * FROM message_live_observations WHERE seq>? ORDER BY seq LIMIT 500',[sub.cursor]);
  150. for (const observation of observations) {
  151. // Notification eligibility follows the first live observation, while
  152. // public change polling and delivery identity retain the original change.
  153. const change={...observation,seq:observation.changeSeq,action:'created',ingestion:'live'};
  154. const record=JSON.parse(observation.payload);
  155. const chat=record.parentId ? store.getRecordByIdentity({source:record.source,accountId:record.accountId,kind:'chat',externalId:record.parentId}) : null;
  156. const signals=classify(record,chat);
  157. if (!matches(sub,change,record,signals)) continue;
  158. const id=randomUUID();
  159. const payload={id,type:'teams.message.created',sequence:change.seq,observedAt:change.at,contentTrust:'untrusted_teams_data',
  160. message:{id:change.recordId,source:record.source,accountId:record.accountId,chatId:record.parentId,occurredAt:record.occurredAt,
  161. senderId:signals.senderId,mentionsSelf:signals.mentionsSelf,isDirect:signals.isDirect,
  162. ...(sub.config.includeContent ? {title:record.title.slice(0,1000),text:record.text.slice(0,20_000),truncated:record.text.length>20_000} : {})}};
  163. const timestamp=at();
  164. queued+=Number(store.execute(`INSERT OR IGNORE INTO webhook_deliveries(id,subscriptionId,seq,subscriptionRevision,payload,status,nextAttemptAt,createdAt,updatedAt)
  165. VALUES(?,?,?,?,?,'pending',?,?,?)`,[id,sub.id,change.seq,sub.revision,JSON.stringify(payload),timestamp,timestamp,timestamp]).changes);
  166. }
  167. if (observations.length) store.execute('UPDATE webhook_subscriptions SET cursor=? WHERE id=?',[observations.at(-1).seq,sub.id]);
  168. }
  169. return queued;
  170. });
  171. }
  172. function claim() {
  173. return store.transaction(() => {
  174. const timestamp=at();
  175. 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]);
  176. const row=store.readOne(`SELECT d.* FROM webhook_deliveries d JOIN webhook_subscriptions s ON s.id=d.subscriptionId
  177. WHERE s.enabled=1 AND s.revision=d.subscriptionRevision AND ((d.status='pending' AND d.nextAttemptAt<=?) OR (d.status='inflight' AND d.leaseUntil<=?))
  178. ORDER BY d.nextAttemptAt,d.id LIMIT 1`,[timestamp,timestamp]);
  179. if (!row) return null;
  180. const leaseToken=randomUUID(), attempts=row.attempts+1;
  181. store.execute("UPDATE webhook_deliveries SET status='inflight',attempts=?,leaseUntil=?,leaseToken=?,updatedAt=? WHERE id=?",
  182. [attempts,new Date(now()+60_000).toISOString(),leaseToken,timestamp,row.id]);
  183. return {...row,attempts,leaseToken,subscription:get(row.subscriptionId)};
  184. });
  185. }
  186. async function dispatchOnce({signal} = {}) {
  187. if (active) return active;
  188. const task=(async()=>{
  189. const result={queued:plan(),attempted:0,sent:0,failed:0};
  190. for (let i=0;i<20 && !signal?.aborted;i++) {
  191. const delivery=claim(); if (!delivery) break;
  192. const sub=delivery.subscription, timestamp=String(Math.floor(now()/1000)); result.attempted++;
  193. const signature=createHmac('sha256',sub.secret).update(timestamp+'.'+delivery.payload).digest('hex');
  194. let response;
  195. try {response=await send({url:sub.url,allowLocal:sub.config.allowLocal,body:delivery.payload,signal,
  196. headers:{'X-Teams-Event-Id':delivery.id,'X-Teams-Timestamp':timestamp,'X-Teams-Signature':`sha256=${signature}`}});} catch {response={status:0};}
  197. const code=Number(response.status)||0,success=code>=200&&code<300;
  198. const retryable=code===0||[408,425,429].includes(code)||code>=500;
  199. const requestedDelay=retryDelay(response.retryAfter,now());
  200. const state=success?'sent':retryable&&delivery.attempts<6&&Number.isFinite(requestedDelay)?'pending':'dead';
  201. const delay=Math.max(Math.min(300_000,1000*2**delivery.attempts),Number.isFinite(requestedDelay)?requestedDelay:0);
  202. // Config changes/removal revoke an outstanding lease. A response from an
  203. // old attempt cannot revive a cancelled delivery or change a new epoch.
  204. store.execute('UPDATE webhook_deliveries SET status=?,nextAttemptAt=?,leaseUntil=NULL,leaseToken=NULL,lastStatus=?,lastError=?,updatedAt=? WHERE id=? AND leaseToken=?',
  205. [state,new Date(now()+delay).toISOString(),code||null,success?null:'接收端拒绝、超时或目标地址不被允许;响应正文未记录。',at(),delivery.id,delivery.leaseToken]);
  206. if(success)result.sent++;else result.failed++;
  207. }
  208. return result;
  209. })();
  210. active=task; try{return await task;}finally{if(active===task)active=null;}
  211. }
  212. async function tick(epoch,control) {
  213. if(!running||epoch!==generation)return;
  214. try {await dispatchOnce({signal:control.signal});} catch {store.addLog('warn','WebHook 投递轮次未完成,将稍后重试。');}
  215. if(running&&epoch===generation)timer=schedule(()=>{timer=null;void tick(epoch,control);},2000);
  216. }
  217. function start(){if(running)return;running=true;controller=new AbortController();void tick(++generation,controller);}
  218. async function stop(){running=false;generation++;if(timer!==null)unschedule(timer);timer=null;controller?.abort();if(active)await active;}
  219. 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),
  220. deliveries:store.readRows('SELECT status,COUNT(*) count FROM webhook_deliveries GROUP BY status')});
  221. return {configure,list,remove,listDeliveries,retry,dispatchOnce,start,stop,status};
  222. }