Skip to content

Commit

Permalink
cloud: publish beta
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Aug 17, 2023
1 parent 4e97443 commit ff28238
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 168 deletions.
196 changes: 31 additions & 165 deletions plugins/cloud/package-lock.json

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

6 changes: 5 additions & 1 deletion plugins/cloud/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,17 @@
"@scrypted/sdk": "file:../../sdk",
"axios": "^1.4.0",
"bpmux": "^8.2.1",
"cloudflared": "^0.4.0",
"exponential-backoff": "^3.1.1",
"http-proxy": "^1.18.1",
"ip": "^1.1.8",
"nat-upnp": "file:./external/node-nat-upnp"
},
"devDependencies": {
"@types/http-proxy": "^1.17.11",
"@types/ip": "^1.1.0",
"@types/nat-upnp": "^1.1.2",
"@types/node": "^20.4.5"
},
"version": "0.1.27"
"version": "0.1.29"
}
35 changes: 33 additions & 2 deletions plugins/cloud/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import { createSelfSignedCertificate } from '../../../server/src/cert';
import { PushManager } from './push';
import { readLine } from '../../../common/src/read-stream';
import { qsparse, qsstringify } from "./qs";
import * as cloudflared from 'cloudflared';
import fs, { mkdirSync } from 'fs';
import { backOff } from "exponential-backoff";
import ip from 'ip';

// import { registerDuckDns } from "./greenlock";

const { deviceManager, endpointManager, systemManager } = sdk;
Expand Down Expand Up @@ -45,6 +50,7 @@ class ScryptedPush extends ScryptedDeviceBase implements BufferConverter {
}

class ScryptedCloud extends ScryptedDeviceBase implements OauthClient, Settings, BufferConverter, DeviceProvider, HttpRequestHandler {
cloudflareTunnel: string;
manager = new PushManager(DEFAULT_SENDER_ID);
server: http.Server;
secureServer: https.Server;
Expand Down Expand Up @@ -313,7 +319,7 @@ class ScryptedCloud extends ScryptedDeviceBase implements OauthClient, Settings,
if (this.storageSettings.values.forwardingMode === 'Custom Domain')
upnpPort = 443;

this.console.log(`Mapped port https://127.0.0.1:${this.securePort} to https://${ip}:${upnpPort}`);
this.console.log(`Scrypted Cloud mapped https://${ip}:${upnpPort} to https://127.0.0.1:${this.securePort}`);

// the ip is not sent, but should be checked to see if it changed.
if (this.storageSettings.values.lastPersistedUpnpPort !== upnpPort || ip !== this.storageSettings.values.lastPersistedIp) {
Expand Down Expand Up @@ -717,7 +723,8 @@ class ScryptedCloud extends ScryptedDeviceBase implements OauthClient, Settings,
this.proxy.on('proxyRes', (res, req) => {
res.headers['X-Scrypted-Cloud'] = req.headers['x-scrypted-cloud'];
res.headers['X-Scrypted-Direct-Address'] = req.headers['x-scrypted-direct-address'];
res.headers['Access-Control-Expose-Headers'] = 'X-Scrypted-Cloud, X-Scrypted-Direct-Address';
res.headers['X-Scrypted-Cloud-Address'] = this.cloudflareTunnel;
res.headers['Access-Control-Expose-Headers'] = 'X-Scrypted-Cloud, X-Scrypted-Direct-Address, X-Scrypted-Cloud-Address';
});

let backoff = 0;
Expand Down Expand Up @@ -771,6 +778,30 @@ class ScryptedCloud extends ScryptedDeviceBase implements OauthClient, Settings,
});
}
});


backOff(async () => {
try {
const pluginVolume = process.env.SCRYPTED_PLUGIN_VOLUME;
const cloudflareD = path.join(pluginVolume, 'cloudflare.d');
mkdirSync(cloudflareD, {
recursive: true,
})
process.chdir(cloudflareD);

if (!fs.existsSync(cloudflared.bin))
await cloudflared.install(cloudflared.bin);
const insecureUrl = `http://127.0.0.1:${port}`;
const cloudflareTunnel = cloudflared.tunnel({
'--url': insecureUrl,
});
this.cloudflareTunnel = await cloudflareTunnel.url;
this.console.log(`cloudflare url mapped ${this.cloudflareTunnel} to ${insecureUrl}`);
}
catch (e) {
this.cloudflareTunnel = undefined;
}
});
}

ensureReverseConnections(registrationId: string) {
Expand Down

0 comments on commit ff28238

Please sign in to comment.