Skip to content

Commit

Permalink
cloud: use permanent cloudflare hostname if token is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Oct 4, 2023
1 parent 7b56e86 commit 2237eb3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 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 @@ -54,5 +54,5 @@
"@types/nat-upnp": "^1.1.2",
"@types/node": "^20.4.5"
},
"version": "0.1.43"
"version": "0.2.1"
}
18 changes: 15 additions & 3 deletions plugins/cloud/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ class ScryptedCloud extends ScryptedDeviceBase implements OauthClient, Settings,
randomBytes = crypto.randomBytes(16).toString('base64');
reverseConnections = new Set<Duplex>();

get cloudflareTunnelHost() {
if (!this.cloudflareTunnel)
return;
return new URL(this.cloudflareTunnel).host;
}

constructor() {
super();

Expand Down Expand Up @@ -340,11 +346,14 @@ class ScryptedCloud extends ScryptedDeviceBase implements OauthClient, Settings,

ip = this.storageSettings.values.duckDnsHostname;
}
else if (this.cloudflareTunnelHost) {
ip = this.cloudflareTunnelHost;
}
else {
ip = (await axios(`https://${SCRYPTED_SERVER}/_punch/ip`)).data.ip;
}

if (this.storageSettings.values.forwardingMode === 'Custom Domain')
if (this.storageSettings.values.forwardingMode === 'Custom Domain' || this.cloudflareTunnelHost)
upnpPort = 443;

this.console.log(`Scrypted Cloud mapped https://${ip}:${upnpPort} to https://127.0.0.1:${this.securePort}`);
Expand All @@ -355,7 +364,7 @@ class ScryptedCloud extends ScryptedDeviceBase implements OauthClient, Settings,

const registrationId = await this.manager.registrationId;
const data = await this.sendRegistrationId(registrationId);
if (ip !== 'localhost' && ip !== data.ip_address) {
if (ip !== 'localhost' && ip !== data.ip_address && ip !== this.cloudflareTunnelHost) {
this.log.a(`Scrypted Cloud could not verify the IP Address of your custom domain ${this.storageSettings.values.hostname}.`);
}
this.storageSettings.values.lastPersistedIp = ip;
Expand Down Expand Up @@ -413,7 +422,7 @@ class ScryptedCloud extends ScryptedDeviceBase implements OauthClient, Settings,
return this.updatePortForward(upnpPort);

if (this.storageSettings.values.forwardingMode === 'Custom Domain')
return this.updatePortForward(upnpPort);
return this.updatePortForward(this.storageSettings.values.upnpPort);

const [localAddress] = await endpointManager.getLocalAddresses() || [];
if (!localAddress) {
Expand Down Expand Up @@ -512,6 +521,8 @@ class ScryptedCloud extends ScryptedDeviceBase implements OauthClient, Settings,
cloudAddresses.push(this.cloudflareTunnel);

await addresses.setExternalAddresses('@scrypted/cloud', cloudAddresses);

await this.updatePortForward(this.storageSettings.values.upnpPort);
}

getAuthority() {
Expand Down Expand Up @@ -606,6 +617,7 @@ class ScryptedCloud extends ScryptedDeviceBase implements OauthClient, Settings,

getSSLHostname() {
const validDomain = (this.storageSettings.values.forwardingMode === 'Custom Domain' && this.storageSettings.values.hostname)
|| (this.storageSettings.values.cloudflaredTunnelToken && this.cloudflareTunnelHost)
|| (this.storageSettings.values.duckDnsCertValid && this.storageSettings.values.duckDnsHostname && this.storageSettings.values.upnpPort && `${this.storageSettings.values.duckDnsHostname}:${this.storageSettings.values.upnpPort}`);
return validDomain;
}
Expand Down

0 comments on commit 2237eb3

Please sign in to comment.