-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrun_bot.js
executable file
·138 lines (117 loc) · 3.95 KB
/
run_bot.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
137
138
#!/usr/bin/nodejs
/**
* Part of the evias/nem-nodejs-bot package.
*
* NOTICE OF LICENSE
*
* Licensed under MIT License.
*
* This source file is subject to the MIT License that is
* bundled with this package in the LICENSE file.
*
* @package evias/nem-nodejs-bot
* @author Grégory Saive <[email protected]>
* @license MIT License
* @copyright (c) 2017, Grégory Saive <[email protected]>
* @link http://github.com/evias/nem-nodejs-bot
*/
var path = require('path'),
SecureConf = require("secure-conf"),
fs = require("fs"),
pw = require("pw");
var environment = process.env["APP_ENV"] || "development";
// core dependencies
var logger = require('./src/utils/logger.js');
var __smartfilename = path.basename(__filename);
// define a helper to process configuration file encryption
var sconf = new SecureConf();
var encryptConfig = function(pass)
{
var dec = fs.readFileSync("config/bot.json");
var enc = sconf.encryptContent(dec, pass);
if (enc === undefined) {
logger.error(__smartfilename, __line, "Configuration file config/bot.json could not be encrypted.");
logger.warn(__smartfilename, __line, "NEM Bot now aborting.");
return false;
}
fs.writeFileSync("config/bot.json.enc", enc);
if (environment == "production")
// don't delete in development mode
fs.unlink("config/bot.json");
return true;
};
/**
* Delayed Bot execution. This function STARTS the bot and will only
* work in case the encrypted configuration file exists AND can be
* decrypted with the provided password (or asks password in console.)
*
* On heroku, as it is not possible to enter data in the console, the password
* must be set in the ENCRYPT_PASS "Config Variable" of your Heroku app which
* you can set under the "Settings" tab.
*/
var startBot = function(pass)
{
if (fs.existsSync("config/bot.json.enc")) {
// Only start the bot in case the file is found
// and can be decrypted.
var enc = process.env["ENCRYPT_DATA"] || fs.readFileSync("config/bot.json.enc", {encoding: "utf8"});
var dec = sconf.decryptContent(enc, pass);
if (dec === undefined) {
logger.error(__smartfilename, __line, "Configuration file config/bot.json could not be decrypted.");
logger.warn(__smartfilename, __line, "NEM Bot now aborting.");
}
else {
try {
var config = JSON.parse(dec);
try {
var server = require("./src/server.js");
// define a helper to get the blockchain service
var blockchain = require('./src/blockchain/service.js');
var chainDataLayer = new blockchain.service(config, logger);
var bot = new server.NEMBot(config, logger, chainDataLayer);
}
catch (e) {
logger.error(__smartfilename, __line, "Error with NEM Bot Server: " + e);
logger.warn(__smartfilename, __line, "NEM Bot now aborting.");
}
}
catch (e) {
logger.error(__smartfilename, __line, "Error with NEM Bot configuration. Invalid encryption password: " + e);
logger.warn(__smartfilename, __line, "NEM Bot now aborting.");
}
}
}
};
/**
* This Bot will only start serving its API when the configuration
* file is encrypted and can be decrypted.
*
* In case the configuration file is not encrypted yet, it will be encrypted
* and the original file will be deleted.
*/
var pass = process.env["ENCRYPT_PASS"] || "";
if (typeof pass == 'undefined' || ! pass.length) {
// get enc-/dec-rypt password from console
if (! fs.existsSync("config/bot.json.enc")) {
// encrypted configuration file not yet created
console.log("Please enter a password for your NEMBot (and save it somewhere safe): ");
pw(function(password) {
encryptConfig(password);
startBot(password);
});
}
else {
// encrypted file exists, ask password for decryption
console.log("Please enter your NEMBot's password: ");
pw(function(password) {
startBot(password);
});
}
}
else {
// use environment variable password
if (! fs.existsSync("config/bot.json.enc"))
// encrypted file must be created
encryptConfig(pass);
startBot(pass);
}