2015-06-24 21:45:54 +00:00
|
|
|
#!/usr/bin/env node
|
2015-06-24 21:36:17 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-04-27 02:16:47 +00:00
|
|
|
var cluster = require('cluster');
|
|
|
|
|
|
|
|
if (!cluster.isMaster) {
|
|
|
|
require('../lib/worker.js');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-09 17:18:05 +00:00
|
|
|
var PromiseA = require('bluebird');
|
|
|
|
var fs = PromiseA.promisifyAll(require('fs'));
|
|
|
|
var configStorage;
|
|
|
|
function mergeSettings(orig, changes) {
|
|
|
|
Object.keys(changes).forEach(function (key) {
|
|
|
|
// TODO: use an API that can properly handle updating arrays.
|
|
|
|
if (!changes[key] || (typeof changes[key] !== 'object') || Array.isArray(changes[key])) {
|
|
|
|
orig[key] = changes[key];
|
|
|
|
}
|
|
|
|
else if (!orig[key] || typeof orig[key] !== 'object') {
|
|
|
|
orig[key] = changes[key];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mergeSettings(orig[key], changes[key]);
|
|
|
|
}
|
|
|
|
});
|
2017-04-27 02:16:47 +00:00
|
|
|
}
|
2017-06-09 17:18:05 +00:00
|
|
|
function createStorage(filename, filetype) {
|
|
|
|
var recase = require('recase').create({});
|
|
|
|
var snakeCopy = recase.snakeCopy.bind(recase);
|
|
|
|
var camelCopy = recase.camelCopy.bind(recase);
|
2017-04-27 02:16:47 +00:00
|
|
|
|
2017-06-09 17:18:05 +00:00
|
|
|
var parse, dump;
|
|
|
|
if (filetype === 'json') {
|
|
|
|
parse = JSON.parse;
|
|
|
|
dump = function (arg) { return JSON.stringify(arg, null, ' '); };
|
|
|
|
} else {
|
|
|
|
var yaml = require('js-yaml');
|
|
|
|
parse = function (text) { return yaml.safeLoad(text) || {}; };
|
|
|
|
dump = yaml.safeDump;
|
|
|
|
}
|
2016-09-13 23:08:08 +00:00
|
|
|
|
2017-06-09 17:18:05 +00:00
|
|
|
function read() {
|
|
|
|
return fs.readFileAsync(filename)
|
|
|
|
.catch(function (err) {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return PromiseA.reject(err);
|
|
|
|
})
|
|
|
|
.then(parse)
|
|
|
|
;
|
2016-09-13 23:08:08 +00:00
|
|
|
}
|
2016-10-06 22:42:38 +00:00
|
|
|
|
2017-06-09 17:18:05 +00:00
|
|
|
var result = {
|
|
|
|
read: function () {
|
|
|
|
return read().then(camelCopy);
|
2016-10-06 22:42:38 +00:00
|
|
|
}
|
2017-06-09 17:18:05 +00:00
|
|
|
, save: function (changes) {
|
|
|
|
if (!changes || typeof changes !== 'object' || Array.isArray(changes)) {
|
|
|
|
return PromiseA.reject(new Error('invalid config'));
|
2016-10-06 22:42:38 +00:00
|
|
|
}
|
2017-06-09 17:18:05 +00:00
|
|
|
changes = snakeCopy(changes);
|
|
|
|
return read()
|
|
|
|
.then(snakeCopy)
|
|
|
|
.then(function (current) {
|
|
|
|
mergeSettings(current, changes);
|
|
|
|
// TODO: validate/lint the config before we actually write it.
|
|
|
|
return dump(current);
|
|
|
|
})
|
|
|
|
.then(function (newText) {
|
|
|
|
return fs.writeFileAsync(filename, newText);
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
return result.read();
|
|
|
|
})
|
|
|
|
;
|
2016-10-06 22:42:38 +00:00
|
|
|
}
|
2017-06-09 17:18:05 +00:00
|
|
|
};
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
function checkConfigLocation(cwd, configFile) {
|
|
|
|
cwd = cwd || process.cwd();
|
|
|
|
var path = require('path');
|
|
|
|
var filename;
|
|
|
|
|
|
|
|
var prom;
|
|
|
|
if (configFile) {
|
|
|
|
filename = path.resolve(cwd, configFile);
|
2017-06-12 17:38:18 +00:00
|
|
|
prom = fs.readFileAsync(filename)
|
|
|
|
.catch(function (err) {
|
|
|
|
if (err.code !== 'ENOENT') {
|
|
|
|
return PromiseA.reject(err);
|
|
|
|
}
|
|
|
|
if (path.extname(filename) === '.json') {
|
|
|
|
return '{}';
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
})
|
|
|
|
;
|
2017-06-09 17:18:05 +00:00
|
|
|
} else {
|
|
|
|
prom = PromiseA.reject('blah')
|
|
|
|
.catch(function () {
|
|
|
|
filename = path.resolve(cwd, 'goldilocks.yml');
|
|
|
|
return fs.readFileAsync(filename);
|
|
|
|
})
|
|
|
|
.catch(function () {
|
|
|
|
filename = path.resolve(cwd, 'goldilocks.json');
|
|
|
|
return fs.readFileAsync(filename);
|
|
|
|
})
|
|
|
|
.catch(function () {
|
|
|
|
filename = path.resolve(cwd, 'etc/goldilocks/goldilocks.yml');
|
|
|
|
return fs.readFileAsync(filename);
|
|
|
|
})
|
|
|
|
.catch(function () {
|
|
|
|
filename = '/etc/goldilocks/goldilocks.yml';
|
|
|
|
return fs.readFileAsync(filename);
|
|
|
|
})
|
|
|
|
.catch(function () {
|
|
|
|
filename = path.resolve(cwd, 'goldilocks.yml');
|
|
|
|
return '';
|
|
|
|
})
|
|
|
|
;
|
2016-10-06 22:42:38 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 17:18:05 +00:00
|
|
|
return prom.then(function (text) {
|
2017-02-23 01:48:34 +00:00
|
|
|
try {
|
2017-06-09 17:18:05 +00:00
|
|
|
JSON.parse(text);
|
|
|
|
return { name: filename, type: 'json' };
|
|
|
|
} catch (err) {}
|
2017-05-15 22:16:15 +00:00
|
|
|
|
2017-06-09 17:18:05 +00:00
|
|
|
try {
|
|
|
|
require('js-yaml').safeLoad(text);
|
|
|
|
return { name: filename, type: 'yaml' };
|
|
|
|
} catch (err) {}
|
|
|
|
|
|
|
|
throw new Error('Could not load "' + filename + '" as JSON nor YAML');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
function createConfigStorage(args) {
|
|
|
|
return checkConfigLocation(args.cwd, args.config)
|
|
|
|
.then(function (result) {
|
|
|
|
console.log('config file', result.name, 'is of type', result.type);
|
|
|
|
configStorage = createStorage(result.name, result.type);
|
|
|
|
return configStorage.read();
|
|
|
|
})
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2017-06-09 18:40:39 +00:00
|
|
|
var tcpProm;
|
2017-06-09 17:18:05 +00:00
|
|
|
function fillConfig(config, args) {
|
2017-05-25 00:20:02 +00:00
|
|
|
config.debug = config.debug || args.debug;
|
2017-05-15 22:16:15 +00:00
|
|
|
|
2017-05-03 19:55:16 +00:00
|
|
|
if (!config.dns) {
|
2017-06-09 18:14:25 +00:00
|
|
|
config.dns = { bind: [ 53 ], modules: [{ name: 'proxy', port: 3053 }] };
|
2017-05-03 19:55:16 +00:00
|
|
|
}
|
2017-05-24 17:42:17 +00:00
|
|
|
// Use Object.assign to add any properties needed but not defined in the mdns config.
|
|
|
|
// It will first copy the defaults into an empty object, then copy any real config over that.
|
|
|
|
var mdnsDefaults = { port: 5353, broadcast: '224.0.0.251', ttl: 300 };
|
2017-06-09 17:18:05 +00:00
|
|
|
config.mdns = Object.assign({}, mdnsDefaults, config.mdns);
|
2017-05-24 17:42:17 +00:00
|
|
|
|
2017-04-27 02:16:47 +00:00
|
|
|
if (!config.tcp) {
|
|
|
|
config.tcp = {};
|
|
|
|
}
|
|
|
|
if (!config.http) {
|
2017-05-24 19:05:37 +00:00
|
|
|
config.http = { modules: [{ name: 'proxy', domains: ['*'], port: 3000 }] };
|
2017-04-27 02:16:47 +00:00
|
|
|
}
|
|
|
|
if (!config.tls) {
|
2017-05-25 00:20:02 +00:00
|
|
|
config.tls = {};
|
|
|
|
}
|
|
|
|
if (!config.tls.acme && (args.email || args.agreeTos)) {
|
|
|
|
config.tls.acme = {};
|
|
|
|
}
|
|
|
|
if (typeof args.agreeTos === 'string') {
|
|
|
|
config.tls.acme.approvedDomains = args.agreeTos.split(',');
|
2017-04-27 02:16:47 +00:00
|
|
|
}
|
|
|
|
if (args.email) {
|
|
|
|
config.email = args.email;
|
2017-05-25 00:20:02 +00:00
|
|
|
config.tls.acme.email = args.email;
|
2017-04-27 02:16:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// maybe this should not go in config... but be ephemeral in some way?
|
2017-06-09 17:18:05 +00:00
|
|
|
config.cwd = args.cwd || config.cwd || process.cwd();
|
2017-04-27 02:16:47 +00:00
|
|
|
|
2017-04-27 22:50:03 +00:00
|
|
|
var ipaddr = require('ipaddr.js');
|
|
|
|
var addresses = [];
|
|
|
|
var ifaces = require('../lib/local-ip.js').find();
|
|
|
|
|
|
|
|
Object.keys(ifaces).forEach(function (ifacename) {
|
|
|
|
var iface = ifaces[ifacename];
|
|
|
|
iface.ipv4.forEach(function (ip) {
|
|
|
|
addresses.push(ip);
|
|
|
|
});
|
|
|
|
iface.ipv6.forEach(function (ip) {
|
|
|
|
addresses.push(ip);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
addresses.sort(function (a, b) {
|
|
|
|
if (a.family !== b.family) {
|
|
|
|
return 'IPv4' === a.family ? 1 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.address > b.address ? 1 : -1;
|
|
|
|
});
|
|
|
|
|
|
|
|
addresses.forEach(function (addr) {
|
|
|
|
addr.range = ipaddr.parse(addr.address).range();
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO maybe move to config.state.addresses (?)
|
|
|
|
config.addresses = addresses;
|
2017-06-09 17:18:05 +00:00
|
|
|
config.device = { hostname: require('os').hostname() };
|
2017-04-27 22:50:03 +00:00
|
|
|
|
2017-05-26 18:11:39 +00:00
|
|
|
config.tunnel = args.tunnel || config.tunnel;
|
|
|
|
|
2017-06-09 18:40:39 +00:00
|
|
|
if (Array.isArray(config.tcp.bind)) {
|
|
|
|
return PromiseA.resolve(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to make sure we only check once, because even though our workers can
|
|
|
|
// all bind on the same port witout issue we cannot. This will lead to failure
|
|
|
|
// to determine which ports will work once the first worker starts.
|
|
|
|
if (!tcpProm) {
|
2017-05-03 19:55:16 +00:00
|
|
|
tcpProm = new PromiseA(function (resolve, reject) {
|
|
|
|
require('../lib/check-ports').checkTcpPorts(function (failed, bound) {
|
2017-06-09 18:40:39 +00:00
|
|
|
var result = Object.keys(bound).map(Number);
|
|
|
|
if (result.length > 0) {
|
|
|
|
resolve(result);
|
2017-05-03 19:55:16 +00:00
|
|
|
} else {
|
|
|
|
reject(failed);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2017-04-27 02:16:47 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 18:40:39 +00:00
|
|
|
return tcpProm.then(
|
|
|
|
function (bound) {
|
|
|
|
config.tcp.bind = bound;
|
|
|
|
return config;
|
2017-06-09 22:03:12 +00:00
|
|
|
}, function (failed) {
|
2017-04-27 02:16:47 +00:00
|
|
|
Object.keys(failed).forEach(function (key) {
|
|
|
|
console.log('[error bind]', key, failed[key].code);
|
|
|
|
});
|
2017-06-09 18:14:25 +00:00
|
|
|
return PromiseA.reject(new Error("could not bind to the default ports"));
|
2017-05-03 19:55:16 +00:00
|
|
|
});
|
2017-04-27 02:16:47 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 17:18:05 +00:00
|
|
|
function run(args) {
|
|
|
|
var workers = {};
|
|
|
|
var cachedConfig;
|
|
|
|
|
|
|
|
cluster.on('message', function (worker, message) {
|
|
|
|
if (message.type !== 'com.daplie.goldilocks.config-change') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
configStorage.save(message.changes)
|
|
|
|
.then(function (config) {
|
|
|
|
return fillConfig(config, args);
|
|
|
|
})
|
|
|
|
.then(function (config) {
|
|
|
|
cachedConfig = config;
|
2017-06-09 18:40:39 +00:00
|
|
|
console.log('changed config', config);
|
|
|
|
Object.keys(workers).forEach(function (key) {
|
|
|
|
workers[key].send(cachedConfig);
|
|
|
|
});
|
2017-06-09 17:18:05 +00:00
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
console.error('error changing config', err);
|
|
|
|
})
|
|
|
|
;
|
|
|
|
});
|
|
|
|
|
|
|
|
cluster.on('online', function (worker) {
|
|
|
|
console.log('[worker]', worker.id, 'online');
|
|
|
|
workers[worker.id] = worker;
|
|
|
|
// Worker is listening
|
|
|
|
worker.send(cachedConfig);
|
|
|
|
});
|
|
|
|
|
|
|
|
cluster.on('exit', function (worker) {
|
|
|
|
delete workers[worker.id];
|
|
|
|
cluster.fork();
|
|
|
|
});
|
|
|
|
|
|
|
|
createConfigStorage(args)
|
|
|
|
.then(function (config) {
|
|
|
|
return fillConfig(config, args);
|
|
|
|
})
|
|
|
|
.then(function (config) {
|
|
|
|
console.log('config.tcp.bind', config.tcp.bind);
|
|
|
|
cachedConfig = config;
|
|
|
|
// TODO spin up multiple workers
|
|
|
|
// TODO use greenlock-cluster
|
|
|
|
cluster.fork();
|
|
|
|
})
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2017-04-27 02:16:47 +00:00
|
|
|
function readEnv(args) {
|
|
|
|
// TODO
|
2017-05-25 00:20:02 +00:00
|
|
|
try {
|
|
|
|
if (process.env.GOLDILOCKS_HOME) {
|
|
|
|
process.chdir(process.env.GOLDILOCKS_HOME);
|
|
|
|
}
|
|
|
|
} catch (err) {}
|
|
|
|
|
2017-04-27 02:16:47 +00:00
|
|
|
var env = {
|
|
|
|
tunnel: process.env.GOLDILOCKS_TUNNEL_TOKEN || process.env.GOLDILOCKS_TUNNEL && true
|
|
|
|
, email: process.env.GOLDILOCKS_EMAIL
|
2017-05-25 00:20:02 +00:00
|
|
|
, cwd: process.env.GOLDILOCKS_HOME || process.cwd()
|
2017-04-27 02:16:47 +00:00
|
|
|
, debug: process.env.GOLDILOCKS_DEBUG && true
|
|
|
|
};
|
2015-07-08 06:43:46 +00:00
|
|
|
|
2017-06-09 17:18:05 +00:00
|
|
|
run(Object.assign({}, env, args));
|
2017-04-13 23:42:37 +00:00
|
|
|
}
|
2016-08-06 18:34:15 +00:00
|
|
|
|
2017-05-06 18:04:46 +00:00
|
|
|
var program = require('commander');
|
|
|
|
|
|
|
|
program
|
|
|
|
.version(require('../package.json').version)
|
|
|
|
.option('--agree-tos [url1,url2]', "agree to all Terms of Service for Daplie, Let's Encrypt, etc (or specific URLs only)")
|
2017-05-08 19:00:45 +00:00
|
|
|
.option('-c --config <file>', 'Path to config file (Goldilocks.json or Goldilocks.yml) example: --config /etc/goldilocks/Goldilocks.json')
|
2017-05-06 18:04:46 +00:00
|
|
|
.option('--tunnel [token]', 'Turn tunnel on. This will enter interactive mode for login if no token is specified.')
|
|
|
|
.option('--email <email>', "(Re)set default email to use for Daplie, Let's Encrypt, ACME, etc.")
|
|
|
|
.option('--debug', "Enable debug output")
|
|
|
|
.parse(process.argv);
|
|
|
|
|
|
|
|
readEnv(program);
|