-
Notifications
You must be signed in to change notification settings - Fork 10
/
sensor_download.ts
111 lines (98 loc) · 3.26 KB
/
sensor_download.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
import { FalconClient, FalconErrorExplain, DomainSensorInstallerV1 } from "./../../src";
let client: FalconClient | null = null;
module.exports = {
onLogin: async function () {
client = new FalconClient({
cloud: "us-1",
clientId: getFormField("clientId"),
clientSecret: getFormField("clientSecret"),
});
await client.sensorDownload
.getCombinedSensorInstallersByQuery()
.catch(async function (err) {
alert("Could not fetch: " + (await FalconErrorExplain(err)));
})
.then((sensors) => {
closeForm();
if (sensors) {
showSensors(sensors.resources);
}
});
},
downloadSensor: async function (id: string, name: string) {
if (client === null) {
return;
}
await client.sensorDownload
.downloadSensorInstallerById(id)
.catch(async function (err) {
alert("Could not download sensor: " + (await FalconErrorExplain(err)));
})
.then((blob) => {
saveAs(blob, name);
});
},
};
function showSensors(sensors: Array<DomainSensorInstallerV1>) {
const heading = "<tr>" + "<th>Description</th>" + "<th>Version</th>" + "<th>OS</th>" + "<th>Download</th></tr>";
const data = sensors
.map((sensor) => {
return (
"<tr><td><div title='Released on " +
sensor.releaseDate +
"'>" +
sensor.description +
"</div></td><td>" +
sensor.version +
"</td><td>" +
sensor.os +
" " +
sensor.osVersion +
"</td><td><button onclick=\"sensor_download.downloadSensor('" +
sensor.sha256 +
"', '" +
sensor.name +
"');\">download (" +
Math.round(sensor.fileSize / 1024 / 1024) +
" MiB)</button>" +
"</td></tr>"
);
})
.join(" ");
show("<table>" + heading + data + "</table>");
}
function show(html: string) {
const main = document.getElementById("loginPopup");
if (main != null) {
main.innerHTML = html;
}
}
function closeForm() {
const login = document.getElementById("popupForm");
if (login != null) {
login.style.display = "none";
}
}
function getFormField(fieldName: string): string {
const fields = document.getElementsByName(fieldName);
if (fields != null && fields.length == 1 && fields[0] instanceof HTMLInputElement) {
return fields[0].value;
}
throw "Internal Error: cannot find input element";
}
function saveAs(blob: Blob | void, fileName: string) {
if (blob == null) {
return;
}
const url = window.URL.createObjectURL(blob);
const anchorElem = document.createElement("a");
anchorElem.style.display = "none";
anchorElem.href = url;
anchorElem.download = fileName;
document.body.appendChild(anchorElem);
anchorElem.click();
document.body.removeChild(anchorElem);
setTimeout(function () {
window.URL.revokeObjectURL(url);
}, 1000);
}