-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
164 lines (146 loc) · 4 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
import * as dotenv from 'dotenv'
import fs, { access } from 'fs';
import path from 'path';
import request from 'request';
import WebSocket from 'ws';
import { parseString } from 'xml2js';
import builder from 'xmlbuilder';
import { setTimeout } from "timers/promises";
dotenv.config()
let accessKey = process.env.ACCESSKEY;
let secret = process.env.SECRETKEY;
let accessScope = process.env.SCOPE;
let streamingURL = process.env.STREAMING;
const KML_DIRECTORY = 'kml-files';
const SEND_TIMEOUT = 2000;
async function parseAndSendKmlData(socket) {
try {
const files = await fs.promises.readdir(KML_DIRECTORY);
for (const file of files) {
const filePath = path.join(KML_DIRECTORY, file);
const kmlString = await fs.promises.readFile(filePath, 'utf8');
let result;
try {
result = await new Promise((resolve, reject) => {
parseString(kmlString, (err, result) => {
if (err) reject(err);
else resolve(result);
});
});
} catch (err) {
console.error(err);
continue;
}
if (!result.kml.Document[0]?.Placemark) {
continue;
}
for (const placemark of result.kml.Document[0].Placemark) {
const cotData = {
name: placemark?.name?.[0]?.trim(),
lon: placemark?.Point?.[0]?.coordinates?.[0]?.trim()?.split(",")?.[0],
lat: placemark?.Point?.[0]?.coordinates?.[0]?.trim()?.split(",")?.[1],
};
await setTimeout(SEND_TIMEOUT);
createCotWithBuilder(cotData, (cotMsg) => {
if (socket) {
socket.send(cotMsg);
}
});
}
}
} catch (err) {
console.error(err);
}
}
function createCotWithBuilder(cotData, callback) {
var start = new Date();
var stale = new Date();
stale.setMinutes(stale.getMinutes() + 2);
callback(builder.create({
event: {
'@uid': `KML SENDER - ${Date.now()}`,
'@version': '2.0',
'@type': 'a-f-G-U-C-I',
'@how': 'h-e',
'@start': start.toISOString(),
'@time': start.toISOString(),
'@stale': stale.toISOString(),
point: {
'@lat': cotData.lat,
'@lon': cotData.lon,
'@hae': Math.random() * 10000,
'@le': '9999999.0',
'@ce': '9999999.0'
},
detail: {
contact: {
'@callsign': `KML SENDER - ${Date.now()}`,
'@endpoint': "1",
},
__group: {
'@name': 'Red',
'@role': 'Victim'
},
track: {
'@course': '0',
'@speed': '0'
},
_takconnect_: {
'@uid': `KML SENDER - ${Date.now()}`,
'@processCount': '1',
'@ackRequired': '0',
'@Subdomain': `KML SENDER - ${Date.now()}`,
'@TakGroup': `KML SENDER - ${Date.now()}`
}
},
}
}, { encoding: 'UTF-8', standalone: 'yes' }).end());
}
async function connectToSitX(callback) {
let accessToken = null;
let url = `https://pardemo.parteamconnect.com/api/v1/client_auth/token`;
let scope = accessScope;
let form = {
grant_type: "client_credentials",
client_id: accessKey,
client_secret: secret,
scope: scope
};
let options = {
url: url,
form: form,
json: true
};
await request.post(options,
function (err, response, body) {
if (err) {
callback(err, null);
} else {
accessToken = body['access_token'];
callback(null, accessToken);
};
}
);
}
async function go() {
var token = "";
var socket = null;
await connectToSitX(async (err, accessToken) => {
if (!err) {
token = accessToken;
let url = streamingURL;
let authHeader = `Bearer ${token}`;
socket = new WebSocket(url, [], { 'headers': { 'Authorization': authHeader } });
socket.on('connection', (client) => {
client.send('hello');
})
socket.on('connect', (client) => {
client.send('hello');
})
socket.on('open', () => {
parseAndSendKmlData(socket);
})
}
});
}
go();