From 8e174842fa7673b84b5e46ff6ecc546f105ac79b Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Thu, 26 Jan 2023 14:35:13 -0800 Subject: [PATCH] cloud: fix upnp port persistence bug --- plugins/cloud/.vscode/settings.json | 2 +- plugins/cloud/package-lock.json | 4 ++-- plugins/cloud/package.json | 2 +- plugins/cloud/src/main.ts | 18 ++++++++++++++---- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/plugins/cloud/.vscode/settings.json b/plugins/cloud/.vscode/settings.json index 44d4d203f..77ccdbd6d 100644 --- a/plugins/cloud/.vscode/settings.json +++ b/plugins/cloud/.vscode/settings.json @@ -1,4 +1,4 @@ { - "scrypted.debugHost": "koushik-ubuntu", + "scrypted.debugHost": "127.0.0.1", } \ No newline at end of file diff --git a/plugins/cloud/package-lock.json b/plugins/cloud/package-lock.json index 7da5c32a1..201fe09c9 100644 --- a/plugins/cloud/package-lock.json +++ b/plugins/cloud/package-lock.json @@ -1,12 +1,12 @@ { "name": "@scrypted/cloud", - "version": "0.0.56", + "version": "0.0.57", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@scrypted/cloud", - "version": "0.0.56", + "version": "0.0.57", "dependencies": { "@eneris/push-receiver": "../../external/push-receiver", "@scrypted/common": "file:../../common", diff --git a/plugins/cloud/package.json b/plugins/cloud/package.json index f6c4a2d38..103f632c6 100644 --- a/plugins/cloud/package.json +++ b/plugins/cloud/package.json @@ -50,5 +50,5 @@ "@types/nat-upnp": "^1.1.2", "@types/node": "^18.11.18" }, - "version": "0.0.56" + "version": "0.0.57" } diff --git a/plugins/cloud/src/main.ts b/plugins/cloud/src/main.ts index ffb66534e..7392a3000 100644 --- a/plugins/cloud/src/main.ts +++ b/plugins/cloud/src/main.ts @@ -111,6 +111,11 @@ class ScryptedCloud extends ScryptedDeviceBase implements OauthClient, Settings, this.updatePortForward(this.storageSettings.values.upnpPort); }, }, + register: { + group: 'Advanced', + title: 'Register', + type: 'button', + }, }); upnpInterval: NodeJS.Timeout; upnpClient = upnp.createClient(); @@ -120,6 +125,11 @@ class ScryptedCloud extends ScryptedDeviceBase implements OauthClient, Settings, constructor() { super(); + this.storageSettings.settings.register.onPut = async () => { + const registrationId = await this.manager.registrationId; + await this.sendRegistrationId(registrationId); + } + this.storageSettings.settings.upnpStatus.onGet = async () => { return { hide: this.storageSettings.values.forwardingMode !== 'UPNP', @@ -197,11 +207,10 @@ class ScryptedCloud extends ScryptedDeviceBase implements OauthClient, Settings, this.console.log('Registering UPNP IP and Port', ip, upnpPort); const registrationId = await this.manager.registrationId; - const data = await this.sendRegistrationId(registrationId, upnpPort); + const data = await this.sendRegistrationId(registrationId); if (this.storageSettings.values.hostname && ip !== data.ip_address) { this.log.a(`Scrypted Cloud could not verify the IP Address of your custom domain ${this.storageSettings.values.hostname}.`); } - this.storageSettings.values.lastPersistedUpnpPort = upnpPort; this.storageSettings.values.lastPersistedIp = ip; } } @@ -348,10 +357,10 @@ class ScryptedCloud extends ScryptedDeviceBase implements OauthClient, Settings, } } - async sendRegistrationId(registration_id: string, upnp_port?: number) { + async sendRegistrationId(registration_id: string) { const registration_secret = this.storageSettings.values.registrationSecret || crypto.randomBytes(8).toString('base64'); const q = qs.stringify({ - upnp_port: upnp_port || this.storageSettings.values.lastPersistedUpnpPort, + upnp_port: this.storageSettings.values.upnpPort, registration_id, sender_id: DEFAULT_SENDER_ID, registration_secret, @@ -366,6 +375,7 @@ class ScryptedCloud extends ScryptedDeviceBase implements OauthClient, Settings, }); this.console.log('registered', response.data); this.storageSettings.values.lastPersistedRegistrationId = registration_id; + this.storageSettings.values.lastPersistedUpnpPort = this.storageSettings.values.upnpPort; this.storageSettings.values.registrationSecret = registration_secret; return response.data; }