stream / index.js
tranquan24's picture
Create index.js
a0b358c verified
raw
history blame
2.57 kB
const express = require('express');
const request = require('request');
const app = express();
const PORT = process.env.PORT || 7860;
const eachfilesize = {};
const REQUEST_URL = process.env.REQUEST_URL;
const TOKEN = process.env.TOKEN;
async function getFileSize(link) {
return new Promise((resolve, reject) => {
request.head(link, (err, res, body) => {
if (err) {
reject(err);
} else {
const contentLength = (parseInt(res.headers['content-length']) || 0.1).toString();
eachfilesize[link] = contentLength;
resolve(contentLength);
}
});
});
}
app.get('*', async (req, res) => {
if (req.method !== 'GET') {
return res.status(405).send('Method Not Allowed');
}
const referer = req.headers.referer;
if (!referer || !referer.includes('https://tpcloud.rf.gd')) {
return res.status(404).send();
}
const filename = req.params[0];
if (filename) {
const originalUrl = `${REQUEST_URL}${filename}`;
const headers = {
'Authorization': `Bearer ${TOKEN}`,
'Range': req.headers.range || ''
};
try {
const originalResponse = await fetch(originalUrl, { headers });
const newResponse = new Response(originalResponse.body, originalResponse);
newResponse.headers.delete('Date');
newResponse.headers.delete('Etag');
newResponse.headers.delete('Last-Modified');
newResponse.headers.delete('Cf-Cache-Status');
newResponse.headers.delete('Content-Disposition');
newResponse.headers.delete('Nel');
newResponse.headers.delete('Cf-Ray');
newResponse.headers.delete('Report-To');
newResponse.headers.delete('X-Amz-Cf-Id');
newResponse.headers.delete('X-Amz-Cf-Pop');
newResponse.headers.delete('X-Amz-Server-Side-Encryption');
newResponse.headers.delete('X-Amz-Storage-Class');
newResponse.headers.delete('X-Cache');
newResponse.headers.set('Cache-Control', 'private, max-age=21188');
newResponse.headers.set('Connection', 'close');
newResponse.headers.set('Server', 'gvs 1.0');
newResponse.headers.set('Vary', 'Origin');
newResponse.headers.set('X-Content-Type-Options', 'nosniff');
newResponse.headers.set('Access-Control-Allow-Origin', 'https://tpcloud.rf.gd');
res.send(newResponse);
} catch (err) {
console.error(err);
return res.status(500).send('500');
}
} else {
return res.status(400).send('400');
}
});
app.listen(PORT, '0.0.0.0', () => {
console.log(`Server is running on http://0.0.0.0:${PORT}`);
});