-
Notifications
You must be signed in to change notification settings - Fork 4
/
snapstart.js
executable file
·198 lines (174 loc) · 7.22 KB
/
snapstart.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
#!/usr/bin/env node
require("./start.js");
return;
// DEBUG
//process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var path = require("path");
var os = require('os');
var url = require('url');
//var request = require("request");
var request = require("./lib/WebRequest");
var exec = require('child_process').exec;
var debug = process.execArgv.find(function (e) { return e.startsWith('--debug'); }) !== undefined;
var MAXIMEITRYCOUNT = 3;
var currentImieTryCount = 0;
// Load settings
var SettingsHelper = require("./lib/SettingsHelper.js");
var settingsHelper = new SettingsHelper();
var packagePath = settingsHelper.nodePackagePath;
process.env.NODE_PATH = process.env.NODE_PATH ? process.env.NODE_PATH + ":" + packagePath : packagePath;
process.env.HOME = os.userInfo().homedir;
require('app-module-path').addPath(packagePath);
require('module').globalPaths.push(packagePath);
var snapLoginHandler = new SnapLoginHandler(settingsHelper);
// If user hasn't logged in before we'll try to get the IMEI id
snapLoginHandler.start(settingsHelper.isFirstStart());
function SnapLoginHandler(settingsHelper) {
console.log("STARTSNAP: SnapLoginHandler started");
var self = this;
this.interval = null;
var count = 0;
this.start = function (isFirstStart) {
this.interval = setInterval(function () {
if (isFirstStart && (currentImieTryCount < MAXIMEITRYCOUNT)) {
tryGetIMEI(function (imei) {
if (imei) {
clearInterval(self.interval);
self.interval = setInterval(function () {
tryLoginUsingICCID(imei, function (done) {
if (done) {
clearInterval(self.interval);
console.log("done");
}
});
}, 30000);
}
else {
currentImieTryCount++;
}
});
}
else if (isFirstStart && (currentImieTryCount >= MAXIMEITRYCOUNT)) {
clearInterval(self.interval);
console.log();
console.log("Was not able to get the IMEI id :(");
console.log("Let's try logging in using whitelist instead...");
console.log();
process.argv.push("-w");
require("./start.js");
return;
}
else {
pingBeforeStart(function (online) {
if (online) {
clearInterval(self.interval);
console.log("STARTSNAP: Online");
try{
require("./start.js");
return;
}
catch(ex){
console.log("Failed to call start.js. " + ex);
setTimeout(() => {
try{
console.log("Retrying to require start.js");
require("./start.js");
return;
}
catch(ex){
console.log("Failed again :(" + ex);
return;
}
}, 1000);
}
}
})
}
}, 10000);
};
function pingBeforeStart(callback) {
var hubUri = url.parse(settingsHelper.settings.hubUri);
var uri = 'https://' + hubUri.host;
console.log("STARTSNAP: pinging..." + uri);
request.post({ url: uri, timeout: 5000 }, function (err, response, body) {
if (err) {
// Offline mode...
if ((err.code === "ECONNREFUSED" ||
err.code === "EACCES" ||
err.code === "ENOTFOUND") &&
settingsHelper.settings.policies &&
settingsHelper.settings.policies.disconnectPolicy.offlineMode) {
console.log('Starting snap in offline mode');
require("./start.js");
callback(true);
}
else {
console.error("STARTSNAP: ERROR: error: " + err);
callback();
return;
}
}
else if (response.statusCode !== 200) {
console.error("STARTSNAP: FAILED: response: " + response.statusCode);
callback();
return;
}
else {
console.log("STARTSNAP: Got response from microServiceBus.com. All good...");
require("./start.js");
callback(true);
}
})
}
function tryGetIMEI(callback) {
exec("mmcli -m 0|grep -oE \"imei: '(.*)'\"|sed 's/imei: //g'|sed \"s/'//g\"", function (error, stdout, stderr) {
console.log('STARTSNAP: imei: ' + stdout);
if (error) {
console.log("STARTSNAP: Unable to get the IMEI id");
console.log('STARTSNAP: ERROR: ' + error);
callback();
}
else {
imei = stdout;
callback(imei);
}
});
}
function tryLoginUsingICCID(imei, callback) {
let host = process.env.MSB_HOST ? process.env.MSB_HOST: url.parse(settingsHelper.settings.hubUri).host;
let uri = `https://${host}/jasper/signInUsingIMEI?imeiId=${imei}&hostname=${os.hostname()}`;
console.log("STARTSNAP: calling jasper service..." + uri);
const options = { url: uri, maxAttempts: 3,retryDelay: 5000, method: "POST" };
request.post(options, function (err, response, body) {
if (err) {
console.error("STARTSNAP: ERROR: error: " + err);
callback();
return;
}
else if (response.statusCode === 302) {
settingsHelper.settings.hubUri = "wss://" + url.parse(response.headers.location).host;
console.log('REDIRECTED TO: ' + settingsHelper.settings.hubUri);
settingsHelper.save();
callback();
return;
}
else if (response.statusCode !== 200) {
console.error("STARTSNAP: FAILED: response: " + response.statusCode);
callback();
return;
}
else {
console.log("STARTSNAP: Got settings from microServiceBus.com. All good...");
var settings = JSON.parse(body);
settingsHelper.settings.id = settings.id;
settingsHelper.settings.nodeName = settings.nodeName;
settingsHelper.settings.organizationId = settings.organizationId;
settingsHelper.settings.sas = settings.sas;
settingsHelper.save();
console.log("STARTSNAP: Saved settings");
require("./start.js");
callback(true);
}
})
}
}