Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: example: event_stream usage within a browser #81

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions examples/browser/event_stream.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<html>
<head>
<title>CrowdStrike Falcon Sensor Download :: Falconjs Example</title>
<script src="event_stream.js"></script>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
border-style: dotted;
}
</style>
</head>
<body>

<div id="loginPopup">
<div id="popupForm">
<form action="javascript:event_stream.onLogin()" id="loginForm">
<h2>CrowdStrike API: Please Log in</h2>
<label for="clientId">
<strong>Client Id</strong>
</label>
<input type="password" id="clientId" placeholder="Your API Client ID" name="clientId" required>
<br/>
<label for="clientSecret">
<strong>Client Secret</strong>
</label>
<input type="password" id="clientId" placeholder="Your Api Client Secret" name="clientSecret" required>
<br/>
<button type="submit">Log in</button>
</form>
</div>
</div>


</body>
</html>


116 changes: 116 additions & 0 deletions examples/browser/event_stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { FalconClient, FalconErrorExplain, DomainSensorInstallerV1 } from "./../../src";

let client: FalconClient | null = null;

module.exports = {
onLogin: async function () {
client = new FalconClient({
cloud: "us-1",
clientId: "",
clientSecret: "",
});

const appName = "falconjs-my-app-id-1";
const resp = await client.availableEventStreams(appName)
.then((streams) => {
streams.forEach((stream) => {
const x = stream.process((item) => {
console.log("callback");
console.log(item);
}
);
console.log(x);
})
})
.catch((err) => {
console.log("Could not list available streams: ", err);
});
console.log(resp);
},
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&nbsp;(" +
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);
}