From 4aa89d2746be912c7c901200996865175800237b Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Sun, 20 Feb 2022 16:58:13 +0100 Subject: [PATCH] * Important: js-controller 3.0+ required! * (Apollon77) Remove some legacy code * (Apollon77) Prevent potential crash case when error occurs --- .github/workflows/test-and-release.yml | 4 +- LICENSE | 2 +- README.md | 8 +- io-package.json | 12 ++- main.js | 126 ++----------------------- package.json | 8 +- 6 files changed, 30 insertions(+), 130 deletions(-) diff --git a/.github/workflows/test-and-release.yml b/.github/workflows/test-and-release.yml index 19d56b6..65dcaac 100644 --- a/.github/workflows/test-and-release.yml +++ b/.github/workflows/test-and-release.yml @@ -28,7 +28,7 @@ jobs: node-version: [12.x] steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v2 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v2.5.1 with: @@ -60,7 +60,7 @@ jobs: os: [ubuntu-latest, windows-latest, macos-latest] steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v2 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v2.5.1 with: diff --git a/LICENSE b/LICENSE index 3a39393..74e2883 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2014-2021 bluefox +Copyright (c) 2014-2022 bluefox Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/README.md b/README.md index 0ff8889..bccdbf4 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,12 @@ When the adapter crashes or an other Code error happens, this error message that --> ## Changelog + +### __WORK IN PROGRESS__ +* Important: js-controller 3.0+ required! +* (Apollon77) Remove some legacy code +* (Apollon77) Prevent potential crash case when error occurs + ### 2.0.5 (2021-06-29) * (bluefox) Corrected error with token @@ -167,7 +173,7 @@ When the adapter crashes or an other Code error happens, this error message that The MIT License (MIT) -Copyright (c) 2014-2021 bluefox +Copyright (c) 2014-2022 bluefox Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/io-package.json b/io-package.json index 2c8c28a..d556be0 100644 --- a/io-package.json +++ b/io-package.json @@ -234,7 +234,12 @@ }, "dependencies": [ { - "admin": ">=3.0.0" + "js-controller": ">=3.0.0" + } + ], + "globalDependencies": [ + { + "admin": ">=4.0.9" } ], "extIcon": "https://raw.githubusercontent.com/ioBroker/ioBroker.pushover/master/admin/pushover.png", @@ -252,6 +257,9 @@ "encryptedNative": [ "token" ], + "protectedNative": [ + "token" + ], "native": { "user": "", "token": "", @@ -260,4 +268,4 @@ "priority": 0 }, "objects": [] -} \ No newline at end of file +} diff --git a/main.js b/main.js index fb6184d..55875de 100644 --- a/main.js +++ b/main.js @@ -23,15 +23,6 @@ function startAdapter(options) { Object.assign(options, {name: adapterName}); adapter = new utils.Adapter(options); - adapter.getEncryptedConfig = adapter.getEncryptedConfig || getEncryptedConfig; - - try { - adapter.tools = adapter.tools || require(utils.controllerDir + '/lib/tools'); - adapter.tools.migrateEncodedAttributes = adapter.tools.migrateEncodedAttributes || migrateEncodedAttributes; - } catch (e) { - adapter.tools = {decrypt, migrateEncodedAttributes}; - } - adapter.on('message', obj => { if (obj && obj.command === 'send' && obj.message) { obj.message && processMessage(adapter, obj); @@ -44,19 +35,7 @@ function startAdapter(options) { }); adapter.on('ready', () => { - if (!adapter.supportsFeature || !adapter.supportsFeature('ADAPTER_AUTO_DECRYPT_NATIVE')) { - adapter.getEncryptedConfig('token') - .then(value => { - adapter.config.token = value; - main(adapter); - }) - .catch(e => { - adapter.config.token = ''; - main(adapter); - }); - } else { - main(adapter); - } + main(adapter); }); return adapter; @@ -97,89 +76,6 @@ function processMessage(adapter, obj) { } } -function migrateEncodedAttributes(adapter, attrs, onlyRename) { - if (typeof attrs === 'string') { - attrs = [attrs]; - } - const toMigrate = []; - attrs.forEach(attr => - adapter.config[attr] !== undefined && adapter.config['enc_' + attr] === undefined && toMigrate.push(attr)); - - if (toMigrate.length) { - return new Promise((resolve, reject) => { - // read system secret - adapter.getForeignObject('system.config', null, (err, data) => { - let systemSecret; - if (data && data.native) { - systemSecret = data.native.secret; - } - if (systemSecret) { - // read instance configuration - adapter.getForeignObject('system.adapter.' + adapter.namespace, (err, obj) => { - if (obj && obj.native) { - toMigrate.forEach(attr => { - if (obj.native[attr]) { - if (onlyRename) { - obj.native['enc_' + attr] = obj.native[attr]; - } else { - obj.native['enc_' + attr] = adapter.tools.encrypt(systemSecret, obj.native[attr]); - } - } else { - obj.native['enc_' + attr] = ''; - } - delete obj.native[attr]; - }); - adapter.setForeignObject('system.adapter.' + adapter.namespace, obj, err => { - err && adapter.log.error(`Cannot write system.adapter.${adapter.namespace}: ${err}`); - !err && adapter.log.info('Attributes are migrated and adapter will be restarted'); - err ? reject(err) : resolve(true); - }); - } else { - adapter.log.error(`system.adapter.${adapter.namespace} not found!`); - reject(`system.adapter.${adapter.namespace} not found!`); - } - }); - } else { - adapter.log.error('No system secret found!'); - reject('No system secret found!'); - } - }); - }) - } else { - return Promise.resolve(false); - } -} - -function getEncryptedConfig(attribute, callback) { - if (adapter.config.hasOwnProperty(attribute)) { - if (typeof callback !== 'function') { - return new Promise((resolve, reject) => { - getEncryptedConfig(attribute, (err, encrypted) => { - if (err) { - reject(err); - } else { - resolve(encrypted); - } - }); - }); - } else { - adapter.getForeignObject('system.config', null, (err, data) => { - let systemSecret; - if (data && data.native) { - systemSecret = data.native.secret; - } - callback(null, adapter.tools.decrypt(systemSecret, adapter.config[attribute])); - }); - } - } else { - if (typeof callback === 'function') { - callback('Attribute not found'); - } else { - return Promise.reject('Attribute not found'); - } - } -} - function sendGlances(adapter, obj) { const json = JSON.stringify(obj.message); if (lastMessageTime && lastMessageText === JSON.stringify(obj.message) && Date.now() - lastMessageTime < 1000) { @@ -231,20 +127,6 @@ function sendGlances(adapter, obj) { }); } -/** - * Decrypt the password/value with given key - * @param {string} key - Secret key - * @param {string} value - value to decript - * @returns {string} - */ -function decrypt(key, value) { - let result = ''; - for(let i = 0; i < value.length; i++) { - result += String.fromCharCode(key[i % key.length].charCodeAt(0) ^ value.charCodeAt(i)); - } - return result; -} - function main(adapter) { // do nothing. Only answer on messages. if (!adapter.config.user || !adapter.config.token) { @@ -306,7 +188,11 @@ function sendNotification(adapter, message, callback) { pushover.send(message, (err, result) => { if (err) { - adapter.log.error('Cannot send notification: ' + JSON.stringify(err)); + try { + adapter.log.error('Cannot send notification: ' + JSON.stringify(err)); + } catch (err) { + adapter.log.error('Cannot send notification: Error'); + } callback && callback(err); return false; } else { diff --git a/package.json b/package.json index 85018b8..59263b0 100644 --- a/package.json +++ b/package.json @@ -20,14 +20,14 @@ "dependencies": { "pushover-notifications": "^1.2.2", "@iobroker/adapter-core": "^2.5.1", - "axios": "^0.24.0" + "axios": "^0.26.0" }, "devDependencies": { - "@alcalzone/release-script": "^2.2.1", + "@alcalzone/release-script": "^2.2.2", "gulp": "^4.0.2", - "mocha": "^9.2.0", + "mocha": "^9.2.1", "chai": "^4.3.6", - "@iobroker/testing": "^2.5.3" + "@iobroker/testing": "^2.5.4" }, "bugs": { "url": "https://github.com/ioBroker/ioBroker.pushover/issues"