Updated Alloy (v2.3)

This commit is contained in:
TheEmeraldStarr 2020-10-13 09:34:59 -07:00
parent c15aef5eb2
commit b83e2fece4
5 changed files with 391 additions and 276 deletions

83
app.js
View file

@ -1,4 +1,4 @@
const express = require('express'), const express = require('express'),
app = express(), app = express(),
http = require('http'), http = require('http'),
https = require('https'), https = require('https'),
@ -6,50 +6,45 @@ const express = require('express'),
querystring = require('querystring'), querystring = require('querystring'),
session = require('express-session'), session = require('express-session'),
sanitizer = require('sanitizer'), sanitizer = require('sanitizer'),
websocket = require('./ws-proxy.js'),
fetch = require('node-fetch'); fetch = require('node-fetch');
const config = JSON.parse(fs.readFileSync('./config.json', { encoding: 'utf8' })); const config = JSON.parse(fs.readFileSync('./config.json', { encoding: 'utf8' }));
if (!config.prefix.startsWith('/')) { if (!config.prefix.startsWith('/')) {
config.prefix = `/${config.prefix}`; config.prefix = `/${config.prefix}`;
} }
if (!config.prefix.endsWith('/')) { if (!config.prefix.endsWith('/')) {
config.prefix = `${config.prefix}/`; config.prefix = `${config.prefix}/`;
} }
let server; let server;
let server_protocol; let server_protocol;
const server_options = { const server_options = {
key: fs.readFileSync('./ssl/default.key'), key: fs.readFileSync('./ssl/default.key'),
cert: fs.readFileSync('./ssl/default.crt') cert: fs.readFileSync('./ssl/default.crt')
} }
if (config.ssl == true) { if (config.ssl == true) { server = https.createServer(server_options, app);
server = https.createServer(server_options, app); server_protocol = 'https://'; } else { server = http.createServer(app);
server_protocol = 'https://'; server_protocol = 'http://'; };
} else {
server = http.createServer(app);
server_protocol = 'http://';
};
// WebSocket Proxying
websocket(server);
console.log(`Alloy Proxy now running on ${server_protocol}0.0.0.0:${config.port}! Proxy prefix is "${config.prefix}"!`); console.log(`Alloy Proxy now running on ${server_protocol}0.0.0.0:${config.port}! Proxy prefix is "${config.prefix}"!`);
server.listen(process.env.PORT || config.port); server.listen(process.env.PORT || config.port);
btoa = (str) => {
var login = require('./auth');
btoa = (str) => {
str = new Buffer.from(str).toString('base64'); str = new Buffer.from(str).toString('base64');
return str; return str;
}; };
atob = (str) => { atob = (str) => {
str = new Buffer.from(str, 'base64').toString('utf-8'); str = new Buffer.from(str, 'base64').toString('utf-8');
return str; return str;
}; };
rewrite_url = (dataURL, option) => { rewrite_url = (dataURL, option) => {
var websiteURL; var websiteURL;
var websitePath; var websitePath;
if (option == 'decode') { if (option == 'decode') {
@ -60,15 +55,17 @@ rewrite_url = (dataURL, option) => {
websitePath = '/' + dataURL.split('/').splice(3).join('/'); websitePath = '/' + dataURL.split('/').splice(3).join('/');
} }
if (websitePath == '/') { return `${websiteURL}`; } else return `${websiteURL}${websitePath}`; if (websitePath == '/') { return `${websiteURL}`; } else return `${websiteURL}${websitePath}`;
}; };
app.use(session({ var login = require('./auth');
app.use(session({
secret: 'alloy', secret: 'alloy',
saveUninitialized: true, saveUninitialized: true,
resave: true resave: true
})); }));
// We made our own version of body-parser instead, due to issues. // We made our own version of body-parser instead, due to issues.
app.use((req, res, next) => { app.use((req, res, next) => {
if (req.method == 'POST') { if (req.method == 'POST') {
req.raw_body = ''; req.raw_body = '';
req.on('data', chunk => { req.on('data', chunk => {
@ -84,9 +81,9 @@ app.use((req, res, next) => {
next(); next();
}); });
} else return next(); } else return next();
}); });
app.use(`${config.prefix}utils/`, async(req, res, next) => { app.use(`${config.prefix}utils/`, async(req, res, next) => {
if (req.url.startsWith('/assets/')) { res.sendFile(__dirname + '/utils' + req.url); } if (req.url.startsWith('/assets/')) { res.sendFile(__dirname + '/utils' + req.url); }
if (req.query.url) { if (req.query.url) {
let url = atob(req.query.url); let url = atob(req.query.url);
@ -99,15 +96,15 @@ app.use(`${config.prefix}utils/`, async(req, res, next) => {
} }
return res.redirect(307, config.prefix + rewrite_url(url)); return res.redirect(307, config.prefix + rewrite_url(url));
} }
}); });
app.post(`${config.prefix}session/`, async(req, res, next) => { app.post(`${config.prefix}session/`, async(req, res, next) => {
let url = querystring.parse(req.raw_body).url; let url = querystring.parse(req.raw_body).url;
if (url.startsWith('//')) { url = 'http:' + url; } else if (url.startsWith('https://') || url.startsWith('http://')) { url = url } else { url = 'http://' + url }; if (url.startsWith('//')) { url = 'http:' + url; } else if (url.startsWith('https://') || url.startsWith('http://')) { url = url } else { url = 'http://' + url };
return res.redirect(config.prefix + rewrite_url(url)); return res.redirect(config.prefix + rewrite_url(url));
}); });
app.use(config.prefix, async(req, res, next) => { app.use(config.prefix, async(req, res, next) => {
var proxy = {}; var proxy = {};
proxy.url = rewrite_url(req.url.slice(1), 'decode'); proxy.url = rewrite_url(req.url.slice(1), 'decode');
proxy.url = { proxy.url = {
@ -282,11 +279,11 @@ app.use(config.prefix, async(req, res, next) => {
}; };
// We send the response from the server rewritten. // We send the response from the server rewritten.
res.send(proxy.sendResponse); res.send(proxy.sendResponse);
}); });
app.use('/', express.static('public')); app.use('/', express.static('public'));
app.use(async(req, res, next) => { app.use(async(req, res, next) => {
if (req.headers['referer']) { if (req.headers['referer']) {
let referer = '/' + String(req.headers['referer']).split('/').splice(3).join('/'); let referer = '/' + String(req.headers['referer']).split('/').splice(3).join('/');
@ -307,4 +304,4 @@ app.use(async(req, res, next) => {
res.redirect(307, config.prefix + btoa(req.session.url) + req.url) res.redirect(307, config.prefix + btoa(req.session.url) + req.url)
} else return next(); } else return next();
}); });

View file

@ -1,3 +1,3 @@
[ [
"example.com" "add-your-website-url.blocked"
] ]

View file

@ -1,6 +1,6 @@
{ {
"name": "alloy-proxy", "name": "alloy-proxy",
"version": "2.2.0", "version": "2.3.0",
"description": "A web proxy capable of proxying websites!", "description": "A web proxy capable of proxying websites!",
"main": "app.js", "main": "app.js",
"scripts": { "scripts": {
@ -21,6 +21,7 @@
"node-fetch": "^2.6.1", "node-fetch": "^2.6.1",
"sanitizer": "^0.1.3", "sanitizer": "^0.1.3",
"session": "^0.1.0", "session": "^0.1.0",
"cookies": "0.4" "cookies": "0.4",
"ws": "^7.3.1"
} }
} }

View file

@ -71,6 +71,36 @@ let setattribute_rewrite = window.Element.prototype.setAttribute; window.Element
return setattribute_rewrite.apply(this, arguments) return setattribute_rewrite.apply(this, arguments)
} }
// Rewriting all incoming websocket request.
WebSocket = new Proxy(WebSocket, {
construct(target, args_array) {
var protocol;
if (location.protocol == 'https:') { protocol = 'wss://' } else { protocol = 'ws://' }
args_array[0] = protocol + location.origin.split('/').splice(2).join('/') + prefix + 'ws/' + btoa(args_array[0]);
return new target(args_array);
}
});
// Rewriting incoming pushstate.
history.pushState = new Proxy(history.pushState, {
apply: (target, thisArg, args_array) => {
args_array[2] = rewrite_url(args_array[2])
return target.apply(thisArg, args_array)
}
});
var previousState = window.history.state; var previousState = window.history.state;
setInterval(function() { setInterval(function() {
if (!window.location.pathname.startsWith(`${prefix}${btoa(url.origin)}/`)) { if (!window.location.pathname.startsWith(`${prefix}${btoa(url.origin)}/`)) {

87
ws-proxy.js Normal file
View file

@ -0,0 +1,87 @@
const WebSocket = require('ws'),
fs = require('fs');
const config = JSON.parse(fs.readFileSync('./config.json', {encoding:'utf8'}));
if (!config.prefix.startsWith('/')) {
config.prefix = `/${config.prefix}`;
}
if (!config.prefix.endsWith('/')) {
config.prefix = `${config.prefix}/`;
}
btoa = (str) => {
str = new Buffer.from(str).toString('base64');
return str;
};
atob = (str) => {
str = new Buffer.from(str, 'base64').toString('utf-8');
return str;
};
module.exports = (server) => {
const wss = new WebSocket.Server({ server: server });
wss.on('connection', (cli, req) => {
try {
const svr = new WebSocket(atob(req.url.toString().replace(`${config.prefix}ws/`, '')));
svr.on('message', (data) => {
try { cli.send(data) } catch(err){}
});
svr.on('open', () => {
cli.on('message', (data) => {
svr.send(data)
});
});
cli.on('close', (code) => {
try { svr.close(code); } catch(err) { svr.close(1006) };
});
svr.on('close', (code) => {
try { cli.close(code); } catch(err) { cli.close(1006) };
});
cli.on('error', (err) => {
try { svr.close(1001); } catch(err) { svr.close(1006) };
});
svr.on('error', (err) => {
try { cli.close(1001); } catch(err) { cli.close(1006) };
});
} catch(err) { cli.close(1001); }
});
}