many many fixes bundled into one commit

This commit is contained in:
MunyDev 2024-08-16 18:29:10 -04:00
parent 5ad24ae697
commit 13147af0ce
No known key found for this signature in database
GPG Key ID: 7DFBB51356064F62
4 changed files with 140 additions and 21 deletions

View File

@ -2,13 +2,14 @@ const net = require('net');
const handlers = require('../../handlers'); const handlers = require('../../handlers');
const path = require('path'); const path = require('path');
const express = require('express'); const express = require('express');
const axios = require('axios').default;
const url = require('url');
/** /**
* *
* @param {import("../../proxy").FilterInfo} f * @param {import("../../proxy").FilterInfo} f
*/ */
function filter(f) { function filter(f) {
console.log(f.tls); console.log("hi");
if (f.tls) { if (f.tls) {
return true; return true;
} }
@ -16,25 +17,53 @@ function filter(f) {
} }
const app = express(); const app = express();
app.post("/*", function (req, res) { app.post("/*", async function (req, res) {
const dat = Buffer.alloc(parseInt(req.header('Content-Length'))); console.log("Reading a post request")
var ptr = 0 console.log(req.header('Content-Length'));
req.on('data', (buf)=>{ var a = parseInt(req.header('Content-Length'))
dat.set(buf, ptr); const dat = Buffer.alloc(a);
ptr += buf.length; var ptr = 0;
}); async function handle() {
req.on('end', async ()=>{ console.log("handling: " + url.parse(req.url).search);
try { try {
const resFromRev = await axios.post('http://localhost:3040/', dat); // console.log(new URL(req.path).search);
fetch = (await import('node-fetch')).default;
const resFromRev = await fetch('http://localhost:3040/' + url.parse(req.url).search, {
body: dat,
"headers": req.headers,
method: "POST"
});
// console.log(resFromRev.data); // console.log(resFromRev.data);
res.writeHead(200, "OK"); res.header('Content-Type', 'application/x-protobuffer');
res.end(resFromRev.data.toString()); res.writeHead(resFromRev.status, resFromRev.statusText);
} catch { res.end(new Uint8Array((await resFromRev.arrayBuffer())));
res.writeHead(500, "Internal server error"); } catch (e) {
res.end("500 - Internal server error. Try again later"); if (!e.response) {
console.log("Error occured here without a response. Weird.");
console.log(e);
res.writeHead(500, 'Internal server error');
res.end('An error has occured');
return;
}
res.header('Content-Type', "application/x-protobuffer");
res.header('Content-Length', e.response.data.length.toString());
res.writeHead(e.response.status, "Internal server error");
res.end(e.response.data);
}
} }
req.on("data", function(r) {
dat.set(r, ptr);
ptr += r.length;
console.log(ptr);
if (ptr === a) {
handle()
}
}) })
}); });
app.get('/*', (req, res)=>{ app.get('/*', (req, res)=>{
res.writeHead(200, "OK"); res.writeHead(200, "OK");
@ -47,8 +76,10 @@ app.get('/*', (req, res)=>{
*/ */
function proxy(config, sock){ function proxy(config, sock){
const ms = handlers.getMiniServer(function (req, res) { const ms = handlers.getMiniServer(function (req, res) {
console.log(req);
app(req, res); app(req, res);
}, path.resolve(__dirname, "public", "google.com.pem"),path.resolve(__dirname, "public", "google.com.key")) }, path.resolve(__dirname, "public", "google.com.pem"),path.resolve(__dirname, "public", "google.com.key"))
console.log("Hi");
const socks = net.createConnection({ const socks = net.createConnection({
host: "localhost", host: "localhost",
port: ms.port port: ms.port

87
package-lock.json generated
View File

@ -6,7 +6,8 @@
"": { "": {
"dependencies": { "dependencies": {
"axios": "^1.7.3", "axios": "^1.7.3",
"express": "^4.19.2" "express": "^4.19.2",
"node-fetch": "^3.3.2"
} }
}, },
"node_modules/accepts": { "node_modules/accepts": {
@ -133,6 +134,14 @@
"resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz",
"integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==" "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ=="
}, },
"node_modules/data-uri-to-buffer": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz",
"integrity": "sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==",
"engines": {
"node": ">= 12"
}
},
"node_modules/debug": { "node_modules/debug": {
"version": "2.6.9", "version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
@ -268,6 +277,28 @@
"node": ">= 0.10.0" "node": ">= 0.10.0"
} }
}, },
"node_modules/fetch-blob": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.2.0.tgz",
"integrity": "sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/jimmywarting"
},
{
"type": "paypal",
"url": "https://paypal.me/jimmywarting"
}
],
"dependencies": {
"node-domexception": "^1.0.0",
"web-streams-polyfill": "^3.0.3"
},
"engines": {
"node": "^12.20 || >= 14.13"
}
},
"node_modules/finalhandler": { "node_modules/finalhandler": {
"version": "1.2.0", "version": "1.2.0",
"resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.2.0.tgz", "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.2.0.tgz",
@ -317,6 +348,17 @@
"node": ">= 6" "node": ">= 6"
} }
}, },
"node_modules/formdata-polyfill": {
"version": "4.0.10",
"resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz",
"integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==",
"dependencies": {
"fetch-blob": "^3.1.2"
},
"engines": {
"node": ">=12.20.0"
}
},
"node_modules/forwarded": { "node_modules/forwarded": {
"version": "0.2.0", "version": "0.2.0",
"resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz",
@ -517,6 +559,41 @@
"node": ">= 0.6" "node": ">= 0.6"
} }
}, },
"node_modules/node-domexception": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz",
"integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/jimmywarting"
},
{
"type": "github",
"url": "https://paypal.me/jimmywarting"
}
],
"engines": {
"node": ">=10.5.0"
}
},
"node_modules/node-fetch": {
"version": "3.3.2",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.3.2.tgz",
"integrity": "sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==",
"dependencies": {
"data-uri-to-buffer": "^4.0.0",
"fetch-blob": "^3.1.4",
"formdata-polyfill": "^4.0.10"
},
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/node-fetch"
}
},
"node_modules/object-inspect": { "node_modules/object-inspect": {
"version": "1.13.2", "version": "1.13.2",
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.2.tgz", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.2.tgz",
@ -760,6 +837,14 @@
"engines": { "engines": {
"node": ">= 0.8" "node": ">= 0.8"
} }
},
"node_modules/web-streams-polyfill": {
"version": "3.3.3",
"resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz",
"integrity": "sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==",
"engines": {
"node": ">= 8"
}
} }
} }
} }

