简体   繁体   中英

How to make gun accept a self-signed certificate?

gun 0.8.8, Node.js-to-Node.js, Node.js-to-browser

I see the following error in browser console:

VM103:161 WebSocket connection to 'wss://127.0.0.1:8080/gun' failed: Error in connection establishment: net::ERR_INSECURE_RESPONSE
VM103:161 WebSocket connection to 'wss://10.42.0.56:8080/gun' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

And there are no messages on Node.js side.

Sorce code of my server:

const Hapi = require('hapi');
const Gun = require('gun');
const pem = require('pem');

pem.createCertificate({ days: 1, selfSigned: true }, function (err, keys) {
  if (err) {
    throw err
  }
  const server = new Hapi.Server;

  var tls = {
    key: keys.serviceKey,
    cert: keys.certificate
  };

  server.connection({
    port: 8080,
    tls
  });

  server.connections.forEach(c => Gun({ web: c.listener, file: 'data.json' }));

  server.route({
    method: 'GET',
    path: '/',
    handler: function (request, reply) {
      reply('Server works!');
    }
  });

  server.start();
})

In order to make gun work with a self-signed certificate you need two things:

  1. Lunch browser ignoring the certificate errors. For example, Chrome

    google-chrome --ignore-certificate-errors

  2. Put the following process option in Node.js code

    process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

    or add the environment variable

    export NODE_TLS_REJECT_UNAUTHORIZED=0

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