-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
235 lines (192 loc) · 6.54 KB
/
index.ts
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
import EventEmitter from 'node:events';
import tls from 'node:tls';
import CoT from '@tak-ps/node-cot';
import type { TLSSocket } from 'node:tls'
/* eslint-disable no-control-regex */
export const REGEX_CONTROL = /[\u000B-\u001F\u007F-\u009F]/g;
// Match <event .../> or <event> but not <events>
export const REGEX_EVENT = /(<event[ >][\s\S]*?<\/event>)([\s\S]*)/
/**
* Store the TAK Client Certificate for a connection
*/
export interface TAKAuth {
cert: string;
key: string;
}
export interface PartialCoT {
event: string;
remainder: string;
}
export default class TAK extends EventEmitter {
id: number | string;
type: string;
url: URL;
auth: TAKAuth;
open: boolean;
destroyed: boolean;
queue: string[];
writing: boolean;
pingInterval?: ReturnType<typeof setTimeout>;
client?: TLSSocket;
version?: string;
constructor(
id: number | string,
type: string,
url: URL,
auth: TAKAuth
) {
super();
this.id = id;
this.type = type;
this.url = url;
this.auth = auth;
this.writing = false;
this.open = false;
this.destroyed = false;
this.queue = [];
}
static async connect(id: number | string, url: URL, auth: TAKAuth): Promise<TAK> {
const tak = new TAK(id, 'ssl', url, auth);
if (url.protocol === 'ssl:') {
if (!tak.auth.cert) throw new Error('auth.cert required');
if (!tak.auth.key) throw new Error('auth.key required');
return await tak.connect_ssl();
} else {
throw new Error('Unknown TAK Server Protocol');
}
}
connect_ssl(): Promise<TAK> {
return new Promise((resolve) => {
this.destroyed = false;
this.client = tls.connect({
host: this.url.hostname,
port: parseInt(this.url.port),
rejectUnauthorized: false,
cert: this.auth.cert,
key: this.auth.key
});
this.client.setNoDelay();
this.client.on('connect', () => {
console.error(`ok - ${this.id} @ connect:${this.client ? this.client.authorized : 'NO CLIENT'} - ${this.client ? this.client.authorizationError : 'NO CLIENT'}`);
});
this.client.on('secureConnect', () => {
console.error(`ok - ${this.id} @ secure:${this.client ? this.client.authorized : 'NO CLIENT'} - ${this.client ? this.client.authorizationError : 'NO CLIENT'}`);
this.emit('secureConnect')
this.ping();
});
let buff = '';
this.client.on('data', (data: Buffer) => {
// Eventually Parse ProtoBuf
buff = buff + data.toString();
let result = TAK.findCoT(buff);
while (result && result.event) {
try {
const cot = new CoT(result.event);
if (cot.raw.event._attributes.type === 't-x-c-t-r') {
this.open = true;
this.emit('ping');
} else if (
cot.raw.event._attributes.type === 't-x-takp-v'
&& cot.raw.event.detail
&& cot.raw.event.detail.TakControl
&& cot.raw.event.detail.TakControl.TakServerVersionInfo
&& cot.raw.event.detail.TakControl.TakServerVersionInfo._attributes
) {
this.version = cot.raw.event.detail.TakControl.TakServerVersionInfo._attributes.serverVersion;
} else {
this.emit('cot', cot);
}
} catch (e) {
console.error('Error parsing', e, data.toString());
}
buff = result.remainder;
result = TAK.findCoT(buff);
}
}).on('timeout', () => {
if (!this.destroyed) this.emit('timeout');
}).on('error', (err: Error) => {
this.emit('error', err);
}).on('end', () => {
this.open = false;
if (!this.destroyed) this.emit('end');
});
this.pingInterval = setInterval(() => {
this.ping();
}, 5000);
return resolve(this);
});
}
async reconnect() {
if (this.destroyed) {
await this.connect_ssl();
} else {
this.destroy();
await this.connect_ssl();
}
}
destroy() {
this.destroyed = true;
if (this.client) {
this.client.destroy();
}
if (this.pingInterval) {
clearInterval(this.pingInterval)
this.pingInterval = undefined;
}
}
async ping() {
this.write([CoT.ping()]);
}
writer(body: string): Promise<boolean> {
return new Promise((resolve, reject) => {
if (!this.client) return reject(new Error('A Connection Client must first be created before it can be written'));
const res: boolean = this.client.write(body + '\n', () => {
return resolve(res)
});
});
}
async process() {
this.writing = true;
while (this.queue.length) {
const body = this.queue.shift()
if (!body) continue;
await this.writer(body);
}
await this.writer('');
if (this.queue.length) {
process.nextTick(() => {
this.process();
});
} else {
this.writing = false;
}
}
/**
* Write a CoT to the TAK Connection
*
* @param {CoT} cot CoT Object
*/
write(cots: CoT[]) {
for (const cot of cots) {
this.queue.push(cot.to_xml());
}
if (this.queue.length && !this.writing) this.process();
}
write_xml(body: string) {
this.queue.push(body);
if (this.queue.length && !this.writing) this.process();
}
// https://github.com/vidterra/multitak/blob/main/app/lib/helper.js#L4
static findCoT(str: string): null | PartialCoT {
str = str.replace(REGEX_CONTROL, "");
const match = str.match(REGEX_EVENT); // find first CoT
if (!match) return null;
return {
event: match[1],
remainder: match[2],
};
}
}
export {
CoT
}