Skip to content

Instantly share code, notes, and snippets.

@shadaxv

shadaxv/main.js Secret

Created May 22, 2024 10:12
Show Gist options
  • Save shadaxv/742cd824de5a8dfc7893c40432ee4ce1 to your computer and use it in GitHub Desktop.
Save shadaxv/742cd824de5a8dfc7893c40432ee4ce1 to your computer and use it in GitHub Desktop.
HTTP Header Non-ASCII Character Limitation
// Modules to control application life and create native browser window
const { app, BrowserWindow, protocol, net, session } = require("electron");
const http = require("node:http");
function createWindow() {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
x: 0,
y: 0,
});
mainWindow.loadURL("http://localhost:3000/app");
const mainWindow2 = new BrowserWindow({
width: 800,
height: 600,
x: 800,
y: 0,
});
mainWindow2.loadURL("http://localhost:3000/sanitized");
mainWindow2.webContents.setUserAgent(`${mainWindow2.webContents.getUserAgent()}✓`);
const mainWindow3 = new BrowserWindow({
width: 800,
height: 600,
x: 1600,
y: 0,
});
mainWindow3.loadURL("http://localhost:3000/sanitized");
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
protocol.handle("http", (request) => {
return net.fetch(request, { bypassCustomProtocolHandlers: true });
});
session.defaultSession.webRequest.onHeadersReceived(
{ urls: ["*://*/*"] },
(details, callback) => {
const responseHeaders = { ...details.responseHeaders };
responseHeaders["X-Custom-Header"] = ["✓"]; // Adding a non-ASCII character in the header
if (details.url === "http://localhost:3000/sanitized") {
// Electron's net.fetch does not support non-ASCII characters in the headers
for (const [key, value] of Object.entries(responseHeaders)) {
// normalize and convert all non-ASCII characters to ASCII using Buffer
responseHeaders[key] = value.map((entry) => {
const normalized = entry.normalize("NFKD");
const buffer = Buffer.from(normalized, "utf8");
return buffer.toString("ascii");
});
}
}
callback({ responseHeaders });
}
);
const server = http.createServer((_req, res) => {
res.writeHead(200, {
"Content-Type": "text/plain",
});
res.end("Hello World");
});
server.listen(3000, () => {
console.log("Server is listening on http://localhost:3000");
});
createWindow();
app.on("activate", function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on("window-all-closed", function () {
if (process.platform !== "darwin") app.quit();
});
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
{
"name": "numberless-berry-multiply-6vf04",
"productName": "numberless-berry-multiply-6vf04",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "Shadax",
"scripts": {
"start": "electron ."
},
"dependencies": {},
"devDependencies": {
"electron": "30.0.6"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment