/
coreConfig.js
136 lines (126 loc) · 3.81 KB
/
coreConfig.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import fs from 'fs';
import path from 'path';
import log from 'electron-log';
import crypto from 'crypto';
import macaddress from 'macaddress';
import { defaultCoreDataDir } from 'consts/paths';
import { loadSettingsFromFile } from 'lib/settings/universal';
function generateDefaultPassword() {
let randomNumbers = ['', ''];
const ranByte = crypto
.randomBytes(64)
.toString('hex')
.split('');
for (let index = 0; index < ranByte.length; index++) {
const element = ranByte[index];
if (index % 2) {
randomNumbers[0] += element.charCodeAt(0);
} else {
randomNumbers[1] += element.charCodeAt(0);
}
}
randomNumbers[0] = parseInt(randomNumbers[0]);
randomNumbers[1] = parseInt(randomNumbers[1]);
const randomValue = randomNumbers[0] * randomNumbers[1];
const secret =
process.platform === 'darwin'
? process.env.USER + process.env.HOME + process.env.SHELL + randomValue
: JSON.stringify(macaddress.networkInterfaces(), null, 2) + randomValue;
return crypto
.createHmac('sha256', secret)
.update('pass')
.digest('hex');
}
const fromKeyValues = rawContent =>
rawContent
? rawContent.split('\n').reduce((obj, line) => {
const equalIndex = line.indexOf('=');
if (equalIndex >= 0) {
const key = line.substring(0, equalIndex);
const value = line.substring(equalIndex + 1);
if (key) obj[key] = value;
}
return obj;
}, {})
: {};
const toKeyValues = obj =>
Object.entries(obj)
.map(([key, value]) => `${key}=${value}`)
.join('\n');
const defaultConfig = {
ip: '127.0.0.1',
port: '9336',
apiPort: '8080',
user: 'rpcserver',
password: generateDefaultPassword(),
apiUser: 'apiserver',
apiPassword: generateDefaultPassword(),
dataDir: defaultCoreDataDir,
verbose: 2,
};
/**
* Returns either the given config or default Config
*
* @export
* @param {*} [config={}]
* @returns
*/
export function customConfig(config = {}) {
const ip = config.ip || defaultConfig.ip;
const port = config.port || defaultConfig.port;
const apiPort = config.apiPort || defaultConfig.apiPort;
return {
ip,
port,
apiPort,
host: `http://${ip}:${port}`,
apiHost: `http://${ip}:${apiPort}`,
user: config.user || config.rpcuser || defaultConfig.user,
password: config.password || config.rpcpassword || defaultConfig.password,
apiUser: config.apiUser || config.apiuser || defaultConfig.apiUser,
apiPassword:
config.apiPassword || config.apipassword || defaultConfig.apiPassword,
dataDir: config.dataDir || defaultConfig.dataDir,
verbose:
config.verbose || config.verbose === 0
? config.verbose
: defaultConfig.verbose,
};
}
/**
* Load user & password from the nexus.conf file
*
* @returns
*/
export function loadNexusConf() {
const { coreDataDir } = loadSettingsFromFile();
const confPath = path.join(coreDataDir, 'nexus.conf');
const confContent = fs.existsSync(confPath)
? fs.readFileSync(confPath).toString()
: '';
const configs = fromKeyValues(confContent);
log.info(
'nexus.conf exists. Importing username and password for RPC server and API server.'
);
// Fallback to default values if empty
const fallbackConf = [
['rpcuser', defaultConfig.user],
['rpcpassword', defaultConfig.password],
['apiuser', defaultConfig.apiUser],
['apipassword', defaultConfig.apiPassword],
];
let updated = false;
fallbackConf.forEach(([key, value]) => {
// Don't replace it if value is an empty string
if (configs[key] === undefined) {
configs[key] = value;
updated = true;
}
});
// Save nexus.conf file if there were changes
if (updated) {
log.info('Filling up some missing configurations in nexus.conf');
fs.writeFileSync(confPath, toKeyValues(configs));
}
return configs;
}