From 13147af0ceff0f8fd250d6fa0a949269cf71523a Mon Sep 17 00:00:00 2001 From: MunyDev Date: Fri, 16 Aug 2024 18:29:10 -0400 Subject: [PATCH] many many fixes bundled into one commit --- configs/m.google.com/index.js | 65 +++++++++++++++++++------- package-lock.json | 87 ++++++++++++++++++++++++++++++++++- package.json | 3 +- proxy.js | 6 ++- 4 files changed, 140 insertions(+), 21 deletions(-) diff --git a/configs/m.google.com/index.js b/configs/m.google.com/index.js index f68e7cd..3be0a36 100644 --- a/configs/m.google.com/index.js +++ b/configs/m.google.com/index.js @@ -2,13 +2,14 @@ const net = require('net'); const handlers = require('../../handlers'); const path = require('path'); const express = require('express'); -const axios = require('axios').default; + +const url = require('url'); /** * * @param {import("../../proxy").FilterInfo} f */ function filter(f) { - console.log(f.tls); + console.log("hi"); if (f.tls) { return true; } @@ -16,25 +17,53 @@ function filter(f) { } const app = express(); -app.post("/*", function (req, res) { - const dat = Buffer.alloc(parseInt(req.header('Content-Length'))); - var ptr = 0 - req.on('data', (buf)=>{ - dat.set(buf, ptr); - ptr += buf.length; - }); - req.on('end', async ()=>{ +app.post("/*", async function (req, res) { + console.log("Reading a post request") + console.log(req.header('Content-Length')); + var a = parseInt(req.header('Content-Length')) + const dat = Buffer.alloc(a); + var ptr = 0; + async function handle() { + console.log("handling: " + url.parse(req.url).search); 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); - res.writeHead(200, "OK"); - res.end(resFromRev.data.toString()); - } catch { - res.writeHead(500, "Internal server error"); - res.end("500 - Internal server error. Try again later"); + res.header('Content-Type', 'application/x-protobuffer'); + res.writeHead(resFromRev.status, resFromRev.statusText); + res.end(new Uint8Array((await resFromRev.arrayBuffer()))); + } catch (e) { + 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)=>{ res.writeHead(200, "OK"); @@ -47,8 +76,10 @@ app.get('/*', (req, res)=>{ */ function proxy(config, sock){ const ms = handlers.getMiniServer(function (req, res) { + console.log(req); app(req, res); }, path.resolve(__dirname, "public", "google.com.pem"),path.resolve(__dirname, "public", "google.com.key")) + console.log("Hi"); const socks = net.createConnection({ host: "localhost", port: ms.port diff --git a/package-lock.json b/package-lock.json index 7e52315..061110f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,7 +6,8 @@ "": { "dependencies": { "axios": "^1.7.3", - "express": "^4.19.2" + "express": "^4.19.2", + "node-fetch": "^3.3.2" } }, "node_modules/accepts": { @@ -133,6 +134,14 @@ "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", "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": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -268,6 +277,28 @@ "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": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.2.0.tgz", @@ -317,6 +348,17 @@ "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": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", @@ -517,6 +559,41 @@ "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": { "version": "1.13.2", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.2.tgz", @@ -760,6 +837,14 @@ "engines": { "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" + } } } } diff --git a/package.json b/package.json index 79ef5cb..4b5d543 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,7 @@ { "dependencies": { "axios": "^1.7.3", - "express": "^4.19.2" + "express": "^4.19.2", + "node-fetch": "^3.3.2" } } diff --git a/proxy.js b/proxy.js index 6a95e25..f898134 100644 --- a/proxy.js +++ b/proxy.js @@ -179,6 +179,8 @@ server.on('connection', (clientToProxySocket) => { path = firstLine.split(' ')[1]; // console.log(serverAddress); } + console.log(serverAddress); + console.log(isTLSConnection); var isFiltered = false; var using = null; Object.keys(serverCallbackMap).forEach((v)=>{ @@ -195,7 +197,7 @@ server.on('connection', (clientToProxySocket) => { // console.log(isFiltered); } }) - + console.log(isFiltered); if (isFiltered) { serverCallbackMap[using].proxy()(serverCallbackMap[using].config, clientToProxySocket); return; @@ -205,13 +207,13 @@ server.on('connection', (clientToProxySocket) => { port: serverPort }, () => { // console.log('PROXY TO SERVER SET UP'); - if (isTLSConnection) { //Send Back OK to HTTPS CONNECT Request clientToProxySocket.write('HTTP/1.1 200 OK\r\n\n'); } else { proxyToServerSocket.write(data); } + // Piping the sockets clientToProxySocket.pipe(proxyToServerSocket); proxyToServerSocket.pipe(clientToProxySocket);