View File

@ -1,6 +1,7 @@
{ {
"dependencies": { "dependencies": {
"axios": "^1.7.3", "axios": "^1.7.3",
"express": "^4.19.2" "express": "^4.19.2",
"node-fetch": "^3.3.2"
} }
} }

View File

@ -179,6 +179,8 @@ server.on('connection', (clientToProxySocket) => {
path = firstLine.split(' ')[1]; path = firstLine.split(' ')[1];
// console.log(serverAddress); // console.log(serverAddress);
} }
console.log(serverAddress);
console.log(isTLSConnection);
var isFiltered = false; var isFiltered = false;
var using = null; var using = null;
Object.keys(serverCallbackMap).forEach((v)=>{ Object.keys(serverCallbackMap).forEach((v)=>{
@ -195,7 +197,7 @@ server.on('connection', (clientToProxySocket) => {
// console.log(isFiltered); // console.log(isFiltered);
} }
}) })
console.log(isFiltered);
if (isFiltered) { if (isFiltered) {
serverCallbackMap[using].proxy()(serverCallbackMap[using].config, clientToProxySocket); serverCallbackMap[using].proxy()(serverCallbackMap[using].config, clientToProxySocket);
return; return;
@ -205,13 +207,13 @@ server.on('connection', (clientToProxySocket) => {
port: serverPort port: serverPort
}, () => { }, () => {
// console.log('PROXY TO SERVER SET UP'); // console.log('PROXY TO SERVER SET UP');
if (isTLSConnection) { if (isTLSConnection) {
//Send Back OK to HTTPS CONNECT Request //Send Back OK to HTTPS CONNECT Request
clientToProxySocket.write('HTTP/1.1 200 OK\r\n\n'); clientToProxySocket.write('HTTP/1.1 200 OK\r\n\n');
} else { } else {
proxyToServerSocket.write(data); proxyToServerSocket.write(data);
} }
// Piping the sockets // Piping the sockets
clientToProxySocket.pipe(proxyToServerSocket); clientToProxySocket.pipe(proxyToServerSocket);
proxyToServerSocket.pipe(clientToProxySocket); proxyToServerSocket.pipe(clientToProxySocket);