From 1ab080ddbeced16d03050882731b7ff3359a2d00 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Thu, 2 Jun 2022 19:20:40 -0700 Subject: [PATCH] cloud: add necessary cors servers --- plugins/cloud/package-lock.json | 2 +- plugins/cloud/src/main.ts | 46 ++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/plugins/cloud/package-lock.json b/plugins/cloud/package-lock.json index 9387399d6..4b58d6c45 100644 --- a/plugins/cloud/package-lock.json +++ b/plugins/cloud/package-lock.json @@ -23,7 +23,7 @@ }, "../../sdk": { "name": "@scrypted/sdk", - "version": "0.0.192", + "version": "0.0.198", "dev": true, "license": "ISC", "dependencies": { diff --git a/plugins/cloud/src/main.ts b/plugins/cloud/src/main.ts index bb8af48dd..8c1a28e39 100644 --- a/plugins/cloud/src/main.ts +++ b/plugins/cloud/src/main.ts @@ -11,8 +11,9 @@ import { once } from 'events'; import path from 'path'; import bpmux from 'bpmux'; import { PushManager } from './push'; +import type { CORSServer } from '../../../server/src/services/cors'; -const { deviceManager, endpointManager } = sdk; +const { deviceManager, endpointManager, systemManager } = sdk; export const DEFAULT_SENDER_ID = '827888101440'; const SCRYPTED_SERVER = 'home.scrypted.app'; @@ -97,6 +98,48 @@ class ScryptedCloud extends ScryptedDeviceBase implements OauthClient, Settings, // if ever adding clockwork push, uncomment this. // this.sendRegistrationId(); }); + + this.updateCors(); + } + + async updateCors() { + try { + const corsControl = await systemManager.getComponent('cors'); + let cors: CORSServer[] = await corsControl.getCORS(); + cors = cors.filter(entry => entry.tag !== '@scrypted/cloud'); + cors.push( + { + tag: '@scrypted/cloud', + server: 'https://home.scrypted.app', + }, + { + tag: '@scrypted/cloud', + server: 'http://home.scrypted.app', + }, + // test + // { + // tag: '@scrypted/cloud', + // server: 'https://localhost:8081', + // }, + ); + const hostname = this.storage.getItem('hostname') + if (hostname) { + cors.push( + { + tag: '@scrypted/cloud', + server: `https://${hostname}`, + }, + { + tag: '@scrypted/cloud', + server: `http://${hostname}`, + }, + ); + } + await corsControl.setCORS(cors); + } + catch (e) { + this.console.error('error updating cors, is your scrypted server up to date?', e); + } } async sendRegistrationId(registrationId: string) { @@ -165,6 +208,7 @@ class ScryptedCloud extends ScryptedDeviceBase implements OauthClient, Settings, async putSetting(key: string, value: string | number | boolean) { this.storage.setItem(key, value.toString()); + this.updateCors(); } async getCloudMessagePath() {