Last active
October 23, 2023 19:44
-
-
Save drhema/4c2ece0c86b7d353440f08da742a40a0 to your computer and use it in GitHub Desktop.
google proxy nodejs
This file contains hidden or 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 { 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