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..bff826d --- /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 || "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 +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..3e138e0 --- /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 || "my_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" + } +}