From 9016e0871cce5d6eac245e30cb627d860f77dd65 Mon Sep 17 00:00:00 2001 From: Grzegorz Date: Sun, 18 Aug 2024 22:23:18 +0200 Subject: [PATCH] release v3.0.2 --- CHANGELOG.md | 10 ++++++++- homebridge-ui/server.js | 3 --- package.json | 2 +- src/localApi/sgcrypto.js | 6 ++---- src/localApi/xboxlocalapi.js | 29 ++++++++++++------------- src/webApi/authentication.js | 41 ++++++++++++++++++------------------ src/webApi/xboxwebapi.js | 28 ++++++++++++------------ 7 files changed, 59 insertions(+), 60 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e26ebae..2cc3620 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### NOTE -## After update to 2.x.x the plugin settings (xboxLiveId) need to be updated +### After update to 2.x.x the plugin settings (xboxLiveId) need to be updated +### After update to v3.0.0 RESTFull and MQTT config settings need to be updated + +## [3.0.2] - (18.08.2024) + +## Changes + +- fixed authorization manager [#204](https://github.com/grzegorz914/homebridge-xbox-tv/issues/204) +- cleanup ## [3.0.0] - (14.08.2024) diff --git a/homebridge-ui/server.js b/homebridge-ui/server.js index 3090b6c..e0d6491 100644 --- a/homebridge-ui/server.js +++ b/homebridge-ui/server.js @@ -30,9 +30,6 @@ class PluginUiServer extends HomebridgePluginUiServer { tokensFile: tokensFile } const authentication = new Authentication(authConfig); - authentication.on('error', (error) => { -; - }) let data = {}; switch (mode) { diff --git a/package.json b/package.json index 16839cd..55b909f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "displayName": "Xbox TV", "name": "homebridge-xbox-tv", - "version": "3.0.1", + "version": "3.0.2", "description": "Homebridge plugin to control Xbox game consoles.", "license": "MIT", "author": "grzegorz914", diff --git a/src/localApi/sgcrypto.js b/src/localApi/sgcrypto.js index f7c4724..78a31a9 100644 --- a/src/localApi/sgcrypto.js +++ b/src/localApi/sgcrypto.js @@ -4,11 +4,9 @@ const Crypto = require('crypto'); const EOL = require('os').EOL; const EC = require('elliptic').ec; const IV = Buffer.from('\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'); -const EventEmitter = require('events'); -class SGCRYPTO extends EventEmitter { +class SGCRYPTO { constructor() { - super(); this.key = false; this.iv = false; this.hashKey = false; @@ -61,7 +59,7 @@ class SGCRYPTO extends EventEmitter { return data; } catch (error) { - this.emit('error', `sign public key error: ${error}`); + throw new Error(`sign public key error: ${error}`); }; }; diff --git a/src/localApi/xboxlocalapi.js b/src/localApi/xboxlocalapi.js index 6fd998d..4e8a05c 100644 --- a/src/localApi/xboxlocalapi.js +++ b/src/localApi/xboxlocalapi.js @@ -16,9 +16,6 @@ class XBOXLOCALAPI extends EventEmitter { super(); this.crypto = new SGCrypto(); - this.crypto.on('error', (error) => { - this.emit('error', error); - }); this.host = config.host; this.xboxLiveId = config.xboxLiveId; @@ -373,7 +370,7 @@ class XBOXLOCALAPI extends EventEmitter { const tokenData = parseData.xsts.Token.length > 0 ? parseData : false; return tokenData; } catch (error) { - this.emit('error', `Read token error: ${error}`); + throw new Error(`Read token error: ${error}`); } } @@ -392,7 +389,7 @@ class XBOXLOCALAPI extends EventEmitter { return true; } catch (error) { - this.emit('error', error); + throw new Error(error); }; }; @@ -408,7 +405,7 @@ class XBOXLOCALAPI extends EventEmitter { await this.sendSocketMessage(message, 'acknowledge'); return true; } catch (error) { - this.emit('error', error); + throw new Error(error); }; } @@ -427,7 +424,7 @@ class XBOXLOCALAPI extends EventEmitter { await this.sendSocketMessage(message, 'channelStartRequest'); return true; } catch (error) { - this.emit('error', error); + throw new Error(error); } } @@ -454,7 +451,7 @@ class XBOXLOCALAPI extends EventEmitter { this.emit('disconnected', 'Power On failed, please try again.'); } catch (error) { this.emit('disconnected', 'Power On error, please try again.'); - this.emit('error', error); + throw new Error(error); }; }; @@ -475,7 +472,7 @@ class XBOXLOCALAPI extends EventEmitter { await this.disconnect(); return true; } catch (error) { - this.emit('error', error); + throw new Error(error); }; }; @@ -495,7 +492,7 @@ class XBOXLOCALAPI extends EventEmitter { await this.sendSocketMessage(message, 'recordGameDvr'); return true; } catch (error) { - this.emit('error', error); + throw new Error(error); }; }; @@ -509,7 +506,7 @@ class XBOXLOCALAPI extends EventEmitter { const channelOpen = this.channels[requestId].open; if (channelCommunicationId === -1 || !channelOpen) { - this.emit('error', `Channel Id: ${channelCommunicationId}, state: ${channelOpen ? 'Open' : 'Closed'}, trying to open it.`); + this.emit('error', `Channel Id: ${channelCommunicationId}, state: ${channelOpen ? 'Open' : 'Closed'}, trying to open it.`); }; command = CONSTANTS.LocalApi.Channels.System[channelName][command]; @@ -534,7 +531,7 @@ class XBOXLOCALAPI extends EventEmitter { await this.sendSocketMessage(message, 'gamepadUnpress'); }, 150) } catch (error) { - this.emit('error', `Send system input command error: ${error}`) + this.emit('error', `Send system input command error: ${error}`) }; break; case 1: @@ -554,7 +551,7 @@ class XBOXLOCALAPI extends EventEmitter { const message = json.pack(this.crypto, sequenceNumber1, this.sourceParticipantId, channelCommunicationId); this.sendSocketMessage(message, 'json'); } catch (error) { - this.emit('error', `Send tv remote command error: ${error}`) + this.emit('error', `Send tv remote command error: ${error}`) }; break; case 2: @@ -571,7 +568,7 @@ class XBOXLOCALAPI extends EventEmitter { this.sendSocketMessage(message, 'mediaCommand'); this.mediaRequestId++; } catch (error) { - this.emit('error', `Send system media command error: ${error}`) + this.emit('error', `Send system media command error: ${error}`) }; break; } @@ -601,7 +598,7 @@ class XBOXLOCALAPI extends EventEmitter { this.emit('disconnected', 'Disconnected.'); return true; } catch (error) { - this.emit('error', error); + throw new Error(error); }; }; @@ -617,7 +614,7 @@ class XBOXLOCALAPI extends EventEmitter { this.socket.send(message, offset, length, 5050, this.host, (error, bytes) => { if (error) { - this.emit('error', error); + throw new Error(error); } const debug = this.debugLog ? this.emit('debug', `Socket send: ${type}, ${bytes}B.`) : false; diff --git a/src/webApi/authentication.js b/src/webApi/authentication.js index 44d6be6..ea02888 100644 --- a/src/webApi/authentication.js +++ b/src/webApi/authentication.js @@ -3,12 +3,10 @@ const QueryString = require('querystring'); const fs = require('fs'); const fsPromises = fs.promises; const axios = require('axios'); -const EventEmitter = require('events'); const CONSTANTS = require('../constants.json'); -class Authentication extends EventEmitter{ +class Authentication { constructor(config) { - super(); this.webApiClientId = config.webApiClientId || CONSTANTS.WebApi.ClientId; this.webApiClientSecret = config.webApiClientSecret; this.tokensFile = config.tokensFile; @@ -32,7 +30,7 @@ class Authentication extends EventEmitter{ await this.refreshTokens('xsts'); return true; } catch (error) { - this.emit('error', error); + throw new Error(error); }; break; case false: @@ -40,7 +38,7 @@ class Authentication extends EventEmitter{ await this.refreshTokens('xsts'); return true; } catch (error) { - this.emit('error', error); + throw new Error(error); }; break; } @@ -50,7 +48,7 @@ class Authentication extends EventEmitter{ await this.refreshTokens('xsts'); return true; } catch (error) { - this.emit('error', error); + throw new Error(error); }; } break; @@ -64,7 +62,7 @@ class Authentication extends EventEmitter{ await this.refreshTokens('xsts'); return true; } catch (error) { - this.emit('error', error); + throw new Error(error); }; break; case false: @@ -76,12 +74,12 @@ class Authentication extends EventEmitter{ await this.getXstsToken(this.tokens.user.Token); return true; } catch (error) { - this.emit('error', error); + throw new Error(error); }; } break; default: - this.emit('error', `Unknow refresh token type: ${type}.`); + throw new Error(`Unknow refresh token type: ${type}.`); break; } } @@ -104,7 +102,7 @@ class Authentication extends EventEmitter{ this.tokens.oauth = refreshToken; return true; } catch (error) { - this.emit('error', error); + throw new Error(error); }; } @@ -128,7 +126,7 @@ class Authentication extends EventEmitter{ this.tokens.xsts = {}; return true; } catch (error) { - this.emit('error', error); + throw new Error(error); }; } @@ -150,7 +148,7 @@ class Authentication extends EventEmitter{ this.tokens.xsts = xstsToken; return true; } catch (error) { - this.emit('error', error); + throw new Error(error); }; } @@ -174,7 +172,7 @@ class Authentication extends EventEmitter{ await this.saveTokens(this.tokens); return true; } catch (error) { - this.emit('error', error); + throw new Error(error); }; } @@ -183,19 +181,20 @@ class Authentication extends EventEmitter{ try { const tokens = await this.readTokens(); this.tokens = !tokens ? this.tokens : tokens; + const refreshToken = this.tokens.oauth.refresh_token ?? false; - if (this.tokens.oauth.refresh_token) { + if (refreshToken) { await this.refreshTokens('user'); await this.saveTokens(this.tokens); return { headers: `XBL3.0 x=${this.tokens.xsts.DisplayClaims.xui[0].uhs};${this.tokens.xsts.Token}`, tokens: this.tokens }; } else { - this.emit('error', 'No oauth token found. Use authorization manager first.') + throw new Error('No oauth token found. Use authorization manager first.') } } catch (error) { - this.emit('error', error); + throw new Error(error); }; } else { - this.emit('error', `Authorization not possible, check plugin settings - Client Id: ${this.webApiClientId}`); + throw new Error(`Authorization not possible, check plugin settings - Client Id: ${this.webApiClientId}`); } } @@ -212,7 +211,7 @@ class Authentication extends EventEmitter{ const oauth2URI = `${CONSTANTS.WebApi.Url.oauth2}?${params}`; return oauth2URI; } catch (error) { - this.emit('error', error); + throw new Error(error); }; } @@ -222,7 +221,7 @@ class Authentication extends EventEmitter{ const tokens = data.length > 0 ? JSON.parse(data) : false; return tokens; } catch (error) { - this.emit('error', error); + throw new Error(error); }; } @@ -232,7 +231,7 @@ class Authentication extends EventEmitter{ await fsPromises.writeFile(this.tokensFile, tokens); return true; } catch (error) { - this.emit('error', error); + throw new Error(error); }; } @@ -246,7 +245,7 @@ class Authentication extends EventEmitter{ await fsPromises.writeFile(this.tokensFile, tokens); return true; } catch (error) { - this.emit('error', error); + throw new Error(error); }; } } diff --git a/src/webApi/xboxwebapi.js b/src/webApi/xboxwebapi.js index 0f8f312..285748b 100644 --- a/src/webApi/xboxwebapi.js +++ b/src/webApi/xboxwebapi.js @@ -86,7 +86,7 @@ class XBOXWEBAPI extends EventEmitter { //await this.userProfile(); return true;; } catch (error) { - this.emit('error', `get xbox live data error: ${error}`); + throw new Error(`get xbox live data error: ${error}`); }; }; @@ -121,7 +121,7 @@ class XBOXWEBAPI extends EventEmitter { return remoteManagementEnabled; } catch (error) { - this.emit('error', `get status error: ${error}`); + throw new Error(`get status error: ${error}`); }; } @@ -201,7 +201,7 @@ class XBOXWEBAPI extends EventEmitter { return true; } catch (error) { - this.emit('error', `Consoles list error: ${error}`); + throw new Error(`Consoles list error: ${error}`); }; } @@ -252,7 +252,7 @@ class XBOXWEBAPI extends EventEmitter { return true; } catch (error) { - this.emit('error', `get installed apps error: ${error}`); + throw new Error(`get installed apps error: ${error}`); }; } @@ -295,7 +295,7 @@ class XBOXWEBAPI extends EventEmitter { return true; } catch (error) { - this.emit('error', `get storage devices error: ${error}`); + throw new Error(`get storage devices error: ${error}`); }; } @@ -338,7 +338,7 @@ class XBOXWEBAPI extends EventEmitter { return true; } catch (error) { - this.emit('error', `User profile error: ${error}`); + throw new Error(`User profile error: ${error}`); }; } @@ -362,7 +362,7 @@ class XBOXWEBAPI extends EventEmitter { return true; } catch (error) { - this.emit('error', error); + throw new Error(error); } }; @@ -371,7 +371,7 @@ class XBOXWEBAPI extends EventEmitter { await this.send('Media', 'Next'); return true;; } catch (error) { - this.emit('error', error); + throw new Error(error); }; } @@ -380,7 +380,7 @@ class XBOXWEBAPI extends EventEmitter { await this.send('Media', 'Previous'); return true;; } catch (error) { - this.emit('error', error); + throw new Error(error); }; } @@ -389,7 +389,7 @@ class XBOXWEBAPI extends EventEmitter { await this.send('Media', 'Pause'); return true;; } catch (error) { - this.emit('error', error); + throw new Error(error); }; } @@ -398,7 +398,7 @@ class XBOXWEBAPI extends EventEmitter { await this.send('Media', 'Play'); return true;; } catch (error) { - this.emit('error', error); + throw new Error(error); }; } @@ -407,13 +407,13 @@ class XBOXWEBAPI extends EventEmitter { await this.send('Shell', 'GoBack'); return true;; } catch (error) { - this.emit('error', error); + throw new Error(error); }; } async send(commandType, command, payload) { if (!this.authorized) { - this.emit('error', 'not authorized.'); + throw new Error('not authorized.'); }; const sessionid = UuIdv4(); @@ -440,7 +440,7 @@ class XBOXWEBAPI extends EventEmitter { return true; } catch (error) { - this.emit('error', `send command type: ${commandType}, command: ${command}, params: ${params}, error: ${error}`); + throw new Error(`send command type: ${commandType}, command: ${command}, params: ${params}, error: ${error}`); }; } }