Code Monkey home page Code Monkey logo

unifi-client's Introduction

unifi-client

NPM version CI codecov Downloads License Known Vulnerabilities unifi-client Donate GitHub stars Package Quality

Bugs Code Smells Duplicated Lines (%) Lines of Code Maintainability Rating Quality Gate Status Reliability Rating Security Rating Technical Debt Vulnerabilities

Dependencies update - renovate

NPM

This library is a nodejs client to talk with unifi rest API .

Start

install it

yarn add unifi-client

or via npm

npm i unifi-client

and import it in your code :

import Controller from 'unifi-client'

or with require :

const { Controller } = require('unifi-client')

Requirements

  • Installed UniFi-Controller version v6 or more, UnifiOs or not ( cloud keys / UDM or just unifi controller software ) ( not tested below v6 )
  • A network connectivy between unifi-client and the controller ( env http_proxy and https_proxy can be handled automaticaly, but not tested )
  • An account, cloud accounts, local accounts and 2FA are available
  • Node.js version >= LTS (14)

Example

Examples are located in examples folder

import Controller from 'unifi-client'

// works with local account, check examples for 2FA
const controller = new Controller({
  username: 'ubnt',
  password: 'ubnt',
  url: 'https://unifi',
  strictSSL: false
});

//login to the controller
await controller.login()

//retrieve sites
const sites = controller.getSites()

//work on one site
const site = sites[0];

//example request to get firewall rules
const rules = await site.firewall.getRules();

To work on multiples sites on the same time

const controller = new Controller({...});

const sites = controller.getSites();

//get firewallRules on two sites
const rules = await site[0].firewall.getRules();
const rules2 = await site[1].firewall.getRules();

How to use the HTTP instances

//use the controller instance directly . Authentication, url construction and other is already managed for you
const self = await controller.getInstance().get('/api/self');

//for a custom site :
const topology = await site.getInstance().get('/topology');

Get firewall rules, it return an array of FWRule

const rules = await site.firewall.getRules();

Instances

The instances returned by getInstance are basicaly some axios instances . With some additions :

//url params
// use them to manage a rest url for example
instance.get('/url/:uuid', {
    urlParams: {
        uuid: 'my-uuid'
    }
})
// will call the url /url/my-uuid


// apiVersion :
// some api call are on the v2 api, so you can set the api version when calling
console.log(site.name); // default
site.getInstance().get('/super/endpoint', {
    apiVersion: 2
});
// will call the url /v2/api/site/default/super/endpoint

how to use URLs from the unifi front :

The urls of unifi are like :

  • /proxy/network/v2/api/site/default/notifications for unifiOs or
  • /v2/api/site/default/notifications for non unifiOs

the url is constructed like : /[v<ApiVersion>/]api/site/<sitename>[/<urlToCall>]

  • ApiVersion : the version of the API (for version > 1)
  • sitename : the name of the site (already included in site instance)

To illustrate, to call the url I see on my browser network tabs :

https://192.168.1.1/proxy/network/v2/api/site/default/notifications

I just need to :

// configure my controller
const controller = new Controller({
    username: 'ubnt',
    password: 'ubnt',
    url: 'https://192.168.1.1',
    strictSSL: false
});

//login to the controller
await controller.login()

//retrieve sites
const sites = controller.getSites()

// select the site "default"
const site = sites.find((site) => site.name === 'default');

//call my url
const notifications = site.getInstance().get('/default/notifications', {
    apiVersion: 2
});

//do something with notifications

More examples in the folder examples

Websockets

This library supports websockets, you can listen on them with :

// initWebSockets for controller :
await controller.initWebSockets();

// listen for controller websockets ( only for unifiOS )
controller.on() / controller.ws.on()

// listen for super site websockets
controller.superWS.on();


// initWebSockets for site :
await site.initWebSockets();
// listen for this site websockets
site.on() / site.ws.on()

// doesn't known the name of the event ? you can listen on joker :
controller.on('*', (eventName, ...args) => {
    console.log(eventName, ...args);
});

// want to listen on all websockets of a controller on the same listener
// all websockets registered :
// - controller websockets if unifiOS
// - super site WS
// - all sites where ws are init ( closed or not )
controller.globalWS.on('*', (eventName, ...args) => {
    console.log(eventName, ...args);
});

the support of websocket is experimental, and with a really bad coverage . Doesn't hesitate to open a PR, to add more websockets types

