lots...
This commit is contained in:
208
app.js
208
app.js
@@ -1,122 +1,118 @@
|
||||
const zlib = require('zlib');
|
||||
const fs = require('fs');
|
||||
var https = require('https');
|
||||
'use strict';
|
||||
|
||||
const Module = require('module');
|
||||
const original_resolveFilename = Module._resolveFilename;
|
||||
|
||||
Module._resolveFilename = function(...args){
|
||||
args[0] = args[0].startsWith('>') ? args[0].replace('>', __dirname) : args[0];
|
||||
return original_resolveFilename(...args);
|
||||
};
|
||||
|
||||
const path = require('path');
|
||||
const ejs = require('ejs')
|
||||
const express = require('express');
|
||||
const proxy = require('http-proxy-middleware');
|
||||
|
||||
// Set up the express app.
|
||||
const app = express();
|
||||
|
||||
// List of front end node modules to be served
|
||||
const frontEndModules = [
|
||||
'jquery', 'jquery-ui', 'moment', 'mustache',
|
||||
// 'bootstrap', '@fortawesome',
|
||||
];
|
||||
|
||||
// Hold list of functions to run when the server is ready
|
||||
app.onListen = [function(){console.log('hello')}];
|
||||
|
||||
// Allow the express app to be exported into other files.
|
||||
module.exports = app;
|
||||
|
||||
// Build the conf object from the conf files.
|
||||
app.conf = require('./conf');
|
||||
|
||||
// Grab the projects PubSub
|
||||
app.contollers = require('./controller');
|
||||
|
||||
// Push pubsub over the socket and back.
|
||||
app.onListen.push(function(){
|
||||
app.io.use(middleware.authIO);
|
||||
|
||||
app.contollers.pubsub.subscribe(/./g, function(data, topic){
|
||||
app.io.emit('P2PSub', { topic, data });
|
||||
});
|
||||
|
||||
app.io.on('connection', (socket) => {
|
||||
// console.log('socket', socket)
|
||||
var user = socket.user;
|
||||
socket.on('P2PSub', (msg) => {
|
||||
app.contollers.pubsub.publish(msg.topic, {...msg.data, __from:socket.user});
|
||||
// socket.broadcast.emit('P2PSub', msg);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
// Hold onto the auth middleware
|
||||
const middleware = require('./middleware/auth');
|
||||
|
||||
// load the JSON parser middleware. Express will parse JSON into native objects
|
||||
// for any request that has JSON in its content type.
|
||||
app.use(express.json());
|
||||
app.set('json spaces', 2);
|
||||
|
||||
const port = process.env.NODE_PORT || '3000';
|
||||
// Set up the templating engine to build HTML for the front end.
|
||||
app.set('views', path.join(__dirname, 'views'));
|
||||
app.set('view engine', 'ejs');
|
||||
|
||||
const inject = fs.readFileSync('./inject.html', 'utf8');
|
||||
const mainjs = fs.readFileSync('./static/main.js', 'utf8');
|
||||
// Have express server static content( images, CSS, browser JS) from the public
|
||||
// local folder.
|
||||
app.use('/__static', express.static(path.join(__dirname, 'public')))
|
||||
|
||||
|
||||
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})
|
||||
}
|
||||
// Server front end modules
|
||||
// https://stackoverflow.com/a/55700773/3140931
|
||||
frontEndModules.forEach(dep => {
|
||||
app.use(`/__static-modules/${dep}`, express.static(path.join(__dirname, `node_modules/${dep}`)))
|
||||
});
|
||||
|
||||
app.get("/__api/torrent/:id", async function(req, res, next){
|
||||
// API route
|
||||
app.use('/__api', require('./routes/api'));
|
||||
|
||||
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})
|
||||
// Routes for front end content.
|
||||
app.use('/', require('./routes/proxy'));
|
||||
|
||||
}catch(e){
|
||||
console.error('error', e)
|
||||
res.status(500).json({message: e})
|
||||
}
|
||||
// Catch 404 and forward to error handler. If none of the above routes are
|
||||
// used, this is what will be called.
|
||||
app.use(function(req, res, next) {
|
||||
var err = new Error('Not Found');
|
||||
err.message = 'Page not found'
|
||||
err.status = 404;
|
||||
next(err);
|
||||
});
|
||||
|
||||
// Error handler. This is where `next()` will go on error
|
||||
app.use(function(err, req, res, next) {
|
||||
console.error(err.status || 500, err.name, req.method, req.url);
|
||||
|
||||
// app.all("/*.js", function(req, res){res.send('')});
|
||||
// Parse key error for Sequilzw
|
||||
let keyErrors = {}
|
||||
if(['SequelizeValidationError'].includes(err.name) && err.errors){
|
||||
for(let item of err.errors){
|
||||
if(item.path){
|
||||
keyErrors[item.path] = item.message;
|
||||
}
|
||||
}
|
||||
err.status = 422;
|
||||
}
|
||||
|
||||
app.all('/static/main.js', function(req,res){
|
||||
res.write(mainjs);
|
||||
if(![404, 422].includes(err.status || res.status)){
|
||||
console.error(err.message);
|
||||
console.error(err.stack);
|
||||
console.error('=========================================');
|
||||
}
|
||||
res.status(err.status || 500);
|
||||
res.json({
|
||||
name: err.name || 'Unknown error',
|
||||
message: err.message,
|
||||
keyErrors,
|
||||
});
|
||||
});
|
||||
|
||||
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}!`))
|
||||
|
||||
Reference in New Issue
Block a user