diff --git a/.gitignore b/.gitignore index 89d46ce..5fe97e3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ node_modules -*_cert \ No newline at end of file +*_cert +*.log +.venv \ No newline at end of file diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..de96ddc --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "dmbackend"] + path = dmbackend + url = https://github.com/MunyDev/dmbackend diff --git a/configs/m.google.com/index.js b/configs/m.google.com/index.js index 3be0a36..73a95fc 100644 --- a/configs/m.google.com/index.js +++ b/configs/m.google.com/index.js @@ -4,6 +4,9 @@ const path = require('path'); const express = require('express'); const url = require('url'); +const child_proc = require('child_process'); +const fs = require('fs'); +const state = { pid: null }; /** * * @param {import("../../proxy").FilterInfo} f @@ -15,28 +18,65 @@ function filter(f) { } return false; } +async function startServerIfNeeded() { + if (state.pid) return; + let realpath = path.resolve('.', 'dmbackend', 'start_server.sh') + let prc = child_proc.spawn("/bin/bash", [path.resolve('.', 'dmbackend', 'start_server.sh'), realpath], {stdio: "pipe"}); + let wstream = fs.createWriteStream("./int_server.log"); + let pidPath = path.resolve(path.dirname(realpath), "pid"); + try { + fs.rmSync(pidPath); + } catch {}; + prc.stdout.pipe(wstream); + + prc.stderr.pipe(wstream); + // prc.stdout.once("data", async (c) => { + // let pid = 0; + // console.log("Started as PID: "+ (pid = parseInt(c.toString('utf-8').split("Server PID: ")[1].split('\n')[0]))); + // state.pid = pid; + let buffer = null; + while (true) { + if ((buffer = await new Promise((resolve)=>{ + fs.readFile(pidPath, (err, data)=>{ + if (err) + resolve(null); + else { + resolve(data); + } + }) + }))) { + break; + } + } + state.pid = parseInt(buffer.toString('utf-8')); + + // }) + console.log("Started internal server with pid of: " + state.pid); +} const app = express(); 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); + const dat = Buffer.alloc(a); var ptr = 0; async function handle() { console.log("handling: " + url.parse(req.url).search); try { // 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.header('Content-Type', 'application/x-protobuffer'); - res.writeHead(resFromRev.status, resFromRev.statusText); - res.end(new Uint8Array((await resFromRev.arrayBuffer()))); + await startServerIfNeeded(); + + const resFromRev = await fetch('http://localhost:3040/' + url.parse(req.url).search, { + body: dat, + "headers": req.headers, + method: "POST" + }); + // console.log(resFromRev.data); + 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."); @@ -48,12 +88,12 @@ app.post("/*", async function (req, res) { 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) { + + req.on("data", function (r) { dat.set(r, ptr); ptr += r.length; console.log(ptr); @@ -61,11 +101,11 @@ app.post("/*", async function (req, res) { handle() } }) - - - + + + }); -app.get('/*', (req, res)=>{ +app.get('/*', (req, res) => { res.writeHead(200, "OK"); res.end("OK"); }) @@ -74,20 +114,20 @@ app.get('/*', (req, res)=>{ * @param {import("../../proxy").ServerConfig} config * @param {net.Socket} sock */ -function proxy(config, sock){ +function proxy(config, sock) { const ms = handlers.getMiniServer(function (req, res) { - console.log(req); + // console.log(req); app(req, res); - }, path.resolve(__dirname, "public", "google.com.pem"),path.resolve(__dirname, "public", "google.com.key")) - console.log("Hi"); + }, path.resolve(__dirname, "public", "google.com.pem"), path.resolve(__dirname, "public", "google.com.key")) + const socks = net.createConnection({ host: "localhost", port: ms.port - }, function(){ + }, function () { sock.write('HTTP/1.1 200 OK\r\n\n'); sock.pipe(socks); socks.pipe(sock); - // sock.write('HTTP/1.1 200 OK\r\n\n'); + // sock.write('HTTP/1.1 200 OK\r\n\n'); }); } diff --git a/dmbackend b/dmbackend new file mode 160000 index 0000000..3d823ab --- /dev/null +++ b/dmbackend @@ -0,0 +1 @@ +Subproject commit 3d823ab09933b151dd5447d98f6f1a98113e1d67