Compare commits

..

31 Commits

Author SHA1 Message Date
Koushik Dutta
f5a32489d7 server: prevent windows from clobbering python path 2024-01-24 23:46:58 -08:00
Koushik Dutta
135ad8e3a8 rebroadcast: add id suffix to rtsp urls to determine ffmpeg usage 2024-01-24 13:52:35 -08:00
Koushik Dutta
3c4021c66b videoanalysis: disable filters for objects that are in detector provided zones 2024-01-23 21:42:36 -08:00
Koushik Dutta
669ab17772 docker: remove nvr storage config prompt 2024-01-23 15:47:48 -08:00
Koushik Dutta
1860d7d8ea Merge branch 'main' of github.com:koush/scrypted 2024-01-23 15:46:53 -08:00
Koushik Dutta
fa266e9dd1 docker: validate the storage directory 2024-01-23 20:10:02 +00:00
Koushik Dutta
4e2f3bf2c7 docker: finish drive setup script 2024-01-23 19:40:16 +00:00
Koushik Dutta
146e27fd13 install: initial pass at disk setup 2024-01-23 19:00:51 +00:00
Koushik Dutta
e4bb50375f postbeta 2024-01-22 20:15:16 -08:00
Koushik Dutta
9686315c02 postbeta 2024-01-22 19:57:24 -08:00
Koushik Dutta
520895f3aa postbeta 2024-01-22 19:50:20 -08:00
Koushik Dutta
ddffc49bcf postbeta 2024-01-22 19:22:20 -08:00
Koushik Dutta
a07f52445d postbeta 2024-01-22 19:13:41 -08:00
Koushik Dutta
5e7b203f11 postbeta 2024-01-22 19:01:13 -08:00
Koushik Dutta
d752298960 postbeta 2024-01-22 17:45:46 -08:00
Koushik Dutta
5253f29831 remove usage of NODE_* env variables which get sanitized by electron. 2024-01-22 17:45:26 -08:00
Koushik Dutta
58d674746d postrelease 2024-01-22 17:22:09 -08:00
Koushik Dutta
8a640758d1 server/cli: fix login issues 2024-01-22 09:11:02 -08:00
Koushik Dutta
9be913af26 sample-cameraprovider: update 2024-01-21 15:39:48 -08:00
Koushik Dutta
da17bee516 sdk: publish 2024-01-21 15:38:28 -08:00
Koushik Dutta
48d9790051 cli: fix https://github.com/koush/scrypted/issues/1277 2024-01-21 14:52:10 -08:00
Koushik Dutta
c43014348d sdk: prevent unnecessary JSON exceptions 2024-01-21 14:42:44 -08:00
Koushik Dutta
cec3a592ba client: add missing dependency 2024-01-21 14:42:24 -08:00
Koushik Dutta
c446ddcdf4 Merge branch 'main' of github.com:koush/scrypted 2024-01-21 12:45:24 -08:00
Koushik Dutta
72f79ea8ef core: fix certificate login error, fix backup/restore in ha 2024-01-21 12:45:19 -08:00
Brett Jia
41988699d0 server: expose backup as a service (#1275)
* server: expose backup as a service

* move restore into new backup service
2024-01-20 21:37:56 -08:00
Koushik Dutta
5151c520d4 Update config.yaml 2024-01-18 19:21:11 -08:00
Koushik Dutta
e1abe717fa postrelease 2024-01-18 13:30:02 -08:00
Koushik Dutta
c7a9ca06be server: use existing service control restart 2024-01-18 13:29:50 -08:00
Koushik Dutta
9827f15f5f server: pass through restart hook 2024-01-18 13:25:22 -08:00
Koushik Dutta
d245a722e2 postrelease 2024-01-18 13:20:25 -08:00
34 changed files with 318 additions and 165 deletions

View File

@@ -1,6 +1,6 @@
# Home Assistant Addon Configuration
name: Scrypted
version: "18-jammy-full.s6-v0.85.0"
version: "18-jammy-full.s6-v0.88.0"
slug: scrypted
description: Scrypted is a high performance home video integration and automation platform
url: "https://github.com/koush/scrypted"

View File

@@ -54,15 +54,6 @@ fi
echo "Setting permissions on $SCRYPTED_HOME"
chown -R $SERVICE_USER $SCRYPTED_HOME
echo "Optional:"
readyn "Edit docker-compose.yml to add external storage for Scrypted NVR?"
if [ "$yn" == "y" ]
then
apt install nano
nano $DOCKER_COMPOSE_YML
fi
set +e
echo "docker compose down"
@@ -85,3 +76,6 @@ echo "Scrypted is now running at: https://localhost:10443/"
echo "Note that it is https and that you'll be asked to approve/ignore the website certificate."
echo
echo
echo "Optional:"
echo "Scrypted NVR Recording storage directory can be configured with an additional script:"
echo "https://docs.scrypted.app/scrypted-nvr/installation.html#docker-volume"

View File

@@ -0,0 +1,140 @@
if [ -z "$SERVICE_USER" ]
then
echo "Scrypted SERVICE_USER environment variable was not specified. NVR Storage can not be configured."
exit 0
fi
if [ "$USER" != "root" ]
then
echo "$USER"
echo "This script must be run as sudo or root."
exit 1
fi
USER_HOME=$(eval echo ~$SERVICE_USER)
SCRYPTED_HOME=$USER_HOME/.scrypted
DOCKER_COMPOSE_YML=$SCRYPTED_HOME/docker-compose.yml
if [ ! -f "$DOCKER_COMPOSE_YML" ]
then
echo "$DOCKER_COMPOSE_YML not found. Install Scrypted first."
exit 1
fi
NVR_MOUNT_LINE=$(cat "$DOCKER_COMPOSE_YML" | grep :/nvr)
if [ -z "$NVR_MOUNT_LINE" ]
then
echo "Unexpected contents in $DOCKER_COMPOSE_YML. Rerun the Scrypted docker compose installer."
exit 1
fi
function backup() {
BACKUP_FILE="$1".scrypted-bak
if [ ! -f "$BACKUP_FILE" ]
then
cp "$1" "$BACKUP_FILE"
fi
}
backup "$DOCKER_COMPOSE_YML"
function readyn() {
while true; do
read -p "$1 (y/n) " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no. (y/n)";;
esac
done
}
if [ -z "$1" ]
then
lsblk
echo ""
echo "Please run the script with an existing mount path or the 'disk' device to format (e.g. sdx)."
exit 1
fi
function stopscrypted() {
cd "$SCRYPTED_HOME"
echo ""
echo "Stopping the Scrypted container. If there are any errors during disk setup, Scrypted will need to be manually restarted with:"
echo "cd $SCRYPTED_HOME && docker compose up -d"
echo ""
docker compose down
}
BLOCK_DEVICE="/dev/$1"
if [ -b "$BLOCK_DEVICE" ]
then
readyn "Format $BLOCK_DEVICE?"
if [ "$yn" == "n" ]
then
exit 1
fi
stopscrypted
umount "$BLOCK_DEVICE"1 2> /dev/null
umount "$BLOCK_DEVICE"2 2> /dev/null
umount /mnt/scrypted-nvr 2> /dev/null
set -e
parted "$BLOCK_DEVICE" --script mklabel gpt
parted -a optimal "$BLOCK_DEVICE" mkpart scrypted-nvr "0%" "100%"
set +e
sync
mkfs -F -t ext4 "$BLOCK_DEVICE"1
sync
# parse/evaluate blkid line as env vars
for attr in $(blkid | grep "$BLOCK_DEVICE")
do
e=$(echo $attr | grep =)
if [ ! -z "$e" ]
then
export "$e"
fi
done
if [ -z "$UUID" ]
then
echo "Error parsing disk UUID."
exit 1
fi
echo "UUID: $UUID"
set -e
backup "/etc/fstab"
grep -v "scrypted-nvr" /etc/fstab > /tmp/fstab && cp /tmp/fstab /etc/fstab
# ensure newline
sed -i -e '$a\' /etc/fstab
mkdir -p /mnt/scrypted-nvr
echo "PARTLABEL=scrypted-nvr /mnt/scrypted-nvr ext4 defaults 0 0" >> /etc/fstab
mount -a
set +e
DIR="/mnt/scrypted-nvr"
else
if [ ! -d "$1" ]
then
echo "$1 is not a valid directory."
exit 1
fi
stopscrypted
DIR="$1"
fi
ESCAPED_DIR=$(echo "$DIR" | sed s/\\//\\\\\\//g)
set -e
sed -i s/'^.*:\/nvr'/" - $ESCAPED_DIR:\/nvr"/ "$DOCKER_COMPOSE_YML"
sed -i s/'^.*SCRYPTED_NVR_VOLUME.*$'/" - SCRYPTED_NVR_VOLUME=\/nvr"/ "$DOCKER_COMPOSE_YML"
set +e
cd "$SCRYPTED_HOME" && docker compose up -d

View File

@@ -21,7 +21,7 @@
],
"preLaunchTask": "npm: build",
"args": [
"shell",
"login",
],
"sourceMaps": true,
"resolveSourceMapLocations": [

View File

@@ -1,12 +1,12 @@
{
"name": "scrypted",
"version": "1.3.6",
"version": "1.3.10",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "scrypted",
"version": "1.3.6",
"version": "1.3.10",
"license": "ISC",
"dependencies": {
"@scrypted/client": "^1.3.3",

View File

@@ -1,6 +1,6 @@
{
"name": "scrypted",
"version": "1.3.6",
"version": "1.3.10",
"description": "",
"main": "./dist/packages/cli/src/main.js",
"bin": {

View File

@@ -4,18 +4,13 @@ import { connectScryptedClient } from '@scrypted/client';
import { FFmpegInput, ScryptedMimeTypes } from '@scrypted/types';
import child_process from 'child_process';
import fs from 'fs';
import https from 'https';
import path from 'path';
import readline from 'readline-sync';
import semver from 'semver';
import { authHttpFetch } from '../../../common/src/http-auth-fetch';
import { httpFetch } from '../../../server/src/fetch/http-fetch';
import { installServe, serveMain } from './service';
import { connectShell } from './shell';
const httpsAgent = new https.Agent({
rejectUnauthorized: false,
});
if (!semver.gte(process.version, '16.0.0')) {
throw new Error('"node" version out of date. Please update node to v16 or higher.')
}
@@ -45,6 +40,12 @@ interface LoginFile {
[host: string]: Login;
}
function basicAuthHeaders(username: string, password: string) {
const headers = new Headers();
headers.set('Authorization', `Basic ${Buffer.from(username + ":" + password).toString('base64')}`);
return headers;
}
async function doLogin(host: string) {
host = toIpAndPort(host);
@@ -54,16 +55,15 @@ async function doLogin(host: string) {
});
const url = `https://${host}/login`;
const response = await authHttpFetch({
const response = await httpFetch({
method: 'GET',
credential: {
username,
password,
},
headers: basicAuthHeaders(username, password),
url,
rejectUnauthorized: false,
responseType: 'json',
});
if (response.body.error)
throw new Error(response.body.error);
fs.mkdirSync(scryptedHome, {
recursive: true,
@@ -81,13 +81,10 @@ async function doLogin(host: string) {
login[host] = response.body;
fs.writeFileSync(loginPath, JSON.stringify(login));
return login;
return login[host];
}
async function getOrDoLogin(host: string): Promise<{
username: string,
token: string,
}> {
async function getOrDoLogin(host: string): Promise<Login> {
let login: LoginFile;
try {
login = JSON.parse(fs.readFileSync(loginPath).toString());
@@ -96,11 +93,12 @@ async function getOrDoLogin(host: string): Promise<{
if (!login[host].username || !login[host].token)
throw new Error();
return login[host];
}
catch (e) {
login = await doLogin(host);
return doLogin(host);
}
return login[host];
}
async function runCommand() {
@@ -150,8 +148,8 @@ async function main() {
}
else if (process.argv[2] === 'login') {
const ip = process.argv[3] || '127.0.0.1';
const token = await doLogin(ip);
console.log('login successful. token:', token);
const login = await doLogin(ip);
console.log('login successful. token:', login.token);
}
else if (process.argv[2] === 'command') {
const { sdk, pendingResult } = await runCommand();
@@ -203,16 +201,15 @@ async function main() {
const login = await getOrDoLogin(ip);
const url = `https://${ip}/web/component/script/install/${pkg}`;
const response = await authHttpFetch({
const response = await httpFetch({
method: 'POST',
credential: {
username: login.username,
password: login.token,
},
headers: basicAuthHeaders(login.username, login.token),
url,
rejectUnauthorized: false,
responseType: 'json',
});
if (response.body.error)
throw new Error(response.body.error);
console.log('install successful. id:', response.body.id);
}
@@ -249,7 +246,6 @@ async function main() {
console.log(' npx scrypted install @scrypted/rtsp');
console.log(' npx scrypted install @scrypted/rtsp/0.0.51');
console.log(' npx scrypted install @scrypted/rtsp/0.0.51 192.168.2.100');
process.exit(1);
}
}

View File

@@ -1,6 +1,6 @@
{
"compilerOptions": {
"module": "commonjs",
"module": "Node16",
"target": "esnext",
"noImplicitAny": true,
"outDir": "./dist",
@@ -8,7 +8,7 @@
"sourceMap": true,
"inlineSources": true,
"declaration": true,
"resolveJsonModule": true,
"moduleResolution": "Node16",
},
"include": [
"src/**/*"

View File

@@ -1,12 +1,12 @@
{
"name": "@scrypted/core",
"version": "0.3.1",
"version": "0.3.2",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@scrypted/core",
"version": "0.3.1",
"version": "0.3.2",
"license": "Apache-2.0",
"dependencies": {
"@scrypted/common": "file:../../common",

View File

@@ -1,6 +1,6 @@
{
"name": "@scrypted/core",
"version": "0.3.1",
"version": "0.3.2",
"description": "Scrypted Core plugin. Provides the UI, websocket, and engine.io APIs.",
"author": "Scrypted",
"license": "Apache-2.0",

View File

@@ -137,7 +137,7 @@ export default {
// cert may need to be reaccepted? Server is down? Go to the
// server root to force the network error to bypass the PWA cache.
if (
e.toString().includes("Network Error") &&
(e.toString().includes("Network Error") || e.toString().includes("Load failed")) &&
window.location.href.startsWith("https:")
) {
window.location = "/";

View File

@@ -21,27 +21,18 @@
</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn
v-if="!canUpdate"
small
text
href="https://github.com/koush/scrypted#installation"
>More Information</v-btn
>
<v-btn v-if="!canUpdate" small text href="https://github.com/koush/scrypted#installation">More
Information</v-btn>
<v-dialog v-else v-model="updateAndRestart" width="500">
<template v-slot:activator="{ on }">
<v-btn small text color="red" v-on="on"
>Update and Restart Scrypted</v-btn
>
<v-btn small text color="red" v-on="on">Update and Restart Scrypted</v-btn>
</template>
<v-card color="red" dark>
<v-card-title primary-title>Restart Scrypted</v-card-title>
<v-card-text
>Are you sure you want to restart the Scrypted
service?</v-card-text
>
<v-card-text>Are you sure you want to restart the Scrypted
service?</v-card-text>
<v-card-text>{{ restartStatus }}</v-card-text>
<v-divider></v-divider>
@@ -67,13 +58,11 @@
</v-card>
<v-card class="mt-2" v-if="showRestart">
<v-toolbar
><v-toolbar-title>Server Management</v-toolbar-title></v-toolbar
>
<v-toolbar><v-toolbar-title>Server Management</v-toolbar-title></v-toolbar>
<v-card-actions>
<v-btn text href="/web/component/backup" color="info">Backup</v-btn>
<v-btn text :href="backupUrl" color="info">Backup</v-btn>
<v-btn text color="info" @click="restoreClick">Restore</v-btn>
<input type="file" ref="restoreFile" style="display: none;" @change="restore"/>
<input type="file" ref="restoreFile" style="display: none;" @change="restore" />
<v-spacer></v-spacer>
<v-dialog v-model="restart" width="500">
<template v-slot:activator="{ on }">
@@ -83,10 +72,8 @@
<v-card color="red" dark>
<v-card-title primary-title>Restart Scrypted</v-card-title>
<v-card-text
>Are you sure you want to restart the Scrypted
service?</v-card-text
>
<v-card-text>Are you sure you want to restart the Scrypted
service?</v-card-text>
<v-card-text>{{ restartStatus }}</v-card-text>
<v-divider></v-divider>
@@ -106,7 +93,8 @@
<script>
import { checkServerUpdate } from "../plugin/plugin";
import Settings from "../../interfaces/Settings.vue"
import {createSystemSettingsDevice} from './system-settings';
import { createSystemSettingsDevice } from './system-settings';
import { combineBaseUrl, getCurrentBaseUrl } from "../../../../../../packages/client/src";
export default {
components: {
@@ -124,6 +112,12 @@ export default {
showRestart: false,
};
},
computed: {
backupUrl() {
const baseUrl = getCurrentBaseUrl();
return combineBaseUrl(baseUrl, 'web/component/backup');
},
},
mounted() {
this.loadEnv();
this.checkUpdateAvailable();
@@ -140,7 +134,9 @@ export default {
return;
console.log(file);
const fileBlob = new Blob([file]);
await fetch('/web/component/restore', {
const baseUrl = getCurrentBaseUrl();
const restoreUrl = combineBaseUrl(baseUrl, 'web/component/restore');
await fetch(restoreUrl, {
method: 'POST',
body: fileBlob,
});

View File

@@ -1,12 +1,12 @@
{
"name": "@scrypted/objectdetector",
"version": "0.1.20",
"version": "0.1.21",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@scrypted/objectdetector",
"version": "0.1.20",
"version": "0.1.21",
"license": "Apache-2.0",
"dependencies": {
"@scrypted/common": "file:../../common",

View File

@@ -1,6 +1,6 @@
{
"name": "@scrypted/objectdetector",
"version": "0.1.20",
"version": "0.1.21",
"description": "Scrypted Video Analysis Plugin. Installed alongside a detection service like OpenCV or TensorFlow.",
"author": "Scrypted",
"license": "Apache-2.0",

View File

@@ -514,10 +514,14 @@ class ObjectDetectionMixin extends SettingsMixinDeviceBase<VideoCamera & Camera
if (!o.boundingBox)
continue;
o.zones = []
const box = normalizeBox(o.boundingBox, detection.inputDimensions);
let included: boolean;
// need a way to explicitly include package zone.
if (o.zones)
included = true;
else
o.zones = [];
for (const [zone, zoneValue] of Object.entries(this.zones)) {
if (zoneValue.length < 3) {
// this.console.warn(zone, 'Zone is unconfigured, skipping.');

View File

@@ -1,12 +1,12 @@
{
"name": "@scrypted/prebuffer-mixin",
"version": "0.10.11",
"version": "0.10.12",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@scrypted/prebuffer-mixin",
"version": "0.10.11",
"version": "0.10.12",
"license": "Apache-2.0",
"dependencies": {
"@scrypted/common": "file:../../common",

View File

@@ -1,6 +1,6 @@
{
"name": "@scrypted/prebuffer-mixin",
"version": "0.10.11",
"version": "0.10.12",
"description": "Video Stream Rebroadcast, Prebuffer, and Management Plugin for Scrypted.",
"author": "Scrypted",
"license": "Apache-2.0",

View File

@@ -1,4 +1,4 @@
import path from 'path'
import { AutoenableMixinProvider } from '@scrypted/common/src/autoenable-mixin-provider';
import { getDebugModeH264EncoderArgs, getH264EncoderArgs } from '@scrypted/common/src/ffmpeg-hardware-acceleration';
import { addVideoFilterArguments } from '@scrypted/common/src/ffmpeg-helpers';
@@ -971,6 +971,7 @@ class PrebufferSession {
const clientPromise = await listenSingleRtspClient({
hostname,
pathToken: path.join(crypto.randomBytes(8).toString('hex'), this.mixin.id),
createServer: duplex => {
sdp = addTrackControls(sdp);
server = new FileRtspServer(duplex, sdp);

4
sdk/package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "@scrypted/sdk",
"version": "0.3.4",
"version": "0.3.5",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@scrypted/sdk",
"version": "0.3.4",
"version": "0.3.5",
"license": "ISC",
"dependencies": {
"@babel/preset-typescript": "^7.18.6",

View File

@@ -1,6 +1,6 @@
{
"name": "@scrypted/sdk",
"version": "0.3.4",
"version": "0.3.5",
"description": "",
"main": "dist/src/index.js",
"exports": {

View File

@@ -19,6 +19,8 @@ function parseValue(value: string, setting: StorageSetting, readDefaultValue: ()
return parseInt(value) || readDefaultValue() || 0;
}
if (type === 'array') {
if (!value)
return readDefaultValue() || [];
try {
return JSON.parse(value);
}

View File

@@ -1,12 +1,12 @@
{
"name": "@scrypted/types",
"version": "0.3.4",
"version": "0.3.5",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@scrypted/types",
"version": "0.3.4",
"version": "0.3.5",
"license": "ISC",
"devDependencies": {
"@types/rimraf": "^3.0.2",

View File

@@ -1,6 +1,6 @@
{
"name": "@scrypted/types",
"version": "0.3.4",
"version": "0.3.5",
"description": "",
"main": "dist/index.js",
"author": "",

View File

@@ -1,12 +1,12 @@
{
"name": "@scrypted/server",
"version": "0.87.0",
"version": "0.91.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@scrypted/server",
"version": "0.87.0",
"version": "0.91.0",
"license": "ISC",
"dependencies": {
"@mapbox/node-pre-gyp": "^1.0.11",

View File

@@ -1,6 +1,6 @@
{
"name": "@scrypted/server",
"version": "0.87.0",
"version": "0.91.6",
"description": "",
"dependencies": {
"@mapbox/node-pre-gyp": "^1.0.11",
@@ -62,7 +62,7 @@
"build": "tsc --outDir dist",
"postbuild": "node test/check-build-output.js",
"beta": "npm publish --tag beta",
"postbeta": "npm version minor && git add package.json && npm run build && git commit -m postbeta",
"postbeta": "npm version patch && git add package.json && npm run build && git commit -m postbeta",
"release": "npm publish",
"prepublishOnly": "npm run build",
"postrelease": "git tag v$npm_package_version && git push origin v$npm_package_version && npm version minor && git add package.json && git commit -m postrelease",

View File

@@ -21,9 +21,7 @@ export class NodeForkWorker extends ChildProcessWorker {
this.worker = child_process.fork(mainFilename, ['child', this.pluginId], {
stdio: ['pipe', 'pipe', 'pipe', 'ipc'],
env: Object.assign({}, process.env, env, {
NODE_PATH: path.join(getPluginNodePath(this.pluginId), 'node_modules'),
}),
env: Object.assign({}, process.env, env),
serialization: 'advanced',
execArgv,
});

View File

@@ -16,9 +16,7 @@ export class NodeThreadWorker extends EventEmitter implements RuntimeWorker {
this.worker = new worker_threads.Worker(mainFilename, {
argv: ['child-thread', this.pluginId],
env: Object.assign({}, process.env, env, {
NODE_PATH: path.join(getPluginNodePath(this.pluginId), 'node_modules'),
}),
env: Object.assign({}, process.env, env),
});
this.worker.on('exit', () => {

View File

@@ -53,10 +53,12 @@ export class PythonRuntimeWorker extends ChildProcessWorker {
const pluginPythonVersion = options.packageJson.scrypted.pythonVersion?.[os.platform()]?.[os.arch()] || options.packageJson.scrypted.pythonVersion?.default;
if (os.platform() === 'win32') {
pythonPath ||= 'py.exe';
const windowsPythonVersion = pluginPythonVersion || process.env.SCRYPTED_WINDOWS_PYTHON_VERSION;
if (windowsPythonVersion)
args.unshift(windowsPythonVersion)
if (!pythonPath) {
pythonPath = 'py.exe';
const windowsPythonVersion = pluginPythonVersion || process.env.SCRYPTED_WINDOWS_PYTHON_VERSION;
if (windowsPythonVersion)
args.unshift(windowsPythonVersion)
}
}
else if (pluginPythonVersion) {
pythonPath = `python${pluginPythonVersion}`;

View File

@@ -46,6 +46,7 @@ import { getNpmPackageInfo, PluginComponent } from './services/plugin';
import { ServiceControl } from './services/service-control';
import { UsersService } from './services/users';
import { getState, ScryptedStateManager, setState } from './state';
import { Backup } from './services/backup';
interface DeviceProxyPair {
handler: PluginDeviceProxyHandler;
@@ -102,6 +103,7 @@ export class ScryptedRuntime extends PluginHttp<HttpPluginData> {
addressSettings = new AddressSettings(this);
usersService = new UsersService(this);
info = new Info();
backup = new Backup(this);
pluginHosts = new Map<string, RuntimeHost>();
constructor(public mainFilename: string, public datastore: Level, insecure: http.Server, secure: https.Server, app: express.Application) {
@@ -443,6 +445,8 @@ export class ScryptedRuntime extends PluginHttp<HttpPluginData> {
return this.addressSettings;
case "users":
return this.usersService;
case 'backup':
return this.backup;
}
}

View File

@@ -1,9 +1,10 @@
import v8 from 'v8';
import vm from 'vm';
import dns from 'dns';
import process from 'process';
import semver from 'semver';
import { RPCResultError, startPeriodicGarbageCollection } from './rpc';
import v8 from 'v8';
import vm from 'vm';
import { PluginError } from './plugin/plugin-error';
import { RPCResultError, startPeriodicGarbageCollection } from './rpc';
import type { Runtime } from './scrypted-server-main';
export function isChildProcess() {
@@ -26,7 +27,7 @@ function start(mainFilename: string, options?: {
// This causes issues with clients that are on "IPv6" networks that are
// actually busted and fail to connect to npm's IPv6 address.
// The workaround is to favor IPv4.
process.env['NODE_OPTIONS'] = '--dns-result-order=ipv4first';
dns.setDefaultResultOrder('ipv4first');
startPeriodicGarbageCollection();

View File

@@ -1,11 +1,16 @@
import { startPluginRemote } from "./plugin/plugin-remote-worker";
import { RpcMessage } from "./rpc";
import worker_threads from "worker_threads";
import v8 from 'v8';
import net from 'net';
import v8 from 'v8';
import worker_threads from "worker_threads";
import { getPluginNodePath } from "./plugin/plugin-npm-dependencies";
import { startPluginRemote } from "./plugin/plugin-remote-worker";
import { SidebandSocketSerializer } from "./plugin/socket-serializer";
import { RpcMessage } from "./rpc";
function start(mainFilename: string) {
const pluginId = process.argv[3];
console.log('starting plugin', pluginId);
module.paths.push(getPluginNodePath(pluginId));
if (process.argv[2] === 'child-thread') {
const peer = startPluginRemote(mainFilename, process.argv[3], (message, reject) => {
try {

View File

@@ -113,7 +113,6 @@ app.use(bodyParser.raw({ type: 'application/zip', limit: 100000000 }) as any)
async function start(mainFilename: string, options?: {
onRuntimeCreated?: (runtime: ScryptedRuntime) => Promise<void>,
restart?: () => void,
}) {
const volumeDir = getScryptedVolume();
await fs.promises.mkdir(volumeDir, {
@@ -150,6 +149,10 @@ async function start(mainFilename: string, options?: {
realm: 'Scrypted',
}, async (username, password, callback) => {
const user = await db.tryGet(ScryptedUser, username);
if (!user) {
callback(false);
return;
}
const salted = user.salt + password;
const hash = crypto.createHash('sha256');
@@ -340,13 +343,10 @@ async function start(mainFilename: string, options?: {
app.post('/web/component/restore', async (req, res) => {
const buffers: Buffer[] = [];
let zip: AdmZip;
req.on('data', b => buffers.push(b));
try {
await once(req, 'end');
zip = new AdmZip(Buffer.concat(buffers));
if (!zip.test())
throw new Error('backup zip test failed.');
await scrypted.backup.restore(Buffer.concat(buffers))
}
catch (e) {
res.send({
@@ -354,62 +354,11 @@ async function start(mainFilename: string, options?: {
});
return;
}
try {
scrypted.kill();
await sleep(5000);
await db.close();
await fs.promises.rm(volumeDir, {
recursive: true,
force: true,
});
await fs.promises.mkdir(volumeDir, {
recursive: true
});
zip.extractAllTo(dbPath, true);
res.send({
success: true,
});
}
catch (e) {
res.send({
error: "Error during restore.",
});
}
if (options?.restart)
options.restart();
else
process.exit();
});
app.get('/web/component/backup', async (req, res) => {
try {
const backupDbPath = path.join(volumeDir, 'backup.db');
await fs.promises.rm(backupDbPath, {
recursive: true,
force: true,
});
const backupDb = new Level(backupDbPath);
await backupDb.open();
for await (const [key, value] of db.iterator()) {
await backupDb.put(key, value);
}
await backupDb.close();
const backupZip = path.join(volumeDir, 'backup.zip');
await fs.promises.rm(backupZip, {
recursive: true,
force: true,
});
const zip = new AdmZip();
await zip.addLocalFolderPromise(backupDbPath, {});
const zipBuffer = await zip.toBufferPromise();
const zipBuffer = await scrypted.backup.createBackup();
// the file is a normal zip file, but an extension is added to prevent safari, etc, from unzipping it automatically.
res.header('Content-Disposition', 'attachment; filename="scrypted.zip.backup"')
res.send(zipBuffer);

View File

@@ -0,0 +1,63 @@
import fs from 'fs';
import path from 'path';
import Level from '../level';
import { sleep } from '../sleep';
import { getScryptedVolume } from '../plugin/plugin-volume';
import AdmZip from 'adm-zip';
import { ScryptedRuntime } from '../runtime';
export class Backup {
constructor(public runtime: ScryptedRuntime) {}
async createBackup(): Promise<Buffer> {
const volumeDir = getScryptedVolume();
const backupDbPath = path.join(volumeDir, 'backup.db');
await fs.promises.rm(backupDbPath, {
recursive: true,
force: true,
});
const backupDb = new Level(backupDbPath);
await backupDb.open();
for await (const [key, value] of this.runtime.datastore.iterator()) {
await backupDb.put(key, value);
}
await backupDb.close();
const backupZip = path.join(volumeDir, 'backup.zip');
await fs.promises.rm(backupZip, {
recursive: true,
force: true,
});
const zip = new AdmZip();
await zip.addLocalFolderPromise(backupDbPath, {});
return zip.toBufferPromise();
}
async restore(b: Buffer): Promise<void> {
const volumeDir = getScryptedVolume();
const dbPath = path.join(volumeDir, 'scrypted.db');
const zip = new AdmZip(b);
if (!zip.test())
throw new Error('backup zip test failed.');
this.runtime.kill();
await sleep(5000);
await this.runtime.datastore.close();
await fs.promises.rm(volumeDir, {
recursive: true,
force: true,
});
await fs.promises.mkdir(volumeDir, {
recursive: true
});
zip.extractAllTo(dbPath, true);
this.runtime.serviceControl.restart();
}
}