117 lines
3.1 KiB
JavaScript
117 lines
3.1 KiB
JavaScript
const zlib = require('zlib');
|
|
const fs = require('fs');
|
|
var https = require('https');
|
|
const express = require('express');
|
|
const proxy = require('http-proxy-middleware');
|
|
|
|
const app = express();
|
|
app.use(express.json());
|
|
|
|
const port = process.env.NODE_PORT || '3000';
|
|
|
|
const inject = fs.readFileSync('./inject.html', 'utf8');
|
|
|
|
const Transmission = require('transmission-promise')
|
|
var tr_client = new Transmission({
|
|
host:'tran.718it.biz',
|
|
ssl: true,
|
|
port: 443,
|
|
username: 'william',
|
|
password: 'palm7',
|
|
})
|
|
|
|
app.post("/__api/torrent", async function(req, res, next){
|
|
|
|
if(req.body.password !== '4412'){
|
|
return res.status(421);
|
|
}
|
|
|
|
try{
|
|
let cres = await tr_client.addUrl(Buffer.from(req.body.magnet, 'base64').toString('binary'));
|
|
res.json(cres)
|
|
|
|
}catch(e){
|
|
console.error('error', e)
|
|
res.status(500).json({message: e})
|
|
}
|
|
});
|
|
|
|
app.get("/__api/torrent/:id", async function(req, res, next){
|
|
|
|
try{
|
|
let cres = await tr_client.get(Number(req.params.id), ["eta", "percentDone", "status", "rateDownload", "errorString", "hashString", 'name']);
|
|
res.json({id:req.params.id, cres})
|
|
|
|
}catch(e){
|
|
console.error('error', e)
|
|
res.status(500).json({message: e})
|
|
}
|
|
});
|
|
|
|
|
|
app.all("/*.js", function(req, res){res.send('')});
|
|
|
|
app.all("/*", proxy({
|
|
target: 'https://piratebay.party',
|
|
agent: https.globalAgent,
|
|
autoRewrite: true,
|
|
secure: false,
|
|
followRedirects: true,
|
|
autoRewrite: true,
|
|
changeOrigin: true,
|
|
secure: false,
|
|
followRedirects: true,
|
|
autoRewrite: true,
|
|
changeOrigin: true,
|
|
headers: {
|
|
host: 'piratebay.party'
|
|
},
|
|
selfHandleResponse: true, // so that the onProxyRes takes care of sending the response
|
|
onProxyRes: function(proxyRes, req, res){
|
|
|
|
if(proxyRes.statusCode === 403 && proxyRes.headers['content-type'] &&
|
|
proxyRes.headers['content-type'].match('html')
|
|
){
|
|
console.log('403')
|
|
var url = (req.protocol + '://' + req.get('host') + req.originalUrl);
|
|
proxyRes.headers['location'] = url.replace(/\??ckattempt\=\d+/, '');
|
|
proxyRes.statusCode == 307
|
|
|
|
return res.end()
|
|
}
|
|
|
|
for(let key of Object.keys(proxyRes.headers)){
|
|
if(['content-encoding'].includes(key)) continue;
|
|
// res.set(key, proxyRes.headers[key].toString().replace('http://', 'https://'))
|
|
}
|
|
|
|
let body = new Buffer('');
|
|
proxyRes.on('error', function(e){
|
|
console.error('ERROR!', e)
|
|
});
|
|
|
|
proxyRes.on('data', function(data){
|
|
body = Buffer.concat([body, data]);
|
|
});
|
|
|
|
proxyRes.on('end', function(){
|
|
body = proxyRes.headers['content-encoding'] === 'gzip' ? zlib.gunzipSync(body).toString('utf8') : body;
|
|
body = proxyRes.headers['content-encoding'] === 'br' ? zlib.brotliDecompressSync(body).toString('utf8') : body;
|
|
if(proxyRes.statusCode === 200 &&
|
|
proxyRes.headers['content-type'] &&
|
|
proxyRes.headers['content-type'].match('html')
|
|
){
|
|
body = body.toString().replace(/<\s*script[^]*?script>/igm, '');
|
|
body = body.replace(/piratebay\.party/ig, 'tpb.718it.biz')
|
|
body = body.replace(/<\s*iframe[^]*?iframe>/igm, '');
|
|
body = body.replace("</html>", '');
|
|
body = body+inject+"</html>";
|
|
}
|
|
res.status(proxyRes.statusCode).end(body);
|
|
});
|
|
|
|
}
|
|
}));
|
|
|
|
app.listen(port, () => console.log(`The Pirate Bay TOR proxy listening on ${port}!`))
|