|
|
const express = require('express'); |
|
|
const fs = require('fs').promises; |
|
|
const fsSync = require('fs'); |
|
|
const path = require('path'); |
|
|
|
|
|
const app = express(); |
|
|
const PORT = 3000; |
|
|
|
|
|
|
|
|
function getCurrentTime() { |
|
|
const now = new Date(); |
|
|
const year = now.getFullYear(); |
|
|
const month = String(now.getMonth() + 1).padStart(2, '0'); |
|
|
const day = String(now.getDate()).padStart(2, '0'); |
|
|
const hours = String(now.getHours()).padStart(2, '0'); |
|
|
const minutes = String(now.getMinutes()).padStart(2, '0'); |
|
|
const seconds = String(now.getSeconds()).padStart(2, '0'); |
|
|
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; |
|
|
} |
|
|
|
|
|
function extractBetweenZUXOSandST(str) { |
|
|
|
|
|
const regex = /_ZUXOS_(.+?)(_ST_|_DEV_)/; |
|
|
const match = str.match(regex); |
|
|
return match ? match[1] : null; |
|
|
} |
|
|
|
|
|
|
|
|
app.use(express.json()); |
|
|
app.use(express.urlencoded({ extended: true })); |
|
|
|
|
|
|
|
|
app.use(express.static(__dirname)); |
|
|
|
|
|
|
|
|
app.post('/upgrade', async (req, res) => { |
|
|
try { |
|
|
|
|
|
const fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl; |
|
|
|
|
|
const version = extractBetweenZUXOSandST(req.originalUrl); |
|
|
|
|
|
|
|
|
const logEntry = `[${getCurrentTime()}] 当前版本:${version} ${fullUrl}\n`; |
|
|
|
|
|
|
|
|
fsSync.appendFileSync(path.join(__dirname, 'log.txt'), logEntry); |
|
|
|
|
|
|
|
|
const xmlFilePath = path.join(__dirname, version + '.xml'); |
|
|
const xmlContent = await fs.readFile(xmlFilePath, 'utf-8'); |
|
|
|
|
|
res.setHeader('Content-Type', 'application/xml'); |
|
|
res.status(200).send(xmlContent); |
|
|
} catch (error) { |
|
|
console.error('Error reading or sending OTA config:', error); |
|
|
if (error.code === 'ENOENT') { |
|
|
res.status(404).send('<error>OTA configuration file not found</error>'); |
|
|
} else { |
|
|
res.status(500).send('<error>Failed to retrieve OTA configuration</error>'); |
|
|
} |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
app.get('/', async (req, res) => { |
|
|
const logPath = path.join(__dirname, 'log.txt'); |
|
|
|
|
|
try { |
|
|
let data = ''; |
|
|
try { |
|
|
data = await fs.readFile(logPath, 'utf8'); |
|
|
} catch (err) { |
|
|
if (err.code !== 'ENOENT') throw err; |
|
|
} |
|
|
|
|
|
res.send(`<html><body><h2>Log Content:</h2><pre>${data}</pre></body></html>`); |
|
|
} catch (err) { |
|
|
console.error('Error reading log file:', err); |
|
|
res.status(500).send('Error reading log file'); |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
app.listen(PORT, () => { |
|
|
console.log(`Server running on http://localhost:${PORT}`); |
|
|
console.log('Endpoints:'); |
|
|
console.log(` - POST /upgrade`); |
|
|
console.log(` - GET / (view logs)`); |
|
|
}); |