This commit is contained in:
2024-01-05 22:06:34 -05:00
parent c8f00cdeaf
commit abc547c642
34 changed files with 6586 additions and 1561 deletions

11
routes/api.js Normal file
View File

@ -0,0 +1,11 @@
'use strict';
const router = require('express').Router();
const middleware = require('>/middleware/auth');
router.use('/auth', require('./auth'));
router.use('/token/auth', require('./authtoken'));
router.use('/torrent', middleware.auth, require('./transmission'));
router.use('/user', middleware.auth, require('./user'));
module.exports = router;

31
routes/auth.js Normal file
View File

@ -0,0 +1,31 @@
'use strict';
const router = require('express').Router();
const { Auth } = require('>/controller/auth');
router.post('/login', async function(req, res, next){
try{
let auth = await Auth.login(req.body);
return res.json({
login: true,
token: auth.token.token,
message:`${req.body.username} logged in!`,
});
}catch(error){
next(error);
}
});
router.all('/logout', async function(req, res, next){
try{
if(req.user){
await req.user.logout();
}
res.json({message: 'Bye'})
}catch(error){
next(error);
}
});
module.exports = router;

67
routes/authtoken.js Normal file
View File

@ -0,0 +1,67 @@
'use strict';
const router = require('express').Router();
const {AuthToken} = require('>/models');
router.get('/', async function(req, res, next){
try{
return res.json(await AuthToken.findAll());
}catch(error){
next(error);
}
});
router.post('/', async function(req, res, next){
try{
return res.json(await AuthToken.create(req.body));
}catch(error){
console.error(error)
next(error);
}
});
router.get('/user/:username', async function(req, res, next){
try{
return res.json(await AuthToken.findAll({where:{
username: req.params.username
}}));
}catch(error){
next(error);
}
});
router.get('/:token', async function(req, res, next){
try{
let token = await AuthToken.findByPk(req.params.token)
token.dataValues.user = await token.getUser()
return res.json(token);
}catch(error){
next(error);
}
});
router.put('/:token', async function(req, res, next){
try{
let token = await AuthToken.findByPk(req.params.token);
await token.update(req.body);
return res.json(token);
}catch(error){
next(error);
}
});
router.delete('/:token', async function(req, res, next){
try{
let token = await AuthToken.findByPk(req.params.token);
await token.destroy();
return res.json({'deleted': true});
}catch(error){
next(error);
}
});
module.exports = router;

95
routes/proxy.js Normal file
View File

@ -0,0 +1,95 @@
'use static';
const router = require('express').Router();
const zlib = require('zlib');
const fs = require('fs');
const https = require('https');
const http = require("http");
const proxy = require('http-proxy-middleware');
const inject = fs.readFileSync('./inject.html', 'utf8');
const mainjs = fs.readFileSync('./static/main.js', 'utf8');
// app.all("/*.js", function(req, res){res.send('')});
router.all('/static/main.js', function(req,res){
res.write(mainjs);
});
const proxyTarget = {
// target: "https://wtfismyip.com",
// host: "wtfismyip.com",
target: 'https://piratebay.party',
host: 'piratebay.party',
// target: 'http://172.16.0.1',
// host: 'piratebayo3klnzokct3wt5yyxb2vpebbuyjl7m623iaxmqhsd52coid.onion'
}
function generateRegexForDomain(domain) {
// Escape special characters in the domain name
const escapedDomain = domain.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
// Construct a regular expression pattern to match the domain with optional http(s):// prefix
const regexPattern = new RegExp(`(?:https?:\\/\\/)?${escapedDomain}`, 'ig');
return regexPattern;
}
router.all("/*", proxy({
target: proxyTarget.target,
agent: proxyTarget.target.startsWith('https') ? https.globalAgent : http.globalAgent,
secure: false,
autoRewrite: true,
changeOrigin: true,
followRedirects: true,
headers: {
host: proxyTarget.host
},
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(generateRegexForDomain(proxyTarget.host), '');
body = body.replace(/<\s*iframe[^]*?iframe>/igm, '');
body = body.replace("</html>", '');
body = body+inject+"</html>";
}
res.status(proxyRes.statusCode).end(body);
});
}
}));
module.exports = router;

42
routes/transmission.js Normal file
View File

@ -0,0 +1,42 @@
'use static';
const router = require('express').Router();
const {Torrent} = require('>/models');
router.get('/', async function(req, res, next){
try{
res.json({results: await Torrent.findAll({where:{added_by: req.user.username}})});
}catch(error){
next(error);
}
});
router.post("/", async function(req, res, next){
try{
res.json(await Torrent.create({...req.body, added_by: req.user.username}))
}catch(error){
next(error);
}
});
router.get('/server', async function(req, res, next){
try{
res.json(await Torrent.trClient.sessionStats())
}catch(error){
next(error);
}
});
router.get("/:id", async function(req, res, next){
try{
let torrent = await Torrent.findByPk(req.params.id);
if('latest' in req.query){
torrent = await torrent.getTorrentData();
}
res.json({result: torrent});
}catch(error){
next(error);
}
});
module.exports = router;

35
routes/user.js Normal file
View File

@ -0,0 +1,35 @@
'use strict';
const router = require('express').Router();
const {User} = require('>/models');
router.get('/', async function(req, res, next){
try{
return res.json({
results: await User[req.query.detail ? "listDetail" : "list"]()
});
}catch(error){
next(error);
}
});
router.get('/me', async function(req, res, next){
try{
return res.json(await User.get({uid: req.user.uid}));
}catch(error){
next(error);
}
});
router.get('/:uid', async function(req, res, next){
try{
return res.json({
results: await User.get(req.params.uid),
});
}catch(error){
next(error);
}
});
module.exports = router;