如何让枪支接受自签名证书?

How to make gun accept a self-signed certificate?

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

我在浏览器控制台中看到以下错误:

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

并且 Node.js 端没有消息。

我的服务器源代码:

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();
})

为了使 gun 使用自签名证书,您需要做两件事:

  1. Lunch 浏览器忽略证书错误。例如,Chrome

    google-chrome --ignore-certificate-errors

  2. 在Node.js代码中加入以下处理选项

    process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

    或者添加环境变量

    export NODE_TLS_REJECT_UNAUTHORIZED=0