|
|
|
|
|
process.on('uncaughtException', (err) => { |
|
|
console.error('[Fatal Error] Uncaught Exception:', err.stack || err.message || err); |
|
|
process.exit(1); |
|
|
}); |
|
|
|
|
|
process.on('unhandledRejection', (reason, promise) => { |
|
|
console.error('[Warning] Unhandled Rejection at:', promise, 'reason:', reason); |
|
|
}); |
|
|
|
|
|
|
|
|
const express = require('express'); |
|
|
const bodyParser = require('body-parser'); |
|
|
const cors = require('cors'); |
|
|
const requestIp = require('request-ip'); |
|
|
const crypto = require('crypto'); |
|
|
const { URL } = require('url'); |
|
|
|
|
|
const app = express(); |
|
|
|
|
|
|
|
|
const PORT = process.env.PORT || 7860; |
|
|
const HOST = '0.0.0.0'; |
|
|
|
|
|
|
|
|
if (!PORT) { |
|
|
console.error("[Error] PORT environment variable is undefined. This is required by Hugging Face Spaces."); |
|
|
process.exit(1); |
|
|
} |
|
|
|
|
|
console.log(`Starting server with HOST: ${HOST}, PORT: ${PORT}`); |
|
|
|
|
|
|
|
|
|
|
|
const corsOptions = { |
|
|
origin: '*', |
|
|
methods: ['GET', 'POST', 'OPTIONS'], |
|
|
allowedHeaders: ['Content-Type'], |
|
|
}; |
|
|
app.use(cors(corsOptions)); |
|
|
|
|
|
|
|
|
app.use(bodyParser.json()); |
|
|
|
|
|
|
|
|
app.use(requestIp.mw()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const urlMap = new Map(); |
|
|
|
|
|
|
|
|
const EXPIRATION_TIME = 5 * 60 * 1000; |
|
|
const CLEANUP_INTERVAL = 5.1 * 60 * 1000; |
|
|
|
|
|
|
|
|
setInterval(() => { |
|
|
const now = Date.now(); |
|
|
const expiredTime = now - EXPIRATION_TIME; |
|
|
|
|
|
for (const [userId, { timestamp }] of urlMap) { |
|
|
if (timestamp < expiredTime) { |
|
|
console.log(`Deleting expired data for user: ${userId}`); |
|
|
urlMap.delete(userId); |
|
|
} |
|
|
} |
|
|
}, CLEANUP_INTERVAL); |
|
|
|
|
|
|
|
|
app.get('/', (req, res) => { |
|
|
res.status(200).json({ |
|
|
message: "Server is running with backup port.", |
|
|
port: PORT || "undefined", |
|
|
environment: process.env, |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
const generateDeviceFingerprint = (req) => { |
|
|
const ip = req.clientIp || ''; |
|
|
const userAgent = req.headers['user-agent'] || ''; |
|
|
const acceptLanguage = req.headers['accept-language'] || ''; |
|
|
const connection = req.headers['connection'] || ''; |
|
|
const encoding = req.headers['accept-encoding'] || ''; |
|
|
const forwardedFor = req.headers['x-forwarded-for'] || ''; |
|
|
|
|
|
|
|
|
const rawFingerprint = `${ip}-${userAgent}-${acceptLanguage}-${connection}-${encoding}-${forwardedFor}`; |
|
|
|
|
|
|
|
|
const fingerprint = crypto.createHash('sha256').update(rawFingerprint).digest('hex'); |
|
|
return fingerprint; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.post('/storeURL', (req, res) => { |
|
|
const url = req.body.url; |
|
|
const ip = req.clientIp; |
|
|
|
|
|
|
|
|
if (!url) { |
|
|
return res.status(400).json({ error: 'URL is required.' }); |
|
|
} |
|
|
try { |
|
|
new URL(url); |
|
|
} catch (err) { |
|
|
return res.status(400).json({ error: 'Invalid URL format.' }); |
|
|
} |
|
|
|
|
|
|
|
|
const deviceFingerprint = generateDeviceFingerprint(req); |
|
|
const userId = `${ip}-${deviceFingerprint}`; |
|
|
|
|
|
|
|
|
urlMap.set(userId, { url, timestamp: Date.now() }); |
|
|
console.log(`Stored URL for user: ${userId}`); |
|
|
|
|
|
|
|
|
res.json({ message: 'URL stored successfully.', userId }); |
|
|
}); |
|
|
|
|
|
|
|
|
app.get('/getURL', (req, res) => { |
|
|
const ip = req.clientIp; |
|
|
|
|
|
|
|
|
const deviceFingerprint = generateDeviceFingerprint(req); |
|
|
const userId = `${ip}-${deviceFingerprint}`; |
|
|
|
|
|
|
|
|
if (urlMap.has(userId)) { |
|
|
const storedData = urlMap.get(userId); |
|
|
storedData.timestamp = Date.now(); |
|
|
urlMap.set(userId, storedData); |
|
|
|
|
|
console.log(`Retrieved URL for user: ${userId}`); |
|
|
return res.json({ url: storedData.url }); |
|
|
} else { |
|
|
console.error(`No URL found for user: ${userId}`); |
|
|
return res.status(404).json({ error: 'URL not found for this user.' }); |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
app.listen(PORT, HOST, (err) => { |
|
|
if (err) { |
|
|
console.error("[Error] Server failed to start:", err); |
|
|
process.exit(1); |
|
|
} |
|
|
console.log(`Server successfully started on http://${HOST}:${PORT}`); |
|
|
}); |
|
|
|
|
|
|
|
|
console.log("Full Environment Variables:", process.env); |
|
|
|