Moved to dependency free nodejs mobile request proxy
This commit is contained in:
@@ -1,5 +1,109 @@
|
||||
const corsAnywhere = require('cors-anywhere');
|
||||
const http = require('http');
|
||||
const https = require('https');
|
||||
|
||||
corsAnywhere.createServer({
|
||||
originWhitelist: [],
|
||||
}).listen(3000, 'localhost');
|
||||
const HOST = 'localhost';
|
||||
const PORT = 3000;
|
||||
const MAX_REDIRECTS = 5;
|
||||
const ORIGIN = `https://${HOST}`;
|
||||
|
||||
function setCorsHeaders(res) {
|
||||
res.setHeader('Access-Control-Allow-Origin', ORIGIN);
|
||||
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
|
||||
res.setHeader('Access-Control-Allow-Headers', '*');
|
||||
res.setHeader('Access-Control-Allow-Credentials', 'true');
|
||||
}
|
||||
|
||||
function fetchWithRedirects(targetUrl, options, redirectCount = 0) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const httpClient = targetUrl.protocol.startsWith('https') ? https : http;
|
||||
|
||||
const req = httpClient.request(targetUrl, options, (res) => {
|
||||
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
|
||||
if (redirectCount >= MAX_REDIRECTS) {
|
||||
return reject(new Error('Too many redirects'));
|
||||
}
|
||||
|
||||
let redirectHeaderLocation = res.headers.location;
|
||||
|
||||
// Handle relative URLs
|
||||
if (redirectHeaderLocation.startsWith('/')) {
|
||||
redirectHeaderLocation = targetUrl.host + redirectHeaderLocation;
|
||||
}
|
||||
|
||||
// Add http:// if missing from the Location header
|
||||
if (!redirectHeaderLocation.startsWith('http')) {
|
||||
redirectHeaderLocation = 'http://' + redirectHeaderLocation;
|
||||
}
|
||||
|
||||
try {
|
||||
// Attempt to create the redirect URL
|
||||
let redirectUrl = new URL(redirectHeaderLocation);
|
||||
return resolve(fetchWithRedirects(redirectUrl, options, redirectCount + 1));
|
||||
} catch (error) {
|
||||
// Catch invalid URL errors
|
||||
return reject(new Error(`Invalid URL in redirect: ${error.message}`));
|
||||
}
|
||||
}
|
||||
resolve(res); // Resolve with the final response if not a redirect
|
||||
});
|
||||
|
||||
req.on('error', reject);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
const server = http.createServer(async (req, res) => {
|
||||
setCorsHeaders(res);
|
||||
|
||||
if (req.method === 'OPTIONS') {
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
return res.end();
|
||||
}
|
||||
|
||||
if (!req.url || req.url === '/') {
|
||||
res.writeHead(400, { 'Content-Type': 'text/plain' });
|
||||
return res.end('No URL provided to fetch.');
|
||||
}
|
||||
|
||||
const targetUrl = req.url.slice(1); // Remove leading '/'
|
||||
let parsedTarget;
|
||||
try {
|
||||
if (!targetUrl.startsWith('http')) {
|
||||
targetUrl = 'http://' + targetUrl;
|
||||
}
|
||||
parsedTarget = new URL(targetUrl);
|
||||
} catch (error) {
|
||||
res.writeHead(400, { 'Content-Type': 'text/plain' });
|
||||
return res.end(`Invalid URL: ${error.message}`);
|
||||
}
|
||||
|
||||
const options = {
|
||||
method: req.method,
|
||||
headers: {
|
||||
...req.headers,
|
||||
host: parsedTarget.hostname, // Ensure correct host header
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
const proxyRes = await fetchWithRedirects(parsedTarget, options);
|
||||
res.writeHead(proxyRes.statusCode, {
|
||||
...proxyRes.headers,
|
||||
'Access-Control-Allow-Origin': ORIGIN,
|
||||
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
|
||||
'Access-Control-Allow-Headers': '*',
|
||||
'Access-Control-Allow-Credentials': 'true',
|
||||
});
|
||||
|
||||
proxyRes.pipe(res); // Pipe response data back to the client
|
||||
} catch (error) {
|
||||
console.error('Proxy error:', error);
|
||||
res.writeHead(500, { 'Content-Type': 'text/plain' });
|
||||
res.end(`Error: ${error.message}`);
|
||||
}
|
||||
});
|
||||
|
||||
server.listen(PORT, () => {
|
||||
console.log(`Server is running on http://${HOST}:${PORT}`);
|
||||
});
|
||||
|
||||
+1
-43
@@ -6,49 +6,7 @@
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "cors-patch",
|
||||
"version": "0.0.1",
|
||||
"dependencies": {
|
||||
"cors-anywhere": "^0.4.4"
|
||||
}
|
||||
},
|
||||
"node_modules/cors-anywhere": {
|
||||
"version": "0.4.4",
|
||||
"resolved": "https://registry.npmjs.org/cors-anywhere/-/cors-anywhere-0.4.4.tgz",
|
||||
"integrity": "sha512-8OBFwnzMgR4mNrAeAyOLB2EruS2z7u02of2bOu7i9kKYlZG+niS7CTHLPgEXKWW2NAOJWRry9RRCaL9lJRjNqg==",
|
||||
"dependencies": {
|
||||
"http-proxy": "1.11.1",
|
||||
"proxy-from-env": "0.0.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/eventemitter3": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-1.2.0.tgz",
|
||||
"integrity": "sha512-DOFqA1MF46fmZl2xtzXR3MPCRsXqgoFqdXcrCVYM3JNnfUeHTm/fh/v/iU7gBFpwkuBmoJPAm5GuhdDfSEJMJA=="
|
||||
},
|
||||
"node_modules/http-proxy": {
|
||||
"version": "1.11.1",
|
||||
"resolved": "https://registry.npmjs.org/http-proxy/-/http-proxy-1.11.1.tgz",
|
||||
"integrity": "sha512-qz7jZarkVG3G6GMq+4VRJPSN4NkIjL4VMTNhKGd8jc25BumeJjWWvnY3A7OkCGa8W1TTxbaK3dcE0ijFalITVA==",
|
||||
"dependencies": {
|
||||
"eventemitter3": "1.x.x",
|
||||
"requires-port": "0.x.x"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/proxy-from-env": {
|
||||
"version": "0.0.1",
|
||||
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-0.0.1.tgz",
|
||||
"integrity": "sha512-B9Hnta3CATuMS0q6kt5hEezOPM+V3dgaRewkFtFoaRQYTVNsHqUvFXmndH06z3QO1ZdDnRELv5vfY6zAj/gG7A=="
|
||||
},
|
||||
"node_modules/requires-port": {
|
||||
"version": "0.0.1",
|
||||
"resolved": "https://registry.npmjs.org/requires-port/-/requires-port-0.0.1.tgz",
|
||||
"integrity": "sha512-AzPDCliPoWDSvEVYRQmpzuPhGGEnPrQz9YiOEvn+UdB9ixBpw+4IOZWtwctmpzySLZTy7ynpn47V14H4yaowtA=="
|
||||
"version": "0.0.1"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
{
|
||||
"name": "cors-patch",
|
||||
"version": "0.0.1",
|
||||
"main": "./index.js",
|
||||
"dependencies": {
|
||||
"cors-anywhere": "^0.4.4"
|
||||
}
|
||||
}
|
||||
"main": "./index.js"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user