Skip to content

Commit

Permalink
maken going (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
dutscher authored Jun 6, 2024
1 parent 3bf142a commit ba6cdc1
Show file tree
Hide file tree
Showing 6 changed files with 3,131 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"plugins": ["prettier-plugin-svelte", "prettier-plugin-tailwindcss"],
"overrides": [
{
"files": ["*.html", "*.svelte"],
"files": ["*.html", "*.svelte", "*.js"],
"options": {
"parser": "svelte",
"singleQuote": true,
Expand Down
8 changes: 8 additions & 0 deletions README-ssh.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ netstat -tulpn
# wenn du ihn wieder deaktiviert moechtest dann:

systemctl disable nginx.service

## Validieren config
nginx -t

## Restart
systemctl restart nginx
systemctl reload nginx

140 changes: 116 additions & 24 deletions cors-proxy-server/index.js
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');
});
Loading

0 comments on commit ba6cdc1

Please sign in to comment.