2015-07-20 00:09:47 -07:00
|
|
|
'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',
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
// convenience getters
|
2015-12-29 11:24:34 +01:00
|
|
|
provider: provider,
|
2015-07-20 00:09:47 -07:00
|
|
|
apiServerOrigin: apiServerOrigin,
|
|
|
|
|
webServerOrigin: webServerOrigin,
|
2018-01-28 14:19:28 -08:00
|
|
|
adminDomain: adminDomain,
|
2018-01-29 14:53:03 -08:00
|
|
|
setFqdn: setAdminDomain,
|
2018-01-29 14:21:16 -08:00
|
|
|
setAdminDomain: setAdminDomain,
|
2018-01-10 20:40:15 -08:00
|
|
|
setAdminFqdn: setAdminFqdn,
|
|
|
|
|
setAdminLocation: setAdminLocation,
|
2015-07-20 00:09:47 -07:00
|
|
|
version: version,
|
2016-01-25 14:48:09 -08:00
|
|
|
setVersion: setVersion,
|
2015-07-20 00:09:47 -07:00
|
|
|
database: database,
|
2018-08-28 18:31:48 -07:00
|
|
|
edition: edition,
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
// these values are derived
|
|
|
|
|
adminOrigin: adminOrigin,
|
2015-09-16 10:13:11 -07:00
|
|
|
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,
|
2017-09-29 19:12:53 +02:00
|
|
|
hasIPv6: hasIPv6,
|
2017-10-31 10:19:52 -07:00
|
|
|
dkimSelector: dkimSelector,
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-08-31 20:51:36 -07:00
|
|
|
isDemo: isDemo,
|
2018-08-28 18:31:48 -07:00
|
|
|
isSpacesEnabled: isSpacesEnabled,
|
2018-08-30 21:20:49 -07:00
|
|
|
allowHyphenatedSubdomains: allowHyphenatedSubdomains,
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
// for testing resets to defaults
|
2016-01-29 14:17:10 +01:00
|
|
|
_reset: _reset
|
2015-07-20 00:09:47 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var assert = require('assert'),
|
|
|
|
|
fs = require('fs'),
|
|
|
|
|
path = require('path'),
|
|
|
|
|
safe = require('safetydance'),
|
|
|
|
|
_ = require('underscore');
|
|
|
|
|
|
2017-11-15 02:29:07 +01:00
|
|
|
|
|
|
|
|
// assert on unknown environment can't proceed
|
|
|
|
|
assert(exports.CLOUDRON || exports.TEST, 'Unknown environment. This should not happen!');
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
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');
|
2017-11-15 02:40:50 +01:00
|
|
|
|
|
|
|
|
// only tests can run without a config file on disk, they use the defaults with runtime overrides
|
|
|
|
|
if (exports.CLOUDRON) assert(fs.existsSync(cloudronConfigFileName), 'No cloudron.conf found, cannot proceed');
|
2015-10-21 16:42:17 +02:00
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
function saveSync() {
|
2017-11-15 02:29:07 +01:00
|
|
|
// only save values we want to have in the cloudron.conf, see start.sh
|
|
|
|
|
var conf = {
|
|
|
|
|
version: data.version,
|
|
|
|
|
apiServerOrigin: data.apiServerOrigin,
|
|
|
|
|
webServerOrigin: data.webServerOrigin,
|
2018-01-28 14:19:28 -08:00
|
|
|
adminDomain: data.adminDomain,
|
2018-01-10 20:40:15 -08:00
|
|
|
adminFqdn: data.adminFqdn,
|
2017-11-15 02:29:07 +01:00
|
|
|
adminLocation: data.adminLocation,
|
|
|
|
|
provider: data.provider,
|
2018-08-22 21:26:05 +02:00
|
|
|
isDemo: data.isDemo,
|
|
|
|
|
edition: data.edition
|
2017-11-15 02:29:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fs.writeFileSync(cloudronConfigFileName, JSON.stringify(conf, null, 4)); // functions are ignored by JSON.stringify
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
2017-02-06 21:53:29 -08:00
|
|
|
function _reset(callback) {
|
2016-01-29 14:17:10 +01:00
|
|
|
safe.fs.unlinkSync(cloudronConfigFileName);
|
|
|
|
|
|
|
|
|
|
initConfig();
|
2016-01-29 16:27:04 +01:00
|
|
|
|
|
|
|
|
if (callback) callback();
|
2016-01-29 14:17:10 +01:00
|
|
|
}
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
function initConfig() {
|
|
|
|
|
// setup defaults
|
2018-01-10 20:40:15 -08:00
|
|
|
data.adminFqdn = '';
|
2018-01-28 14:19:28 -08:00
|
|
|
data.adminDomain = '';
|
2017-10-25 20:35:24 -07:00
|
|
|
data.adminLocation = 'my';
|
2017-11-15 02:29:07 +01:00
|
|
|
data.port = 3000;
|
2015-07-20 00:09:47 -07:00
|
|
|
data.version = null;
|
2017-11-15 02:29:07 +01:00
|
|
|
data.apiServerOrigin = null;
|
2015-07-20 00:09:47 -07:00
|
|
|
data.webServerOrigin = null;
|
2018-01-18 13:34:41 -08:00
|
|
|
data.provider = 'generic';
|
2017-11-15 02:29:07 +01:00
|
|
|
data.smtpPort = 2525; // this value comes from mail container
|
2016-04-15 12:33:54 -07:00
|
|
|
data.sysadminPort = 3001;
|
2015-07-20 00:09:47 -07:00
|
|
|
data.ldapPort = 3002;
|
2018-08-13 21:10:53 +02:00
|
|
|
data.dockerProxyPort = 3003;
|
2018-08-22 21:26:05 +02:00
|
|
|
data.edition = '';
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2017-11-15 02:29:07 +01:00
|
|
|
// keep in sync with start.sh
|
|
|
|
|
data.database = {
|
|
|
|
|
hostname: '127.0.0.1',
|
|
|
|
|
username: 'root',
|
|
|
|
|
password: 'password',
|
|
|
|
|
port: 3306,
|
|
|
|
|
name: 'box'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// overrides for local testings
|
|
|
|
|
if (exports.TEST) {
|
2017-11-27 15:30:55 -08:00
|
|
|
data.version = '1.1.1-test';
|
2015-07-20 00:09:47 -07:00
|
|
|
data.port = 5454;
|
2017-11-15 02:29:07 +01:00
|
|
|
data.apiServerOrigin = 'http://localhost:6060'; // hock doesn't support https
|
|
|
|
|
data.database.password = '';
|
|
|
|
|
data.database.name = 'boxtest';
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
2017-11-15 02:29:07 +01:00
|
|
|
// overwrite defaults with saved config
|
|
|
|
|
var existingData = safe.JSON.parse(safe.fs.readFileSync(cloudronConfigFileName, 'utf8'));
|
|
|
|
|
_.extend(data, existingData);
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2018-01-29 10:11:09 +01:00
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
saveSync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function get(key) {
|
|
|
|
|
assert.strictEqual(typeof key, 'string');
|
|
|
|
|
|
|
|
|
|
return safe.query(data, key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function apiServerOrigin() {
|
|
|
|
|
return get('apiServerOrigin');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function webServerOrigin() {
|
|
|
|
|
return get('webServerOrigin');
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-29 14:21:16 -08:00
|
|
|
function setAdminDomain(domain) {
|
|
|
|
|
set('adminDomain', domain);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-28 14:19:28 -08:00
|
|
|
function adminDomain() {
|
|
|
|
|
return get('adminDomain');
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2018-01-10 20:40:15 -08:00
|
|
|
function setAdminLocation(location) {
|
|
|
|
|
set('adminLocation', location);
|
2017-10-25 20:35:24 -07:00
|
|
|
}
|
|
|
|
|
|
2018-01-10 20:40:15 -08:00
|
|
|
|
2017-10-25 20:35:24 -07:00
|
|
|
function adminLocation() {
|
|
|
|
|
return get('adminLocation');
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-10 20:40:15 -08:00
|
|
|
function setAdminFqdn(adminFqdn) {
|
|
|
|
|
set('adminFqdn', adminFqdn);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-25 20:35:24 -07:00
|
|
|
function adminFqdn() {
|
2018-01-10 20:40:15 -08:00
|
|
|
return get('adminFqdn');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function mailFqdn() {
|
|
|
|
|
return adminFqdn();
|
2016-05-11 09:53:57 -07:00
|
|
|
}
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
function adminOrigin() {
|
2017-11-02 22:17:44 +01:00
|
|
|
return 'https://' + adminFqdn();
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
2015-09-16 10:13:11 -07:00
|
|
|
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');
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
function version() {
|
|
|
|
|
return get('version');
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-25 14:48:09 -08:00
|
|
|
function setVersion(version) {
|
|
|
|
|
set('version', version);
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
function database() {
|
|
|
|
|
return get('database');
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-31 20:51:36 -07:00
|
|
|
function isDemo() {
|
|
|
|
|
return get('isDemo') === true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-28 18:31:48 -07:00
|
|
|
function isSpacesEnabled() {
|
2018-08-30 21:20:49 -07:00
|
|
|
return get('edition') === 'education';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function allowHyphenatedSubdomains() {
|
|
|
|
|
return get('edition') === 'hostingprovider';
|
2018-08-28 18:31:48 -07:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
2017-01-05 14:38:36 -08:00
|
|
|
|
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
|
|
|
|
2018-01-24 21:30:06 -08:00
|
|
|
// it has to change with the adminLocation so that multiple cloudrons
|
|
|
|
|
// can send out emails at the same time.
|
2017-10-31 10:19:52 -07:00
|
|
|
function dkimSelector() {
|
|
|
|
|
var loc = adminLocation();
|
|
|
|
|
return loc === 'my' ? 'cloudron' : `cloudron-${loc.replace(/\./g, '')}`;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-22 21:26:05 +02:00
|
|
|
function edition() {
|
|
|
|
|
return get('edition');
|
|
|
|
|
}
|