Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Self-Signed Certificate for Node Web Server inside Electron

I have an Electron app with a node web server where our website can POST and GET information from the Electron app. The issue is that the website is SSL enabled (HTTPS) but the Electron app is HTTP. Of course, this causes a mixed content exception.

Current Electron Web Server example

http.createServer(function(req,res){
    var pathname=url.parse(req.url).pathname;
    var query = url.parse(req.url).query;
    //enable CORS
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Request-Method', '*');
    res.setHeader('Access-Control-Allow-Headers', '*');
    switch(pathname){
        case '/post':
            createPrintWindow(query);
            res.end('{"status":"200 OK"}');
        break;
        default:
            res.end('{"status":"running","currentVersion":"'+app.getVersion()+'","isUpdateAvailable":"false"}');
        break;
    }
}).listen(3000);

I see that I can enable SSL pretty easily:

var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');

// This line is from the Node.js HTTPS documentation.
var options = {
  key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
  cert: fs.readFileSync('test/fixtures/keys/agent2-cert.cert')
};

// Create a service (the app object is just a callback).
var app = express();

// Create an HTTP service.
http.createServer(app).listen(80);
// Create an HTTPS service identical to the HTTP service.
https.createServer(options, app).listen(443);

How to create an HTTPS server in Node.js?

My problem is, I'm not sure how to generate the actual certificates for both win32 and 64x during the installation process (I'm using electron-buider). I know I can do openSSL, but most machines with not have openSSL installed.

I've even found this, but I don't see any documentation or usage examples of it. https://github.com/electron-userland/electron-builder/blob/master/packages/electron-builder/src/cli/create-self-signed-cert.ts

like image 478
Cory Fail Avatar asked Jun 23 '26 06:06

Cory Fail


1 Answers

It seems like you don't need to generate the .cert and .pem files from each computer that installs it. You can simply just create them once, then package the certificates together in the Electron app.

However, you may want to note that you'll need to visit the page once https://localhost:3000 and accept/ignore and warnings the browser gives you beforehand or else it will not work correctly.

like image 152
Cory Fail Avatar answered Jun 25 '26 21:06

Cory Fail



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!