From 2014a3e9f0feb4ad19b7e3ebf9488608cb5fdbd4 Mon Sep 17 00:00:00 2001 From: Michael Bierman Date: Mon, 28 Aug 2023 17:23:34 -0700 Subject: [PATCH 1/5] Contribution --- README.md | 5 +- export-Filtered-flows/README.md | 20 ++++++++ export-Filtered-flows/flow-export.js | 22 +++++++++ export-Filtered-flows/flow-export_csv.js | 62 ++++++++++++++++++++++++ export-Filtered-flows/package.json | 5 ++ 5 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 export-Filtered-flows/README.md create mode 100644 export-Filtered-flows/flow-export.js create mode 100644 export-Filtered-flows/flow-export_csv.js create mode 100644 export-Filtered-flows/package.json diff --git a/README.md b/README.md index cad1695..eb065a8 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ This repository contains code examples that demonstrate how to use [Firewalla MS ## Quick Start -You can use either file or environment variable to setup your MSP domain and credential, check each example for details +You can use either file or environment variable to setup your MSP domain and credential, check each example for details: ```bash git clone https://github.com/firewalla/msp-api-examples.git @@ -29,6 +29,9 @@ domain="" token="" node ./flow-pagination/index | [Get Topk Bandwidth Usage Devices Within A Custom Period](./get-topk-bandwidth-usage-devices/README.md) | TBD | | | [Target list with CloudFlare](./target-list-with-cloudflare/README.md) | [Target List](https://docs.firewalla.net/api-reference/target-lists/) | [@CozMedic](https://github.com/CozMedic) | | [Target list with CrowdSec](./target-list-with-crowdsec/README.md) | [Target List](https://docs.firewalla.net/api-reference/target-lists/) | [@CozMedic](https://github.com/CozMedic) | +| [Export Filtered Flows](./export-Filtered-flows/README.md) |[Flow](https://docs.firewalla.net/api-reference/flow/) | [@mbierman](https://github.com/mbierman) | + + ## Disclaimer diff --git a/export-Filtered-flows/README.md b/export-Filtered-flows/README.md new file mode 100644 index 0000000..7c60fef --- /dev/null +++ b/export-Filtered-flows/README.md @@ -0,0 +1,20 @@ +### Story +TBD + + +### Quick Start + +Assume you've already cloned `https://github.com/firewalla/msp-api-examples.git` and `cd export-Filtered-flows` + +```bash +cd flow-pagination +npm install +domain="" token="" node ./index.js + +``` + +### Dependencies +- [Node.js](https://nodejs.org/) +- [npm](https://www.npmjs.com/package/npm), [pnpm](https://pnpm.io/installation), or the package manager you prefer + +Contributor: [@mbierman](https://github.com/mbierman) diff --git a/export-Filtered-flows/flow-export.js b/export-Filtered-flows/flow-export.js new file mode 100644 index 0000000..03322e8 --- /dev/null +++ b/export-Filtered-flows/flow-export.js @@ -0,0 +1,22 @@ +const axios = require('axios'); + +// Change these three configurations to what you need +const msp_domain = process.env.msp_domain || "kaleb.firewalla.net"; +const token = process.env.token || "35960c24c3967b6092c0726d348c8766"; +const block = process.env.block || "1"; +const limit = process.env.limit || "10"; +const begin = process.env.begin || Date.now() / 1000 - 24 * 3600 +const end = process.env.end || Date.now() / 1000 + + +axios({ + method: 'get', + url: `https://${msp_domain}/v2/flows?begin=${begin}&end=${end}&block=${block}&limit=${limit}`, + headers: { + Authorization: `Token ${token}`, + "Content-Type": "application/json" + } +}).then((res) => { + let data = res.data; + console.log(data); +}) diff --git a/export-Filtered-flows/flow-export_csv.js b/export-Filtered-flows/flow-export_csv.js new file mode 100644 index 0000000..9df7708 --- /dev/null +++ b/export-Filtered-flows/flow-export_csv.js @@ -0,0 +1,62 @@ +const axios = require('axios'); +// Output is CSV to console + +// Change these three configurations to what you need +// set to your MSP domain +const msp_domain = process.env.msp_domain || "my_msp.firewalla.net"; + +// replace with your MSP token +const token = process.env.token || "your_token"; + +// block: 1 means blocked flows 0 = unblocked flows +const block = process.env.block || "0"; + +// default to within the last 24 hours +const begin = process.env.begin || Date.now() / 1000 - 24 * 3600; + +// You could also do within the last hour +// const begin = process.env.begin || Math.floor(Date.now() / 1000) - 3600; +// end as of "now" +const end = process.env.end || Date.now() / 1000; + +// this sets max responses +const limit = process.env.limit || 100; + +// replace with the GID of a particular firewalla. +const gid = process.env.gid || "GID"; + + +// In this example, we are going to get up to 10 unblocked flows within the last 24 hours from a specific box: +// and output the +// * date stamp +// * remote IP +// * and remote Domain + +axios({ + method: 'get', + url: `https://${msp_domain}/v2/flows?begin=${begin}&end=${end}&block=${block}&limit=${limit}&gid=${gid}`, + headers: { + Authorization: `Token ${token}`, + "Content-Type": "application/json" + } +}).then((res) => { + let data = res.data; + if (data.results && data.results.length > 0) { + const result = data.results[0]; + const ts = result.ts; + const deviceName = result.device.name; + const remoteIP = result.remote.ip; + const remoteDomain = result.remote.domain || ""; + + const csvData = `${ts},${deviceName},${remoteIP},${remoteDomain}`; + console.log(csvData); + } else { + console.log("No data found."); + } +}).catch((err) => { + console.error("Error fetching data:", err.message); +}); + +// sample output +// % node ./flow-export_csv.js +// 1693266677.467,Pigpen / Synology NAS 🗄,192.241.187.136,api.openweathermap.org diff --git a/export-Filtered-flows/package.json b/export-Filtered-flows/package.json new file mode 100644 index 0000000..cb17ec7 --- /dev/null +++ b/export-Filtered-flows/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "axios": "^1.4.0" + } +} From e0a037be32d667d035a021d9da8cc00502ccfb20 Mon Sep 17 00:00:00 2001 From: Michael Bierman Date: Tue, 29 Aug 2023 09:25:50 -0700 Subject: [PATCH 2/5] removed token & renamed directory --- export-Filtered-flows/flow-export.js | 4 ++-- export-Filtered-flows/flow-export_csv.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/export-Filtered-flows/flow-export.js b/export-Filtered-flows/flow-export.js index 03322e8..bff826d 100644 --- a/export-Filtered-flows/flow-export.js +++ b/export-Filtered-flows/flow-export.js @@ -1,8 +1,8 @@ const axios = require('axios'); // Change these three configurations to what you need -const msp_domain = process.env.msp_domain || "kaleb.firewalla.net"; -const token = process.env.token || "35960c24c3967b6092c0726d348c8766"; +const msp_domain = process.env.msp_domain || "my_mnsp.firewalla.net"; +const token = process.env.token || "my_token"; const block = process.env.block || "1"; const limit = process.env.limit || "10"; const begin = process.env.begin || Date.now() / 1000 - 24 * 3600 diff --git a/export-Filtered-flows/flow-export_csv.js b/export-Filtered-flows/flow-export_csv.js index 9df7708..3e138e0 100644 --- a/export-Filtered-flows/flow-export_csv.js +++ b/export-Filtered-flows/flow-export_csv.js @@ -6,7 +6,7 @@ const axios = require('axios'); const msp_domain = process.env.msp_domain || "my_msp.firewalla.net"; // replace with your MSP token -const token = process.env.token || "your_token"; +const token = process.env.token || "my_token"; // block: 1 means blocked flows 0 = unblocked flows const block = process.env.block || "0"; From 288e3cf2dc463fbbf74e53608a4e73183f092603 Mon Sep 17 00:00:00 2001 From: Michael Bierman Date: Tue, 29 Aug 2023 11:47:01 -0700 Subject: [PATCH 3/5] Create export-filtered-flows --- export-filtered-flows | 1 + 1 file changed, 1 insertion(+) create mode 100644 export-filtered-flows diff --git a/export-filtered-flows b/export-filtered-flows new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/export-filtered-flows @@ -0,0 +1 @@ + From 7d3aad423aca7025a9e33a846c2a95e5223ae148 Mon Sep 17 00:00:00 2001 From: Michael Bierman Date: Tue, 29 Aug 2023 09:25:50 -0700 Subject: [PATCH 4/5] removed token & renamed directory --- {export-Filtered-flows => exporting-filtered-flows}/README.md | 0 .../flow-export.js | 4 ++-- .../flow-export_csv.js | 2 +- .../package.json | 0 4 files changed, 3 insertions(+), 3 deletions(-) rename {export-Filtered-flows => exporting-filtered-flows}/README.md (100%) rename {export-Filtered-flows => exporting-filtered-flows}/flow-export.js (80%) rename {export-Filtered-flows => exporting-filtered-flows}/flow-export_csv.js (97%) rename {export-Filtered-flows => exporting-filtered-flows}/package.json (100%) diff --git a/export-Filtered-flows/README.md b/exporting-filtered-flows/README.md similarity index 100% rename from export-Filtered-flows/README.md rename to exporting-filtered-flows/README.md diff --git a/export-Filtered-flows/flow-export.js b/exporting-filtered-flows/flow-export.js similarity index 80% rename from export-Filtered-flows/flow-export.js rename to exporting-filtered-flows/flow-export.js index 03322e8..bff826d 100644 --- a/export-Filtered-flows/flow-export.js +++ b/exporting-filtered-flows/flow-export.js @@ -1,8 +1,8 @@ const axios = require('axios'); // Change these three configurations to what you need -const msp_domain = process.env.msp_domain || "kaleb.firewalla.net"; -const token = process.env.token || "35960c24c3967b6092c0726d348c8766"; +const msp_domain = process.env.msp_domain || "my_mnsp.firewalla.net"; +const token = process.env.token || "my_token"; const block = process.env.block || "1"; const limit = process.env.limit || "10"; const begin = process.env.begin || Date.now() / 1000 - 24 * 3600 diff --git a/export-Filtered-flows/flow-export_csv.js b/exporting-filtered-flows/flow-export_csv.js similarity index 97% rename from export-Filtered-flows/flow-export_csv.js rename to exporting-filtered-flows/flow-export_csv.js index 9df7708..3e138e0 100644 --- a/export-Filtered-flows/flow-export_csv.js +++ b/exporting-filtered-flows/flow-export_csv.js @@ -6,7 +6,7 @@ const axios = require('axios'); const msp_domain = process.env.msp_domain || "my_msp.firewalla.net"; // replace with your MSP token -const token = process.env.token || "your_token"; +const token = process.env.token || "my_token"; // block: 1 means blocked flows 0 = unblocked flows const block = process.env.block || "0"; diff --git a/export-Filtered-flows/package.json b/exporting-filtered-flows/package.json similarity index 100% rename from export-Filtered-flows/package.json rename to exporting-filtered-flows/package.json From 6beba24240d2f1648dddf901a5711be60e144239 Mon Sep 17 00:00:00 2001 From: Michael Bierman Date: Tue, 29 Aug 2023 12:08:26 -0700 Subject: [PATCH 5/5] Fixed --- export-filtered-flows | 1 - {exporting-filtered-flows => export-filtered-flows}/README.md | 0 .../flow-export.js | 0 .../flow-export_csv.js | 0 {exporting-filtered-flows => export-filtered-flows}/package.json | 0 5 files changed, 1 deletion(-) delete mode 100644 export-filtered-flows rename {exporting-filtered-flows => export-filtered-flows}/README.md (100%) rename {exporting-filtered-flows => export-filtered-flows}/flow-export.js (100%) rename {exporting-filtered-flows => export-filtered-flows}/flow-export_csv.js (100%) rename {exporting-filtered-flows => export-filtered-flows}/package.json (100%) diff --git a/export-filtered-flows b/export-filtered-flows deleted file mode 100644 index 8b13789..0000000 --- a/export-filtered-flows +++ /dev/null @@ -1 +0,0 @@ - diff --git a/exporting-filtered-flows/README.md b/export-filtered-flows/README.md similarity index 100% rename from exporting-filtered-flows/README.md rename to export-filtered-flows/README.md diff --git a/exporting-filtered-flows/flow-export.js b/export-filtered-flows/flow-export.js similarity index 100% rename from exporting-filtered-flows/flow-export.js rename to export-filtered-flows/flow-export.js diff --git a/exporting-filtered-flows/flow-export_csv.js b/export-filtered-flows/flow-export_csv.js similarity index 100% rename from exporting-filtered-flows/flow-export_csv.js rename to export-filtered-flows/flow-export_csv.js diff --git a/exporting-filtered-flows/package.json b/export-filtered-flows/package.json similarity index 100% rename from exporting-filtered-flows/package.json rename to export-filtered-flows/package.json