add option for specific OS's in scripts.
change view in script editor to match the block view (looks much nicer and much more organized).
This commit is contained in:
@@ -123,6 +123,9 @@ db.run(`
|
||||
db.run(
|
||||
`CREATE INDEX IF NOT EXISTS idx_auto_scripts_trigger ON auto_scripts(trigger, enabled);`,
|
||||
);
|
||||
try {
|
||||
db.run(`ALTER TABLE auto_scripts ADD COLUMN os_filter TEXT NOT NULL DEFAULT '[]'`);
|
||||
} catch { /* column already exists */ }
|
||||
|
||||
db.run(`
|
||||
CREATE TABLE IF NOT EXISTS auto_script_runs (
|
||||
@@ -482,11 +485,17 @@ export type AutoScript = {
|
||||
script: string;
|
||||
scriptType: string;
|
||||
enabled: boolean;
|
||||
osFilter: string[];
|
||||
createdAt: number;
|
||||
updatedAt: number;
|
||||
};
|
||||
|
||||
function mapAutoScriptRow(row: any): AutoScript {
|
||||
let osFilter: string[] = [];
|
||||
try {
|
||||
const parsed = JSON.parse(row.os_filter || "[]");
|
||||
osFilter = Array.isArray(parsed) ? parsed : [];
|
||||
} catch { }
|
||||
return {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
@@ -494,6 +503,7 @@ function mapAutoScriptRow(row: any): AutoScript {
|
||||
script: row.script,
|
||||
scriptType: row.script_type,
|
||||
enabled: row.enabled === 1,
|
||||
osFilter,
|
||||
createdAt: Number(row.created_at) || 0,
|
||||
updatedAt: Number(row.updated_at) || 0,
|
||||
};
|
||||
@@ -525,11 +535,12 @@ export function createAutoScript(input: {
|
||||
script: string;
|
||||
scriptType: string;
|
||||
enabled: boolean;
|
||||
osFilter: string[];
|
||||
}): AutoScript {
|
||||
const now = Date.now();
|
||||
db.run(
|
||||
`INSERT INTO auto_scripts (id, name, trigger, script, script_type, enabled, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
|
||||
`INSERT INTO auto_scripts (id, name, trigger, script, script_type, enabled, os_filter, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
||||
,
|
||||
input.id,
|
||||
input.name,
|
||||
@@ -537,6 +548,7 @@ export function createAutoScript(input: {
|
||||
input.script,
|
||||
input.scriptType,
|
||||
input.enabled ? 1 : 0,
|
||||
JSON.stringify(input.osFilter ?? []),
|
||||
now,
|
||||
now,
|
||||
);
|
||||
@@ -551,6 +563,7 @@ export function updateAutoScript(
|
||||
script: string;
|
||||
scriptType: string;
|
||||
enabled: boolean;
|
||||
osFilter: string[];
|
||||
}>,
|
||||
): AutoScript | null {
|
||||
const current = getAutoScript(id);
|
||||
@@ -562,16 +575,18 @@ export function updateAutoScript(
|
||||
script: input.script ?? current.script,
|
||||
scriptType: input.scriptType ?? current.scriptType,
|
||||
enabled: typeof input.enabled === "boolean" ? input.enabled : current.enabled,
|
||||
osFilter: Array.isArray(input.osFilter) ? input.osFilter : current.osFilter,
|
||||
};
|
||||
|
||||
db.run(
|
||||
`UPDATE auto_scripts SET name=?, trigger=?, script=?, script_type=?, enabled=?, updated_at=? WHERE id=?`
|
||||
`UPDATE auto_scripts SET name=?, trigger=?, script=?, script_type=?, enabled=?, os_filter=?, updated_at=? WHERE id=?`
|
||||
,
|
||||
next.name,
|
||||
next.trigger,
|
||||
next.script,
|
||||
next.scriptType,
|
||||
next.enabled ? 1 : 0,
|
||||
JSON.stringify(next.osFilter),
|
||||
Date.now(),
|
||||
id,
|
||||
);
|
||||
|
||||
@@ -28,6 +28,13 @@ export function dispatchAutoScriptsForConnection(
|
||||
}
|
||||
|
||||
for (const script of scripts) {
|
||||
if (script.osFilter.length > 0) {
|
||||
const clientOs = (info.os || "").toLowerCase();
|
||||
if (!script.osFilter.includes(clientOs)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (script.trigger === "on_connect_once") {
|
||||
if (hasAutoScriptRun(script.id, info.id)) {
|
||||
continue;
|
||||
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
updateAutoScript,
|
||||
} from "../../db";
|
||||
import { requirePermission } from "../../rbac";
|
||||
import { AUTO_SCRIPT_TRIGGERS, ALLOWED_SCRIPT_TYPES } from "../validation-constants";
|
||||
import { AUTO_SCRIPT_TRIGGERS, ALLOWED_SCRIPT_TYPES, ALLOWED_OS_FILTERS } from "../validation-constants";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
|
||||
export async function handleAutoScriptsRoutes(
|
||||
@@ -59,6 +59,8 @@ export async function handleAutoScriptsRoutes(
|
||||
const script = String(body?.script || "");
|
||||
const scriptTypeRaw = String(body?.scriptType || "powershell").trim();
|
||||
const enabled = typeof body?.enabled === "boolean" ? body.enabled : true;
|
||||
const osFilterRaw = Array.isArray(body?.osFilter) ? body.osFilter : [];
|
||||
const osFilter = osFilterRaw.filter((v: unknown) => typeof v === "string" && ALLOWED_OS_FILTERS.has(v));
|
||||
|
||||
if (!name) {
|
||||
return Response.json({ error: "Name is required" }, { status: 400 });
|
||||
@@ -81,6 +83,7 @@ export async function handleAutoScriptsRoutes(
|
||||
script,
|
||||
scriptType,
|
||||
enabled,
|
||||
osFilter,
|
||||
});
|
||||
|
||||
return Response.json({ ok: true, item });
|
||||
@@ -121,12 +124,18 @@ export async function handleAutoScriptsRoutes(
|
||||
: "powershell"
|
||||
: undefined;
|
||||
|
||||
const osFilterRaw = Array.isArray(body?.osFilter) ? body.osFilter : undefined;
|
||||
const osFilter = osFilterRaw
|
||||
? osFilterRaw.filter((v: unknown) => typeof v === "string" && ALLOWED_OS_FILTERS.has(v))
|
||||
: undefined;
|
||||
|
||||
const updated = updateAutoScript(autoScriptMatch[1], {
|
||||
name: body?.name ? String(body.name).trim() : undefined,
|
||||
trigger: triggerRaw as AutoScriptTrigger | undefined,
|
||||
script: body?.script ? String(body.script) : undefined,
|
||||
scriptType,
|
||||
enabled: typeof body?.enabled === "boolean" ? body.enabled : undefined,
|
||||
osFilter,
|
||||
});
|
||||
|
||||
if (!updated) {
|
||||
|
||||
@@ -29,3 +29,11 @@ export const ALLOWED_PLATFORMS = new Set([
|
||||
"android-amd64",
|
||||
"android-armv7",
|
||||
]);
|
||||
|
||||
export const ALLOWED_OS_FILTERS = new Set([
|
||||
"windows",
|
||||
"linux",
|
||||
"darwin",
|
||||
"android",
|
||||
"freebsd",
|
||||
]);
|
||||
|
||||
Reference in New Issue
Block a user