config: refactor. handle paths better.
This commit is contained in:
parent
ea53dc0fca
commit
7b61e32a70
1
etc/authorized-peers
Normal file
1
etc/authorized-peers
Normal file
@ -0,0 +1 @@
|
|||||||
|
02b9f7499c4166e76d3a64326bbbe92dc02f0e07dc184f94995da61683c311cb0f
|
||||||
1
etc/known-peers
Normal file
1
etc/known-peers
Normal file
@ -0,0 +1 @@
|
|||||||
|
node.bcoin.io,52.39.113.206 02b9f7499c4166e76d3a64326bbbe92dc02f0e07dc184f94995da61683c311cb0f
|
||||||
@ -13,7 +13,7 @@ log-console: true
|
|||||||
log-file: true
|
log-file: true
|
||||||
|
|
||||||
# Node
|
# Node
|
||||||
# prefix: ~/.bcoin
|
prefix: ~/.bcoin
|
||||||
db: leveldb
|
db: leveldb
|
||||||
max-files: 64
|
max-files: 64
|
||||||
# fast: false
|
# fast: false
|
||||||
@ -34,29 +34,31 @@ index-address: false
|
|||||||
|
|
||||||
# Pool
|
# Pool
|
||||||
# selfish: false
|
# selfish: false
|
||||||
headers: true
|
headers: false
|
||||||
compact: true
|
compact: true
|
||||||
bip151: true
|
bip151: true
|
||||||
# bip150: false
|
|
||||||
# identity-key: 74b4147957813b62cc8987f2b711ddb31f8cb46dcbf71502033da66053c8780a
|
|
||||||
# proxy-server: localhost
|
# proxy-server: localhost
|
||||||
# preferred-seed: seed.bitcoin.sipa.be
|
# preferred-seed: seed.bitcoin.sipa.be
|
||||||
# ignore-discovery: false
|
# ignore-discovery: false
|
||||||
# port: 8333
|
# port: 8333
|
||||||
listen: true
|
listen: true
|
||||||
# max-peers: 8
|
max-peers: 8
|
||||||
# max-leeches: 8
|
max-leeches: 30
|
||||||
|
bip150: true
|
||||||
|
identity-key: 74b4147957813b62cc8987f2b711ddb31f8cb46dcbf71502033da66053c8780a
|
||||||
|
auth-peers: ./authorized-peers
|
||||||
|
known-peers: ./known-peers
|
||||||
|
|
||||||
# Miner
|
# Miner
|
||||||
# payout-address:
|
# payout-address: 1111111111111111111114oLvT2
|
||||||
# coinbase-flags: Mined by bcoin
|
# coinbase-flags: mined by bcoin
|
||||||
# parallel: false
|
# parallel: false
|
||||||
|
|
||||||
# HTTP
|
# HTTP
|
||||||
# ssl-cert: /path/to/cert
|
# ssl-cert: @/ssl/cert.crt
|
||||||
# ssl-key: /path/to/key
|
# ssl-key: @/ssl/priv.key
|
||||||
# http-port: 8332
|
# http-port: 8332
|
||||||
# http-host: 0.0.0.0
|
# http-host: 0.0.0.0
|
||||||
# api-key: foobar
|
api-key: bikeshed
|
||||||
# wallet-auth: true
|
wallet-auth: false
|
||||||
# no-auth: false
|
# no-auth: false
|
||||||
|
|||||||
@ -47,37 +47,45 @@ config.parse = function parse(options) {
|
|||||||
var env = {};
|
var env = {};
|
||||||
var arg = {};
|
var arg = {};
|
||||||
var data = {};
|
var data = {};
|
||||||
var text, prefix;
|
var text, prefix, filename, dirname;
|
||||||
|
|
||||||
if (!options)
|
if (!options)
|
||||||
options = {};
|
options = {};
|
||||||
|
|
||||||
if (options.env)
|
|
||||||
env = config.parseEnv();
|
|
||||||
|
|
||||||
if (options.arg)
|
|
||||||
arg = config.parseArg();
|
|
||||||
|
|
||||||
merge(data, options);
|
merge(data, options);
|
||||||
merge(data, env);
|
|
||||||
merge(data, arg);
|
if (options.env) {
|
||||||
|
env = config.parseEnv();
|
||||||
|
merge(data, env);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.arg) {
|
||||||
|
arg = config.parseArg();
|
||||||
|
merge(data, arg);
|
||||||
|
}
|
||||||
|
|
||||||
if (data.config) {
|
if (data.config) {
|
||||||
if (typeof data.config === 'string') {
|
prefix = config.getPrefix(data);
|
||||||
text = config.parseFile(data.config);
|
filename = data.config;
|
||||||
} else {
|
|
||||||
prefix = config.getPrefix(data);
|
|
||||||
text = config.parseFile(prefix + '/bcoin.conf');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (typeof filename !== 'string')
|
||||||
|
filename = resolve(prefix, 'bcoin.conf');
|
||||||
|
|
||||||
|
dirname = utils.normalize(filename, true);
|
||||||
|
text = config.readConfig(filename, prefix, dirname);
|
||||||
data = merge(text, data);
|
data = merge(text, data);
|
||||||
|
|
||||||
prefix = config.getPrefix(data);
|
prefix = config.getPrefix(data);
|
||||||
|
|
||||||
if (!data.knownPeers)
|
if (!data.knownPeers) {
|
||||||
data.knownPeers = config.parseKnown(prefix + '/known-peers');
|
filename = resolve(prefix, 'known-peers');
|
||||||
|
data.knownPeers = config.readKnown(filename);
|
||||||
|
}
|
||||||
|
|
||||||
if (!data.authPeers)
|
if (!data.authPeers) {
|
||||||
data.authPeers = config.parseAuth(prefix + '/authorized-peers');
|
filename = resolve(prefix, 'authorized-peers');
|
||||||
|
data.authPeers = config.readAuth(filename);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
@ -112,7 +120,7 @@ config.getPrefix = function getPrefix(data) {
|
|||||||
* @param {Object} data
|
* @param {Object} data
|
||||||
*/
|
*/
|
||||||
|
|
||||||
config.parseData = function parseData(data) {
|
config.parseData = function parseData(data, prefix, dirname) {
|
||||||
var options = {};
|
var options = {};
|
||||||
|
|
||||||
// Config
|
// Config
|
||||||
@ -125,17 +133,21 @@ config.parseData = function parseData(data) {
|
|||||||
options.workerTimeout = num(data.workertimeout);
|
options.workerTimeout = num(data.workertimeout);
|
||||||
options.sigcacheSize = num(data.sigcachesize);
|
options.sigcacheSize = num(data.sigcachesize);
|
||||||
|
|
||||||
// Logger
|
|
||||||
options.logLevel = str(data.loglevel);
|
|
||||||
options.logConsole = bool(data.logconsole);
|
|
||||||
options.logFile = boolpath(data.logfile);
|
|
||||||
|
|
||||||
// Node
|
// Node
|
||||||
options.prefix = path(data.prefix);
|
options.prefix = path(data.prefix, null, dirname);
|
||||||
options.db = str(data.db);
|
options.db = str(data.db);
|
||||||
options.maxFiles = num(data.maxfiles);
|
options.maxFiles = num(data.maxfiles);
|
||||||
options.fast = bool(data.fast);
|
options.fast = bool(data.fast);
|
||||||
|
|
||||||
|
// Update the prefix if we're using one.
|
||||||
|
if (prefix && options.prefix)
|
||||||
|
prefix = config.getPrefix(options);
|
||||||
|
|
||||||
|
// Logger
|
||||||
|
options.logLevel = str(data.loglevel);
|
||||||
|
options.logConsole = bool(data.logconsole);
|
||||||
|
options.logFile = boolpath(data.logfile, prefix, dirname);
|
||||||
|
|
||||||
// Chain
|
// Chain
|
||||||
options.witness = bool(data.witness);
|
options.witness = bool(data.witness);
|
||||||
options.prune = bool(data.prune);
|
options.prune = bool(data.prune);
|
||||||
@ -166,6 +178,8 @@ config.parseData = function parseData(data) {
|
|||||||
options.ignoreDiscovery = bool(data.ignorediscovery);
|
options.ignoreDiscovery = bool(data.ignorediscovery);
|
||||||
options.port = num(data.port);
|
options.port = num(data.port);
|
||||||
options.listen = bool(data.listen);
|
options.listen = bool(data.listen);
|
||||||
|
options.knownPeers = file(data.knownpeers, prefix, dirname, 'utf8');
|
||||||
|
options.authPeers = file(data.authpeers, prefix, dirname, 'utf8');
|
||||||
|
|
||||||
// Miner
|
// Miner
|
||||||
options.payoutAddress = str(data.payoutaddress);
|
options.payoutAddress = str(data.payoutaddress);
|
||||||
@ -173,8 +187,8 @@ config.parseData = function parseData(data) {
|
|||||||
options.parallel = bool(data.parallel);
|
options.parallel = bool(data.parallel);
|
||||||
|
|
||||||
// HTTP
|
// HTTP
|
||||||
options.sslCert = file(data.sslcert);
|
options.sslCert = file(data.sslcert, prefix, dirname);
|
||||||
options.sslKey = file(data.sslkey);
|
options.sslKey = file(data.sslkey, prefix, dirname);
|
||||||
options.httpPort = num(data.httpport);
|
options.httpPort = num(data.httpport);
|
||||||
options.httpHost = str(data.httphost);
|
options.httpHost = str(data.httphost);
|
||||||
options.apiKey = str(data.apikey);
|
options.apiKey = str(data.apikey);
|
||||||
@ -183,6 +197,12 @@ config.parseData = function parseData(data) {
|
|||||||
|
|
||||||
options.data = data;
|
options.data = data;
|
||||||
|
|
||||||
|
if (options.knownPeers != null)
|
||||||
|
options.knownPeers = config.parseKnown(options.knownPeers);
|
||||||
|
|
||||||
|
if (options.authPeers != null)
|
||||||
|
options.authPeers = config.parseAuth(options.authPeers);
|
||||||
|
|
||||||
return options;
|
return options;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -192,8 +212,8 @@ config.parseData = function parseData(data) {
|
|||||||
* @returns {Object}
|
* @returns {Object}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
config.parseFile = function parseFile(file) {
|
config.readConfig = function readConfig(file, prefix, dirname) {
|
||||||
return config.parseText(readFile(file));
|
return config.parseConfig(readFile(file), prefix, dirname);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -202,8 +222,8 @@ config.parseFile = function parseFile(file) {
|
|||||||
* @returns {Object}
|
* @returns {Object}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
config.parseKnown = function parseKnown(file) {
|
config.readKnown = function readKnown(file) {
|
||||||
return config.parseKnownText(readFile(file));
|
return config.parseKnown(readFile(file));
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -212,8 +232,8 @@ config.parseKnown = function parseKnown(file) {
|
|||||||
* @returns {Object}
|
* @returns {Object}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
config.parseAuth = function parseAuth(file) {
|
config.readAuth = function readAuth(file) {
|
||||||
return config.parseAuthText(readFile(file));
|
return config.parseAuth(readFile(file));
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -222,7 +242,7 @@ config.parseAuth = function parseAuth(file) {
|
|||||||
* @returns {Object}
|
* @returns {Object}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
config.parseText = function parseText(text) {
|
config.parseConfig = function parseConfig(text, prefix, dirname) {
|
||||||
var data = {};
|
var data = {};
|
||||||
var i, parts, line, key, value, eq, col, alias;
|
var i, parts, line, key, value, eq, col, alias;
|
||||||
|
|
||||||
@ -269,7 +289,7 @@ config.parseText = function parseText(text) {
|
|||||||
data[key] = value;
|
data[key] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
return config.parseData(data);
|
return config.parseData(data, prefix, dirname);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -402,7 +422,7 @@ config.parseEnv = function parseEnv(env) {
|
|||||||
* @returns {Object}
|
* @returns {Object}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
config.parseKnownText = function parseKnownText(text) {
|
config.parseKnown = function parseKnown(text) {
|
||||||
var lines = text.split(/\n+/);
|
var lines = text.split(/\n+/);
|
||||||
var map = {};
|
var map = {};
|
||||||
var i, line, parts, hostname, host, ip, key;
|
var i, line, parts, hostname, host, ip, key;
|
||||||
@ -455,7 +475,7 @@ config.parseKnownText = function parseKnownText(text) {
|
|||||||
* @returns {Buffer[]} keys
|
* @returns {Buffer[]} keys
|
||||||
*/
|
*/
|
||||||
|
|
||||||
config.parseAuthText = function parseAuthText(text) {
|
config.parseAuth = function parseAuth(text) {
|
||||||
var lines = text.split(/\n+/);
|
var lines = text.split(/\n+/);
|
||||||
var keys = [];
|
var keys = [];
|
||||||
var i, line, key;
|
var i, line, key;
|
||||||
@ -507,10 +527,25 @@ function key(value) {
|
|||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
function path(value) {
|
function path(value, prefix, dirname) {
|
||||||
if (!value)
|
if (!value)
|
||||||
return null;
|
return null;
|
||||||
return utils.normalize(value.replace(/^~/, utils.HOME));
|
|
||||||
|
switch (value[0]) {
|
||||||
|
case '~': // home dir
|
||||||
|
value = utils.HOME + value.substring(1);
|
||||||
|
break;
|
||||||
|
case '@': // prefix
|
||||||
|
if (prefix)
|
||||||
|
value = prefix + value.substring(1);
|
||||||
|
break;
|
||||||
|
default: // dirname of config, or cwd
|
||||||
|
if (dirname)
|
||||||
|
value = resolve(dirname, value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return utils.normalize(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
function bool(value) {
|
function bool(value) {
|
||||||
@ -538,7 +573,7 @@ function num(value) {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
function boolpath(value) {
|
function boolpath(value, prefix, dirname) {
|
||||||
if (!value)
|
if (!value)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
@ -548,25 +583,43 @@ function boolpath(value) {
|
|||||||
if (value === 'false' || value === '0')
|
if (value === 'false' || value === '0')
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return path(value);
|
return path(value, prefix, dirname);
|
||||||
}
|
}
|
||||||
|
|
||||||
function file(value) {
|
function file(value, prefix, dirname, enc) {
|
||||||
if (!fs)
|
if (!fs)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
value = path(value);
|
value = path(value, prefix, dirname);
|
||||||
|
|
||||||
if (!value)
|
if (!value)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
return fs.readFileSync(value);
|
try {
|
||||||
|
return fs.readFileSync(value, enc);
|
||||||
|
} catch (e) {
|
||||||
|
if (e.code === 'ENOENT')
|
||||||
|
return null;
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolve(a, b) {
|
||||||
|
if (b[0] === '/')
|
||||||
|
return b;
|
||||||
|
return utils.normalize(a + '/' + b);
|
||||||
}
|
}
|
||||||
|
|
||||||
function readFile(file) {
|
function readFile(file) {
|
||||||
if (!fs)
|
if (!fs)
|
||||||
return '';
|
return '';
|
||||||
|
|
||||||
|
if (!file)
|
||||||
|
return '';
|
||||||
|
|
||||||
|
if (typeof file !== 'string')
|
||||||
|
return '';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return fs.readFileSync(file, 'utf8');
|
return fs.readFileSync(file, 'utf8');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
@ -2382,7 +2382,8 @@ utils.normalize = function normalize(path, dirname) {
|
|||||||
var parts;
|
var parts;
|
||||||
|
|
||||||
path = path.replace(/\\/g, '/');
|
path = path.replace(/\\/g, '/');
|
||||||
path = path.replace(/\/+$/, '');
|
path = path.replace(/(^|\/)\.\//, '$1');
|
||||||
|
path = path.replace(/\/+\.?$/, '');
|
||||||
parts = path.split(/\/+/);
|
parts = path.split(/\/+/);
|
||||||
|
|
||||||
if (dirname)
|
if (dirname)
|
||||||
@ -2403,7 +2404,8 @@ utils.mkdirp = function mkdirp(path) {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
path = path.replace(/\\/g, '/');
|
path = path.replace(/\\/g, '/');
|
||||||
path = path.replace(/\/+$/, '');
|
path = path.replace(/(^|\/)\.\//, '$1');
|
||||||
|
path = path.replace(/\/+\.?$/, '');
|
||||||
parts = path.split(/\/+/);
|
parts = path.split(/\/+/);
|
||||||
path = '';
|
path = '';
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user