# Webhooks 分享被访问或被举报时,PageDrop 会向你配置的地址发送一个 POST 请求。每次请求都用该 webhook 的密钥做 HMAC-SHA256 签名,接收方可以据此确认请求来自 PageDrop。 ## 事件 | 事件 | 触发时机 | |---|---| | `share_visited` | 分享每成功打开一次就触发一次,不只是首次 | | `share_reported` | 有人举报了这个分享 | | `share_expiring_soon` | 预留,目前不会触发 | 每个 webhook 可以只订阅其中一部分事件。投递超时为 5 秒,每次投递都会写入审计记录。 ## 创建 webhook webhook 只能用登录会话管理,API 令牌无权创建或修改: ```bash curl -X POST $BASE/api/webhooks \ -b cookies.txt \ -H "Content-Type: application/json" \ -d '{"url":"https://your.app/pagedrop-hook","events":["share_visited","share_reported"]}' ``` 响应里的 `secret` 只返回这一次,请立即保存;之后无法再查看。需要更换密钥时,删除后重新创建。 ```json { "webhook": { "id": "cm...", "url": "...", "events": ["share_visited","share_reported"], "active": true, "createdAt": "..." }, "secret": "whsec_AbC...DeF" } ``` ## 请求格式 ### `share_visited` ```http POST /your-endpoint HTTP/1.1 Content-Type: application/json User-Agent: PageDrop-Webhook/1.0 X-PageDrop-Event: share_visited X-PageDrop-Signature: sha256= { "event": "share_visited", "timestamp": "2026-05-23T08:30:12.345Z", "data": { "shareId": "cm...", "token": "abcd1234", "fileId": "f_...", "fileName": "report.md", "fileKind": "MARKDOWN", "visitCount": 42, "uniqueVisitCount": 18, "surface": "share", "ipPrefix": "203.0.113.0/24", "ipAddress": "203.0.113.42", "ipCountry": "CN", "ipCity": "Shanghai", "referer": "https://twitter.com/i/web/..." } } ``` > ⚠️ `share_visited` 会把访客的**完整 IP 地址**和所在国家、城市发给你(`ipAddress`、`ipCountry`、`ipCity`)。`ipPrefix` 是同一地址的网段:IPv4 为 /24,IPv6 为 /48。访客不会收到任何提示,这些字段只出现在 webhook 请求里,请按你所在地区的隐私规定处理。 ### `share_reported` ```http POST /your-endpoint HTTP/1.1 Content-Type: application/json User-Agent: PageDrop-Webhook/1.0 X-PageDrop-Event: share_reported X-PageDrop-Signature: sha256= { "event": "share_reported", "timestamp": "2026-05-23T08:30:12.345Z", "data": { "reportId": "cm...", "shareId": "cm...", "shareToken": "abcd1234", "reason": "PHISHING", "fileName": "report.md" } } ``` ## 验证签名 用原始请求体(解析 JSON 之前的 UTF-8 字节)计算签名,与 `X-PageDrop-Signature` 比较: ```js import crypto from 'node:crypto'; function verify(req, secret) { const sig = req.headers['x-pagedrop-signature']; const expected = 'sha256=' + crypto.createHmac('sha256', secret) .update(req.rawBody) .digest('hex'); return crypto.timingSafeEqual(Buffer.from(sig), Buffer.from(expected)); } ``` 缺少签名或签名不匹配的请求一律拒绝。 ## 查看投递记录 ```bash # 列出所有 webhook curl -b cookies.txt $BASE/api/webhooks # 某个 webhook 最近 50 次投递 curl -b cookies.txt $BASE/api/webhooks/{webhookId}/deliveries ``` 每条记录包含 `event`、`status`、`responseCode`、`attempts` 和 `deliveredAt`。 ## 限制 - 每个账号最多 10 个 webhook,已停用的也计入。 - 每次投递超时 5 秒。 - 失败的投递**目前不会自动重试**。`POST /api/internal/maintenance` 会重发失败超过 60 秒、尝试次数少于 3 的投递(每次最多 25 条),但线上没有定时任务调用它。如果你依赖重试,请自行定时调用。 - 投递记录接口只返回最近 50 条,旧记录不会被清理;删除 webhook 时,它的记录一并删除。