This repository has been archived by the owner on Apr 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
engine.js
138 lines (118 loc) · 3.61 KB
/
engine.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
'use strict';
const fs = require('fs');
const net = require('net');
const logger = require('kaho');
const missive = require('missive');
const engine = require('./engine/engine');
const mode = require('./engine/mode');
const communicator = require('./engine/communicator');
mode.setStandaloneEngineMode(true);
const encode = missive.encode();
communicator.setEncoder(encode);
const noop = () => {};
// parse command line args
const argv = require('yargs')
.usage('Usage: $0 <command> [options]')
.describe('ip', 'IP address of server')
.alias('ip', 'i')
.default('ip', '127.0.0.1')
.describe('port', 'Port number of server')
.alias('port', 'p')
.describe('key', 'path to secure key')
.alias('key', 'k')
.help('help')
.alias('help', 'h')
.example(
'$0 -i 192.168.1.23 -p 8000 -k ~/my-key.txt',
'Connect to server on 192.168.1.23:8000 with given key location'
)
.demandOption(['i', 'p', 'k']).argv;
const secureKey = fs.readFileSync(argv.k, 'utf-8').trim();
// channel property validator
const isValid = channel => {
const validNameDescription =
typeof channel.name === 'string' && typeof channel.description === 'string';
const validSourceAddress = typeof channel.sourceAddress === 'string';
const validSourcePort =
typeof channel.sourcePort === 'number' &&
channel.sourcePort > 0 &&
channel.sourcePort < 65535;
const validHeaderSize =
typeof channel.headerSize === 'number' && channel.headerSize >= 0;
const validISSC = typeof channel.isSmartSourceClient === 'boolean';
const validSplitterMonitorPort =
(channel.splitterPort > 0 &&
channel.splitterPort < 65535 &&
typeof channel.splitterPort === 'number' &&
channel.monitorPort > 0 &&
channel.monitorPort < 65535 &&
typeof channel.monitorPort === 'number') ||
(typeof channel.splitterPort === 'undefined' &&
typeof channel.monitorPort === 'undefined');
const validRest =
channel.isSmartSourceClient ||
(!channel.isSmartSourceClient &&
net.isIP(channel.sourceAddress) !== 0 &&
channel.sourcePort > 0 &&
channel.sourcePort < 65535 &&
typeof channel.sourcePort === 'number');
return (
validNameDescription &&
validSourceAddress &&
validSourcePort &&
validHeaderSize &&
validISSC &&
validSplitterMonitorPort &&
validRest
);
};
const reqHandler = async req => {
let result = false;
let payload = [];
switch (req.type) {
case 'add':
if (isValid(req.channel)) {
try {
payload = await engine.launch(req.channel);
result = true;
} catch (e) {
result = false;
}
}
encode.write({ type: 'result', url: req.channel.url, result, payload });
break;
case 'remove':
engine.stop(req.url);
break;
}
};
let authenticate = req => {
if (req.type === 'auth' && req.key === secureKey) {
logger('SUCCESS', 'Authentication successfully with supplied key');
} else {
logger('ERROR', 'Supplied key do not match. Exiting!');
process.exit(1);
}
authenticate = noop;
};
const msgHandler = req => {
// runs only 1 time
authenticate(req);
// runs each time
reqHandler(req);
};
// connect to crossroads server
const client = net.createConnection({ host: argv.h, port: argv.p });
logger('SUCCESS', 'Connection established with Crossroads server');
encode.pipe(client);
client
.pipe(missive.parse())
.on('message', msgHandler)
.on('close', () => {
logger('ERROR', 'Crossroads server closed connection');
process.exit(1);
})
.on('error', e => {
logger('ERROR', 'Error with crossroads connection', e);
process.exit(1);
});