简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM