This repository has been archived by the owner on Dec 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
app.js
executable file
·352 lines (264 loc) · 9.16 KB
/
app.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/env node
'use strict';
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require("express-session");
var config = require("./app/config.js");
var simpleGit = require('simple-git');
var utils = require("./app/utils.js");
var moment = require("moment");
var Decimal = require('decimal.js');
var bitcoinCore = require("bitcoin-core");
var grpc = require("grpc");
var fs = require("fs");
var pug = require("pug");
var momentDurationFormat = require("moment-duration-format");
var coins = require("./app/coins.js");
var request = require("request");
var qrcode = require("qrcode");
var rpcApi = require("./app/rpcApi.js");
var Influx = require("influx");
var baseActionsRouter = require('./routes/baseActionsRouter');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
// ref: https://blog.stigok.com/post/disable-pug-debug-output-with-expressjs-web-app
app.engine('pug', (path, options, fn) => {
options.debug = false;
return pug.__express.call(null, path, options, fn);
});
app.set('view engine', 'pug');
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(session({
secret: config.cookiePassword,
resave: false,
saveUninitialized: false
}));
app.use(express.static(path.join(__dirname, 'public')));
function logNetworkStats() {
if (global.influxdb) {
rpcApi.getNetworkStats().then(function(response) {
var vals = [];
for (var x in response) {
var valX = response[x];
if (x == "total_network_capacity") {
valX = parseInt(valX);
}
vals.push({measurement:("lightning.network." + x), fields:{value:valX}})
}
global.influxdb.writePoints(vals).catch(err => {
console.error(`Error saving data to InfluxDB: ${err.stack}`)
});
});
}
}
app.runOnStartup = function() {
global.config = config;
global.coinConfig = coins[config.coin];
global.coinConfigs = coins;
if (config.credentials.influxdb.active) {
global.influxdb = new Influx.InfluxDB(config.credentials.influxdb);
console.log(`Connected to InfluxDB: ${config.credentials.influxdb.host}:${config.credentials.influxdb.port}/${config.credentials.influxdb.database}`);
}
if (config.donationAddresses) {
var getDonationAddressQrCode = function(coinId) {
qrcode.toDataURL(config.donationAddresses[coinId].address, function(err, url) {
global.donationAddressQrCodeUrls[coinId] = url;
});
};
global.donationAddressQrCodeUrls = {};
config.donationAddresses.coins.forEach(function(item) {
getDonationAddressQrCode(item);
});
}
if (global.sourcecodeVersion == null) {
simpleGit(".").log(["-n 1"], function(err, log) {
global.sourcecodeVersion = log.all[0].hash.substring(0, 10);
global.sourcecodeDate = log.all[0].date.substring(0, "0000-00-00".length);
});
}
if (global.exchangeRates == null) {
utils.refreshExchangeRates();
}
// refresh exchange rate periodically
setInterval(utils.refreshExchangeRates, 30 * 60000);
// connect and pull down the current network description
connectViaRpc().then(function() {
rpcApi.refreshFullNetworkDescription();
rpcApi.refreshLocalChannels();
// refresh periodically
setInterval(rpcApi.refreshFullNetworkDescription, 60000);
setInterval(rpcApi.refreshLocalChannels, 60000);
setInterval(logNetworkStats, 5 * 60000);
});
};
function connectViaRpc() {
return new Promise(function(resolve, reject) {
// Ref: https://github.com/lightningnetwork/lnd/blob/master/docs/grpc/javascript.md
// Due to updated ECDSA generated tls.cert we need to let gprc know that
// we need to use that cipher suite otherwise there will be a handhsake
// error when we communicate with the lnd rpc server.
process.env.GRPC_SSL_CIPHER_SUITES = 'HIGH+ECDSA';
// Lnd admin macaroon is at ~/.lnd/admin.macaroon on Linux and
// ~/Library/Application Support/Lnd/admin.macaroon on Mac
var m = fs.readFileSync(config.credentials.rpc.adminMacaroonFilepath);
var macaroon = m.toString('hex');
// build meta data credentials
var metadata = new grpc.Metadata()
metadata.add('macaroon', macaroon)
var macaroonCreds = grpc.credentials.createFromMetadataGenerator((_args, callback) => {
callback(null, metadata);
});
// Lnd cert is at ~/.lnd/tls.cert on Linux and
// ~/Library/Application Support/Lnd/tls.cert on Mac
var lndCert = fs.readFileSync(config.credentials.rpc.tlsCertFilepath);
var sslCreds = grpc.credentials.createSsl(lndCert);
// combine the cert credentials and the macaroon auth credentials
// such that every call is properly encrypted and authenticated
var credentials = grpc.credentials.combineChannelCredentials(sslCreds, macaroonCreds);
var lnrpcDescriptor = grpc.load("./rpc.proto"); // "rpc.proto"
var lnrpc = lnrpcDescriptor.lnrpc;
// uncomment to print available function of RPC protocol
//console.log(lnrpc);
var lightning = new lnrpc.Lightning(config.credentials.rpc.host + ":" + config.credentials.rpc.port, credentials, {'grpc.max_receive_message_length': 50*1024*1024});
lightning.GetInfo({}, function(err, response) {
if (err) {
console.log("Error connecting to LND @ " + config.credentials.rpc.host + ":" + config.credentials.rpc.port + " via RPC: " + err + ", error json: " + JSON.stringify(err));
}
if (response != null) {
console.log("Connected to LND @ " + config.credentials.rpc.host + ":" + config.credentials.rpc.port + " via RPC.\n\nGetInfo=" + JSON.stringify(response, null, 4));
}
global.lightning = lightning;
resolve();
});
});
}
app.use(function(req, res, next) {
// make session available in templates
res.locals.session = req.session;
if (config.credentials.rpc) {
req.session.host = config.credentials.rpc.host;
req.session.port = config.credentials.rpc.port;
req.session.username = config.credentials.rpc.username;
global.client = new bitcoinCore({
host: config.credentials.rpc.host,
port: config.credentials.rpc.port,
username: config.credentials.rpc.username,
password: config.credentials.rpc.password,
timeout: 5000
});
}
res.locals.admin = false;
req.session.userErrors = [];
res.locals.config = global.config;
res.locals.coinConfig = global.coinConfig;
res.locals.host = req.session.host;
res.locals.port = req.session.port;
// currency format type
if (!req.session.currencyFormatType) {
var cookieValue = req.cookies['user-setting-currencyFormatType'];
if (cookieValue) {
req.session.currencyFormatType = cookieValue;
} else {
req.session.currencyFormatType = "";
}
}
// theme
if (!req.session.uiTheme) {
var cookieValue = req.cookies['user-setting-uiTheme'];
if (cookieValue) {
req.session.uiTheme = cookieValue;
} else {
req.session.uiTheme = "";
}
}
// homepage banner
if (!req.session.hideHomepageBanner) {
var cookieValue = req.cookies['user-setting-hideHomepageBanner'];
if (cookieValue) {
req.session.hideHomepageBanner = cookieValue;
} else {
req.session.hideHomepageBanner = "false";
}
}
res.locals.currencyFormatType = req.session.currencyFormatType;
if (!["/", "/connect"].includes(req.originalUrl)) {
if (utils.redirectToConnectPageIfNeeded(req, res)) {
return;
}
}
if (req.session.userMessage) {
res.locals.userMessage = req.session.userMessage;
if (req.session.userMessageType) {
res.locals.userMessageType = req.session.userMessageType;
} else {
res.locals.userMessageType = "warning";
}
req.session.userMessage = null;
req.session.userMessageType = null;
}
if (req.session.userErrors && req.session.userErrors.length > 0) {
res.locals.userErrors = req.session.userErrors;
req.session.userErrors = null;
}
if (req.session.query) {
res.locals.query = req.session.query;
req.session.query = null;
}
// make some var available to all request
// ex: req.cheeseStr = "cheese";
rpcApi.getFullNetworkDescription().then(function(fnd) {
res.locals.fullNetworkDescription = fnd;
rpcApi.getLocalChannels().then(function(localChannels) {
res.locals.localChannels = localChannels;
next();
}).catch(function(err) {
utils.logError("37921hdasudfgd", err);
next();
});
}).catch(function(err) {
utils.logError("3297rhgdgvsf1", err);
next();
});
});
app.use('/', baseActionsRouter);
/// catch 404 and forwarding to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
/// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
app.locals.moment = moment;
app.locals.Decimal = Decimal;
app.locals.utils = utils;
module.exports = app;