Removed old UniFi username check from info.js. Implemented temporary guest features warning in info.js. Cleanup unifi.js to remove legacy implementation and switch to UniFi Integrations. Removed deprecated UNIFI_USERNAME and UNIFI_PASSWORD from variables.js. Updated docker-compose.yml. Removed old dependencies. Updated README.md

This commit is contained in:
Glenn de Haan
2025-08-26 18:51:48 +02:00
parent 19d7e4c0c6
commit 7b8a996452
7 changed files with 14 additions and 386 deletions

View File

@@ -134,13 +134,6 @@ module.exports = () => {
*/
log.info(`[UniFi] Using Controller on: ${variables.unifiIp}:${variables.unifiPort} (Site ID: ${variables.unifiSiteId}${variables.unifiSsid !== '' ? `, SSID: ${variables.unifiSsid}` : ''})`);
/**
* Check for valid UniFi username
*/
if(variables.unifiUsername.includes('@')) {
log.error('[UniFi] Incorrect username detected! UniFi Cloud credentials are not supported!');
}
/**
* Check if UniFi Token is set
*/
@@ -148,4 +141,9 @@ module.exports = () => {
log.error('[UniFi] Integration API Key is not set within UNIFI_TOKEN environment variable!');
process.exit(1);
}
/**
* Temporary warning that guests lookup feature is unavailable
*/
log.warn('[UniFi] Guests features are temporary disabled in this version of UniFi Voucher Site (Not supported in current Integrations API). Please view and upvote: https://community.ui.com/questions/Feature-Request-Network-API-Guest-Access-Voucher-ID/d3c470e2-433d-4386-8a13-211712311202')
};

View File

@@ -1,77 +1,15 @@
/**
* Import vendor modules
*/
const unifi = require('node-unifi');
/**
* Import own modules
*/
const variables = require('./variables');
const log = require('./log');
const fetch = require('../utils/fetch');
/**
* UniFi Settings
*/
const settings = {
ip: variables.unifiIp,
port: variables.unifiPort,
username: variables.unifiUsername,
password: variables.unifiPassword,
siteID: variables.unifiSiteId
};
/**
* Controller session
*/
let controller = null;
/**
* Start a UniFi controller reusable session
*
* @return {Promise<unknown>}
*/
const startSession = () => {
return new Promise((resolve, reject) => {
// Check if we have a current session already
if(controller !== null) {
resolve();
return;
}
if(settings.username.includes('@')) {
reject('[UniFi] Incorrect username detected! UniFi Cloud credentials are not supported!');
return;
}
// Create new UniFi controller object
controller = new unifi.Controller({
host: settings.ip,
port: settings.port,
site: settings.siteID,
sslverify: false
});
// Login to UniFi Controller
controller.login(settings.username, settings.password).then(() => {
log.debug('[UniFi] Login successful!');
resolve();
}).catch((e) => {
// Something went wrong so clear the current controller so a user can retry
controller = null;
log.error('[UniFi] Error while logging in!');
log.debug(e);
reject('[UniFi] Error while logging in!');
});
});
}
/**
* UniFi module functions
*
* @type {{create: (function(*, number=, string=): Promise<*>), remove: (function(*): Promise<*>), list: (function(): Promise<*>), guests: (function(boolean=): Promise<*>)}}
* @type {{create: (function(*, number=, string=): Promise<*>), remove: (function(*): Promise<*>), list: (function(): Promise<*>), guests: (function(): Promise<*>)}}
*/
const unifiModule = {
module.exports = {
/**
* Creates a new UniFi Voucher
*
@@ -171,62 +109,24 @@ const unifiModule = {
/**
* Returns a list with all UniFi Guests
*
* @param retry
* @return {Promise<unknown>}
*/
guests: (retry = true) => {
guests: () => {
return new Promise((resolve, reject) => {
// fetch('/clients', 'GET', {
// filter: 'access.type.eq(\'GUEST\')',
// limit: 10000
// }).then((clients) => {
// console.log(clients)
// console.log(clients);
// log.info(`[UniFi] Found ${clients.length} guest(s)`);
// }).catch((e) => {
// log.error('[UniFi] Error while getting vouchers!');
// log.error('[UniFi] Error while getting guests!');
// log.debug(e);
// reject('[UniFi] Error while getting vouchers!');
// reject('[UniFi] Error while getting guests!');
// });
startSession().then(() => {
controller.getGuests().then((guests) => {
log.info(`[UniFi] Found ${guests.length} guest(s)`);
resolve(guests);
}).catch((e) => {
log.error('[UniFi] Error while getting guests!');
log.debug(e);
// Check if token expired, if true attempt login then try again
if (e.response) {
if(e.response.status === 401 && retry) {
log.info('[UniFi] Attempting re-authentication & retry...');
controller = null;
unifiModule.guests(false).then((e) => {
resolve(e);
}).catch((e) => {
reject(e);
});
} else {
// Something else went wrong lets clear the current controller so a user can retry
log.error(`[UniFi] Unexpected ${JSON.stringify({status: e.response.status, retry})} cleanup controller...`);
controller = null;
reject('[UniFi] Error while getting guests!');
}
} else {
// Something else went wrong lets clear the current controller so a user can retry
log.error('[UniFi] Unexpected cleanup controller...');
controller = null;
reject('[UniFi] Error while getting guests!');
}
});
}).catch((e) => {
reject(e);
});
// Currently disabled! Waiting on: https://community.ui.com/questions/Feature-Request-Network-API-Guest-Access-Voucher-ID/d3c470e2-433d-4386-8a13-211712311202
resolve([]);
});
}
}
/**
* Exports the UniFi module functions
*/
module.exports = unifiModule;

View File

@@ -14,8 +14,6 @@ const config = require('./config');
module.exports = {
unifiIp: config('unifi_ip') || process.env.UNIFI_IP || '192.168.1.1',
unifiPort: config('unifi_port') || process.env.UNIFI_PORT || 443,
unifiUsername: config('unifi_username') || process.env.UNIFI_USERNAME || 'admin',
unifiPassword: config('unifi_password') || process.env.UNIFI_PASSWORD || 'password',
unifiToken: config('unifi_token') || process.env.UNIFI_TOKEN || '',
unifiSiteId: config('unifi_site_id') || process.env.UNIFI_SITE_ID || 'default',
unifiSsid: config('unifi_ssid') || process.env.UNIFI_SSID || '',