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

Fixed directory name and files #14

Open
wants to merge 6 commits 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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,6 +29,9 @@ domain="<YOUR-MSP-DOMAIN>" token="<YOUR-MSP-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) |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

export-filtered-flows




## Disclaimer

Expand Down
20 changes: 20 additions & 0 deletions export-filtered-flows/README.md
Original file line number Diff line number Diff line change
@@ -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="<YOUR-MSP-DOMAIN>" token="<YOUR-MSP-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)
22 changes: 22 additions & 0 deletions export-filtered-flows/flow-export.js
Original file line number Diff line number Diff line change
@@ -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}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use params

headers: {
Authorization: `Token ${token}`,
"Content-Type": "application/json"
}
}).then((res) => {
let data = res.data;
console.log(data);
})
62 changes: 62 additions & 0 deletions export-filtered-flows/flow-export_csv.js
Original file line number Diff line number Diff line change
@@ -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}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use params

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
5 changes: 5 additions & 0 deletions export-filtered-flows/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"axios": "^1.4.0"
}
}