We add some optionnal dependencies to improve performances, more informations read here they can be skipped by using npm install --no-optional

Technical documentation

All the technical documentation is available here

Work In Progress

check technical documentation for available methods

Tests

This library is auto-tested on :

  • UDM-pro : latest (7.3.83)

  • Unifi controller : latest, 7.3.83, 7.2.95, 7.1.68, 7.0.25, 6.5.55, 6.4.54, 6.2.26, 6.0.45 (only some functions are tested, in an empty environment)

with the last node LTS

References

This nodejs package/class uses functionality/Know-How gathered from different third-party projects:

Debug

To debug, you can use the debug library, just set the DEBUG environnement variable (check this) .

  • unifi-client : base debug namespace
  • unifi-client:<className> : will show debug for this class (eg : unifi-client:Controller will log controller logs )
  • unifi-client:axios : will logs axios request (can contain secrets informations)
  • unifi-client:axios:verbose : will logs more things on axios, like send and receive payload (can contain secrets informations)
  • unifi-client:axios:curl : will log curl cmd corresponding to the axios request (can contain secrets informations)

Useful links

ubntwiki : https://ubntwiki.com/products/software/unifi-controller/api

unifi-client's People

Contributors

dependabot[bot] avatar kylebrandon avatar renovate-bot avatar renovate[bot] avatar thib3113 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

unifi-client's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Pending Status Checks

These updates await pending status checks. To force their creation now, click the checkbox below.

  • fix(deps): update dependency debug to v4.3.6
  • chore(deps): update dependency @types/cookie to v0.6.0
  • chore(deps): update dependency @types/node to v20.14.14
  • chore(deps): update dependency dotenv to v16.4.5
  • chore(deps): update dependency eslint to v8.57.0
  • chore(deps): update dependency eslint-plugin-prettier to v5.2.1
  • chore(deps): update dependency eslint-plugin-tsdoc to v0.3.0
  • chore(deps): update dependency glob to v10.4.5
  • chore(deps): update dependency nock to v13.5.4
  • chore(deps): update dependency prettier to v3.3.3
  • chore(deps): update dependency ts-jest to v29.2.4
  • chore(deps): update dependency typedoc to v0.26.5
  • chore(deps): update typescript-eslint monorepo to v7.18.0 (@typescript-eslint/eslint-plugin, @typescript-eslint/parser)
  • fix(deps): update dependency axios to v1.7.3
  • fix(deps): update dependency semver to v7.6.3
  • fix(deps): update dependency set-cookie-parser to v2.7.0 (set-cookie-parser, @types/set-cookie-parser)
  • fix(deps): update dependency ws to v8.18.0 (ws, @types/ws)
  • chore(deps): update dependency glob to v11
  • chore(deps): update dependency husky to v9
  • chore(deps): update dependency uuid to v10
  • chore(deps): update peaceiris/actions-gh-pages action to v4

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Ignored or Blocked

These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.

Detected dependencies

github-actions
.github/workflows/CI.yml
  • actions/checkout v4
  • actions/setup-node v4
  • actions/setup-node v4
  • actions/upload-artifact v3
  • actions/checkout v4
  • actions/download-artifact v3
  • actions/setup-node v4
  • mattallty/jest-github-action v1
  • actions/upload-artifact v3
  • actions/checkout v4
  • actions/upload-artifact v3
  • actions/checkout v4
  • actions/download-artifact v3
  • codecov/codecov-action v4
  • actions/checkout v4
  • actions/download-artifact v3
.github/workflows/codeql-analysis.yml
  • actions/checkout v4
  • github/codeql-action v3
  • github/codeql-action v3
  • github/codeql-action v3
.github/workflows/documentation.yml
  • actions/checkout v4
  • actions/setup-node v4
  • peaceiris/actions-gh-pages v3
.github/workflows/release.yml
  • actions/checkout v4
  • actions/setup-node v4
  • actions/setup-node v4
  • actions/upload-artifact v3
  • actions/checkout v4
  • actions/download-artifact v3
  • actions/setup-node v4
  • actions/checkout v4
  • actions/setup-node v4
  • actions/checkout v4
  • actions/setup-node v4
.github/workflows/security.yml
  • github/codeql-action v3
.github/workflows/stale.yml
  • actions/stale v9
