This repository has been archived by the owner on Jul 18, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
app.js
298 lines (262 loc) · 9.42 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
const bodyParser = require('koa-bodyparser');
const chalk = require('chalk');
const http = require('http');
const Integrations = require('@sentry/integrations');
const IO = require('socket.io');
const Koa = require('koa');
const staticCache = require('koa-static-cache');
const program = require('commander');
const Router = require('koa-router');
const send = require('koa-send');
const Sentry = require('@sentry/node');
const { flatten } = require('lodash');
const { arch, platform, release } = require('os');
const Config = require('./lib/config');
const Dashboard = require('./lib/cli-dashboard');
const database = require('./models');
const eventBus = require('./lib/services/event-bus');
const latestVersionService = require('./lib/services/latest-version-service');
const selfUpdateService = require('./lib/services/self-update-service');
const logger = require('./lib/services/logger');
const Proxy = require('./lib/proxy');
const store = require('./lib/services/store');
const mailService = require('./lib/services/mail-service');
const profitabilityService = require('./lib/services/profitability-service');
const version = require('./lib/version');
const usageStatisticsService = require('./lib/services/usage-statistics-service');
const foxyPoolGateway = require('./lib/services/foxy-pool-gateway');
const startupMessage = require('./lib/startup-message');
const {
HttpSinglePortTransport,
HttpMultiplePortsTransport,
SocketIoTransport,
} = require('./lib/transports');
program
.version(version)
.option('--config <config.yaml>', 'The custom config.yaml file path')
.option('--db <db.sqlite>', 'The custom db.sqlite file path')
.option('--live', 'Show a live dashboard with stats')
.option('--no-colors', 'Do not use colors in the cli output')
.parse(process.argv);
const programOptions = program.opts();
if (!programOptions.colors) {
store.setUseColors(false);
}
startupMessage();
if (programOptions.config) {
store.setConfigFilePath(programOptions.config);
}
if (programOptions.db) {
store.setDbFilePath(programOptions.db);
}
let dashboard = null;
if (programOptions.live) {
store.setUseLiveDashboard(true);
dashboard = new Dashboard();
dashboard.start();
}
const config = new Config();
if (dashboard && config.config.dashboardLogLines) {
dashboard.maxLogLines = config.config.dashboardLogLines;
}
Sentry.init({
dsn: 'https://[email protected]/1402474',
release: `Foxy-Proxy@${version}`,
integrations: [
new Integrations.Dedupe(),
new Integrations.ExtraErrorData(),
new Integrations.Transaction(),
],
ignoreErrors: [
/ENOSYS/,
/SequelizeUniqueConstraintError/,
/SQLITE_BUSY/,
/Please install sqlite3 package manually/,
],
});
Sentry.configureScope((scope) => {
scope.setTag('os.arch', arch());
scope.setTag('os.platform', platform());
scope.setTag('os.release', release());
});
process.on('unhandledRejection', (err) => {
eventBus.publish('log/error', `Error: ${err.message}`);
});
process.on('uncaughtException', (err) => {
eventBus.publish('log/error', `Error: ${err.message}`);
});
store.logging.level = config.logLevel || store.logging.level;
store.logging.dir = config.logDir;
store.logging.maxFiles = config.logMaxFiles;
if (config.logToFile) {
logger.enableFileLogging();
}
store.setIsInstalledGlobally(!!config.config.isInstalledGlobally);
store.setMailSettings(config.config.mail);
mailService.init();
const proxyConfigs = config.proxies
.map(proxyConfig => JSON.parse(JSON.stringify(proxyConfig)))
.map((proxyConfig, index) => ({
...proxyConfig,
index,
}))
.filter(proxyConfig => !proxyConfig.disabled);
(async () => {
// sync() creates missing tables
await database().sequelize.sync({
force: false, // Do not drop tables
alter: true,
});
const app = new Koa();
app.on('error', err => {
eventBus.publish('log/error', `Error: ${err.message}`);
});
app.use(staticCache(`${__dirname}/app/dist`, {
maxAge: 365 * 24 * 60 * 60, // 1 year
files: {
'/index.html' : {
maxAge: 0,
},
},
}));
const router = new Router();
app.use(bodyParser());
const proxiesWithUpstreams = proxyConfigs.filter(proxyConfig => proxyConfig.upstreams);
if (proxiesWithUpstreams.length === 0) {
eventBus.publish('log/error', 'No proxies with upstreams configured, exiting ..');
process.exit(1);
}
if (proxiesWithUpstreams.some(proxyConfig => proxyConfig.useProfitability)) {
await profitabilityService.init(config.useEcoBlockRewardsForProfitability);
}
const coins = [...new Set(flatten(proxiesWithUpstreams.map((proxyConfig) =>
proxyConfig.upstreams
.filter(upstreamConfig => !upstreamConfig.disabled)
.filter(upstreamConfig => upstreamConfig.type === 'foxypool' && upstreamConfig.coin && !upstreamConfig.url)
.map(upstreamConfig => upstreamConfig.coin.toUpperCase())
)))];
if (coins.length > 0) {
foxyPoolGateway.coins = coins;
await foxyPoolGateway.init({ allowLongPolling: config.allowLongPolling });
}
const proxies = await Promise.all(proxiesWithUpstreams.map(async (proxyConfig) => {
const proxy = new Proxy(proxyConfig);
await proxy.init();
return proxy;
}));
if (config.transports.indexOf('http') !== -1) {
let transport;
if (config.useMultiplePorts) {
transport = new HttpMultiplePortsTransport(config.listenHost, config.listenPort);
} else {
transport = new HttpSinglePortTransport(router, config.listenAddr);
}
transport.addProxies(proxies);
}
app.use(router.routes());
app.use(router.allowedMethods());
// redirect everything else to index.html
app.use(async ctx => {
await send(ctx, 'app/dist/index.html', {
root: __dirname,
});
});
const server = http.createServer(app.callback());
const io = IO(server, {
cors: {
origin: true,
methods: ["GET", "POST"],
},
allowEIO3: true,
});
if (config.transports.indexOf('socket.io') !== -1) {
const transport = new SocketIoTransport(io, config.listenAddr);
transport.addProxies(proxies);
}
server.on('error', (err) => {
eventBus.publish('log/error', `Error: ${err.message}`);
if (err.code === 'EADDRINUSE' || err.code === 'EACCES') {
process.exit(1);
}
});
server.listen(config.listenPort, config.listenHost);
const authenticatedClients = {};
const webUiSocketIo = io.of('web-ui');
webUiSocketIo.on('connection', async client => {
let authenticated = !config.webAuth; // Without any auth set, allow all
if (authenticated) {
authenticatedClients[client.id] = client;
}
client.on('authenticate', ({username, passHash}, cb) => {
if (authenticated) {
cb(true);
return;
}
if (username === config.webAuth.username && passHash === config.webAuth.passHash) {
authenticatedClients[client.id] = client;
authenticated = true;
}
cb(authenticated);
});
client.on('stats/init', async (cb) => {
if (!authenticated) {
client.emit('unauthorized');
return;
}
const stats = await Promise.all(proxies.map((proxy) => proxy.getStats()));
cb(stats);
});
client.on('version/info', (cb) => cb({
latestVersion: latestVersionService.getLatestVersion(),
changelog: latestVersionService.getChangelog(),
runningVersion: version,
}));
client.on('version/update', () => {
if (!authenticated) {
client.emit('unauthorized');
return;
}
eventBus.publish('version/update');
});
client.on('disconnect', () => {
if (!authenticatedClients[client.id]) {
return;
}
delete authenticatedClients[client.id];
});
});
eventBus.subscribe('stats/proxy', (proxyName, proxyStats) => {
const clients = Object.keys(authenticatedClients).map(id => authenticatedClients[id]);
clients.forEach(client => client.emit('stats/proxy', proxyName, proxyStats));
});
eventBus.subscribe('stats/current-round', (upstreamName, currentRoundStats) => {
const clients = Object.keys(authenticatedClients).map(id => authenticatedClients[id]);
clients.forEach(client => client.emit('stats/current-round', upstreamName, currentRoundStats));
});
eventBus.subscribe('stats/connection-stats', (upstreamName, connectionStats) => {
const clients = Object.keys(authenticatedClients).map(id => authenticatedClients[id]);
clients.forEach(client => client.emit('stats/connection-stats', upstreamName, connectionStats));
});
eventBus.subscribe('stats/historical', (upstreamName, historicalStats) => {
const clients = Object.keys(authenticatedClients).map(id => authenticatedClients[id]);
clients.forEach(client => client.emit('stats/historical', upstreamName, historicalStats));
});
store.setProxies(proxies);
const startupLine = `Foxy-Proxy ${version} initialized. The WebUI is reachable on http://${config.listenAddr}`;
eventBus.publish('log/info', store.getUseColors() ? chalk.green(startupLine) : startupLine);
if (dashboard) {
await dashboard.initStats();
}
eventBus.subscribe('version/new', newVersion => {
const newVersionLine = `Newer version ${newVersion} is available!`;
eventBus.publish('log/info', store.getUseColors() ? chalk.magentaBright(newVersionLine) : newVersionLine);
if (!config.config.automaticUpdates) {
return;
}
eventBus.publish('version/update');
});
await latestVersionService.init();
if (!config.config.disableAnonymousStatistics) {
await usageStatisticsService.init();
}
})();