-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
74 lines (59 loc) · 2.54 KB
/
index.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
const server = require('./game_server');
const fs = require('fs');
const assert = require('assert');
const { reportBug } = require('./src/common/util');
const process = require('process');
const path = require('path');
let baseDir = path.dirname(require.main.filename);
if (baseDir.endsWith('src')) {
baseDir = baseDir.substring(0, baseDir.length - 3);
}
const linkHelper = require('./src/util/link-helper');
const { guaranteeCerts, guaranteeDir, authWorkflow, getConfigValue } = require('homegames-common');
const log = process.env.LOGGER_LOCATION ? require(process.env.LOGGER_LOCATION) : { info: (msg) => console.log(msg), error: (msg) => console.error(msg)};
const LINK_ENABLED = getConfigValue('LINK_ENABLED', true);
const HTTPS_ENABLED = getConfigValue('HTTPS_ENABLED', false);
const linkInit = (username) => new Promise((resolve, reject) => {
linkHelper.linkConnect(null, username).then((wsClient) => {
log.info('Initialized connection to homegames.link');
resolve();
}).catch(err => {
log.error('Failed to initialize link', err);
reject();
});
});
const certPathArgs = process.argv.filter(a => a.startsWith('--cert-path=')).map(a => a.replace('--cert-path=', ''));
const usernameArgs = process.argv.filter(a => a.startsWith('--username=')).map(a => a.replace('--username=', ''));
let certPathArg = certPathArgs && certPathArgs.length > 0 ? certPathArgs[0] : null;
let usernameArg = usernameArgs && usernameArgs.length > 0 ? usernameArgs[0] : null;
if (!usernameArg && fs.existsSync(`${baseDir}/.hg_auth/username`)) {
usernameArg = fs.readFileSync(`${baseDir}/.hg_auth/username`).toString();
}
if (HTTPS_ENABLED && fs.existsSync(`${baseDir}/hg-certs`)) {
certPathArg = `${baseDir}/hg-certs`;
}
if (certPathArg) {
process.env.HTTPS_ENABLED = true;
}
if (LINK_ENABLED) {
linkInit(usernameArg).then(() => {
log.info('starting server with link enabled');
server(certPathArg, null, usernameArg);
}).catch(() => {
log.info('encountered error with link connection. starting server with link disabled');
try {
server(certPathArg, null, usernameArg);
} catch (serverErr) {
log.error('Server error: ' + serverErr);
reportBug(`Error starting server: ${serverErr}`);
}
});
} else {
log.info('starting server with link disabled');
try {
server(certPathArg, null, usernameArg);
} catch (serverErr) {
log.error('Server error: ' + serverErr);
reportBug(`Error starting server: ${serverErr}`);
}
}