npm
package.json
  • axios 1.6.7
  • axios-curlirize 1.3.7
  • cookie 0.6.0
  • debug 4.3.4
  • jsonwebtoken 9.0.2
  • semver 7.5.4
  • set-cookie-parser 2.6.0
  • ws 8.17.1
  • @types/cookie 0.5.4
  • @types/debug 4.1.12
  • @types/jest 29.5.12
  • @types/moxios 0.4.17
  • @types/node 20.9.2
  • @types/set-cookie-parser 2.4.6
  • @types/ws 8.5.9
  • @typescript-eslint/eslint-plugin 7.0.2
  • @typescript-eslint/parser 7.0.2
  • cross-env 7.0.3
  • dotenv 16.3.1
  • eslint 8.56.0
  • eslint-config-prettier 9.1.0
  • eslint-plugin-prettier 5.1.3
  • eslint-plugin-tsdoc 0.2.17
  • glob 10.3.10
  • husky 8.0.3
  • jest 29.7.0
  • jest-environment-node 29.7.0
  • jest-sonar 0.2.16
  • moxios 0.4.0
  • nock 13.3.8
  • prettier 3.1.0
  • source-map-support 0.5.21
  • ts-jest 29.1.2
  • ts-node 10.9.2
  • typedoc 0.25.12
  • typescript 5.4.2
  • uuid 9.0.1
  • bufferutil ^4.0.8
  • utf-8-validate ^6.0.3
  • node >= 14

  • Check this box to trigger a request for Renovate to run again on this repository

BuildUrl improvments

Is your feature request related to a problem? Please describe.
Actually, the buildUrl function can't handle all url ( protect / talk / access for example can't be called ), some url also use url like : /v2/api or /api/v2

Describe the solution you'd like
An update of the buildUrl function

Describe alternatives you've considered
Not possible for the moment . The default buildUrl will add /proxy/network by default

getByMac doesn't seem to work.

Prerequisites

Please answer the following questions for yourself before submitting an issue.

  • I am running the latest version
  • I checked the documentation and found no answer
  • I checked to make sure that this issue has not already been filed

Current Behavior

    const client = clients.filter(d=> d.mac === 'ca:f8:f4:50:33:6f')[0];
    console.log(client.mac)                         // prints ca:f8:f4:50:33:6f
    await site.clients.getByMac(client.mac);        // err api.err.UnknownStation

getByMac return an error. getById works.

Expected Behavior

I expect getByMac to retrieve clients by mac address.

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

  • Library version: [email protected]
  • Unifi Controller :UDM SE v3.1.16
  • Unifi OS : yes?
  • NodeJS version: v20.2.0
  • Operating System: Linux (WSL)

Failure Logs

Coundn't get that to work, but the essential is that it makes this request :
GET /proxy/network/api/s/default/stat/sta/ca%3Af8%3Af4%3A50%3A33%3A6f
and the response is

{
        meta: {
          rc: 'error',
          mac: 'ca%3af8%3af4%3a50%3a33%3a6f',
          msg: 'api.err.UnknownStation'
        },
        data: []
      }

Clients-device managment

Add functions :

  • Object Client :
    • kick
    • block
    • unblock
    • forget / delete
    • save (note / name)
    • get5minStats
    • getHourlyUserStats
    • getDailyUserStats
    • getMonthlyUserStats
  • Object Clients :
    • createClient

Support IPv6 Firewall rules

Is your feature request related to a problem? Please describe.

Actually, FWRule can handle only ipv4 rules .

Describe the solution you'd like

Update of the FWRule object to allow ipv6 properties

Describe alternatives you've considered

Additional context

2FA don't work on non unifiOS

Prerequisites

Please answer the following questions for yourself before submitting an issue.

  • I am running the latest version
  • I checked the documentation and found no answer
  • I checked to make sure that this issue has not already been filed

Current Behavior

2FA works only on unifios

Expected Behavior

2FA works on non unifios too

Failure Information

Art-of-WiFi/UniFi-API-client#195 (comment)

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  1. login to non unifios with 2FA

Sites stats

Functions :

  • get5minStats
  • getHourlyStats
  • getDailyStats
  • getMonthlySiteStats

Allow to login unifi.ui.com and to use the controller remotely

Is your feature request related to a problem? Please describe.

Actually, its only possible to control controller from same network, it can be interessting to control it from outside throudh the unifi.ui.com website .

Describe the solution you'd like

  • implementing sso login
  • change the default url depending the return
  • other differences ?

Improve the Clients part

Is your feature request related to a problem? Please describe.
Need some work to determine what to do

Describe the solution you'd like

Describe alternatives you've considered

Additional context

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.