Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

auto-sni

DylanPiercey228MIT2.3.2TypeScript support: definitely-typed

Automatically generate tls credentials using letsencrypt.

auto, auto-sni, http, https, lets-encrypt, letsencrypt, letsencrypt-auto, renew, secure, sni, ssl, tls, web

readme

js-standard-style Join the chat at https://gitter.im/DylanPiercey/auto-sni

Auto SNI

SSL Certificates using SNI with almost zero configuration for free with https://letsencrypt.org!

If you have any questions, throw them up on gitter.

Installation

Npm

npm install auto-sni

Features

  • Fetch SSL certificates from letsencrypt.
  • Automatically renew certificates.
  • Forward all incoming http requests to https.

Example

var createServer = require("auto-sni");

var server = createServer({
  email: ..., // Emailed when certificates expire.
  agreeTos: true, // Required for letsencrypt.
  debug: true, // Add console messages and uses staging LetsEncrypt server. (Disable in production)
  domains: ["mysite.com", ["test.com", "www.test.com"]], // List of accepted domain names. (You can use nested arrays to register bundles with LE).
  dir: "~/letsencrypt/etc", // Directory for storing certificates. Defaults to "~/letsencrypt/etc" if not present.
  ports: {
    http: 80, // Optionally override the default http port.
    https: 443 // // Optionally override the default https port.
  }
});

// Server is a "https.createServer" instance.
server.once("listening", ()=> {
  console.log("We are ready to go.");
});

Usage with express.

var createServer = require("auto-sni");
var express      = require("express");
var app          = express();

app.get("/test", ...);

createServer({ email: ..., domains: ..., agreeTos: true }, app);

Usage with koa.

var createServer = require("auto-sni");
var koa          = require("koa");
var app          = koa();

app.use(...);

createServer({ email: ..., domains: ..., agreeTos: true }, app.callback());

Usage with rill.

var createServer = require("auto-sni");
var rill         = require("rill");
var app          = rill();

app.get("/test", ...);

createServer({ email: ..., domains: ..., agreeTos: true }, app.handler());

Usage with hapi.

// Untested (Let me know in gitter if this doesn't work.)
var createServer = require("auto-sni");
var hapi         = require("hapi");
var server       = new hapi.Server();
var secureServer = createServer({ email: ..., domains: ..., agreeTos: true });

server.connection({ listener: secureServer, autoListen: false, tls: true });

Usage with restify.

// Untested (Let me know in gitter if this doesn't work.)
var createServer = require("auto-sni");
var restify      = require("restify");
var app          = restify.createServer({ name: 'myapp', version: '1.0.0' });

app.get("/test", ...);

createServer({ email: ..., domains: ..., agreeTos: true }, app.server);

Dynamic Domains

You can also specify an async function to approve domains like so:

createServer({
  ...,
  domains: (options, cert, cb) => {
    setTimeout(() => cb({ options, cert }), 1000)
  }
})

Root Access

AutoSNI requires access to low level ports 80 (http) and 443 (https) to operate by default. These ports are typically restricted by the operating system.

In production (on linux servers) you can use the following command to give Node access to these ports.

sudo setcap cap_net_bind_service=+ep `readlink -f \`which node\``

For development it's best to set the "ports" option manually to something like:

{
  ports: {
    http: 3001,
    https: 3002
  }
}

// Access server on localhost:3002

Rate Limits

Currently LetsEncrypt imposes some rate limits on certificate creation. Click here for the current rate limits.

Contributions

Please use npm test for tests and feel free to create a PR!

changelog

2.3.2 2017-10-05

  • Add dir option to readme example.

2.3.0, 2.3.1 2017-07-22

  • Add ability to approve domains with a function.

2.2.1 2017-06-11

  • Update readme regarding root access to privileged ports.

2.2.0 2017-06-05

  • Flatten domains option for compatibility with @1.x

2.1.1 2017-04-13

  • Internal implementation switched to use greenlock-express.
  • No longer falls back to self signed when unable to communicate with letsencrypt.
  • Removed ability to disable https redirection (should just use vanilla http server).
  • Simplified api.

1.5.2 2016-09-21

  • Fix travis ci.

1.5.1 2016-08-26

  • Update letsencrypt rate limit link in docs.
  • Update bluebird.

1.5.0 2016-08-26

1.3.0 2016-08-01

  • Add ability to change redirect code when forceSSL is enabled.

1.2.1 2016-05-16

  • Fix issue with self signed fallback certificate.

1.1.3 2016-05-16

  • Update dependencies (fixes issue with node 6).

1.1.1 2016-04-23 (Will reset renew certificates)

  • Clears certificate when switching between debug modes.
  • Clears certificate when bundle changes (previously bundles were tied to the first domain).
  • Attempt to fix some issues with restify.

1.0.0 / 2016-04-17

  • Remove bundle option.
  • No longer fuzzy match domains with path-to-regexp.
  • Allow for nested arrays in domains for bundled certificates.