(sentry) use tunnel to circumvent tracker blocking

see: https://docs.sentry.io/platforms/javascript/troubleshooting/, Dealing with Ad-Blockers
code inspired from: https://github.com/BLOCKMATERIAL/sentry-tunnel-using-node/blob/main/routes/tunnel.js
This commit is contained in:
Valentyne Stigloher 2024-08-27 10:41:05 +02:00
parent d3b54484af
commit 9ce94f8383
3 changed files with 39 additions and 0 deletions

View File

@ -242,6 +242,9 @@ const nuxtConfig: NuxtConfig = {
return event;
},
},
clientConfig: {
tunnel: '/api/sentry/tunnel',
},
},
publicRuntimeConfig: {
...config,

View File

@ -42,6 +42,7 @@ import calendarRoute from './routes/calendar.ts';
import translationsRoute from './routes/translations.ts';
import subscriptionRoute from './routes/subscription.ts';
import discord from './routes/discord.ts';
import sentryRoute from './routes/sentry.ts';
const MemoryStore = memorystore(session);
@ -60,6 +61,10 @@ app.use(express.json({
},
}));
app.use(express.urlencoded({ extended: true }));
app.use(express.raw({
limit: '10 MB',
type: 'text/plain',
}));
app.use(cookieParser());
app.use(session({
secret: process.env.SECRET!,
@ -179,6 +184,7 @@ app.use(calendarRoute);
app.use(translationsRoute);
app.use(subscriptionRoute);
app.use(discord);
app.use(sentryRoute);
app.use(Sentry.Handlers.errorHandler());

30
server/routes/sentry.ts Normal file
View File

@ -0,0 +1,30 @@
import { Router } from 'express';
import { handleErrorAsync } from '~/src/helpers.ts';
const router = Router();
router.post('/sentry/tunnel', handleErrorAsync(async (req, res) => {
const envelope = req.body.toString('utf-8');
const piece = envelope.slice(0, envelope.indexOf('\n'));
const header = JSON.parse(piece);
if (header.dsn !== process.env.SENTRY_DSN) {
return res.status(400).send({ message: `Invalid Sentry DSN: ${header.dsn}` });
}
const dsn = new URL(header.dsn);
const url = `https://${dsn.hostname}/api/${dsn.pathname.substring(1)}/envelope/`;
const response = await fetch(url, {
method: 'POST',
body: envelope,
headers: {
'Content-Type': 'application/x-sentry-envelope',
},
});
return res.status(response.status).send(response.body);
}));
export default router;