Skip to content

Commit

Permalink
cloud: add option to disable cloud registration completely to suppres…
Browse files Browse the repository at this point in the history
…s warnings.
  • Loading branch information
koush committed Feb 28, 2024
1 parent 2f1c45f commit 441cd0d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 24 deletions.
4 changes: 2 additions & 2 deletions plugins/cloud/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/cloud/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@
"@types/nat-upnp": "^1.1.5",
"@types/node": "^20.11.19"
},
"version": "0.2.9"
"version": "0.2.10"
}
67 changes: 46 additions & 21 deletions plugins/cloud/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ class ScryptedCloud extends ScryptedDeviceBase implements OauthClient, Settings,
description: 'The name of this server. This is used to identify this server in the Scrypted Cloud.',
persistedDefaultValue: os.hostname()?.split('.')[0] || 'Scrypted Server',
},
connectHomeScryptedApp: {
group: 'Connection',
title: `Connect to ${SCRYPTED_SERVER}`,
description: `Connect this server to ${SCRYPTED_SERVER}. This is required to use the Scrypted Cloud.`,
type: 'boolean',
persistedDefaultValue: true,
},
register: {
group: 'Connection',
title: 'Register',
Expand Down Expand Up @@ -298,7 +305,7 @@ class ScryptedCloud extends ScryptedDeviceBase implements OauthClient, Settings,
if (!this.storageSettings.values.certificate)
this.storageSettings.values.certificate = createSelfSignedCertificate();

this.setupProxyServer();
const proxy = this.setupProxyServer();
this.setupCloudPush();
this.updateCors();

Expand All @@ -317,7 +324,7 @@ class ScryptedCloud extends ScryptedDeviceBase implements OauthClient, Settings,
this.storageSettings.values.token_info = process.env.SCRYPTED_CLOUD_TOKEN;
this.manager.registrationId.then(r => {
this.sendRegistrationId(r, true);
observeRegistrations();
proxy.then(observeRegistrations);
});
}
else {
Expand All @@ -326,7 +333,7 @@ class ScryptedCloud extends ScryptedDeviceBase implements OauthClient, Settings,
this.sendRegistrationId(registrationId);
});

observeRegistrations();
proxy.then(observeRegistrations);
}
}

Expand Down Expand Up @@ -450,7 +457,7 @@ class ScryptedCloud extends ScryptedDeviceBase implements OauthClient, Settings,
let { upnpPort } = this.storageSettings.values;

if (!upnpPort)
upnpPort = Math.round(Math.random() * 30000 + 20000);
upnpPort = Math.round(Math.random() * 20000 + 40000);

if (this.storageSettings.values.forwardingMode === 'Disabled') {
this.updatePortForward(upnpPort);
Expand Down Expand Up @@ -621,27 +628,45 @@ class ScryptedCloud extends ScryptedDeviceBase implements OauthClient, Settings,
force: force ? 'true' : '',
});

if (!this.storageSettings.values.connectHomeScryptedApp) {
return {
error: `Scrypted Cloud connection to ${SCRYPTED_SERVER} is disabled.`,
};
}

const { token_info } = this.storageSettings.values;
if (!token_info)
throw new Error('Scrypted Cloud is not logged in. Skipping home.scrypted.app registration.');
const response = await httpFetch({
url: `https://${SCRYPTED_SERVER}/_punch/register?${q}`,
headers: {
Authorization: `Bearer ${token_info}`
},
responseType: 'json',
});
const error = response.body?.error;
if (error) {
this.console.log('registration error', response.body);
if (!token_info) {
const error = `Login to the Scrypted Cloud plugin to reach this server from the cloud, or disable this alert in the Scrypted Cloud plugin Connection settings.`;
this.log.a(error);
return response.body;
return {
error,
};
}
try {
const response = await httpFetch({
url: `https://${SCRYPTED_SERVER}/_punch/register?${q}`,
headers: {
Authorization: `Bearer ${token_info}`
},
responseType: 'json',
})
const error = response.body?.error;
if (error) {
this.console.log('registration error', response.body);
this.log.a(error);
return response.body;
}

this.console.log('registered', response.body);
this.storageSettings.values.lastPersistedRegistrationId = registration_id;
this.storageSettings.values.lastPersistedUpnpPort = authority.upnp_port;
return response.body;
this.console.log('registered', response.body);
this.storageSettings.values.lastPersistedRegistrationId = registration_id;
this.storageSettings.values.lastPersistedUpnpPort = authority.upnp_port;
return response.body;
}
catch (e) {
return {
error: e.toString(),
};
}
}

async setupCloudPush() {
Expand Down

0 comments on commit 441cd0d

Please sign in to comment.