Skip to content

Instantly share code, notes, and snippets.

@drhema
Last active October 23, 2023 19:44
Show Gist options
  • Save drhema/4c2ece0c86b7d353440f08da742a40a0 to your computer and use it in GitHub Desktop.
Save drhema/4c2ece0c86b7d353440f08da742a40a0 to your computer and use it in GitHub Desktop.
google proxy nodejs
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const url = require('url');
const app = express();
app.use('/', (req, res, next) => {
const targetUrl = req.query.url;
if (!targetUrl) {
res.status(400).send("Please provide a URL to proxy to in the 'url' query parameter.");
return;
}
const decodedUrl = decodeURIComponent(targetUrl);
const urlObj = new URL(decodedUrl);
if (urlObj.hostname.includes('google.com')) {
const params = urlObj.searchParams;
const googleParams = {
q: params.get('q'),
lr: params.get('lr'),
hl: params.get('hl'),
cr: params.get('cr'),
gl: params.get('gl'),
as_qdr: params.get('as_qdr'),
as_rights: params.get('as_rights'),
as_sitesearch: params.get('as_sitesearch'),
};
console.log('Google Parameters:', googleParams);
}
const proxy = createProxyMiddleware({
target: `${urlObj.protocol}//${urlObj.hostname}`,
changeOrigin: true,
pathRewrite: (path, req) => {
return urlObj.pathname + urlObj.search;
}
});
proxy(req, res, next);
});
app.listen(2023, '0.0.0.0', () => {
console.log(`Proxy server running and accessible from any network on port 2023`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment