Spaces:
Sleeping
Sleeping
File size: 2,554 Bytes
94b4ce6 5936c9d 94b4ce6 5936c9d 94b4ce6 5936c9d 94b4ce6 5936c9d 94b4ce6 5936c9d 94b4ce6 5936c9d 94b4ce6 5936c9d 94b4ce6 944b7dd 5936c9d 944b7dd 5936c9d 944b7dd 5936c9d 944b7dd 5936c9d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | /**
* Surf Mystery Box 赠链追踪 — single-process server.
*
* 1. restore the SQLite DB (local file → encrypted HF dataset backup → legacy snapshot import)
* 2. start the background loops (X search, Telegram, invite checks, pushes, backups)
* 3. serve /api and the built frontend on one port
*/
const path = require('path')
const express = require('express')
const log = require('./lib/log')
const backup = require('./lib/backup')
const alerts = require('./lib/alerts')
const invites = require('./lib/invites')
const scheduler = require('./lib/scheduler')
const xscan = require('./lib/x/scan')
const tg = require('./lib/tg')
const db = require('./lib/db')
const port = Number(process.env.PORT || 7860)
const publicDir = process.env.PUBLIC_DIR || path.join(__dirname, 'public')
async function main() {
const bootTs = log.now()
const restored = await backup.restore()
log.info('backup', `数据来源:${backup.status().restoredFrom} ${JSON.stringify(restored)}`)
// No push history to go on → don't flood the group with codes that may already have been sent.
const counts = db.counts()
if (String(backup.status().restoredFrom || '').startsWith('empty') && !counts.codes) {
alerts.holdThenBaseline(8 * 60000, bootTs - 2 * 3600)
}
xscan.init()
invites.setUsableHandler(() => alerts.pushPending())
const app = express()
app.disable('x-powered-by')
app.use(express.json({ limit: '1mb' }))
app.use('/api', require('./routes/api'))
app.use('/api', (req, res) => res.status(404).json({ error: 'not found' }))
app.use(express.static(publicDir, { index: false, maxAge: '1h' }))
app.get('*', (req, res) => {
res.set('Cache-Control', 'no-cache')
res.sendFile(path.join(publicDir, 'index.html'), (err) => {
if (err) res.status(404).send('frontend not built')
})
})
app.listen(port, () => log.info('server', `已启动,端口 ${port}`))
backup.start()
scheduler.start()
}
let stopping = false
async function shutdown(signal) {
if (stopping) return
stopping = true
log.info('server', `收到 ${signal},保存数据后退出`)
await Promise.race([
(async () => {
await tg.stop().catch(() => {})
await backup.save('shutdown').catch((err) => log.error('backup', err.message))
})(),
new Promise((resolve) => setTimeout(resolve, 20000)),
])
process.exit(0)
}
process.on('SIGTERM', () => shutdown('SIGTERM'))
process.on('SIGINT', () => shutdown('SIGINT'))
main().catch((err) => {
console.error('启动失败:', err)
process.exit(1)
})
|