This repository has been archived by the owner on Oct 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
391 lines (340 loc) · 11.9 KB
/
server.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
const bodyParser = require('body-parser');
const express = require('express');
const https = require('https');
const kill = require('tree-kill');
const proxy = require('http-proxy-middleware');
const puppeteer = require('puppeteer');
const { spawn } = require('child_process');
const twilio = require('twilio');
// TODO: for now the proxy forwarding doesn't seem to work that well
// the ib gateway rejects/doesn't respond to some of those requests
// have to debug further, but maybe it has to do with header info?
// for now just communicating directly to the ib gateway
// consts from environment
const LOG_LEVEL = process.env.IB_GATEWAY_LOG_LEVEL || 'info';
const IB_GATEWAY_SERVICE_PORT = process.env.IB_GATEWAY_SERVICE_PORT || 5050;
const IB_GATEWAY_BIN = process.env.IB_GATEWAY_BIN;
const IB_GATEWAY_CONF = process.env.IB_GATEWAY_CONF;
const IB_GATEWAY_DOMAIN = process.env.IB_GATEWAY_DOMAIN || 'localhost';
const IB_GATEWAY_PORT = process.env.IB_GATEWAY_PORT || 5000;
const IB_GATEWAY_SCHEME = process.env.IB_GATEWAY_SCHEME || 'https://';
const IB_GATEWAY = IB_GATEWAY_SCHEME + IB_GATEWAY_DOMAIN + ':' + IB_GATEWAY_PORT;
const TWILIO_ACCOUNT_SID = process.env.IB_AUTH_TWILIO_ACCOUNT_SID;
const TWILIO_AUTH_TOKEN = process.env.IB_AUTH_TWILIO_AUTH_TOKEN;
// configure app to use bodyParser
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// API ROUTES
// =============================================================================
const router = express.Router();
router.route('/service')
// POST
// starts the IB gateway
.post((req, res) => {
startIBGateway().then(() => {
startLockResolve();
startLock = null;
res.status(200).json('OK');
}).catch((err) => {
startLockReject(err);
startLock = null;
res.status(400).json('Error launching gateway: ' + err)
});
})
// PUT
// authenticates the ib gateway using the credentials passed in
// body: { username: <USERNAME>, password: <PASSWORD> }
.put((req, res) => {
doAuth(req.body.username, req.body.password).then((data) => {
authLockResolve(data);
authLock = null;
res.status(200).json(data);
}).catch((err) => {
authLockReject && authLockReject(err);
authLock = null;
res.status(400).json('Error authenticating: ' + err);
});
})
// DELETE
// stops the IB gateway
.delete((req, res) => {
stopIBGateway().then(() => {
res.status(200).json('OK');
}).catch((err) => {
res.status(400).json('Error stopping gateway: ' + err);
});
});
// // LOG REQUESTS
app.use((req, res, next) => {
console.log(`IB GATEWAY SERVICE: ${req.method} ${req.originalUrl}`);
next();
});
// REGISTER OUR ROUTES -------------------------------
app.use('/api', router);
// REGISTER PROXY ------------------------------------
// const proxyOptions = {
// target: IB_GATEWAY,
// ws: true, // proxy websockets
// secure: false, // don't verify ssl certs
// pathRewrite: {
// '^/api/gateway': '', // remove base api path
// },
// logLevel: LOG_LEVEL,
// };
// app.use('/api/gateway', proxy(proxyOptions));
// START THE SERVICE
// =============================================================================
app.listen(IB_GATEWAY_SERVICE_PORT);
console.log('Magic happens on PORT:' + IB_GATEWAY_SERVICE_PORT);
// PUPPETEER-CHROME INTERACTION
// =============================================================================
let authLock;
let authLockResolve;
let authLockReject;
let browser;
async function handleTwoFactor(page, startDate) {
const codeInputSelector = '#chlginput';
await page.waitForSelector(codeInputSelector);
console.log('Two factor authentication is required');
const client = twilio(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN);
async function getCode() {
const messages = await client.messages.list({
dateSentAfter: startDate,
limit: 1,
});
if (messages.length !== 1) {
throw new Error(`Twilio returned ${messages.length} messages`);
}
const message = messages[0];
console.log('Retrieved message from twilio');
const match = message.body.match(/\d{6}/);
if (match === null) {
throw new Error(`Could not extract code from message body: "${message.body}"`)
}
return match[0];
}
let err;
for (let i = 0; i <= 5; i++) {
console.log(`Checking for auth SMS in ${i}s`)
await new Promise(resolve => setTimeout(resolve, i * 1000));
try {
const code = await getCode();
await page.type(codeInputSelector, code);
await page.click('#submitForm');
return page.waitForNavigation();
}
catch (e) {
err = e;
}
}
// could not complete two factor after retries
throw new Error(`Two factor authentication could not complete in time due to ${err}`);
}
async function doAuth(username, password) {
if (!gateway) {
throw new Error('IB gateway needs to be first started before trying to login');
}
if (authLock) {
// another request already started authentication, just wait for that
console.log('Already an ongoing auth request, waiting on that');
return authLock;
}
// grab the lock
authLock = new Promise((resolve, reject) => {
authLockResolve = resolve;
authLockReject = reject;
});
authLock.catch(() => {});
browser = await puppeteer.launch({
executablePath: 'google-chrome-unstable', // uncomment to use chrome
headless: true,
args: ['--no-sandbox'],
ignoreHTTPSErrors: true, // ssl cert not valid for ib gateway
});
let successMessage = '';
try {
// Open login page
const startDate = new Date();
const page = await browser.newPage();
await page.goto(IB_GATEWAY);
// Submit credentials
await page.type('#user_name', username);
await page.type('#password', password);
await page.click('#submitForm');
// Wait for redirect to happen or for two factor to complete
await Promise.race([
page.waitForNavigation(),
handleTwoFactor(page, startDate),
]);
// Verify
successMessage = await page.evaluate(() => document.body.innerText);
}
finally {
await browser.close();
browser = null;
}
if (successMessage !== 'Client login succeeds') {
throw Error('Login could not be verified! msg: ' + successMessage);
}
// Wait for gateway to be authenticated
const isGatewayAuthenticated = () => {
return new Promise((resolve, reject) => {
console.log('checking if gateway is authenticated')
const options = {
hostname: IB_GATEWAY_DOMAIN,
port: IB_GATEWAY_PORT,
path: '/v1/api/iserver/auth/status',
method: 'GET',
rejectUnauthorized: false, // disable SSL check
};
const req = https.request(options, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
let body='';
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', () => {
if (res.statusCode >= 200 && res.statusCode < 400) {
const data = JSON.parse(body);
console.log(body);
if (data.authenticated) {
resolve(data);
}
else {
reject(new Error(body));
}
}
else {
reject(new Error(res.statusCode));
}
});
}).on('error', (e) => {
console.error(e);
reject(e);
});
req.setHeader('user-agent', 'ib-gateway-service');
req.setHeader('content-type', 'application/json');
req.setHeader('accept', 'application/json');
req.end();
});
}
let err;
for (let i = 0; i < 10; i++) {
console.log(`Gateway is not authenticated yet, checking again in ${i}s`)
await new Promise(resolve => setTimeout(resolve, i * 1000));
try {
return await isGatewayAuthenticated();
}
catch (e) {
err = e;
}
}
throw new Error(`Gateway did not authenticate in time due to ${err}`);
}
// IB GATEWAY
// =============================================================================
let gateway = null;
function log(msg) {
console.log('stdout: ' + msg);
}
function warn(msg) {
console.warn('stdout: ' + msg);
}
let startLock;
let startLockResolve;
let startLockReject;
async function startIBGateway() {
if (gateway) {
// gateway is already running
return;
}
if (!IB_GATEWAY_BIN || !IB_GATEWAY_CONF) {
throw new Error('Missing bin and/or conf for ib gateway');
}
if (startLock) {
// another request already started the gateway, just wait for that
console.log('Already an ongoing start request, waiting on that');
return startLock;
}
// grab the lock
startLock = new Promise((resolve, reject) => {
startLockResolve = resolve;
startLockReject = reject;
});
startLock.catch(() => {});
console.log('Starting IB Gateway');
gateway = spawn(IB_GATEWAY_BIN, [IB_GATEWAY_CONF]);
gateway.stdout.on('data', log);
gateway.stderr.on('data', warn);
gateway.on('exit', function (code) {
console.log('IB Gateway exited with code ' + code);
gateway = null;
});
const isGatewayUp = () => {
return new Promise((resolve, reject) => {
console.log('checking if gateway is up')
const options = {
hostname: IB_GATEWAY_DOMAIN,
port: IB_GATEWAY_PORT,
method: 'GET',
rejectUnauthorized: false, // disable SSL check
};
https.request(options, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', () => {});
res.on('end', () => {
if (res.statusCode >= 200 && res.statusCode < 400) {
resolve();
}
else {
reject(new Error(res.statusCode));
}
});
}).on('error', (e) => {
console.error(e);
reject(e);
}).end();
});
}
let err;
for (let i = 1; i < 10; i++) {
console.log(`Gateway is not up yet, checking again in ${i}s`)
await new Promise(resolve => setTimeout(resolve, i * 1000));
try {
return await isGatewayUp();
}
catch (e) {
err = e;
}
}
throw new Error(`Gateway did not start up in time due to ${err}`);
}
async function stopIBGateway() {
if (browser) {
// kill the browser too if it's still open
browser.close();
browser = null;
}
if (!gateway) {
// no gateway or it was already killed
return;
}
gateway.stdout.off('data', log);
gateway.stderr.off('data', warn);
return new Promise((resolve, reject) => {
// Kill whole subtree
kill(gateway.pid, 'SIGTERM', (err) => {
if (err) {
reject(err);
}
else {
gateway = null;
console.log('IB Gateway stopped');
resolve();
}
});
});
}