Spaces:
Running
Running
Surf Studio commited on
Commit ·
bb015c0
1
Parent(s): 801d27b
密钥自动同步:工作室每 2 小时把轮换后的 Surf 密钥同步到线上并重启;推送记录来源(studio/space)
Browse files- README.md +4 -0
- cron.json +9 -1
- cron/hf-key-sync.js +67 -0
- db/schema.js +1 -0
- lib/notify.js +5 -2
README.md
CHANGED
|
@@ -49,6 +49,10 @@ X Cookie、Telegram 登录、企业微信地址都保存在数据库里,可以
|
|
| 49 |
|
| 50 |
Telegram 若被限流(FLOOD_WAIT)会自动暂停读取;赠链接口被限流会自动冷却 10 分钟;企业微信只在出现有效赠链时推送,且每个赠链只推一次(多个实例并发也不会重复推送)。
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
## 接口
|
| 53 |
|
| 54 |
- `GET /api/tracker` — 收录的帖子与统计
|
|
|
|
| 49 |
|
| 50 |
Telegram 若被限流(FLOOD_WAIT)会自动暂停读取;赠链接口被限流会自动冷却 10 分钟;企业微信只在出现有效赠链时推送,且每个赠链只推一次(多个实例并发也不会重复推送)。
|
| 51 |
|
| 52 |
+
## 密钥自动同步
|
| 53 |
+
|
| 54 |
+
工作室(开发环境)的 Surf 密钥是会自动轮换的会话密钥,线上 Space 里的副本会过期。工作室每 2 小时检查一次:一旦密钥变化就自动更新到 Space 的 Secret 并重启 Space,避免线上出现「invalid session key / 数据加载失败」。若要彻底免维护,可在 Surf 平台申请长期密钥后直接写入 Space 的 `SURF_API_KEY`。
|
| 55 |
+
|
| 56 |
## 接口
|
| 57 |
|
| 58 |
- `GET /api/tracker` — 收录的帖子与统计
|
cron.json
CHANGED
|
@@ -22,5 +22,13 @@
|
|
| 22 |
"handler": "cron/mystery-refresh.js",
|
| 23 |
"enabled": true,
|
| 24 |
"timeout": 280
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
}
|
| 26 |
-
]
|
|
|
|
| 22 |
"handler": "cron/mystery-refresh.js",
|
| 23 |
"enabled": true,
|
| 24 |
"timeout": 280
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
"id": "hf-key-sync",
|
| 28 |
+
"name": "Sync the rotating Surf API key to the deployed Space",
|
| 29 |
+
"schedule": "0 */2 * * *",
|
| 30 |
+
"handler": "cron/hf-key-sync.js",
|
| 31 |
+
"enabled": true,
|
| 32 |
+
"timeout": 90
|
| 33 |
}
|
| 34 |
+
]
|
cron/hf-key-sync.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Keep the deployed Space usable.
|
| 3 |
+
*
|
| 4 |
+
* The studio's Surf key is a rotating session key (it changes roughly daily),
|
| 5 |
+
* while the deployed Space keeps whatever was copied into its secret. When the
|
| 6 |
+
* studio sees a new key, push it to the Space secret and restart the Space so
|
| 7 |
+
* it picks it up — otherwise the deployed app starts answering with
|
| 8 |
+
* "invalid session key" until someone updates it by hand.
|
| 9 |
+
*/
|
| 10 |
+
const { getSetting, setSetting } = require('../lib/settings')
|
| 11 |
+
|
| 12 |
+
const HF_REPO = process.env.HF_SPACE_REPO || 'Ethscriptions/cut'
|
| 13 |
+
|
| 14 |
+
async function hf(path, init = {}) {
|
| 15 |
+
const token = await getSetting('hf_token')
|
| 16 |
+
if (!token) {
|
| 17 |
+
const err = new Error('还没有配置 Hugging Face token')
|
| 18 |
+
err.status = 400
|
| 19 |
+
throw err
|
| 20 |
+
}
|
| 21 |
+
const res = await fetch(`https://huggingface.co/api/spaces/${HF_REPO}${path}`, {
|
| 22 |
+
...init,
|
| 23 |
+
headers: {
|
| 24 |
+
authorization: `Bearer ${token}`,
|
| 25 |
+
'content-type': 'application/json',
|
| 26 |
+
...(init.headers || {}),
|
| 27 |
+
},
|
| 28 |
+
signal: AbortSignal.timeout(30000),
|
| 29 |
+
})
|
| 30 |
+
return res
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
/** Push the current studio key to the Space and restart it if it changed. */
|
| 34 |
+
async function syncKey({ force = false } = {}) {
|
| 35 |
+
const key = process.env.SURF_API_KEY
|
| 36 |
+
if (!key) return { skipped: 'no-key' }
|
| 37 |
+
|
| 38 |
+
const synced = await getSetting('hf_key_synced')
|
| 39 |
+
if (!force && synced === key) return { skipped: 'unchanged' }
|
| 40 |
+
|
| 41 |
+
const res = await hf('/secrets', {
|
| 42 |
+
method: 'POST',
|
| 43 |
+
body: JSON.stringify({
|
| 44 |
+
key: 'SURF_API_KEY',
|
| 45 |
+
value: key,
|
| 46 |
+
description: 'Surf platform key (auto-synced)',
|
| 47 |
+
}),
|
| 48 |
+
})
|
| 49 |
+
if (!res.ok) throw new Error(`更新 Space 密钥失败:HTTP ${res.status}`)
|
| 50 |
+
|
| 51 |
+
await setSetting('hf_key_synced', key)
|
| 52 |
+
await hf('/restart', { method: 'POST' }).catch(() => {})
|
| 53 |
+
return { updated: true }
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
exports.handler = async () => {
|
| 57 |
+
// Only the studio holds a fresh key — the deployed Space must never push its
|
| 58 |
+
// own (possibly stale) value back.
|
| 59 |
+
if (!process.env.BASE_PATH && process.env.HF_KEY_SYNC !== '1') {
|
| 60 |
+
return { skipped: 'not-studio' }
|
| 61 |
+
}
|
| 62 |
+
const result = await syncKey()
|
| 63 |
+
if (result.updated) console.log('Synced Surf API key to the Space and restarted it')
|
| 64 |
+
return result
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
exports.syncKey = syncKey
|
db/schema.js
CHANGED
|
@@ -39,6 +39,7 @@ exports.mystery_posts = pgTable('mystery_posts', {
|
|
| 39 |
box_tier: text('box_tier'),
|
| 40 |
invite_checked_at: timestamp('invite_checked_at'),
|
| 41 |
notified_at: timestamp('notified_at'),
|
|
|
|
| 42 |
first_seen_at: timestamp('first_seen_at').defaultNow(),
|
| 43 |
updated_at: timestamp('updated_at').defaultNow(),
|
| 44 |
})
|
|
|
|
| 39 |
box_tier: text('box_tier'),
|
| 40 |
invite_checked_at: timestamp('invite_checked_at'),
|
| 41 |
notified_at: timestamp('notified_at'),
|
| 42 |
+
notified_by: text('notified_by'),
|
| 43 |
first_seen_at: timestamp('first_seen_at').defaultNow(),
|
| 44 |
updated_at: timestamp('updated_at').defaultNow(),
|
| 45 |
})
|
lib/notify.js
CHANGED
|
@@ -29,6 +29,9 @@ async function send(payload) {
|
|
| 29 |
return true
|
| 30 |
}
|
| 31 |
|
|
|
|
|
|
|
|
|
|
| 32 |
const SOURCE_LABELS = {
|
| 33 |
telegram: 'Telegram',
|
| 34 |
timeline: 'X 主页',
|
|
@@ -103,10 +106,10 @@ async function pendingAlerts() {
|
|
| 103 |
async function claim(post) {
|
| 104 |
const { rows } = await dbQuery(
|
| 105 |
`UPDATE mystery_posts
|
| 106 |
-
SET notified_at = now()
|
| 107 |
WHERE gift_code = $1 AND notified_at IS NULL
|
| 108 |
RETURNING tweet_id`,
|
| 109 |
-
[post.gift_code]
|
| 110 |
)
|
| 111 |
return rows.length > 0
|
| 112 |
}
|
|
|
|
| 29 |
return true
|
| 30 |
}
|
| 31 |
|
| 32 |
+
/** Which deployment is sending this push (studio preview vs deployed app). */
|
| 33 |
+
const INSTANCE = process.env.INSTANCE_NAME || (process.env.BASE_PATH ? 'studio' : 'space')
|
| 34 |
+
|
| 35 |
const SOURCE_LABELS = {
|
| 36 |
telegram: 'Telegram',
|
| 37 |
timeline: 'X 主页',
|
|
|
|
| 106 |
async function claim(post) {
|
| 107 |
const { rows } = await dbQuery(
|
| 108 |
`UPDATE mystery_posts
|
| 109 |
+
SET notified_at = now(), notified_by = $2
|
| 110 |
WHERE gift_code = $1 AND notified_at IS NULL
|
| 111 |
RETURNING tweet_id`,
|
| 112 |
+
[post.gift_code, INSTANCE]
|
| 113 |
)
|
| 114 |
return rows.length > 0
|
| 115 |
}
|