greenlock-cluster.js/README.md

250 行
7.8 KiB
Markdown

# Replaced: Use Greenlock Express v3
See https://git.rootprojects.org/root/greenlock-express.js
```js
"use strict";
var pkg = require("./package.json");
require("greenlock-express")
.init(function getConfig() {
// Greenlock Config
return {
package: { name: pkg.name, version: pkg.version },
maintainerEmail: pkg.author,
// put cluster on full throttle!
cloudnative: true
webscale: true
cluster: true
};
})
.serve(httpsWorker);
function httpsWorker(glx) {
// Serves on 80 and 443
// Get's SSL certificates magically!
glx.serveApp(function(req, res) {
res.end("Hello, Encrypted World!");
});
}
```
# OLD STUFF BELOW
# (Preserved for historical reference)
2018-11-30 03:05:55 +00:00
| A [Root](https://therootcompany.com) Project
2018-04-20 06:34:42 +00:00
| [greenlock (lib)](https://git.coolaj86.com/coolaj86/greenlock.js)
2018-11-30 03:05:55 +00:00
| [greenlock-cli](https://git.coolaj86.com/coolaj86/greenlock-cli.js)
2018-04-20 06:34:42 +00:00
| [greenlock-express](https://git.coolaj86.com/coolaj86/greenlock-express.js)
2017-01-25 21:53:56 +00:00
| **greenlock-cluster**
2018-04-20 06:34:42 +00:00
| [greenlock-koa](https://git.coolaj86.com/coolaj86/greenlock-koa.js)
| [greenlock-hapi](https://git.coolaj86.com/coolaj86/greenlock-hapi.js)
2016-08-16 17:39:00 +00:00
|
# greenlock-cluster
2018-04-20 06:34:42 +00:00
(previously letsencrypt-cluster)
2016-08-10 07:43:11 +00:00
2016-08-12 07:02:33 +00:00
Use automatic letsencrypt with node on multiple cores or even multiple machines.
- Take advantage of multi-core computing
- Process certificates in master
- Serve https from multiple workers
- Can work with any clustering strategy [#1](https://github.com/Daplie/letsencrypt-cluster/issues/1)
2016-08-12 07:02:33 +00:00
# Install
2016-08-12 07:02:33 +00:00
```bash
2017-01-25 21:53:56 +00:00
npm install --save greenlock-cluster@2.x
2016-08-12 07:02:33 +00:00
```
# Usage
2016-08-12 07:02:33 +00:00
In a cluster environment you have some main file that boots your app
and then conditionally loads certain code based on whether that fork
is the master or just a worker.
In such a file you might want to define some of the options that need
to be shared between both the master and the worker, like this:
`boot.js`:
2016-08-12 07:02:33 +00:00
```javascript
"use strict";
2016-08-12 07:02:33 +00:00
var cluster = require("cluster");
var path = require("path");
var os = require("os");
2016-08-12 07:02:33 +00:00
var main;
var sharedOptions = {
webrootPath: path.join(os.tmpdir(), "acme-challenge"), // /tmp/acme-challenge
// used by le-challenge-fs, the default plugin
2016-08-12 07:02:33 +00:00
renewWithin: 14 * 24 * 60 * 60 * 1000, // 10 days before expiration
2016-08-12 07:02:33 +00:00
debug: true
2016-08-12 07:02:33 +00:00
};
if (cluster.isMaster) {
main = require("./master");
} else {
main = require("./worker");
2016-08-12 07:02:33 +00:00
}
main.init(sharedOptions);
```
## Master
2016-08-12 07:02:33 +00:00
2017-01-25 21:53:56 +00:00
We think it makes the most sense to load greenlock in master.
2018-11-30 03:05:55 +00:00
This can prevent race conditions (see [node-letsencrypt#45](https://github.com/Daplie/node-letsencrypt/issues/45))
2016-08-12 07:02:33 +00:00
as only one process is writing the to file system or database at a time.
The main implementation detail here is `approveDomains(options, certs, cb)` for new domain certificates
and potentially `agreeToTerms(opts, cb)` for new accounts.
2017-01-25 21:53:56 +00:00
The master takes **the same arguments** as `node-greenlock` (`challenge`, `store`, etc),
2016-08-12 07:13:33 +00:00
plus a few extra (`approveDomains`... okay, just one extra):
2016-08-12 07:02:33 +00:00
`master.js`:
2016-08-12 07:06:32 +00:00
```javascript
2016-08-12 07:02:33 +00:00
'use strict';
var cluster = require('cluster');
module.exports.init = function (sharedOpts) {
var cores = require('os').cpus();
2017-01-25 21:53:56 +00:00
var leMaster = require('greenlock-cluster/master').create({
2016-08-12 07:02:33 +00:00
debug: sharedOpts.debug
2018-11-30 03:05:55 +00:00
2018-04-20 06:33:20 +00:00
// You MUST change this to 'https://acme-v02.api.letsencrypt.org/directory' in production
server: 'https://acme-staging-v02.api.letsencrypt.org/directory'
, version: 'draft-11' // Let's Encrypt v2
2016-08-12 07:02:33 +00:00
, renewWithin: sharedOpts.renewWithin
, webrootPath: sharedOpts.webrootPath
, approveDomains: function (masterOptions, certs, cb) {
// Do any work that must be done by master to approve this domain
// (in this example, it's assumed to be done by the worker)
var results = { domain: masterOptions.domain // required
, options: masterOptions // domains, email, agreeTos
, certs: certs }; // altnames, privkey, cert
cb(null, results);
}
});
cores.forEach(function () {
var worker = cluster.fork();
2016-08-12 07:48:21 +00:00
leMaster.addWorker(worker);
2016-08-12 07:02:33 +00:00
});
};
```
2016-08-12 07:48:21 +00:00
### API
2017-01-25 21:53:56 +00:00
All options are passed directly to `node-greenlock`
(in other works, `leMaster` is a `greenlock` instance),
but a few are only actually used by `greenlock-cluster`.
2016-08-12 07:48:21 +00:00
- `leOptions.approveDomains(options, certs, cb)` is special for `greenlock-cluster`, but will probably be included in `node-greenlock` in the future (no API change).
2016-08-12 07:48:21 +00:00
- `leMaster.addWorker(worker)` is added by `greenlock-cluster` and **must be called** for each new worker.
2016-08-12 07:48:21 +00:00
## Worker
2016-08-12 07:02:33 +00:00
The worker takes _similar_ arguments to `node-greenlock`,
2016-08-12 07:02:33 +00:00
but only ones that are useful for determining certificate
renewal and for `le.challenge.get`.
If you want to a non-default `le.challenge`
2016-08-12 07:02:33 +00:00
`worker.js`:
```javascript
"use strict";
module.exports.init = function(sharedOpts) {
var leWorker = require("greenlock-cluster/worker").create({
debug: sharedOpts.debug,
renewWithin: sharedOpts.renewWithin,
webrootPath: sharedOpts.webrootPath,
// , challenge: require('le-challenge-fs').create({ webrootPath: '...', ... })
approveDomains: function(workerOptions, certs, cb) {
// opts = { domains, email, agreeTos, tosUrl }
// certs = { subject, altnames, expiresAt, issuedAt }
var results = {
domain: workerOptions.domains[0],
options: {
domains: workerOptions.domains
},
certs: certs
};
if (certs) {
// modify opts.domains to match the original request
// email is not necessary, because the account already exists
// this will only fail if the account has become corrupt
results.options.domains = certs.altnames;
cb(null, results);
return;
}
// This is where one would check one's application-specific database:
// 1. Lookup the domain to see which email it belongs to
// 2. Assign a default email if it isn't in the system
// 3. If the email has no le account, `agreeToTerms` will fire unless `agreeTos` is preset
results.options.email = "john.doe@example.com";
results.options.agreeTos = true; // causes agreeToTerms to be skipped
cb(null, results);
2016-08-12 07:02:33 +00:00
}
});
2016-08-12 07:02:33 +00:00
function app(req, res) {
res.end("Hello, World!");
}
2016-08-12 07:02:33 +00:00
var redirectHttps = require("redirect-https")();
var plainServer = require("http").createServer(leWorker.middleware(redirectHttps));
plainServer.listen(80);
2016-08-12 07:02:33 +00:00
var server = require("https").createServer(leWorker.httpsOptions, leWorker.middleware(app));
server.listen(443);
2016-08-12 07:02:33 +00:00
};
```
2016-08-12 07:56:19 +00:00
### API
2017-01-25 21:53:56 +00:00
`node-greenlock` is **not used** directly by the worker,
2016-08-12 07:56:19 +00:00
but certain options are shared because certain logic is duplicated.
- `leOptions.renewWithin` is shared so that the worker knows how earlier to request a new cert
- `leOptions.renewBy` is passed to `le-sni-auto` so that it staggers renewals between `renewWithin` (latest) and `renewBy` (earlier)
- `leWorker.middleware(nextApp)` uses `greenlock/middleware` for GET-ing `http-01`, hence `sharedOptions.webrootPath`
- `leWorker.httpsOptions` has a default localhost certificate and the `SNICallback`.
2016-08-12 07:56:19 +00:00
There are a few options that aren't shown in these examples, so if you need to change something
that isn't shown here, look at the code (it's not that much) or open an issue.
## Message Passing
2016-08-12 07:02:33 +00:00
The master and workers will communicate through `process.on('message', fn)`, `process.send({})`,
`worker.on('message', fn)`and `worker.send({})`.
All messages have a `type` property which is a string and begins with `LE_`.
All other messages are ignored.