Created
August 8, 2024 13:34
-
-
Save 3dprogramin/90552d363b46b13ed6b5c82b95ebe049 to your computer and use it in GitHub Desktop.
Intercept and forward HTTPS requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require("express"); | |
const https = require("https"); | |
const fs = require("fs"); | |
const path = require("path"); | |
const axios = require("axios"); | |
// environment variables | |
const IP_ADDRESS = process.env.IP_ADDRESS; | |
/** | |
* Forward request to original server / IP address | |
* @param {*} req | |
* @returns | |
*/ | |
async function forward(req) { | |
const url = `https://${IP_ADDRESS}` + req.originalUrl; // Append the original URL path and query parameters | |
const headers = req.headers; | |
const body = req.body; | |
try { | |
const response = await axios({ | |
method: req.method, // Use the same HTTP method as the incoming request | |
url: url, | |
headers: headers, | |
data: body, | |
httpsAgent: new https.Agent({ rejectUnauthorized: false }), // Ignore SSL certificate validation for the forwarded request | |
}); | |
// Return the response data, status, and headers to be used in the middleware | |
return { | |
data: response.data, | |
status: response.status, | |
headers: response.headers, | |
}; | |
} catch (error) { | |
console.error("Error forwarding request:", error.message); | |
throw error; // Re-throw the error to be handled by the middleware | |
} | |
} | |
// Create an Express application | |
const app = express(); | |
// Middleware to parse JSON bodies | |
app.use(express.json()); | |
// Middleware to parse URL-encoded bodies | |
app.use(express.urlencoded({ extended: true })); | |
/** | |
* Middleware to catch all incoming requests | |
*/ | |
app.use(async (req, res, next) => { | |
// console.log(req) | |
// console.log(req.query) | |
// console.log(req.headers) | |
// console.log(req.method) | |
// next() | |
// forward the request | |
try { | |
const forwardResponse = await forward(req); | |
// Set the status code and headers from the forwarded response | |
res.status(forwardResponse.status); | |
Object.keys(forwardResponse.headers).forEach((key) => { | |
res.setHeader(key, forwardResponse.headers[key]); | |
}); | |
console.log( | |
req.originalUrl, | |
forwardResponse.status, | |
JSON.stringify(forwardResponse.data, null, 2) | |
); | |
// Send the forwarded response data | |
res.send(forwardResponse.data); | |
} catch (err) { | |
console.log("ERROR", err); | |
} | |
}); | |
// Read the certificate and key | |
const key = fs.readFileSync(path.join(__dirname, "key.pem")); | |
const cert = fs.readFileSync(path.join(__dirname, "cert.pem")); | |
// Create the HTTPS server | |
const server = https.createServer({ key: key, cert: cert }, app); | |
// Start listening on port 443 | |
server.listen(443, () => { | |
console.log("Express server is running on https://127.0.0.1:443"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment