plaster/app.js

147 lines
4.7 KiB
JavaScript
Raw Normal View History

2024-12-18 21:47:33 +00:00
const express = require('express');
const fetch = require('node-fetch');
2024-12-20 00:12:43 +00:00
const cookieParser = require('cookie-parser');
2024-12-20 04:49:02 +00:00
const path = require('path');
2024-12-20 15:19:06 +00:00
const fs = require('fs');
2024-12-21 01:58:55 +00:00
const captcha = require('trek-captcha');
const JSZip = require('jszip');
2024-12-18 21:47:33 +00:00
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.static('public'));
2024-12-20 00:12:43 +00:00
app.use(cookieParser());
2024-12-20 04:44:51 +00:00
// Environment variable to toggle ASCII art
const showAsciiArt = process.env.SHOW_ASCII_ART === 'true';
const asciiArtFolder = process.env.ASCII_ART_FOLDER || path.join(__dirname, 'ascii');
// Get ASCII art files
const asciiArtFiles = showAsciiArt ? fs.readdirSync(asciiArtFolder).filter(file => file.endsWith('.txt')) : [];
// Serve a random ASCII art
app.get('/ascii', (req, res) => {
if (!showAsciiArt || asciiArtFiles.length === 0) {
res.json({ enabled: false });
return;
}
const randomFile = asciiArtFiles[Math.floor(Math.random() * asciiArtFiles.length)];
const art = fs.readFileSync(path.join(asciiArtFolder, randomFile), 'utf-8');
res.json({ enabled: true, art });
});
2024-12-20 00:38:42 +00:00
const autoCopyDefault = process.env.AUTO_COPY_DEFAULT === 'true';
2024-12-20 00:12:43 +00:00
app.get('/auto-copy-default', (req, res) => {
res.json({ autoCopyDefault });
});
2024-12-18 21:47:33 +00:00
2024-12-18 22:01:20 +00:00
// Route to handle raw Pastebin requests
app.get('/:pasteId', async (req, res) => {
const pasteId = req.params.pasteId;
const rawUrl = `https://pastebin.com/raw/${pasteId}`;
2024-12-18 21:16:43 +00:00
2024-12-18 22:01:20 +00:00
try {
const response = await fetch(rawUrl);
if (!response.ok) {
throw new Error('Failed to fetch data from Pastebin');
2024-12-18 21:53:05 +00:00
}
2024-12-18 22:01:20 +00:00
const text = await response.text();
res.setHeader('Content-Type', 'text/plain'); // Ensure raw text response
res.send(text); // Send the raw paste content
} catch (error) {
res.status(500).json({ error: error.message }); // Return error in JSON format
2024-12-18 21:53:05 +00:00
}
2024-12-18 21:16:43 +00:00
});
2024-12-18 21:47:33 +00:00
2024-12-21 01:58:55 +00:00
let lastDownloadTime = {}; // To track cooldowns for each user
// Function to check cooldown
const checkCooldown = (userId) => {
const now = Date.now();
if (lastDownloadTime[userId] && now - lastDownloadTime[userId] < 180000) { // 3-minute cooldown
return false;
}
lastDownloadTime[userId] = now;
return true;
};
// Function to fetch paste data from Pastebin
const fetchPasteData = async (pasteId) => {
const rawUrl = `https://pastebin.com/raw/${pasteId}`;
const response = await fetch(rawUrl);
if (!response.ok) {
throw new Error('Failed to fetch paste data');
}
return response.text();
};
// Function to generate Trek-Captcha
const generateCaptcha = async () => {
const { token, buffer } = await captcha();
return { token, buffer };
};
// Bulk download route
app.post('/bulk-download', async (req, res) => {
const { userId, pasteIds, captchaToken } = req.body;
if (!checkCooldown(userId)) {
return res.status(429).json({ message: 'Please wait 3 minutes before making another bulk download request.' });
}
// Validate CAPTCHA
const isCaptchaValid = await validateCaptcha(captchaToken);
if (!isCaptchaValid) {
return res.status(400).json({ message: 'Invalid CAPTCHA.' });
}
if (pasteIds.length > 64) {
return res.status(400).json({ message: 'You can only request up to 64 pastes at a time.' });
}
try {
// Create a new ZIP file
const zip = new JSZip();
for (const pasteId of pasteIds) {
const pasteData = await fetchPasteData(pasteId);
zip.file(`${pasteId}.txt`, pasteData); // Save each paste as a text file in the ZIP
}
// Generate the ZIP file
const zipBuffer = await zip.generateAsync({ type: 'nodebuffer' });
// Send the ZIP file as a response
res.set('Content-Type', 'application/zip');
res.set('Content-Disposition', 'attachment; filename=pastes.zip');
res.send(zipBuffer);
} catch (error) {
console.error(error);
res.status(500).json({ message: 'Failed to generate the ZIP file.' });
}
});
// Function to validate the CAPTCHA token (you'll need to implement this on your server)
const validateCaptcha = async (captchaToken) => {
// This is where you would validate the token against Trek-Captcha's verification endpoint.
// For now, assuming a mock validation function that always returns true.
return true;
};
// Route to show CAPTCHA for bulk download
app.get('/captcha', async (req, res) => {
try {
const { token, buffer } = await generateCaptcha();
res.status(200).json({ token, image: buffer.toString('base64') });
} catch (error) {
res.status(500).json({ message: 'Failed to generate CAPTCHA.' });
}
});
2024-12-18 21:47:33 +00:00
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});