incognitolm commited on
Commit ·
a3272f1
1
Parent(s): 453935d
Update index.js
Browse files- server/index.js +53 -7
server/index.js
CHANGED
|
@@ -7,6 +7,7 @@ import path from 'path';
|
|
| 7 |
import { fileURLToPath } from 'url';
|
| 8 |
import fetch from 'node-fetch';
|
| 9 |
import rateLimit from 'express-rate-limit';
|
|
|
|
| 10 |
|
| 11 |
import { handleWsMessage } from './wsHandler.js';
|
| 12 |
import { sessionStore, initStoreConfig } from './sessionStore.js';
|
|
@@ -32,6 +33,30 @@ const ADMIN_TOKEN = process.env.ADMIN_TOKEN || 'supersecret';
|
|
| 32 |
// Rate limiter for admin endpoints (5 attempts per IP per minute)
|
| 33 |
const verifyLimiter = rateLimit({ windowMs: 60*1000, max: 5, standardHeaders: true, legacyHeaders: false });
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
app.use(express.json({ limit: '10mb' }));
|
| 36 |
|
| 37 |
// --- API Turnstile Protection ---
|
|
@@ -79,14 +104,25 @@ app.post('/api/turnstile', async (req,res)=>{
|
|
| 79 |
}catch(e){ console.error('turnstile verify',e); return res.status(500).json({error:'Server error'});}
|
| 80 |
});
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
try{
|
| 85 |
const res = await fetch(`https://api.github.com/repos/${GITHUB_REPO}/commits/main`);
|
| 86 |
const data = await res.json();
|
| 87 |
latestSHA = data.sha;
|
| 88 |
console.log('Updated latest SHA:', latestSHA);
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
}
|
| 91 |
|
| 92 |
// --- Admin endpoints ---
|
|
@@ -113,11 +149,21 @@ app.get('/admin/verify',verifyLimiter,(req,res)=>{
|
|
| 113 |
res.json({success: token===ADMIN_TOKEN});
|
| 114 |
});
|
| 115 |
|
| 116 |
-
app.get('/admin/refresh',verifyLimiter,async (req,res)=>{
|
| 117 |
const token = req.query.token;
|
| 118 |
-
if(token!==ADMIN_TOKEN) return res.status(403).send('Forbidden');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
await fetchLatestSHA();
|
| 120 |
-
res.send(
|
| 121 |
});
|
| 122 |
|
| 123 |
// --- MIME type helper ---
|
|
|
|
| 7 |
import { fileURLToPath } from 'url';
|
| 8 |
import fetch from 'node-fetch';
|
| 9 |
import rateLimit from 'express-rate-limit';
|
| 10 |
+
import fs from 'fs';
|
| 11 |
|
| 12 |
import { handleWsMessage } from './wsHandler.js';
|
| 13 |
import { sessionStore, initStoreConfig } from './sessionStore.js';
|
|
|
|
| 33 |
// Rate limiter for admin endpoints (5 attempts per IP per minute)
|
| 34 |
const verifyLimiter = rateLimit({ windowMs: 60*1000, max: 5, standardHeaders: true, legacyHeaders: false });
|
| 35 |
|
| 36 |
+
const DATA_DIR = path.join(__dirname, 'data');
|
| 37 |
+
const VERSION_FILE = path.join(DATA_DIR, 'version.json');
|
| 38 |
+
|
| 39 |
+
function loadStoredSHA() {
|
| 40 |
+
try {
|
| 41 |
+
if (!fs.existsSync(DATA_DIR)) fs.mkdirSync(DATA_DIR, { recursive: true });
|
| 42 |
+
if (!fs.existsSync(VERSION_FILE)) return null;
|
| 43 |
+
const data = JSON.parse(fs.readFileSync(VERSION_FILE, 'utf-8'));
|
| 44 |
+
return data.sha || null;
|
| 45 |
+
} catch (e) {
|
| 46 |
+
console.error('Failed to load stored SHA:', e);
|
| 47 |
+
return null;
|
| 48 |
+
}
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
function saveStoredSHA(sha) {
|
| 52 |
+
try {
|
| 53 |
+
if (!fs.existsSync(DATA_DIR)) fs.mkdirSync(DATA_DIR, { recursive: true });
|
| 54 |
+
fs.writeFileSync(VERSION_FILE, JSON.stringify({ sha }, null, 2), 'utf-8');
|
| 55 |
+
} catch (e) {
|
| 56 |
+
console.error('Failed to save SHA:', e);
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
app.use(express.json({ limit: '10mb' }));
|
| 61 |
|
| 62 |
// --- API Turnstile Protection ---
|
|
|
|
| 104 |
}catch(e){ console.error('turnstile verify',e); return res.status(500).json({error:'Server error'});}
|
| 105 |
});
|
| 106 |
|
| 107 |
+
async function fetchLatestSHA() {
|
| 108 |
+
try {
|
|
|
|
| 109 |
const res = await fetch(`https://api.github.com/repos/${GITHUB_REPO}/commits/main`);
|
| 110 |
const data = await res.json();
|
| 111 |
latestSHA = data.sha;
|
| 112 |
console.log('Updated latest SHA:', latestSHA);
|
| 113 |
+
saveStoredSHA(latestSHA); // persist it
|
| 114 |
+
} catch (e) {
|
| 115 |
+
console.error('Failed to fetch latest commit SHA', e);
|
| 116 |
+
}
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
// Load persisted SHA if exists; otherwise fetch latest
|
| 120 |
+
latestSHA = loadStoredSHA();
|
| 121 |
+
if (!latestSHA) {
|
| 122 |
+
console.log('No stored SHA found, fetching latest...');
|
| 123 |
+
await fetchLatestSHA();
|
| 124 |
+
} else {
|
| 125 |
+
console.log('Using stored SHA:', latestSHA);
|
| 126 |
}
|
| 127 |
|
| 128 |
// --- Admin endpoints ---
|
|
|
|
| 149 |
res.json({success: token===ADMIN_TOKEN});
|
| 150 |
});
|
| 151 |
|
| 152 |
+
app.get('/admin/refresh', verifyLimiter, async (req, res) => {
|
| 153 |
const token = req.query.token;
|
| 154 |
+
if (token !== ADMIN_TOKEN) return res.status(403).send('Forbidden');
|
| 155 |
+
|
| 156 |
+
const sha = req.query.sha?.trim();
|
| 157 |
+
if (sha) {
|
| 158 |
+
if (!/^[0-9a-f]{7,40}$/.test(sha)) return res.status(400).send('Invalid SHA');
|
| 159 |
+
latestSHA = sha;
|
| 160 |
+
saveStoredSHA(latestSHA); // persist manual SHA
|
| 161 |
+
console.log(`Manual SHA set by admin: ${latestSHA}`);
|
| 162 |
+
return res.send(`Version set to commit ${latestSHA}`);
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
await fetchLatestSHA();
|
| 166 |
+
res.send(`Latest version refreshed: ${latestSHA}`);
|
| 167 |
});
|
| 168 |
|
| 169 |
// --- MIME type helper ---
|