-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
3,131 additions
and
37 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,117 @@ | ||
// Source from: https://www.npmjs.com/package/cors-anywhere | ||
const fs = require("node:fs"); | ||
const cors_proxy = require("cors-anywhere"); | ||
|
||
// const options = { | ||
// key: fs.readFileSync("/etc/ssl/neofonie/privkey.pem"), | ||
// cert: fs.readFileSync("/etc/ssl/neofonie/fullchain.pem"), | ||
// }; | ||
|
||
// Listen on a specific host via the HOST environment variable | ||
const host = process.env.HOST || "0.0.0.0"; | ||
|
||
// Listen on a specific port via the PORT environment variable | ||
const port = process.env.PORT || 8080; | ||
|
||
cors_proxy | ||
.createServer({ | ||
//...options, | ||
originWhitelist: [], // Allow all origins | ||
requireHeader: ["origin", "x-requested-with"], | ||
removeHeaders: ["cookie", "cookie2"], | ||
}) | ||
.listen(port, host, function () { | ||
console.log("Running CORS Anywhere on " + host + ":" + port); | ||
import puppeteer from 'puppeteer-core'; | ||
import express from 'express'; | ||
import url from 'url'; | ||
|
||
const app = express(); | ||
const port = 1337; | ||
|
||
const handleBrowser = async () => { | ||
return await puppeteer.launch({ | ||
//executablePath: './node_modules/puppeteer/.local-chromium/linux-800071/chrome-linux/chrome', | ||
//executablePath: './node_modules/chromium/lib/chromium/chrome-linux/chrome', | ||
//executablePath: 'google-chrome-stable', | ||
//executablePath: 'google-chrome', | ||
// unix: snap install chromium | ||
executablePath: "/snap/bin/chromium", | ||
// windows | ||
//executablePath: 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe', | ||
args: ["--no-sandbox", "--disable-setuid-sandbox"] | ||
}); | ||
}; | ||
|
||
const exec = async (url, default_, callback) => { | ||
let browser; | ||
let page; | ||
let data = default_; | ||
|
||
console.log('exec start', { url }); | ||
|
||
//console.log("before handleBrowser"); | ||
try { | ||
browser = await handleBrowser(); | ||
} catch (e) { | ||
console.log('browser error', e); | ||
data.error = 'browser cant init'; | ||
data.stack = e; | ||
} | ||
|
||
if (browser) { | ||
//console.log("before browser.newPage"); | ||
try { | ||
page = await browser.newPage(); | ||
} catch (e) { | ||
console.log('newPage error', e); | ||
data.error = 'page cant init'; | ||
data.stack = e; | ||
} | ||
|
||
if (page) { | ||
//console.log("before page.goto"); | ||
try { | ||
await page.goto(url, { | ||
waitUntil: 'networkidle2', | ||
timeout: 60000 | ||
}); | ||
} catch (e) { | ||
data.error = 'page cant goto'; | ||
data.stack = e; | ||
} | ||
//console.log("before callback"); | ||
|
||
try { | ||
data = await callback(page); | ||
} catch (e) { | ||
data.error = 'callback cant exec'; | ||
data.stack = e; | ||
} | ||
|
||
// console.log({ data }); | ||
|
||
console.log('exec done', { url }); | ||
} | ||
await browser.close(); | ||
} | ||
return data; | ||
}; | ||
|
||
app.get('/cors', async function(req, res) { | ||
res.writeHead(200, { 'Content-Type': 'application/json' }); | ||
|
||
const q = url.parse(req.url, true).query; | ||
let _url = q.url.replace('/cors?url=', ''); | ||
let data = {}; | ||
|
||
try { | ||
data = await exec( | ||
_url, | ||
{ | ||
title: 'not found', | ||
url: req.url | ||
html: '', | ||
}, | ||
async (page) => { | ||
return page.evaluate(() => { | ||
const url = location.href; | ||
let title = document.querySelector("title").innerText; | ||
let html = document.documentElement.outerHTML; | ||
|
||
return { | ||
title, | ||
url, | ||
html, | ||
}; | ||
}); | ||
} | ||
); | ||
} catch (e) { | ||
data = { error: 'not possible', stack: e }; | ||
} | ||
res.end(JSON.stringify(data)); | ||
}); | ||
|
||
app.listen(port, () => { | ||
console.log('\n\n\n\nProxy gestartet --port ' + port); | ||
console.log('http://localhost:1337/cors?url=https://www.neofonie.de'); | ||
console.log('http://a11y.neofonie.de:1337/cors?url=https://www.neofonie.de'); | ||
console.log('https://a11y.neofonie.de/cors?url=https://www.neofonie.de'); | ||
}); |
Oops, something went wrong.