UNPKG

@homebridge-plugins/homebridge-plugin-update-check

Version:
120 lines 4.49 kB
/* eslint-disable node/prefer-global/process */ /* eslint-disable style/operator-linebreak */ /* eslint-disable object-shorthand */ /* eslint no-console: ["error", { allow: ["info", "warn", "error"] }] */ import { readFileSync } from 'node:fs'; import https from 'node:https'; import path from 'node:path'; import axios from 'axios'; import jwt from 'jsonwebtoken'; export class UiApi { log; secrets; baseUrl; httpsAgent; token; dockerUrl; constructor(hbStoragePath, log) { this.log = log; const configPath = path.resolve(hbStoragePath, 'config.json'); const hbConfig = JSON.parse(readFileSync(configPath, 'utf8')); const config = hbConfig.platforms.find((config) => config.platform === 'config' || config.platform === 'homebridge-config-ui-x.config'); if (config) { const secretPath = path.resolve(hbStoragePath, '.uix-secrets'); this.secrets = JSON.parse(readFileSync(secretPath, 'utf8')); const ssl = !!config.ssl?.key || !!config.ssl?.pfx; const protocol = ssl ? 'https://' : 'http://'; const host = config.host ?? 'localhost'; const port = config.port ?? 8581; this.baseUrl = `${protocol + host}:${port.toString()}`; const dockerProtocol = 'https://'; const dockerHost = 'hub.docker.com'; this.dockerUrl = `${dockerProtocol + dockerHost}`; if (ssl) { this.httpsAgent = new https.Agent({ rejectUnauthorized: false }); // don't reject self-signed certs } } } isConfigured() { return this.secrets !== undefined; } async getHomebridge() { if (this.isConfigured()) { return await this.makeCall('/api/status/homebridge-version'); } else { return { name: '', installedVersion: '', latestVersion: '', updateAvailable: false, }; } } async getPlugins() { if (this.isConfigured()) { return await this.makeCall('/api/plugins'); } else { return []; } } async getDocker() { const currentDockerVersion = process.env.DOCKER_HOMEBRIDGE_VERSION; let dockerInfo = { name: '', installedVersion: '', latestVersion: '', updateAvailable: false, }; if (this.isConfigured() && currentDockerVersion !== undefined) { const json = await this.makeDockerCall('/v2/repositories/homebridge/homebridge/tags/?page_size=30&page=1&ordering=last_updated'); const versions = json.results; const installedVersion = versions.filter(version => version.name === currentDockerVersion)[0]; const installedVersionDate = Date.parse(installedVersion.last_updated); const availableVersions = versions.filter(version => !version.name.includes('beta') && (Date.parse(version.last_updated) > installedVersionDate)); if (availableVersions.length > 0) { dockerInfo = { name: 'Docker image', installedVersion: installedVersion, latestVersion: availableVersions[0].name, updateAvailable: true, }; } } return dockerInfo; } async makeDockerCall(apiPath) { const response = await axios.get(this.dockerUrl + apiPath, { httpsAgent: this.httpsAgent, }); return response.data; } async makeCall(apiPath) { const response = await axios.get(this.baseUrl + apiPath, { headers: { Authorization: `Bearer ${this.getToken()}`, }, httpsAgent: this.httpsAgent, }); return response.data; } getToken() { if (this.token) { return this.token; } const user = { username: '@homebridge-plugins/homebridge-plugin-update-check', name: '@homebridge-plugins/homebridge-plugin-update-check', admin: true, instanceId: 'xxxxxxx', }; this.token = jwt.sign(user, this.secrets.secretKey, { expiresIn: '1m' }); setTimeout(() => { this.token = undefined; }, 30 * 1000); return this.token; } } //# sourceMappingURL=ui-api.js.map