UNPKG

1.01 kBMarkdownView Raw
1# webhook
2
3webhook 會對目標伺服器 `webhook-url` 發送一個 `POST` 請求,內容大致如下
4
5```json
6{
7 "title": "標題",
8 "link": "magnet 連結"
9}
10```
11
12## 驗證
13
14如果伺服器在收到任何請求就直接下載 `link` 中的檔案是很危險的,不過這可以透過 `x-dmhy-token` 這個 header 來驗證。
15
16首先請先設定一個只有你知道的 token:
17
18```bash
19$ dmhy config webhook-token YOUR_TOKEN
20```
21
22接下來請在伺服器比較 `x-dmhy-token` 的內容與 `sha1("YOUR_TOKEN")` 是否相同,若不相同代表這個 request 可能是假的。
23
24### Express 範例
25
26```js
27const app = require('express')();
28const crypto = require('crypto');
29
30const TOK = 'YOUR_TOKEN'; // 這邊輸入你的 dmhy config webhook-token 的值
31
32const enc = crypto
33 .createHash('sha1')
34 .update(TOK)
35 .digest('hex');
36
37app.all('*', (req, res) => {
38 const tok = req.headers['x-dmhy-token'];
39 if(tok === enc){
40 res.end();
41 }
42 else{
43 res.status(403).end();
44 }
45});
46
47app.listen(1234);
48```