Files
cloudron-box/src/config.js
T

257 lines
5.7 KiB
JavaScript
Raw Normal View History

'use strict';
exports = module.exports = {
baseDir: baseDir,
// values set here will be lost after a upgrade/update. use the sqlite database
// for persistent values that need to be backed up
get: get,
set: set,
// ifdefs to check environment
2015-07-24 01:42:28 -07:00
CLOUDRON: process.env.BOX_ENV === 'cloudron',
TEST: process.env.BOX_ENV === 'test',
// convenience getters
2015-12-29 11:24:34 +01:00
provider: provider,
apiServerOrigin: apiServerOrigin,
webServerOrigin: webServerOrigin,
fqdn: fqdn,
2017-10-31 10:19:52 -07:00
zoneName: zoneName,
2017-06-08 19:22:58 -07:00
setFqdn: setFqdn,
token: token,
version: version,
setVersion: setVersion,
isCustomDomain: isCustomDomain,
database: database,
// these values are derived
adminOrigin: adminOrigin,
internalAdminOrigin: internalAdminOrigin,
2016-04-15 12:33:54 -07:00
sysadminOrigin: sysadminOrigin, // caas routes
2017-10-25 20:35:24 -07:00
adminLocation: adminLocation,
2015-12-10 13:14:13 -08:00
adminFqdn: adminFqdn,
2017-10-25 20:35:24 -07:00
mailLocation: mailLocation,
2016-05-11 09:53:57 -07:00
mailFqdn: mailFqdn,
appFqdn: appFqdn,
2017-06-08 19:22:58 -07:00
setZoneName: setZoneName,
2017-09-29 19:12:53 +02:00
hasIPv6: hasIPv6,
2017-10-31 10:19:52 -07:00
dkimSelector: dkimSelector,
2016-08-31 20:51:36 -07:00
isDemo: isDemo,
tlsCert: tlsCert,
tlsKey: tlsKey,
// for testing resets to defaults
_reset: _reset
};
var assert = require('assert'),
fs = require('fs'),
path = require('path'),
safe = require('safetydance'),
2017-06-08 19:22:58 -07:00
tld = require('tldjs'),
_ = require('underscore');
var homeDir = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
var data = { };
function baseDir() {
if (exports.CLOUDRON) return homeDir;
if (exports.TEST) return path.join(homeDir, '.cloudron_test');
}
var cloudronConfigFileName = path.join(baseDir(), 'configs/cloudron.conf');
2015-10-21 16:42:17 +02:00
function saveSync() {
fs.writeFileSync(cloudronConfigFileName, JSON.stringify(data, null, 4)); // functions are ignored by JSON.stringify
}
2017-02-06 21:53:29 -08:00
function _reset(callback) {
safe.fs.unlinkSync(cloudronConfigFileName);
initConfig();
if (callback) callback();
}
function initConfig() {
// setup defaults
data.fqdn = 'localhost';
2017-06-08 19:22:58 -07:00
data.zoneName = '';
2017-10-25 20:35:24 -07:00
data.adminLocation = 'my';
data.token = null;
data.version = null;
2017-02-06 23:27:04 -08:00
data.isCustomDomain = true;
data.webServerOrigin = null;
2016-05-24 00:52:35 -07:00
data.smtpPort = 2525; // // this value comes from mail container
2016-04-15 12:33:54 -07:00
data.sysadminPort = 3001;
data.ldapPort = 3002;
2015-12-29 11:24:34 +01:00
data.provider = 'caas';
if (exports.CLOUDRON) {
data.port = 3000;
data.apiServerOrigin = null;
data.database = null;
} else if (exports.TEST) {
data.port = 5454;
data.apiServerOrigin = 'http://localhost:6060'; // hock doesn't support https
data.database = {
2017-08-31 10:55:08 -07:00
hostname: '127.0.0.1',
username: 'root',
password: '',
port: 3306,
name: 'boxtest'
};
data.token = 'APPSTORE_TOKEN';
} else {
assert(false, 'Unknown environment. This should not happen!');
}
if (safe.fs.existsSync(cloudronConfigFileName)) {
var existingData = safe.JSON.parse(safe.fs.readFileSync(cloudronConfigFileName, 'utf8'));
_.extend(data, existingData); // overwrite defaults with saved config
return;
}
saveSync();
}
initConfig();
// set(obj) or set(key, value)
function set(key, value) {
if (typeof key === 'object') {
var obj = key;
for (var k in obj) {
assert(k in data, 'config.js is missing key "' + k + '"');
data[k] = obj[k];
}
} else {
data = safe.set(data, key, value);
}
saveSync();
}
function get(key) {
assert.strictEqual(typeof key, 'string');
return safe.query(data, key);
}
function apiServerOrigin() {
return get('apiServerOrigin');
}
function webServerOrigin() {
return get('webServerOrigin');
}
2017-06-08 19:22:58 -07:00
function setFqdn(fqdn) {
set('fqdn', fqdn);
}
function fqdn() {
return get('fqdn');
}
2017-06-08 19:22:58 -07:00
function setZoneName(zone) {
set('zoneName', zone);
}
function zoneName() {
var zone = get('zoneName');
if (zone) return zone;
// TODO: move this to migration code path instead
return tld.getDomain(fqdn()) || '';
}
// keep this in sync with start.sh admin.conf generation code
function appFqdn(location) {
assert.strictEqual(typeof location, 'string');
if (location === '') return fqdn();
return isCustomDomain() ? location + '.' + fqdn() : location + '-' + fqdn();
}
2017-10-25 20:35:24 -07:00
function mailLocation() {
return get('adminLocation'); // not a typo! should be same as admin location until we figure out certificates
2015-12-10 13:14:13 -08:00
}
2016-05-11 09:53:57 -07:00
function mailFqdn() {
2017-10-25 20:35:24 -07:00
return appFqdn(mailLocation());
}
function adminLocation() {
return get('adminLocation');
}
function adminFqdn() {
return appFqdn(adminLocation());
2016-05-11 09:53:57 -07:00
}
function adminOrigin() {
2017-10-25 20:35:24 -07:00
return 'https://' + appFqdn(adminLocation());
}
function internalAdminOrigin() {
return 'http://127.0.0.1:' + get('port');
}
2016-04-15 12:33:54 -07:00
function sysadminOrigin() {
return 'http://127.0.0.1:' + get('sysadminPort');
}
function token() {
return get('token');
}
function version() {
return get('version');
}
function setVersion(version) {
set('version', version);
}
function isCustomDomain() {
return get('isCustomDomain');
}
function database() {
return get('database');
}
2016-08-31 20:51:36 -07:00
function isDemo() {
return get('isDemo') === true;
}
2015-12-29 11:24:34 +01:00
function provider() {
2016-12-13 10:18:16 -08:00
return get('provider');
2015-12-29 11:24:34 +01:00
}
function tlsCert() {
var certFile = path.join(baseDir(), 'configs/host.cert');
return safe.fs.readFileSync(certFile, 'utf8');
}
function tlsKey() {
var keyFile = path.join(baseDir(), 'configs/host.key');
return safe.fs.readFileSync(keyFile, 'utf8');
}
2017-09-29 19:12:53 +02:00
function hasIPv6() {
2017-10-10 19:47:21 -07:00
const IPV6_PROC_FILE = '/proc/net/if_inet6';
return fs.existsSync(IPV6_PROC_FILE);
2017-10-25 20:35:24 -07:00
}
2017-10-31 10:19:52 -07:00
function dkimSelector() {
var loc = adminLocation();
return loc === 'my' ? 'cloudron' : `cloudron-${loc.replace(/\./g, '')}`;
}