-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
110 lines (93 loc) · 3.03 KB
/
index.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
require('dotenv').config();
const util = require('util');
const FeedParser = require('feedparser');
const fetch = require('node-fetch');
const functions = require('./lib/functions.js');
const tls = require('tls');
const fs = require('fs');
const url = process.env.REMOTE_SERVER_URL;
const sslCert = process.env.REMOTE_SSL_USER_CERTIFICATE;
const sslKey = process.env.REMOTE_SSL_USER_KEY;
if (!functions.checkFile(sslCert)) process.exit();
if (!functions.checkFile(sslKey)) process.exit();
let intervalSecs = (typeof process.env.PULL_INTERVAL !== 'undefined') ? process.env.PULL_INTERVAL : 300;
if (intervalSecs < 300) intervalSecs = 300;
const logCot = (typeof process.env.LOGCOT !== 'undefined') ? (process.env.LOGCOT == "true") : false;
var interval = intervalSecs * 1000;
process.env.TZ = 'UTC';
const run = () => {
const urlMatch = url.match(/^ssl:\/\/(.+):([0-9]+)/)
if (!urlMatch) return
const options = {
host: urlMatch[1],
port: urlMatch[2],
cert: fs.readFileSync(sslCert),
key: fs.readFileSync(sslKey),
rejectUnauthorized: false
}
const client = tls.connect(options, () => {
if (client.authorized) {
console.log("Connection authorized by a Certificate Authority.")
} else {
console.log("Connection not authorized: " + client.authorizationError + " - ignoring")
}
heartbeat();
pullandfeed();
})
client.on('data', (data) => {
if (logCot === true) {
//console.log(data.toString());
}
})
client.on('error', (err) => {
console.error(`Could not connect to SSL host ${url}`);
console.error(err);
process.exit();
})
client.on('close', () => {
console.info(`Connection to SSL host ${url} closed`)
process.exit();
})
function heartbeat() {
client.write(functions.heartbeatcot(intervalSecs));
if (logCot === true) {
console.log(functions.heartbeatcot(intervalSecs));
console.log('-----')
}
}
function pullandfeed() {
heartbeat();
var feedparser = new FeedParser();
var req = fetch('https://deepstatemap.live/api/history/last',
{
headers: {
"Accept":"application/json, text/javascript, */*; q=0.01",
"Alt-Used":"deepstatemap.live",
"User-Agent": "DeepState TAK feeder",
"Connection": "close",
"Host": "deepstatemap.live",
"Referer": "https://deepstatemap.live/",
"X-Requested-With": "XMLHttpRequest"
}
})
.then((response) => response.json())
.then((data) => {
//console.log(util.inspect(data, false, null, true));
let ptime = data.id;
for (var no in data["map"]["features"]) {
let item = data["map"]["features"][no];
if (item["geometry"]["type"] == "Point") {
let cot = functions.deepstate2cot(item, intervalSecs, ptime);
if (cot != null) {
if (logCot === true) console.log(cot);
client.write(cot);
}
}
}
});
setTimeout(pullandfeed, interval);
};
};
if (url && sslCert && sslKey) {
run();
}