2018-05-10 19:35:48 +00:00
|
|
|
# Greenlock™ for hapi
|
2016-12-30 09:22:46 +00:00
|
|
|
|
2018-05-10 19:35:48 +00:00
|
|
|
An Automated HTTPS ACME client (Let's Encrypt v2) for hapi
|
2016-11-02 00:30:09 +00:00
|
|
|
|
2018-05-10 19:35:48 +00:00
|
|
|
| Sponsered by [ppl](https://ppl.family)
|
|
|
|
| Greenlock™ is for
|
|
|
|
[Browsers](https://git.coolaj86.com/coolaj86/greenlock.html),
|
|
|
|
[Node.js](https://git.coolaj86.com/coolaj86/greenlock.js),
|
|
|
|
[Commandline](https://git.coolaj86.com/coolaj86/greenlock-cli.js),
|
|
|
|
[Express.js](https://git.coolaj86.com/coolaj86/greenlock-express.js),
|
|
|
|
[Node.js Cluster](https://git.coolaj86.com/coolaj86/greenlock-cluster.js),
|
|
|
|
**hapi**,
|
|
|
|
[Koa](https://git.coolaj86.com/coolaj86/greenlock-koa.js),
|
|
|
|
and [rill](https://git.coolaj86.com/coolaj86/greenlock-rill.js) |
|
2016-04-22 18:20:43 +00:00
|
|
|
|
2018-05-10 19:35:48 +00:00
|
|
|
Features
|
|
|
|
========
|
2016-04-19 04:29:03 +00:00
|
|
|
|
2018-05-10 19:35:48 +00:00
|
|
|
* [x] Automatic Registration via SNI (`httpsOptions.SNICallback`)
|
|
|
|
* [x] Secure domain approval callback
|
|
|
|
* [x] Automatic renewal between 10 and 14 days before expiration
|
|
|
|
* [x] Virtual Hosting (vhost) with Multiple Domains & SAN
|
|
|
|
* [x] plugins for AWS, redis, etc
|
|
|
|
* [x] and [more](https://git.coolaj86.com/coolaj86/greenlock-express.js)
|
2016-04-19 04:29:03 +00:00
|
|
|
|
2018-05-10 19:35:48 +00:00
|
|
|
This module is just an alias for greenlock-express.js,
|
|
|
|
which works with any middleware system.
|
2016-04-19 04:29:03 +00:00
|
|
|
|
2018-05-10 19:35:48 +00:00
|
|
|
Install
|
|
|
|
=======
|
2016-04-19 04:29:03 +00:00
|
|
|
|
|
|
|
```
|
2018-05-10 19:35:48 +00:00
|
|
|
npm install --save greenlock-hapi@2.x
|
2016-04-19 04:29:03 +00:00
|
|
|
```
|
|
|
|
|
2018-05-10 19:35:48 +00:00
|
|
|
QuickStart
|
|
|
|
==========
|
2016-04-19 04:29:03 +00:00
|
|
|
|
|
|
|
```javascript
|
|
|
|
'use strict';
|
|
|
|
|
2018-05-10 19:35:48 +00:00
|
|
|
//////////////////////
|
|
|
|
// Greenlock Setup //
|
|
|
|
//////////////////////
|
|
|
|
|
|
|
|
var greenlock = require('greenlock-hapi').create({
|
|
|
|
version: 'draft-11' // Let's Encrypt v2
|
2018-04-20 06:30:13 +00:00
|
|
|
// You MUST change this to 'https://acme-v02.api.letsencrypt.org/directory' in production
|
2018-05-10 19:35:48 +00:00
|
|
|
, server: 'https://acme-staging-v02.api.letsencrypt.org/directory'
|
2016-04-19 04:29:03 +00:00
|
|
|
|
2018-05-10 19:35:48 +00:00
|
|
|
, email: 'jon@example.com'
|
|
|
|
, agreeTos: true
|
|
|
|
, approveDomains: [ 'example.com' ]
|
2016-04-19 04:29:03 +00:00
|
|
|
|
2018-05-10 19:35:48 +00:00
|
|
|
// Join the community to get notified of important updates
|
|
|
|
// and help make greenlock better
|
|
|
|
, communityMember: true
|
2016-04-19 04:29:03 +00:00
|
|
|
|
2018-05-10 19:35:48 +00:00
|
|
|
, configDir: require('os').homedir() + '/acme/etc'
|
2016-04-19 04:29:03 +00:00
|
|
|
|
2018-05-10 19:35:48 +00:00
|
|
|
//, debug: true
|
|
|
|
});
|
2016-04-19 04:29:03 +00:00
|
|
|
|
|
|
|
|
2018-05-10 19:35:48 +00:00
|
|
|
///////////////////
|
|
|
|
// Just add hapi //
|
|
|
|
///////////////////
|
|
|
|
|
2016-04-19 04:29:03 +00:00
|
|
|
var hapi = require('hapi');
|
2018-05-10 19:35:48 +00:00
|
|
|
var https = require('https');
|
2016-04-19 04:29:03 +00:00
|
|
|
var server = new hapi.Server();
|
2018-05-10 19:35:48 +00:00
|
|
|
var acmeResponder = greenlock.middleware();
|
|
|
|
var httpsServer = https.createServer(greenlock.httpsOptions).listen(443);
|
2016-04-19 04:29:03 +00:00
|
|
|
|
|
|
|
server.connection({ listener: httpsServer, autoListen: false, tls: true });
|
|
|
|
|
|
|
|
server.route({
|
|
|
|
method: 'GET'
|
|
|
|
, path: '/.well-known/acme-challenge'
|
|
|
|
, handler: function (request, reply) {
|
|
|
|
var req = request.raw.req;
|
|
|
|
var res = request.raw.res;
|
|
|
|
|
|
|
|
reply.close(false);
|
|
|
|
acmeResponder(req, res);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
server.route({
|
|
|
|
method: 'GET'
|
|
|
|
, path: '/'
|
|
|
|
, handler: function (request, reply) {
|
|
|
|
reply("Hello, I'm so Hapi!");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2018-05-10 19:35:48 +00:00
|
|
|
//
|
|
|
|
// http redirect to https
|
|
|
|
//
|
2016-04-19 04:29:03 +00:00
|
|
|
var http = require('http');
|
2016-08-16 20:41:18 +00:00
|
|
|
var redirectHttps = require('redirect-https')();
|
2016-04-19 04:29:03 +00:00
|
|
|
|
2018-05-10 19:35:48 +00:00
|
|
|
http.createServer(greenlock.middleware(redirectHttps)).listen(80, function () {
|
|
|
|
console.log('Listening on port 80 to handle ACME http-01 challenge and redirect to https');
|
2016-08-16 20:41:18 +00:00
|
|
|
});
|
2016-04-19 04:29:03 +00:00
|
|
|
```
|
2018-05-10 19:35:48 +00:00
|
|
|
|
|
|
|
Usage & Troubleshooting
|
|
|
|
============================
|
|
|
|
|
|
|
|
See <https://git.coolaj86.com/coolaj86/greenlock-express.js>
|
|
|
|
|
|
|
|
Handling a dynamic list of domains
|
|
|
|
========================
|
|
|
|
|
|
|
|
In the oversimplified exapmple above we handle a static list of domains.
|
|
|
|
If you add domains programmatically you'll want to use the `approveDomains`
|
|
|
|
callback.
|
|
|
|
|
|
|
|
**SECURITY**: Be careful with this.
|
|
|
|
If you don't check that the domains being requested are the domains you
|
|
|
|
allow an attacker can make you hit your rate limit for failed verification
|
|
|
|
attempts.
|
|
|
|
|
|
|
|
We have a
|
|
|
|
[vhost example](https://git.coolaj86.com/coolaj86/greenlock-express.js/src/branch/master/examples/vhost.js)
|
|
|
|
that allows any domain for which there is a folder on the filesystem in a specific location.
|
|
|
|
|
|
|
|
See that example for an idea of how this is done.
|