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