UNPKG

crowdsec-client

Version:

A Crowdsec client that allow you to easily create bouncer or watcher

153 lines (119 loc) 8.81 kB
# crowdsec-client [![NPM version](https://img.shields.io/npm/v/crowdsec-client.svg)](https://www.npmjs.com/package/crowdsec-client) [![CI](https://github.com/thib3113/node-crowdsec/actions/workflows/CI.yml/badge.svg)](https://github.com/thib3113/node-crowdsec/actions/workflows/CI.yml) [![Coverage](https://sonarcloud.io/api/project_badges/measure?project=thib3113_node-crowdsec&metric=coverage)](https://sonarcloud.io/summary/new_code?id=thib3113_node-crowdsec) [![Downloads](https://img.shields.io/npm/dm/crowdsec-client.svg)](https://www.npmjs.com/package/crowdsec-client) [![License](https://img.shields.io/npm/l/crowdsec-client)](https://github.com/thib3113/node-crowdsec/blob/main/LICENSE) [![Known Vulnerabilities](https://snyk.io/test/github/thib3113/node-crowdsec/badge.svg)](https://snyk.io/test/github/thib3113/node-crowdsec) [![crowdsec-client-snyk](https://snyk.io/advisor/npm-package/crowdsec-client/badge.svg)](https://snyk.io/advisor/npm-package/crowdsec-client) [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg?logo=paypal)](https://paypal.me/thib3113) [![GitHub stars](https://img.shields.io/github/stars/thib3113/node-crowdsec.svg?style=social&label=Star)](https://github.com/thib3113/node-crowdsec/stargazers/) [![Package Quality](https://packagequality.com/shield/crowdsec-client.svg)](https://packagequality.com/#?package=crowdsec-client) [![Bugs](https://sonarcloud.io/api/project_badges/measure?project=thib3113_node-crowdsec&metric=bugs)](https://sonarcloud.io/dashboard?id=thib3113_node-crowdsec) [![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=thib3113_node-crowdsec&metric=code_smells)](https://sonarcloud.io/dashboard?id=thib3113_node-crowdsec) [![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=thib3113_node-crowdsec&metric=duplicated_lines_density)](https://sonarcloud.io/dashboard?id=thib3113_node-crowdsec) [![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=thib3113_node-crowdsec&metric=ncloc)](https://sonarcloud.io/dashboard?id=thib3113_node-crowdsec) [![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=thib3113_node-crowdsec&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=thib3113_node-crowdsec) [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=thib3113_node-crowdsec&metric=alert_status)](https://sonarcloud.io/dashboard?id=thib3113_node-crowdsec) [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=thib3113_node-crowdsec&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=thib3113_node-crowdsec) [![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=thib3113_node-crowdsec&metric=security_rating)](https://sonarcloud.io/dashboard?id=thib3113_node-crowdsec) [![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=thib3113_node-crowdsec&metric=sqale_index)](https://sonarcloud.io/dashboard?id=thib3113_node-crowdsec) [![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=thib3113_node-crowdsec&metric=vulnerabilities)](https://sonarcloud.io/dashboard?id=thib3113_node-crowdsec) ![Dependencies update - renovate](https://img.shields.io/badge/renovate-enabled-green?style=for-the-badge&logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjUgNSAzNzAgMzcwIj48Y2lyY2xlIGN4PSIxODkiIGN5PSIxOTAiIHI9IjE4NCIgZmlsbD0iI2ZlMiIvPjxwYXRoIGZpbGw9IiM4YmIiIGQ9Ik0yNTEgMjU2bC0zOC0zOGExNyAxNyAwIDAxMC0yNGw1Ni01NmMyLTIgMi02IDAtN2wtMjAtMjFhNSA1IDAgMDAtNyAwbC0xMyAxMi05LTggMTMtMTNhMTcgMTcgMCAwMTI0IDBsMjEgMjFjNyA3IDcgMTcgMCAyNGwtNTYgNTdhNSA1IDAgMDAwIDdsMzggMzh6Ii8+PHBhdGggZmlsbD0iI2Q1MSIgZD0iTTMwMCAy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DYgMCAwMDkgMGwxMDEtMTAxYTYgNiAwIDAwMC05bC0yNC0yMy00LTJ6Ii8+PC9zdmc+ ) [![NPM](https://nodei.co/npm/crowdsec-client.png)](https://nodei.co/npm/crowdsec-client/) This library is a Node.js client to talk with crowdsec rest API . ## Start install it ``` npm i crowdsec-client ``` and then read the documentation in the [wiki](https://github.com/thib3113/node-crowdsec/wiki) ## Usage ### as a Bouncer First, create a client, pointing to your crowdsec instance . With a bouncer api key ([doc](https://doc.crowdsec.net/docs/user_guides/bouncers_configuration/)) ````typescript const client = new BouncerClient({ url: process.env.CROWDSEC_URL, auth: { apiKey: process.env.CROWDSEC_API_KEY || '' }, //use this option if you use a self signed ssl certificate strictSSL: false }); await client.login(); ```` Second, ask for a decision ````typescript const stream = client.Decisions.getStream({ //the stream will poll the API at the interval . in ms interval: 10000 }); //or with filters const filteredStream = client.Decisions.getStream({ //the stream will poll the API at the interval . in ms interval: 10000, scopes: ['ip', 'range'], origins: ['capi'] , scenarios_containing: ['bruteforce'], scenarios_not_containing: ['slow'], }); ```` now, use this stream ````typescript import * as stream from "stream"; stream.on('added', (decision) => { //will be emited when a new decision is added }); stream.on('deleted', (decision) => { //will be emitted when a decision is deleted }); //you can control the stream //start the stream stream.resume(); //pause the stream stream.pause() //check if the stream is paused if(stream.paused) { } ```` ### as a Watcher First, create a client, pointing to your crowdsec instance . With a machine login/password ([doc](https://doc.crowdsec.net/docs/user_guides/machines_mgmt)) ````typescript const client = new WatcherClient({ url: process.env.CROWDSEC_URL, auth: { machineID: 'nameOfTheMachine', password: 'password', //the crowdsec token is valid for only 1h ... did you want to autorenew it ? autoRenew: true, }, //use this option if you use a self signed ssl certificate strictSSL: false }); await client.login(); ```` Search for Alert ```typescript //get alerts with an active decision const alerts = await client.Alerts.search({ has_active_decision: true }); //select one alert const alert = alerts[0] if(!alert.id) { //do something if no id } //delete it ? await client.Alerts.deleteById(alert.id); //or delete all the alerts about an ip await client.Alerts.delete({ ip: '127.0.0.1' }); ``` More authentications options (like TLS) are documented in the [wiki](https://github.com/thib3113/node-crowdsec/wiki) ## Debug this library include [debug](https://www.npmjs.com/package/debug), to debug, you can set the env variable : ````dotenv DEBUG=crowdsec-client:* ````