Compare commits

..
2 Commits
Author SHA1 Message Date
Zaid Abdulla 84de431872 Complete implementation of ChangeDUState 2021-08-27 15:02:04 -07:00
Zaid Abdulla 716f2c6df3 Implement support for ChangeDUState method 2021-08-02 13:12:12 -07:00
65 changed files with 5792 additions and 2282 deletions

No files matched your search

-32
View File
@@ -1,37 +1,5 @@
# Change Log # Change Log
## 1.2.6 (2021-09-16)
- New config option `cwmp.downloadSuccessOnTimeout` to enable a workaround for
CPEs that neglect to send a TransferComplete message after a successful
download.
- Display a progress bar when uploading new files.
- Default to dual stack interface binding (i.e. `::` instead of `0.0.0.0`).
Unless the binding interface is explicitly set, this will cause IPv4 addresses
in the logs to be displayed as IPv4-mapped IPv6 addresses (e.g.
`::ffff:127.0.0.1` instead of `127.0.0.1`).
- Detect and correct for client side (browser) clock skew that would otherwise
alter the numbers in the pie charts.
- Various improvements relating to dealing with buggy TR-069 client
implementations.
- Fix a bug causing "lock expired" exceptions when a CWMP session remains open
for a very long time due to slow clients.
- Fix metadata of uploaded files going missing due to nginx stripping away what
it considers to be invalid headers. The nginx directive
`ignore_invalid_headers` is no longer required.
- Fix crash when a CPE is assigned a tag containing a dot.
- Fix bug preventing the user from closing the preset pop-up after being
presented with unsupported preset message.
- Fix exceptions raised from ext scripts manifesting as timeout faults.
- Fix download getting triggered repeatedly when the value passed to `Download`
parameter is greater than the current timestamp.
- Fix crash when passing invalid attributes to `declare`.
- Fix crash in NBI when pushing tasks to non-existing devices.
- Fix crash in NBI when passing invalid JSON to various API endpoints.
- Fix crash when the output from `ping` command cannot be parsed in some rare
edge cases.
- A number of other fixes and stability improvements.
## 1.2.5 (2021-03-12) ## 1.2.5 (2021-03-12)
- Support specifying custom types when uploading files. - Support specifying custom types when uploading files.
+1 -1
View File
@@ -72,5 +72,5 @@ For commercial support options, please visit
## License ## License
Copyright 2013-2021 GenieACS Inc. GenieACS is released under the Copyright 2013-2020 GenieACS Inc. GenieACS is released under the
[AGPLv3 license terms](https://raw.githubusercontent.com/genieacs/genieacs/master/LICENSE). [AGPLv3 license terms](https://raw.githubusercontent.com/genieacs/genieacs/master/LICENSE).
+5 -9
View File
@@ -29,7 +29,7 @@ import postcss from "postcss";
import postcssImport from "postcss-import"; import postcssImport from "postcss-import";
import postcssPresetEnv from "postcss-preset-env"; import postcssPresetEnv from "postcss-preset-env";
import cssnano from "cssnano"; import cssnano from "cssnano";
import { optimize } from "svgo"; import { optimize, extendDefaultPlugins } from "svgo";
import * as xmlParser from "../lib/xml-parser"; import * as xmlParser from "../lib/xml-parser";
const MODE = process.env["NODE_ENV"] || "production"; const MODE = process.env["NODE_ENV"] || "production";
@@ -299,16 +299,12 @@ async function generateIconsSprite(): Promise<void> {
const id = path.parse(file).name; const id = path.parse(file).name;
const filePath = path.join(iconsDir, file); const filePath = path.join(iconsDir, file);
const { data } = await optimize(fs.readFileSync(filePath).toString(), { const { data } = await optimize(fs.readFileSync(filePath).toString(), {
plugins: [ plugins: extendDefaultPlugins([
{ {
name: "preset-default", name: "removeViewBox",
params: { active: false,
overrides: {
removeViewBox: false,
}, },
}, ]),
},
],
}); });
symbols.push(generateSymbol(id, data)); symbols.push(generateSymbol(id, data));
} }
-3
View File
@@ -165,6 +165,3 @@ Brotli
CSV CSV
hostInfo hostInfo
IPv IPv
downloadSuccessOnTimeout
TransferComplete
nginx
+4 -4
View File
@@ -64,7 +64,7 @@ CWMP_PORT
CWMP_INTERFACE CWMP_INTERFACE
The network interface that genieacs-cwmp binds to. The network interface that genieacs-cwmp binds to.
Default: ``::`` Default: ``0.0.0.0``
CWMP_SSL_CERT CWMP_SSL_CERT
Path to certificate file. If omitted, non-secure HTTP will be used. Path to certificate file. If omitted, non-secure HTTP will be used.
@@ -102,7 +102,7 @@ NBI_PORT
NBI_INTERFACE NBI_INTERFACE
The network interface that genieacs-nbi binds to. The network interface that genieacs-nbi binds to.
Default: ``::`` Default: ``0.0.0.0``
NBI_SSL_CERT NBI_SSL_CERT
Path to certificate file. If omitted, non-secure HTTP will be used. Path to certificate file. If omitted, non-secure HTTP will be used.
@@ -140,7 +140,7 @@ FS_PORT
FS_INTERFACE FS_INTERFACE
The network interface that genieacs-fs binds to. The network interface that genieacs-fs binds to.
Default: ``::`` Default: ``0.0.0.0``
FS_SSL_CERT FS_SSL_CERT
Path to certificate file. If omitted, non-secure HTTP will be used. Path to certificate file. If omitted, non-secure HTTP will be used.
@@ -186,7 +186,7 @@ UI_PORT
UI_INTERFACE UI_INTERFACE
The network interface that genieacs-ui binds to. The network interface that genieacs-ui binds to.
Default: ``::`` Default: ``0.0.0.0``
UI_SSL_CERT UI_SSL_CERT
Path to certificate file. If omitted, non-secure HTTP will be used. Path to certificate file. If omitted, non-secure HTTP will be used.
+1 -1
View File
@@ -19,7 +19,7 @@ Prerequisites
.. topic:: Node.js .. topic:: Node.js
GenieACS requires Node.js 12.13 and up. Refer to https://nodejs.org/ for GenieACS requires Node.js 10.x and up. Refer to https://nodejs.org/ for
instructions. instructions.
.. topic:: MongoDB .. topic:: MongoDB
+4 -3
View File
@@ -17,6 +17,7 @@
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>. * along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/ */
import { parse } from "url";
import * as db from "./db"; import * as db from "./db";
import * as common from "./common"; import * as common from "./common";
import * as cache from "./cache"; import * as cache from "./cache";
@@ -79,7 +80,7 @@ export async function connectionRequest(
).value || [""])[0]; ).value || [""])[0];
} }
const remoteAddress = new URL(connectionRequestUrl).hostname; const remoteAddress = parse(connectionRequestUrl).host;
const evalCallback = (exp): Expression => { const evalCallback = (exp): Expression => {
if (!Array.isArray(exp)) return exp; if (!Array.isArray(exp)) return exp;
@@ -94,7 +95,7 @@ export async function connectionRequest(
else if (name === "password") return password; else if (name === "password") return password;
const p = device[name]; const p = device[name];
if (p?.value) return p.value[0]; if (p && p.value) return p.value[0];
} else if (exp[0] === "FUNC") { } else if (exp[0] === "FUNC") {
if (exp[1] === "REMOTE_ADDRESS") return remoteAddress; if (exp[1] === "REMOTE_ADDRESS") return remoteAddress;
else if (exp[1] === "USERNAME") return username; else if (exp[1] === "USERNAME") return username;
@@ -317,7 +318,7 @@ function sanitizeTask(task): void {
export async function insertTasks(tasks: any[]): Promise<Task[]> { export async function insertTasks(tasks: any[]): Promise<Task[]> {
if (tasks && !Array.isArray(tasks)) tasks = [tasks]; if (tasks && !Array.isArray(tasks)) tasks = [tasks];
else if (!tasks?.length) return tasks || []; else if (!tasks || tasks.length === 0) return tasks || [];
for (const task of tasks) { for (const task of tasks) {
sanitizeTask(task); sanitizeTask(task);
+3 -3
View File
@@ -52,9 +52,9 @@ function parseHeaderFeilds(str: string): Record<string, string> {
return res; return res;
} }
export function parseAuthorizationHeader(authHeader: string): { export function parseAuthorizationHeader(
method: string; authHeader: string
} { ): { method: string } {
authHeader = authHeader.trim(); authHeader = authHeader.trim();
const method = authHeader.split(" ", 1)[0]; const method = authHeader.split(" ", 1)[0];
const res = { method: method }; const res = { method: method };
+4 -4
View File
@@ -59,7 +59,7 @@ export async function set(
export async function pop(key: string): Promise<any> { export async function pop(key: string): Promise<any> {
const res = await cacheCollection.findOneAndDelete({ _id: key }); const res = await cacheCollection.findOneAndDelete({ _id: key });
if (res?.["value"]) return res["value"]["value"]; if (res && res["value"]) return res["value"]["value"];
return null; return null;
} }
@@ -78,9 +78,9 @@ export async function acquireLock(
value: token, value: token,
expire: new Date(now + ttl + CLOCK_SKEW_TOLERANCE), expire: new Date(now + ttl + CLOCK_SKEW_TOLERANCE),
}, },
$currentDate: { timestamp: true as unknown as Date }, $currentDate: { timestamp: true },
}, },
{ upsert: true, returnDocument: "after" } { upsert: true, returnOriginal: false }
); );
const v = r.value; const v = r.value;
if (Math.abs(v["timestamp"].getTime() - now) > CLOCK_SKEW_TOLERANCE) if (Math.abs(v["timestamp"].getTime() - now) > CLOCK_SKEW_TOLERANCE)
@@ -114,5 +114,5 @@ export async function releaseLock(
_id: lockName, _id: lockName,
value: token, value: token,
}); });
if (res.deletedCount !== 1) throw new Error("Lock expired"); if (res["result"]["n"] !== 1) throw new Error("Lock expired");
} }
+2 -2
View File
@@ -17,14 +17,14 @@
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>. * along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/ */
import cluster, { Worker } from "cluster"; import cluster from "cluster";
import { cpus } from "os"; import { cpus } from "os";
import * as logger from "./logger"; import * as logger from "./logger";
let respawnTimestamp = 0; let respawnTimestamp = 0;
let crashes: number[] = []; let crashes: number[] = [];
function fork(): Worker { function fork(): cluster.Worker {
const w = cluster.fork(); const w = cluster.fork();
w.on("error", (err: NodeJS.ErrnoException) => { w.on("error", (err: NodeJS.ErrnoException) => {
// Avoid exception when attempting to kill the process just as it's exiting // Avoid exception when attempting to kill the process just as it's exiting
+4 -4
View File
@@ -21,13 +21,13 @@ import { PermissionSet, Expression } from "../types";
import { evaluate, or } from "./expression"; import { evaluate, or } from "./expression";
export default class Authorizer { export default class Authorizer {
private declare permissionSets: PermissionSet[]; private permissionSets: PermissionSet[];
private declare validatorCache: WeakMap< private validatorCache: WeakMap<
any, any,
(mutationType, mutation, any) => boolean (mutationType, mutation, any) => boolean
>; >;
private declare hasAccessCache: Map<string, boolean>; private hasAccessCache: Map<string, boolean>;
private declare getFilterCache: Map<string, Expression>; private getFilterCache: Map<string, Expression>;
public constructor(permissionSets: PermissionSet[]) { public constructor(permissionSets: PermissionSet[]) {
this.permissionSets = permissionSets; this.permissionSets = permissionSets;
+241 -271
View File
@@ -29,8 +29,8 @@ const TWO = BI.BigInt(2);
const NEGATIVE_ONE = BI.BigInt(-1); const NEGATIVE_ONE = BI.BigInt(-1);
class Indeterminates { class Indeterminates {
public declare map: Map<string, number>; public map: Map<string, number>;
public declare sortedKeys: string[]; public sortedKeys: string[];
public constructor(str?: string) { public constructor(str?: string) {
this.map = new Map(); this.map = new Map();
@@ -118,7 +118,7 @@ function findGcd(a: BI.bigint, b: BI.bigint): BI.bigint {
} }
class Polynomial { class Polynomial {
public declare terms: Term[]; public terms: Term[];
public constructor(terms: Term[]) { public constructor(terms: Term[]) {
this.terms = terms; this.terms = terms;
@@ -298,11 +298,12 @@ class Polynomial {
} }
type Minterm = number[]; type Minterm = number[];
type Context = Map<string, number>;
abstract class BoolExprSynth { abstract class BoolExprSynth {
public abstract true(context: SynthContext): Minterm[]; public abstract true(context: Context): Minterm[];
public abstract false(context: SynthContext): Minterm[]; public abstract false(context: Context): Minterm[];
public abstract null(context: SynthContext): Minterm[]; public abstract null(context: Context): Minterm[];
} }
class TrueSynth extends BoolExprSynth { class TrueSynth extends BoolExprSynth {
@@ -342,8 +343,8 @@ class NullSynth extends BoolExprSynth {
} }
class VarSynth extends BoolExprSynth { class VarSynth extends BoolExprSynth {
private declare exp: Expression; private expStr: string;
private declare negate: boolean; private negate: boolean;
public constructor(exp: Expression) { public constructor(exp: Expression) {
super(); super();
this.negate = false; this.negate = false;
@@ -367,49 +368,52 @@ class VarSynth extends BoolExprSynth {
this.negate = true; this.negate = true;
} }
} }
this.exp = exp; this.expStr = JSON.stringify(exp);
} }
public true(context: SynthContext): Minterm[] { public true(context: Context): Minterm[] {
const v = context.getVariable(this.exp); let v = context.get(this.expStr);
if (v == null) context.set(this.expStr, (v = context.size));
return [[(v << 2) ^ (this.negate ? 1 : 3)]]; return [[(v << 2) ^ (this.negate ? 1 : 3)]];
} }
public false(context: SynthContext): Minterm[] { public false(context: Context): Minterm[] {
const v = context.getVariable(this.exp); let v = context.get(this.expStr);
if (v == null) context.set(this.expStr, (v = context.size));
return [[(v << 2) ^ (this.negate ? 3 : 1)]]; return [[(v << 2) ^ (this.negate ? 3 : 1)]];
} }
public null(context: SynthContext): Minterm[] { public null(context: Context): Minterm[] {
const v = context.getVariable(this.exp); let v = context.get(this.expStr);
if (v == null) context.set(this.expStr, (v = context.size));
return [[v << 2, (v << 2) ^ 2]]; return [[v << 2, (v << 2) ^ 2]];
} }
} }
class NotSynth extends BoolExprSynth { class NotSynth extends BoolExprSynth {
private declare exprSynth: BoolExprSynth; private exprSynth: BoolExprSynth;
public constructor(e: BoolExprSynth) { public constructor(e: BoolExprSynth) {
super(); super();
this.exprSynth = e; this.exprSynth = e;
} }
public true(context: SynthContext): Minterm[] { public true(context: Context): Minterm[] {
return this.exprSynth.false(context); return this.exprSynth.false(context);
} }
public false(context: SynthContext): Minterm[] { public false(context: Context): Minterm[] {
return this.exprSynth.true(context); return this.exprSynth.true(context);
} }
public null(context: SynthContext): Minterm[] { public null(context: Context): Minterm[] {
return this.exprSynth.null(context); return this.exprSynth.null(context);
} }
} }
class IsNullSynth extends BoolExprSynth { class IsNullSynth extends BoolExprSynth {
private declare exprSynth: BoolExprSynth; private exprSynth: BoolExprSynth;
public constructor(e: BoolExprSynth) { public constructor(e: BoolExprSynth) {
super(); super();
this.exprSynth = e; this.exprSynth = e;
} }
public true(context: SynthContext): Minterm[] { public true(context: Context): Minterm[] {
return this.exprSynth.null(context); return this.exprSynth.null(context);
} }
public false(context: SynthContext): Minterm[] { public false(context: Context): Minterm[] {
return [...this.exprSynth.true(context), ...this.exprSynth.false(context)]; return [...this.exprSynth.true(context), ...this.exprSynth.false(context)];
} }
public null(): Minterm[] { public null(): Minterm[] {
@@ -418,7 +422,7 @@ class IsNullSynth extends BoolExprSynth {
} }
class OrSynth extends BoolExprSynth { class OrSynth extends BoolExprSynth {
private declare exprSynths: BoolExprSynth[]; private exprSynths: BoolExprSynth[];
public constructor(...e: BoolExprSynth[]) { public constructor(...e: BoolExprSynth[]) {
super(); super();
this.exprSynths = e.filter((ee) => !(ee instanceof FalseSynth)); this.exprSynths = e.filter((ee) => !(ee instanceof FalseSynth));
@@ -432,13 +436,13 @@ class OrSynth extends BoolExprSynth {
}); });
this.exprSynths.push(...unpacked); this.exprSynths.push(...unpacked);
} }
public true(context: SynthContext): Minterm[] { public true(context: Context): Minterm[] {
if (this.exprSynths.length === 0) return []; if (this.exprSynths.length === 0) return [];
if (this.exprSynths.length === 1) return this.exprSynths[0].true(context); if (this.exprSynths.length === 1) return this.exprSynths[0].true(context);
if (this.exprSynths.some((e) => e instanceof TrueSynth)) return [[]]; if (this.exprSynths.some((e) => e instanceof TrueSynth)) return [[]];
return this.exprSynths.map((e) => e.true(context)).flat(); return this.exprSynths.map((e) => e.true(context)).flat();
} }
public false(context: SynthContext): Minterm[] { public false(context: Context): Minterm[] {
if (this.exprSynths.length === 0) return [[]]; if (this.exprSynths.length === 0) return [[]];
if (this.exprSynths.length === 1) return this.exprSynths[0].false(context); if (this.exprSynths.length === 1) return this.exprSynths[0].false(context);
if ( if (
@@ -451,7 +455,7 @@ class OrSynth extends BoolExprSynth {
this.exprSynths.map((e) => complement(e.false(context))).flat() this.exprSynths.map((e) => complement(e.false(context))).flat()
); );
} }
public null(context: SynthContext): Minterm[] { public null(context: Context): Minterm[] {
if (this.exprSynths.length === 0) return []; if (this.exprSynths.length === 0) return [];
if (this.exprSynths.length === 1) return this.exprSynths[0].null(context); if (this.exprSynths.length === 1) return this.exprSynths[0].null(context);
const n = this.exprSynths.map((e) => e.null(context)).flat(); const n = this.exprSynths.map((e) => e.null(context)).flat();
@@ -461,7 +465,7 @@ class OrSynth extends BoolExprSynth {
} }
class AndSynth extends BoolExprSynth { class AndSynth extends BoolExprSynth {
private declare exprSynths: BoolExprSynth[]; private exprSynths: BoolExprSynth[];
public constructor(...e: BoolExprSynth[]) { public constructor(...e: BoolExprSynth[]) {
super(); super();
this.exprSynths = e.filter((ee) => !(ee instanceof TrueSynth)); this.exprSynths = e.filter((ee) => !(ee instanceof TrueSynth));
@@ -475,7 +479,7 @@ class AndSynth extends BoolExprSynth {
}); });
this.exprSynths.push(...unpacked); this.exprSynths.push(...unpacked);
} }
public true(context: SynthContext): Minterm[] { public true(context: Context): Minterm[] {
if (this.exprSynths.length === 0) return [[]]; if (this.exprSynths.length === 0) return [[]];
if (this.exprSynths.length === 1) return this.exprSynths[0].true(context); if (this.exprSynths.length === 1) return this.exprSynths[0].true(context);
if ( if (
@@ -488,13 +492,13 @@ class AndSynth extends BoolExprSynth {
this.exprSynths.map((e) => complement(e.true(context))).flat() this.exprSynths.map((e) => complement(e.true(context))).flat()
); );
} }
public false(context: SynthContext): Minterm[] { public false(context: Context): Minterm[] {
if (this.exprSynths.length === 0) return []; if (this.exprSynths.length === 0) return [];
if (this.exprSynths.length === 1) return this.exprSynths[0].false(context); if (this.exprSynths.length === 1) return this.exprSynths[0].false(context);
if (this.exprSynths.some((e) => e instanceof FalseSynth)) return [[]]; if (this.exprSynths.some((e) => e instanceof FalseSynth)) return [[]];
return this.exprSynths.map((e) => e.false(context)).flat(); return this.exprSynths.map((e) => e.false(context)).flat();
} }
public null(context: SynthContext): Minterm[] { public null(context: Context): Minterm[] {
if (this.exprSynths.length === 0) return []; if (this.exprSynths.length === 0) return [];
if (this.exprSynths.length === 1) return this.exprSynths[0].null(context); if (this.exprSynths.length === 1) return this.exprSynths[0].null(context);
const n = this.exprSynths.map((e) => e.null(context)).flat(); const n = this.exprSynths.map((e) => e.null(context)).flat();
@@ -504,12 +508,12 @@ class AndSynth extends BoolExprSynth {
} }
class CaseSynth extends BoolExprSynth { class CaseSynth extends BoolExprSynth {
private declare exprSynths: BoolExprSynth[]; private exprSynths: BoolExprSynth[];
public constructor(e: BoolExprSynth[]) { public constructor(e: BoolExprSynth[]) {
super(); super();
this.exprSynths = e; this.exprSynths = e;
} }
public true(context: SynthContext): Minterm[] { public true(context: Context): Minterm[] {
const minterms: Minterm[] = []; const minterms: Minterm[] = [];
const cumulative: Minterm[] = []; const cumulative: Minterm[] = [];
for (let i = 0; i < this.exprSynths.length; i += 2) { for (let i = 0; i < this.exprSynths.length; i += 2) {
@@ -529,7 +533,7 @@ class CaseSynth extends BoolExprSynth {
} }
return minterms; return minterms;
} }
public false(context: SynthContext): Minterm[] { public false(context: Context): Minterm[] {
const minterms: Minterm[] = []; const minterms: Minterm[] = [];
const cumulative: Minterm[] = []; const cumulative: Minterm[] = [];
for (let i = 0; i < this.exprSynths.length; i += 2) { for (let i = 0; i < this.exprSynths.length; i += 2) {
@@ -549,7 +553,7 @@ class CaseSynth extends BoolExprSynth {
} }
return minterms; return minterms;
} }
public null(context: SynthContext): Minterm[] { public null(context: Context): Minterm[] {
const minterms: Minterm[] = []; const minterms: Minterm[] = [];
const cumulative: Minterm[] = []; const cumulative: Minterm[] = [];
for (let i = 0; i < this.exprSynths.length; i += 2) { for (let i = 0; i < this.exprSynths.length; i += 2) {
@@ -662,10 +666,10 @@ function normalizeCallback(exp: Expression): Expression {
if (p == null) return null; if (p == null) return null;
args.push(p); args.push(p);
} }
return args.reduce( return (args.reduce(
(previousValue, currentValue) => previousValue.add(currentValue), (previousValue, currentValue) => previousValue.add(currentValue),
ADDITIVE_IDENTITY ADDITIVE_IDENTITY
) as unknown as Expression; ) as unknown) as Expression;
} else if (op === "*") { } else if (op === "*") {
const args: Polynomial[] = []; const args: Polynomial[] = [];
for (let i = 1; i < exp.length; ++i) { for (let i = 1; i < exp.length; ++i) {
@@ -673,10 +677,10 @@ function normalizeCallback(exp: Expression): Expression {
if (p == null) return null; if (p == null) return null;
args.push(p); args.push(p);
} }
return args.reduce( return (args.reduce(
(previousValue, currentValue) => previousValue.multiply(currentValue), (previousValue, currentValue) => previousValue.multiply(currentValue),
MULTIPLICATIVE_IDENTITY MULTIPLICATIVE_IDENTITY
) as unknown as Expression; ) as unknown) as Expression;
} else if (op === "-") { } else if (op === "-") {
const args: Polynomial[] = []; const args: Polynomial[] = [];
for (let i = 1; i < exp.length; ++i) { for (let i = 1; i < exp.length; ++i) {
@@ -684,9 +688,9 @@ function normalizeCallback(exp: Expression): Expression {
if (p == null) return null; if (p == null) return null;
args.push(p); args.push(p);
} }
return args.reduce((previousValue, currentValue) => return (args.reduce((previousValue, currentValue) =>
previousValue.subtract(currentValue) previousValue.subtract(currentValue)
) as unknown as Expression; ) as unknown) as Expression;
} else if (op === "/") { } else if (op === "/") {
const args: Polynomial[] = []; const args: Polynomial[] = [];
for (let i = 1; i < exp.length; ++i) { for (let i = 1; i < exp.length; ++i) {
@@ -694,9 +698,9 @@ function normalizeCallback(exp: Expression): Expression {
if (p == null) return null; if (p == null) return null;
args.push(p); args.push(p);
} }
return args.reduce((previousValue, currentValue) => return (args.reduce((previousValue, currentValue) =>
previousValue.divide(currentValue) previousValue.divide(currentValue)
) as unknown as Expression; ) as unknown) as Expression;
} else if (["=", "<>", ">", ">=", "<", "<="].includes(op)) { } else if (["=", "<>", ">", ">=", "<", "<="].includes(op)) {
if (exp[1] == null || exp[2] == null) return null; if (exp[1] == null || exp[2] == null) return null;
let lhs: Polynomial, rhs: Polynomial; let lhs: Polynomial, rhs: Polynomial;
@@ -794,14 +798,17 @@ function toBoolExprSynth(e: Expression | BoolExprSynth): BoolExprSynth {
return new FalseSynth(); return new FalseSynth();
} }
function sopToExpression(sop: number[][], context: SynthContext): Expression { function sopToExpression(
sop: number[][],
expressions: Map<number, Expression>
): Expression {
if (!sop.length) return false; if (!sop.length) return false;
const res: Expression[] = []; const res: Expression[] = [];
for (const s of sop) { for (const s of sop) {
if (!s.length) return true; if (!s.length) return true;
const conjs: Expression[] = []; const conjs: Expression[] = [];
for (const i of s) { for (const i of s) {
let expr = context.getExpression(i >>> 2); let expr = expressions.get(i >>> 2);
if (!(i & 1) !== !(i & 2)) expr = ["NOT", expr]; if (!(i & 1) !== !(i & 2)) expr = ["NOT", expr];
if (Array.isArray(expr) && expr[0] === "NOT" && Array.isArray(expr[1])) { if (Array.isArray(expr) && expr[0] === "NOT" && Array.isArray(expr[1])) {
const e: Expression[] = expr[1]; const e: Expression[] = expr[1];
@@ -825,196 +832,163 @@ function sopToExpression(sop: number[][], context: SynthContext): Expression {
return res[0]; return res[0];
} }
function findIsNullDeps(exp: Expression): Expression[] { function generateDcSetAndIsNull(
if (!Array.isArray(exp)) return []; variables: Map<number, Expression>
const op = exp[0]; ): { dcSet: number[][]; isNull: Map<number, number> } {
if (op === "IS NULL" || op === "IS NOT NULL") return []; const relations: Map<
if (op === "FUNC") {
if (exp[1] === "NOW") {
return [];
} else if (exp[1] === "LOWER" || exp[1] === "UPPER") {
return findIsNullDeps(exp[2]);
} else if (exp[1] === "ROUND") {
return exp
.slice(2, 4)
.map((e) => findIsNullDeps(e))
.flat();
}
} else if (op !== "PARAM") {
return exp
.slice(1)
.map((e) => findIsNullDeps(e))
.flat();
}
return [exp];
}
class SynthContext {
private declare _dcSet: number[][];
private declare _variables: Map<string, number>;
private declare _expressions: Map<number, Expression>;
private declare _isNullRelations: Map<number, number[]>;
private declare _isNullVars: Set<number>;
private declare _comparisons: Map<
string, string,
Map<string, { eq: number; gt: number; lt: number }> { op: string; rhs: boolean | number | string; var: number }[]
>; > = new Map();
constructor() { for (const [n, e] of variables) {
this._dcSet = []; const op = e[0] as string;
this._variables = new Map(); if (["=", ">", "<"].includes(op) && !Array.isArray(e[2])) {
this._expressions = new Map(); const lhs = JSON.stringify(e[1]);
this._isNullRelations = new Map(); let r = relations.get(lhs);
this._isNullVars = new Set(); if (!r) relations.set(lhs, (r = []));
this._comparisons = new Map(); r.push({ op: op, rhs: e[2], var: n });
} else if (op === "LIKE" && !Array.isArray(e[2]) && !Array.isArray(e[3])) {
const lhs = JSON.stringify(e[1]);
let r = relations.get(lhs);
if (!r) relations.set(lhs, (r = []));
r.push({ op: "", rhs: null, var: n });
} else {
const lhs = JSON.stringify(e);
let r = relations.get(lhs);
if (!r) relations.set(lhs, (r = []));
r.push({ op: "", rhs: null, var: n });
}
} }
getVariable(exp: Expression): number { const dcSet: number[][] = [];
const expStr = JSON.stringify(exp); const isNull: Map<number, number> = new Map();
if (this._variables.has(expStr)) return this._variables.get(expStr);
const v = this._variables.size;
this._variables.set(expStr, v);
this._expressions.set(v, exp);
this.generateIsNullDc(v);
this.generateComparisionDc(v);
if ( for (const [key, rels] of relations) {
expStr === '["IS NULL",["PARAM","DeviceID.ID"]]' || const n = variables.size;
expStr === '["IS NULL",["PARAM","_id"]]' variables.set(n, ["IS NULL", JSON.parse(key)]);
)
this._dcSet.push([(v << 2) ^ 3]);
return v; if (key === '["PARAM","DeviceID.ID"]' || key === '["PARAM","_id"]')
dcSet.push([(n << 2) ^ 3]);
const existingOps = new Map() as Map<string, Map<string, number>>;
for (const rel of rels) {
if (!rel.op) continue;
const rhsStr = JSON.stringify(rel.rhs);
const s = existingOps.get(rhsStr);
if (s) s.set(rel.op, rel.var);
else existingOps.set(rhsStr, new Map([[rel.op, rel.var]]));
} }
private generateIsNullDc(v: number): void { for (const [k, v] of existingOps) {
this._isNullRelations.set(v, []); if (v.size > 1) {
const exp = this._expressions.get(v); if (!v.has("=")) {
const va = variables.size;
const isNull = new Set( variables.set(va, ["=", JSON.parse(key), JSON.parse(k)]);
findIsNullDeps(exp).map((e) => this.getVariable(["IS NULL", e])) rels.push({ op: "=", rhs: JSON.parse(k), var: va });
); v.set("=", va);
if (!isNull.size) return;
for (const n of isNull) {
this._isNullVars.add(n);
this._isNullRelations.get(n).push(v);
this._isNullRelations.get(v).push(n);
this._dcSet.push([(n << 2) ^ 3, (v << 2) ^ 1]);
this._dcSet.push([(n << 2) ^ 3, (v << 2) ^ 3]);
}
this._dcSet.push(
[...isNull].map((n) => (n << 2) ^ 2).concat([(v << 2) ^ 0, (v << 2) ^ 2])
);
} }
private generateComparisionDc(v: number): void { if (!v.has(">")) {
const exp = this._expressions.get(v); const va = variables.size;
if (!Array.isArray(exp)) return; variables.set(va, [">", JSON.parse(key), JSON.parse(k)]);
const op = exp[0]; rels.push({ op: ">", rhs: JSON.parse(k), var: va });
if (![">", "<", "="].includes(op)) return; v.set(">", va);
const rhs = exp[2]; }
const rhsStr = JSON.stringify(rhs);
const lhsStr = JSON.stringify(exp[1]);
let allComps = this._comparisons.get(lhsStr);
if (!allComps) this._comparisons.set(lhsStr, (allComps = new Map()));
if (allComps.has(rhsStr)) return;
const comp = { if (!v.has("<")) {
eq: -1, const va = variables.size;
gt: -1, variables.set(va, ["<", JSON.parse(key), JSON.parse(k)]);
lt: -1, rels.push({ op: "<", rhs: JSON.parse(k), var: va });
}; v.set("<", va);
}
allComps.set(rhsStr, comp); dcSet.push([
(v.get("=") << 2) ^ 1,
comp.eq = this.getVariable(["=", exp[1], exp[2]]); (v.get(">") << 2) ^ 1,
comp.gt = this.getVariable([">", exp[1], exp[2]]); (v.get("<") << 2) ^ 1,
comp.lt = this.getVariable(["<", exp[1], exp[2]]);
this._dcSet.push([
(comp.eq << 2) ^ 1,
(comp.gt << 2) ^ 1,
(comp.lt << 2) ^ 1,
]); ]);
this._dcSet.push([(comp.eq << 2) ^ 3, (comp.gt << 2) ^ 3]);
this._dcSet.push([(comp.eq << 2) ^ 3, (comp.lt << 2) ^ 3]);
this._dcSet.push([(comp.gt << 2) ^ 3, (comp.lt << 2) ^ 3]);
const type1 = typeof exp[2];
if (!["boolean", "number", "string"].includes(type1)) return;
for (const [rhs2Str, comp2] of allComps) {
if (comp2 === comp) continue;
const rhs2 = JSON.parse(rhs2Str);
const type2 = typeof rhs2;
if (!["boolean", "number", "string"].includes(type2)) continue;
let cmp = 0;
if (type1 === type2) cmp = rhs > rhs2 ? 1 : -1;
else if (type1 === "string") cmp = 1;
else if (type2 === "string") cmp = -1;
else cmp = +rhs - +rhs2;
const c1 = cmp > 0 ? comp : comp2;
const c2 = cmp > 0 ? comp2 : comp;
// This is the minimum clauses required if all relavent vars
// were included in the DC set.
// this._dcSet.push([(c1.eq << 2) ^ 3, (c2.eq << 2) ^ 3]);
// this._dcSet.push([(c1.lt << 2) ^ 1, (c2.gt << 2) ^ 1]);
// But we use non-minimal set because intermediate vars
// between any two may not be present in the DC set.
this._dcSet.push([(c1.lt << 2) ^ 1, (c2.gt << 2) ^ 1]);
this._dcSet.push([(c1.lt << 2) ^ 1, (c2.eq << 2) ^ 3]);
this._dcSet.push([(c1.lt << 2) ^ 1, (c2.lt << 2) ^ 3]);
this._dcSet.push([(c1.eq << 2) ^ 3, (c2.gt << 2) ^ 1]);
this._dcSet.push([(c1.eq << 2) ^ 3, (c2.eq << 2) ^ 3]);
this._dcSet.push([(c1.eq << 2) ^ 3, (c2.lt << 2) ^ 3]);
this._dcSet.push([(c1.gt << 2) ^ 3, (c2.gt << 2) ^ 1]);
this._dcSet.push([(c1.gt << 2) ^ 3, (c2.eq << 2) ^ 3]);
this._dcSet.push([(c1.gt << 2) ^ 3, (c2.lt << 2) ^ 3]);
} }
} }
getExpression(v: number): Expression { for (let i = 0; i < rels.length; ++i) {
return this._expressions.get(v); const rel1 = rels[i];
isNull.set(rel1.var, n);
dcSet.push([(n << 2) ^ 3, (rel1.var << 2) ^ 1]);
dcSet.push([(n << 2) ^ 3, (rel1.var << 2) ^ 3]);
dcSet.push([(rel1.var << 2) ^ 0, (rel1.var << 2) ^ 2, (n << 2) ^ 2]);
if (!rel1.op) continue;
for (let j = i + 1; j < rels.length; ++j) {
const rel2 = rels[j];
if (!rel2.op) continue;
let comp = 0;
if (Array.isArray(rel1.rhs) || Array.isArray(rel2.rhs)) {
if (JSON.stringify(rel1.rhs) !== JSON.stringify(rel2.rhs)) continue;
} else if (typeof rel1.rhs === "string") {
if (typeof rel2.rhs === "string") {
if (rel1.rhs > rel2.rhs) comp = 1;
else if (rel1.rhs < rel2.rhs) comp = -1;
} else {
comp = 1;
}
} else if (typeof rel2.rhs === "string") {
comp = -1;
} else {
comp = +rel1.rhs - +rel2.rhs;
} }
getIsNullRelations(v: number): number[] { if (rel1.op === rel2.op) {
return this._isNullRelations.get(v); if (comp === 0)
dcSet.push([(rel1.var << 2) ^ 1, (rel2.var << 2) ^ 1]);
else if (rel1.op === "=")
dcSet.push([(rel1.var << 2) ^ 3, (rel2.var << 2) ^ 3]);
else if (
(rel1.op === ">" && comp > 0) ||
(rel1.op === "<" && comp < 0)
)
dcSet.push([(rel1.var << 2) ^ 3, (rel2.var << 2) ^ 1]);
else if (
(rel1.op === "<" && comp > 0) ||
(rel1.op === ">" && comp < 0)
)
dcSet.push([(rel1.var << 2) ^ 1, (rel2.var << 2) ^ 3]);
} else if (rel1.op === "=" || rel2.op === "=") {
let r1 = rel1,
r2 = rel2,
cmp = comp;
if (rel1.op !== "=") {
r2 = rel1;
r1 = rel2;
cmp = -comp;
}
if (r2.op === ">" || r2.op === "<") {
if (cmp === 0) {
dcSet.push([(r1.var << 2) ^ 3, (r2.var << 2) ^ 3]);
} else {
if ((cmp > 0 && r2.op === ">") || (cmp < 0 && r2.op === "<"))
dcSet.push([(r1.var << 2) ^ 3, (r2.var << 2) ^ 1]);
else dcSet.push([(r1.var << 2) ^ 3, (r2.var << 2) ^ 3]);
}
}
} else if (
[">", "<"].includes(rel1.op) &&
[">", "<"].includes(rel2.op)
) {
if ((rel1.op === ">" && comp < 0) || (rel1.op === "<" && comp > 0))
dcSet.push([(rel1.var << 2) ^ 1, (rel2.var << 2) ^ 1]);
else dcSet.push([(rel1.var << 2) ^ 3, (rel2.var << 2) ^ 3]);
} }
isIsNull(v: number): boolean {
return this._isNullVars.has(v);
} }
getDcSet(minterms?: Minterm[]): number[][] {
if (!minterms) return this._dcSet;
const vars = new Set(minterms.flat().map((v) => v >> 2));
for (const comps of this._comparisons.values()) {
for (const comp of comps.values()) {
const c = Object.values(comp).filter((v) => !vars.has(v));
if (c.length === 1) vars.add(c[0]);
} }
} }
for (const n of this._isNullVars) return { dcSet, isNull };
if (this._isNullRelations.get(n).some((v) => vars.has(v))) vars.add(n);
const dcSet = this._dcSet.filter((m) => m.every((v) => vars.has(v >> 2)));
return dcSet;
}
} }
function sanitizeMinterms( function sanitizeMinterms(
minterms: number[][], minterms: number[][],
context: SynthContext isNull: Map<number, number>
): number[][] { ): number[][] {
const res = [] as number[][]; const res = [] as number[][];
loop: for (const m of minterms) { loop: for (const m of minterms) {
@@ -1025,20 +999,13 @@ function sanitizeMinterms(
const perms: number[][] = []; const perms: number[][] = [];
for (const [k, v] of merged) { for (const [k, v] of merged) {
if (v === 0b1010) continue loop; if (v === 0b1010) continue loop;
const isNullVars = context.getIsNullRelations(k); const n = isNull.get(k) << 2;
const t = k << 2; const t = k << 2;
if (v === 0b0101) { if (v === 0b0101) minterm.push(n ^ 3);
if (isNullVars.length === 1) minterm.push((isNullVars[0] << 2) ^ 3); else if (v === 0b0001) perms.push([n ^ 3, t ^ 3]);
else perms.push(isNullVars.map((n) => (n << 2) ^ 3)); else if (v === 0b0100) perms.push([n ^ 3, t ^ 1]);
} else if (v === 0b0001) { else if (v & 0b1000) minterm.push(t ^ 3);
perms.push([...isNullVars.map((n) => (n << 2) ^ 3), t ^ 3]); else if (v & 0b0010) minterm.push(t ^ 1);
} else if (v === 0b0100) {
perms.push([...isNullVars.map((n) => (n << 2) ^ 3), t ^ 1]);
} else if (v & 0b1000) {
minterm.push(t ^ 3);
} else if (v & 0b0010) {
minterm.push(t ^ 1);
}
} }
let ms = [minterm]; let ms = [minterm];
while (perms.length) { while (perms.length) {
@@ -1053,13 +1020,17 @@ function sanitizeMinterms(
} }
function boolExprSynthToExpression(boolExpr: BoolExprSynth): Expression { function boolExprSynthToExpression(boolExpr: BoolExprSynth): Expression {
const context = new SynthContext(); const context: Map<string, number> = new Map();
let minterms = boolExpr.true(context); let minterms = boolExpr.true(context);
minterms = sanitizeMinterms(minterms, context); const variables = new Map(
const canRaise = getCanRaiseCallback(context); Array.from(context).map(([k, v]) => [v, JSON.parse(k) as Expression])
const dcSet = context.getDcSet(minterms); );
const { dcSet, isNull } = generateDcSetAndIsNull(variables);
minterms = sanitizeMinterms(minterms, isNull);
const canRaise = getCanRaiseCallback(isNull);
minterms = espresso(minterms, dcSet, { canRaise }); minterms = espresso(minterms, dcSet, { canRaise });
return sopToExpression(minterms, context); return sopToExpression(minterms, variables);
} }
function mapCallback(exp: Expression | BoolExprSynth): Expression { function mapCallback(exp: Expression | BoolExprSynth): Expression {
@@ -1072,22 +1043,22 @@ function mapCallback(exp: Expression | BoolExprSynth): Expression {
const op = exp[0]; const op = exp[0];
if (op === "IS NULL") if (op === "IS NULL")
return new IsNullSynth(toBoolExprSynth(exp[1])) as unknown as Expression; return (new IsNullSynth(toBoolExprSynth(exp[1])) as unknown) as Expression;
if (op === "IS NOT NULL") { if (op === "IS NOT NULL") {
return new NotSynth( return (new NotSynth(
new IsNullSynth(toBoolExprSynth(exp[1])) new IsNullSynth(toBoolExprSynth(exp[1]))
) as unknown as Expression; ) as unknown) as Expression;
} else if (op === "NOT") { } else if (op === "NOT") {
return new NotSynth(toBoolExprSynth(exp[1])) as unknown as Expression; return (new NotSynth(toBoolExprSynth(exp[1])) as unknown) as Expression;
} else if (op === "OR") { } else if (op === "OR") {
return new OrSynth( return (new OrSynth(
...exp.slice(1).map((a) => toBoolExprSynth(a)) ...exp.slice(1).map((a) => toBoolExprSynth(a))
) as unknown as Expression; ) as unknown) as Expression;
} else if (op === "AND") { } else if (op === "AND") {
return new AndSynth( return (new AndSynth(
...exp.slice(1).map((a) => toBoolExprSynth(a)) ...exp.slice(1).map((a) => toBoolExprSynth(a))
) as unknown as Expression; ) as unknown) as Expression;
} }
for (let i = 1; i < exp.length; ++i) { for (let i = 1; i < exp.length; ++i) {
if (exp[i] instanceof BoolExprSynth) { if (exp[i] instanceof BoolExprSynth) {
@@ -1103,20 +1074,19 @@ function mapCallback(exp: Expression | BoolExprSynth): Expression {
} }
function getCanRaiseCallback( function getCanRaiseCallback(
context: SynthContext isNull: Map<number, number>
): (idx: number, set: Set<number>) => boolean { ): (idx: number, set: Set<number>) => boolean {
return (idx: number, set: Set<number>): boolean => { return (idx: number, set: Set<number>): boolean => {
const i = idx >> 2; const i = idx >> 2;
const vars = context.getIsNullRelations(i); const n = isNull.get(i);
if (!context.isIsNull(i)) { if (n != null) {
if (!(idx & 1)) return true; if (!(idx & 1)) return true;
if (!set.has(idx ^ 3)) return true; if (set.has((n << 2) ^ 2)) return true;
for (const n of vars) if (set.has((n << 2) ^ 3)) return true; if (set.has((n << 2) ^ 3)) return true;
for (const n of vars) if (!set.has((n << 2) ^ 2)) return false; return !set.has(idx ^ 3);
return true;
} }
for (const [k, v] of isNull) {
for (const k of vars) { if (v !== i) continue;
if (set.has((k << 2) ^ 1)) continue; if (set.has((k << 2) ^ 1)) continue;
if (set.has((k << 2) ^ 3)) continue; if (set.has((k << 2) ^ 3)) continue;
if (set.has((k << 2) ^ 0)) return false; if (set.has((k << 2) ^ 0)) return false;
@@ -1131,31 +1101,33 @@ export function minimize(expr: Expression, boolean = false): Expression {
expr = map(expr, mapCallback); expr = map(expr, mapCallback);
if (Array.isArray(expr) && expr[0] === "CASE") { if (Array.isArray(expr) && expr[0] === "CASE") {
if (!boolean) { if (!boolean) {
const context = new SynthContext(); const context: Map<string, number> = new Map();
const whens = expr.filter((e, i) => i % 2).map((e) => e.true(context)); const whens = expr.filter((e, i) => i % 2).map((e) => e.true(context));
const caseDcSet: Minterm[] = []; const variables = new Map(
const canRaise = getCanRaiseCallback(context); Array.from(context).map(([k, v]) => [v, JSON.parse(k) as Expression])
);
const { dcSet, isNull } = generateDcSetAndIsNull(variables);
const canRaise = getCanRaiseCallback(isNull);
const res: Expression = ["CASE"]; const res: Expression = ["CASE"];
for (let i = 1; i < expr.length; i += 2) { for (let i = 1; i < expr.length; i += 2) {
let minterms = sanitizeMinterms(whens[(i - 1) / 2], context); let minterms = sanitizeMinterms(whens[(i - 1) / 2], isNull);
const dcSet = context.getDcSet(minterms.concat(caseDcSet));
minterms = espresso(minterms, dcSet, { canRaise }); minterms = espresso(minterms, dcSet, { canRaise });
if (!minterms.length) continue; if (!minterms.length) continue;
const w = sopToExpression(minterms, context); const w = sopToExpression(minterms, variables);
let t = expr[i + 1]; let t = expr[i + 1];
if (t instanceof BoolExprSynth) t = boolExprSynthToExpression(t); if (t instanceof BoolExprSynth) t = boolExprSynthToExpression(t);
res.push(w, t); res.push(w, t);
if (w === true) break; if (w === true) break;
caseDcSet.push(...minterms); dcSet.push(...minterms);
} }
while (res[res.length - 1] == null) res.splice(-2); while (res[res.length - 1] == null) res.splice(-2);
if (res.length < 3) return null; if (res.length < 3) return null;
return res; return res;
} }
expr = toBoolExprSynth(expr) as unknown as Expression; expr = (toBoolExprSynth(expr) as unknown) as Expression;
} }
if (boolean) expr = toBoolExprSynth(expr) as unknown as Expression; if (boolean) expr = (toBoolExprSynth(expr) as unknown) as Expression;
if (expr instanceof BoolExprSynth) expr = boolExprSynthToExpression(expr); if (expr instanceof BoolExprSynth) expr = boolExprSynthToExpression(expr);
return expr; return expr;
@@ -1173,7 +1145,7 @@ export function unionDiff(
if (!expr1) { if (!expr1) {
if (Array.isArray(expr2) && expr2[0] === "CASE") if (Array.isArray(expr2) && expr2[0] === "CASE")
expr2 = toBoolExprSynth(expr2) as unknown as Expression; expr2 = (toBoolExprSynth(expr2) as unknown) as Expression;
if (expr2 instanceof BoolExprSynth) if (expr2 instanceof BoolExprSynth)
expr2 = boolExprSynthToExpression(expr2); expr2 = boolExprSynthToExpression(expr2);
return [expr2, expr2]; return [expr2, expr2];
@@ -1184,37 +1156,35 @@ export function unionDiff(
expr1 = map(expr1, mapCallback); expr1 = map(expr1, mapCallback);
const b1 = toBoolExprSynth(expr1); const b1 = toBoolExprSynth(expr1);
const context = new SynthContext(); const context: Map<string, number> = new Map();
const expr2Minterms = b2.true(context); const expr2Minterms = b2.true(context);
const expr1Minterms = b1.true(context); const expr1Minterms = b1.true(context);
const expr1NullMinterms = b1.null(context); const expr1NullMinterms = b1.null(context);
const expr1FalseMinterms = b1.false(context); const expr1FalseMinterms = b1.false(context);
const variables = new Map(
Array.from(context).map(([k, v]) => [v, JSON.parse(k) as Expression])
);
const { dcSet, isNull } = generateDcSetAndIsNull(variables);
const unionMinterms = sanitizeMinterms( const unionMinterms = sanitizeMinterms(
[...expr1Minterms, ...expr2Minterms], [...expr1Minterms, ...expr2Minterms],
context isNull
); );
const diffMinterms = sanitizeMinterms( const diffMinterms = sanitizeMinterms(
complement([ complement([
...complement([...expr1NullMinterms, ...expr1FalseMinterms]), ...complement([...expr1NullMinterms, ...expr1FalseMinterms]),
...complement(expr2Minterms), ...complement(expr2Minterms),
]), ]),
context isNull
); );
const canRaise = getCanRaiseCallback(context); const canRaise = getCanRaiseCallback(isNull);
const union = espresso(unionMinterms, context.getDcSet(unionMinterms), { const union = espresso(unionMinterms, dcSet, { canRaise });
canRaise, const diff = espresso(diffMinterms, dcSet, { canRaise });
});
const diff = espresso(diffMinterms, context.getDcSet(diffMinterms), { return [sopToExpression(union, variables), sopToExpression(diff, variables)];
canRaise,
});
return [sopToExpression(union, context), sopToExpression(diff, context)];
} }
export function covers(expr1: Expression, expr2: Expression): boolean { export function covers(expr1: Expression, expr2: Expression): boolean {
@@ -1228,13 +1198,13 @@ export function covers(expr1: Expression, expr2: Expression): boolean {
expr2 = map(expr2, mapCallback); expr2 = map(expr2, mapCallback);
const b2 = toBoolExprSynth(expr2); const b2 = toBoolExprSynth(expr2);
const context = new SynthContext(); const context: Map<string, number> = new Map();
const expr1Minterms = b1.true(context); const expr1Minterms = b1.true(context);
const expr2Minterms = b2.true(context); const expr2Minterms = b2.true(context);
const variables = new Map(
Array.from(context).map(([k, v]) => [v, JSON.parse(k) as Expression])
);
const { dcSet } = generateDcSetAndIsNull(variables);
return tautology([ return tautology([...complement(expr2Minterms), ...dcSet, ...expr1Minterms]);
...complement(expr2Minterms),
...context.getDcSet(),
...expr1Minterms,
]);
} }
+1 -1
View File
@@ -29,7 +29,7 @@ function interpretEscapes(str): string {
r: "\r", r: "\r",
t: "\t", t: "\t",
}; };
return str.replace(/\\(u[0-9a-fA-F]{4}|[^u])/g, (_, escape) => { return str.replace(/\\(u[0-9a-fA-F]{4}|[^u])/, (_, escape) => {
const type = escape.charAt(0); const type = escape.charAt(0);
const hex = escape.slice(1); const hex = escape.slice(1);
if (type === "u") return String.fromCharCode(parseInt(hex, 16)); if (type === "u") return String.fromCharCode(parseInt(hex, 16));
+11 -3
View File
@@ -178,11 +178,11 @@ export function evaluateCallback(exp: Expression): Expression {
} else if (exp[0] === "IS NULL") { } else if (exp[0] === "IS NULL") {
if (isArray(exp[1])) return exp; if (isArray(exp[1])) return exp;
else if (exp[1] == null) return true; else if (exp[1] == null) return true;
else return false; else return null;
} else if (exp[0] === "IS NOT NULL") { } else if (exp[0] === "IS NOT NULL") {
if (isArray(exp[1])) return exp; if (isArray(exp[1])) return exp;
else if (exp[1] != null) return true; else if (exp[1] != null) return true;
else return false; else return null;
} else if (exp[0] === "LIKE") { } else if (exp[0] === "LIKE") {
if (isArray(exp[1]) || isArray(exp[2]) || isArray(exp[3])) return exp; if (isArray(exp[1]) || isArray(exp[2]) || isArray(exp[3])) return exp;
else if ( else if (
@@ -334,7 +334,15 @@ export async function evaluateAsync(
if (!isArray(e)) return e; if (!isArray(e)) return e;
if (e[0] === "FUNC") { if (e[0] === "FUNC") {
if (e[1] === "NOW") if (now) return now; if (e[1] === "NOW") {
if (now) return now;
} else if (e[1] === "UPPER") {
if (e[2] == null) return null;
if (!isArray(e[2])) return `${e[2]}`.toUpperCase();
} else if (e[1] === "LOWER") {
if (e[2] == null) return null;
if (!isArray(e[2])) return `${e[2]}`.toLowerCase();
}
} else if (e[0] === "PARAM") { } else if (e[0] === "PARAM") {
if (e[1] == null) return null; if (e[1] == null) return null;
if (obj && !isArray(e[1])) { if (obj && !isArray(e[1])) {
+3 -3
View File
@@ -20,9 +20,9 @@
import Path from "./path"; import Path from "./path";
export default class PathSet { export default class PathSet {
private declare lengthIndex: Set<Path>[]; private lengthIndex: Set<Path>[];
private declare fragmentIndex: Map<string, Set<Path>>[]; private fragmentIndex: Map<string, Set<Path>>[];
private declare stringIndex: Map<string, Path>; private stringIndex: Map<string, Path>;
public constructor() { public constructor() {
this.lengthIndex = []; this.lengthIndex = [];
+5 -5
View File
@@ -49,11 +49,11 @@ function legalChar(c: number): boolean {
} }
export default class Path { export default class Path {
public declare readonly segments: Segments; public readonly segments: Segments;
public declare readonly wildcard: number; public readonly wildcard: number;
public declare readonly alias: number; public readonly alias: number;
protected declare _string: string; protected _string: string;
protected declare _stringIndex: number[]; protected _stringIndex: number[];
protected static parseAliasValue( protected static parseAliasValue(
pattern: string, pattern: string,
+4 -4
View File
@@ -43,7 +43,7 @@ const options = {
CWMP_WORKER_PROCESSES: { type: "int", default: 0 }, CWMP_WORKER_PROCESSES: { type: "int", default: 0 },
CWMP_PORT: { type: "int", default: 7547 }, CWMP_PORT: { type: "int", default: 7547 },
CWMP_INTERFACE: { type: "string", default: "::" }, CWMP_INTERFACE: { type: "string", default: "0.0.0.0" },
CWMP_SSL_CERT: { type: "string", default: "" }, CWMP_SSL_CERT: { type: "string", default: "" },
CWMP_SSL_KEY: { type: "string", default: "" }, CWMP_SSL_KEY: { type: "string", default: "" },
CWMP_LOG_FILE: { type: "path", default: "" }, CWMP_LOG_FILE: { type: "path", default: "" },
@@ -51,7 +51,7 @@ const options = {
NBI_WORKER_PROCESSES: { type: "int", default: 0 }, NBI_WORKER_PROCESSES: { type: "int", default: 0 },
NBI_PORT: { type: "int", default: 7557 }, NBI_PORT: { type: "int", default: 7557 },
NBI_INTERFACE: { type: "string", default: "::" }, NBI_INTERFACE: { type: "string", default: "0.0.0.0" },
NBI_SSL_CERT: { type: "string", default: "" }, NBI_SSL_CERT: { type: "string", default: "" },
NBI_SSL_KEY: { type: "string", default: "" }, NBI_SSL_KEY: { type: "string", default: "" },
NBI_LOG_FILE: { type: "path", default: "" }, NBI_LOG_FILE: { type: "path", default: "" },
@@ -59,7 +59,7 @@ const options = {
FS_WORKER_PROCESSES: { type: "int", default: 0 }, FS_WORKER_PROCESSES: { type: "int", default: 0 },
FS_PORT: { type: "int", default: 7567 }, FS_PORT: { type: "int", default: 7567 },
FS_INTERFACE: { type: "string", default: "::" }, FS_INTERFACE: { type: "string", default: "0.0.0.0" },
FS_SSL_CERT: { type: "string", default: "" }, FS_SSL_CERT: { type: "string", default: "" },
FS_SSL_KEY: { type: "string", default: "" }, FS_SSL_KEY: { type: "string", default: "" },
FS_URL_PREFIX: { type: "string", default: "" }, FS_URL_PREFIX: { type: "string", default: "" },
@@ -68,7 +68,7 @@ const options = {
UI_WORKER_PROCESSES: { type: "int", default: 0 }, UI_WORKER_PROCESSES: { type: "int", default: 0 },
UI_PORT: { type: "int", default: 3000 }, UI_PORT: { type: "int", default: 3000 },
UI_INTERFACE: { type: "string", default: "::" }, UI_INTERFACE: { type: "string", default: "0.0.0.0" },
UI_SSL_CERT: { type: "string", default: "" }, UI_SSL_CERT: { type: "string", default: "" },
UI_SSL_KEY: { type: "string", default: "" }, UI_SSL_KEY: { type: "string", default: "" },
UI_LOG_FILE: { type: "path", default: "" }, UI_LOG_FILE: { type: "path", default: "" },
+13 -13
View File
@@ -19,7 +19,7 @@
import * as crypto from "crypto"; import * as crypto from "crypto";
import * as dgram from "dgram"; import * as dgram from "dgram";
import { URL } from "url"; import { parse } from "url";
import * as http from "http"; import * as http from "http";
import { evaluateAsync } from "./common/expression"; import { evaluateAsync } from "./common/expression";
import { Expression } from "./types"; import { Expression } from "./types";
@@ -61,26 +61,24 @@ async function extractAuth(
} }
function httpGet( function httpGet(
url: URL,
options: http.RequestOptions, options: http.RequestOptions,
_debug: boolean, _debug: boolean,
deviceId: string deviceId: string
): Promise<{ statusCode: number; headers: http.IncomingHttpHeaders }> { ): Promise<{ statusCode: number; headers: http.IncomingHttpHeaders }> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const req = http const req = http
.get(url, options, (res) => { .get(options, (res) => {
res.resume(); res.resume();
resolve({ statusCode: res.statusCode, headers: res.headers }); resolve({ statusCode: res.statusCode, headers: res.headers });
if (_debug) { if (_debug) {
debug.outgoingHttpRequest(req, deviceId, "GET", url, null); debug.outgoingHttpRequest(req, deviceId, options, null);
debug.incomingHttpResponse(res, deviceId, null); debug.incomingHttpResponse(res, deviceId, null);
} }
}) })
.on("error", (err) => { .on("error", (err) => {
req.destroy(); req.destroy();
reject(err); reject(err);
if (_debug) if (_debug) debug.outgoingHttpRequestError(req, deviceId, options, err);
debug.outgoingHttpRequestError(req, deviceId, "GET", url, err);
}) })
.on("timeout", () => { .on("timeout", () => {
req.destroy(); req.destroy();
@@ -96,13 +94,15 @@ export async function httpConnectionRequest(
_debug: boolean, _debug: boolean,
deviceId: string deviceId: string
): Promise<string> { ): Promise<string> {
const url = new URL(address); const options: http.RequestOptions = parse(address);
if (url.protocol !== "http:") if (options.protocol !== "http:")
return "Invalid connection request URL or protocol"; return "Invalid connection request URL or protocol";
const options: http.RequestOptions = { options.agent = new http.Agent({
agent: new http.Agent({ maxSockets: 1, keepAlive: true, timeout: timeout }), maxSockets: 1,
}; keepAlive: true,
timeout: timeout,
});
let authHeader: Record<string, string>; let authHeader: Record<string, string>;
let username: string; let username: string;
@@ -145,12 +145,12 @@ export async function httpConnectionRequest(
let res: { statusCode: number; headers: http.IncomingHttpHeaders }; let res: { statusCode: number; headers: http.IncomingHttpHeaders };
try { try {
res = await httpGet(url, opts, _debug, deviceId); res = await httpGet(opts, _debug, deviceId);
} catch (err) { } catch (err) {
// Workaround for some devices unexpectedly closing the connection // Workaround for some devices unexpectedly closing the connection
if (authHeader) { if (authHeader) {
try { try {
res = await httpGet(url, opts, _debug, deviceId); res = await httpGet(opts, _debug, deviceId);
} catch (err) { } catch (err) {
return `Connection request error: ${err.message}`; return `Connection request error: ${err.message}`;
} }
+202 -133
View File
@@ -93,7 +93,7 @@ async function authenticate(
} }
} }
if (authentication?.method === "Digest") { if (authentication && authentication.method === "Digest") {
const sessionNonce = sessionsNonces.get(sessionContext.httpRequest.socket); const sessionNonce = sessionsNonces.get(sessionContext.httpRequest.socket);
if ( if (
@@ -361,6 +361,39 @@ async function transferComplete(sessionContext, rpc): Promise<void> {
return writeResponse(sessionContext, res); return writeResponse(sessionContext, res);
} }
async function duStateChangeComplete(sessionContext, rpc): Promise<void> {
const { acsResponse, operation, fault } = await session.duStateChangeComplete(
sessionContext,
rpc.cpeRequest
);
if (!operation) {
logger.accessWarn({
sessionContext: sessionContext,
message: "Unrecognized command key",
rpc: rpc,
});
}
if (fault) {
Object.assign(sessionContext.retries, operation.retries);
recordFault(
sessionContext,
fault,
operation.provisions,
operation.channels
);
}
const res = soap.response({
id: rpc.id,
acsResponse: acsResponse,
cwmpVersion: sessionContext.cwmpVersion,
});
return writeResponse(sessionContext, res);
}
// Append provisions and remove duplicates // Append provisions and remove duplicates
function appendProvisions(original, toAppend): boolean { function appendProvisions(original, toAppend): boolean {
let modified = false; let modified = false;
@@ -434,7 +467,7 @@ async function applyPresets(sessionContext: SessionContext): Promise<void> {
const deviceEvents = {}; const deviceEvents = {};
for (const p of deviceData.paths.find(Path.parse("Events.*"), false, true)) { for (const p of deviceData.paths.find(Path.parse("Events.*"), false, true)) {
const attrs = deviceData.attributes.get(p); const attrs = deviceData.attributes.get(p);
if (attrs?.value && attrs.value[1][0] >= sessionContext.timestamp) if (attrs && attrs.value && attrs.value[1][0] >= sessionContext.timestamp)
deviceEvents[p.segments[1] as string] = true; deviceEvents[p.segments[1] as string] = true;
} }
@@ -458,7 +491,7 @@ async function applyPresets(sessionContext: SessionContext): Promise<void> {
if (!eventsMatch) continue; if (!eventsMatch) continue;
if (preset.schedule?.schedule) { if (preset.schedule && preset.schedule.schedule) {
const r = scheduling.cron( const r = scheduling.cron(
sessionContext.timestamp, sessionContext.timestamp,
preset.schedule.schedule preset.schedule.schedule
@@ -497,11 +530,10 @@ async function applyPresets(sessionContext: SessionContext): Promise<void> {
defer: true, defer: true,
})); }));
const { const { fault: flt, rpcId: reqId, rpc: acsReq } = await session.rpcRequest(
fault: flt, sessionContext,
rpcId: reqId, declarations
rpc: acsReq, );
} = await session.rpcRequest(sessionContext, declarations);
if (flt) { if (flt) {
recordFault(sessionContext, flt); recordFault(sessionContext, flt);
@@ -573,11 +605,10 @@ async function applyPresets(sessionContext: SessionContext): Promise<void> {
deviceData.timestamps.dirty = 0; deviceData.timestamps.dirty = 0;
deviceData.attributes.dirty = 0; deviceData.attributes.dirty = 0;
const { const { fault: fault, rpcId: id, rpc: acsRequest } = await session.rpcRequest(
fault: fault, sessionContext,
rpcId: id, null
rpc: acsRequest, );
} = await session.rpcRequest(sessionContext, null);
if (fault) { if (fault) {
recordFault(sessionContext, fault); recordFault(sessionContext, fault);
@@ -607,11 +638,10 @@ async function applyPresets(sessionContext: SessionContext): Promise<void> {
} }
async function nextRpc(sessionContext: SessionContext): Promise<void> { async function nextRpc(sessionContext: SessionContext): Promise<void> {
const { const { fault: fault, rpcId: id, rpc: acsRequest } = await session.rpcRequest(
fault: fault, sessionContext,
rpcId: id, null
rpc: acsRequest, );
} = await session.rpcRequest(sessionContext, null);
if (fault) { if (fault) {
recordFault(sessionContext, fault); recordFault(sessionContext, fault);
@@ -789,7 +819,7 @@ async function endSession(sessionContext: SessionContext): Promise<void> {
} }
} }
if (sessionContext.doneTasks?.length) { if (sessionContext.doneTasks && sessionContext.doneTasks.length) {
saveCache = true; saveCache = true;
promises.push( promises.push(
db.clearTasks(sessionContext.deviceId, sessionContext.doneTasks) db.clearTasks(sessionContext.deviceId, sessionContext.doneTasks)
@@ -881,6 +911,27 @@ async function sendAcsRequest(
return writeResponse(sessionContext, res); return writeResponse(sessionContext, res);
} }
async function getSession(
connection: Socket,
sessionId: string
): Promise<SessionContext> {
let sessionContext = currentSessions.get(connection);
if (sessionContext) {
currentSessions.delete(connection);
return sessionContext;
}
if (!sessionId) return null;
await new Promise((resolve) => setTimeout(resolve, 100));
const sessionContextString = await cache.pop(`session_${sessionId}`);
if (!sessionContextString) return null;
sessionContext = await session.deserialize(sessionContextString);
connection.setTimeout(sessionContext.timeout * 1000);
return sessionContext;
}
// Only needed to prevent tree shaking from removing the remoteAddress // Only needed to prevent tree shaking from removing the remoteAddress
// workaround in onConnection function. // workaround in onConnection function.
const remoteAddressWorkaround = new WeakMap<Socket, string>(); const remoteAddressWorkaround = new WeakMap<Socket, string>();
@@ -1027,13 +1078,13 @@ async function reportBadState(sessionContext: SessionContext): Promise<void> {
sessionContext: sessionContext, sessionContext: sessionContext,
}); });
const httpResponse = sessionContext.httpResponse; const httpResponse = sessionContext.httpResponse;
currentSessions.delete(httpResponse.socket);
const body = "Bad session state"; const body = "Bad session state";
httpResponse.setHeader("Content-Length", Buffer.byteLength(body)); httpResponse.setHeader("Content-Length", Buffer.byteLength(body));
httpResponse.writeHead(400, { Connection: "close" }); httpResponse.writeHead(400, { Connection: "close" });
if (sessionContext.debug) if (sessionContext.debug)
debug.outgoingHttpResponse(httpResponse, sessionContext.deviceId, body); debug.outgoingHttpResponse(httpResponse, sessionContext.deviceId, body);
httpResponse.end(body); httpResponse.end(body);
return endSession(sessionContext);
} }
async function responseUnauthorized( async function responseUnauthorized(
@@ -1085,7 +1136,7 @@ async function processRequest(
} }
if (sessionContext.state === 0) { if (sessionContext.state === 0) {
if (rpc.cpeRequest?.name !== "Inform") if (!rpc.cpeRequest || rpc.cpeRequest.name !== "Inform")
return reportBadState(sessionContext); return reportBadState(sessionContext);
const res = await inform(sessionContext, rpc); const res = await inform(sessionContext, rpc);
@@ -1221,6 +1272,16 @@ async function processRequest(
cwmpVersion: sessionContext.cwmpVersion, cwmpVersion: sessionContext.cwmpVersion,
}); });
return writeResponse(sessionContext, res); return writeResponse(sessionContext, res);
} else if (rpc.cpeRequest.name === "DUStateChangeComplete") {
if (sessionContext.state !== 1) return reportBadState(sessionContext);
logger.accessInfo({
sessionContext: sessionContext,
message: "CPE request",
rpc: rpc,
});
return duStateChangeComplete(sessionContext, rpc);
} else { } else {
if (sessionContext.state !== 1 || rpc.cpeRequest.name === "Inform") if (sessionContext.state !== 1 || rpc.cpeRequest.name === "Inform")
return void reportBadState(sessionContext); return void reportBadState(sessionContext);
@@ -1305,48 +1366,6 @@ export function listener(
}); });
} }
async function clientError(
httpRequest: IncomingMessage,
httpResponse: ServerResponse,
sessionContext: SessionContext,
body: string,
msg: string
): Promise<void> {
let debugEnabled = false;
let deviceId: string = null;
if (sessionContext) {
debugEnabled = sessionContext.debug;
deviceId = sessionContext.deviceId;
} else {
const cacheSnapshot = await localCache.getCurrentSnapshot();
debugEnabled = !!localCache.getConfig(
cacheSnapshot,
"cwmp.debug",
{
remoteAddress: getRequestOrigin(httpRequest).remoteAddress,
},
Date.now(),
(e) => {
if (Array.isArray(e) && e[0] === "FUNC" && e[1] === "REMOTE_ADDRESS")
return getRequestOrigin(httpRequest).remoteAddress;
return e;
}
);
}
httpResponse.setHeader("Content-Length", Buffer.byteLength(msg));
httpResponse.writeHead(400, { Connection: "close" });
if (debugEnabled) {
debug.incomingHttpRequest(httpRequest, deviceId, body);
debug.outgoingHttpResponse(httpResponse, deviceId, msg);
}
httpResponse.end(msg);
if (sessionContext) await endSession(sessionContext);
}
function decodeString(buffer: Buffer, charset: string): string { function decodeString(buffer: Buffer, charset: string): string {
try { try {
return buffer.toString(charset as BufferEncoding); return buffer.toString(charset as BufferEncoding);
@@ -1373,8 +1392,7 @@ async function listenerAsync(
let sessionId; let sessionId;
// Separation by comma is important as some devices don't comform to standard // Separation by comma is important as some devices don't comform to standard
const COOKIE_REGEX = const COOKIE_REGEX = /\s*([a-zA-Z0-9\-_]+?)\s*=\s*"?([a-zA-Z0-9\-_]*?)"?\s*(,|;|$)/g;
/\s*([a-zA-Z0-9\-_]+?)\s*=\s*"?([a-zA-Z0-9\-_]*?)"?\s*(,|;|$)/g;
let match; let match;
while ((match = COOKIE_REGEX.exec(httpRequest.headers.cookie))) while ((match = COOKIE_REGEX.exec(httpRequest.headers.cookie)))
if (match[1] === "session") sessionId = match[2]; if (match[1] === "session") sessionId = match[2];
@@ -1435,14 +1453,15 @@ async function listenerAsync(
// Request aborted // Request aborted
if (!body) return; if (!body) return;
let sessionContext = currentSessions.get(httpRequest.socket); const newConnection = !currentSessions.has(httpRequest.socket);
const sessionContext = await getSession(httpRequest.socket, sessionId);
if (sessionContext) { if (sessionContext) {
currentSessions.delete(httpRequest.socket);
sessionContext.httpRequest = httpRequest; sessionContext.httpRequest = httpRequest;
sessionContext.httpResponse = httpResponse; sessionContext.httpResponse = httpResponse;
if ( if (
sessionContext.sessionId !== sessionId || (newConnection && sessionContext.sessionId !== sessionId) ||
sessionContext.lastActivity + sessionContext.timeout * 1000 < Date.now() sessionContext.lastActivity + sessionContext.timeout * 1000 < Date.now()
) { ) {
logger.accessError({ logger.accessError({
@@ -1450,14 +1469,29 @@ async function listenerAsync(
sessionContext: sessionContext, sessionContext: sessionContext,
}); });
return clientError( const _body = "Invalid session";
httpRequest, httpResponse.setHeader("Content-Length", Buffer.byteLength(_body));
httpResponse.writeHead(400, { Connection: "close" });
if (sessionContext.debug) {
debug.outgoingHttpResponse(
httpResponse, httpResponse,
sessionContext, sessionContext.deviceId,
body.toString(), _body
"Invalid session"
); );
} }
httpResponse.end(_body);
await endSession(sessionContext);
return;
}
if (newConnection && sessionContext.authState !== 1)
sessionContext.authState = 0;
} else if (stats.concurrentRequests > MAX_CONCURRENT_REQUESTS) {
// Check again just in case device included old session ID
// from the previous session
httpResponse.writeHead(503, { "Retry-after": 60, Connection: "close" });
httpResponse.end("503 Service Unavailable");
stats.droppedRequests += 1;
return;
} }
let charset: string; let charset: string;
@@ -1477,16 +1511,6 @@ async function listenerAsync(
const bodyStr = decodeString(body, charset); const bodyStr = decodeString(body, charset);
if (bodyStr == null) { if (bodyStr == null) {
if (!sessionContext && sessionId) {
await new Promise((resolve) => setTimeout(resolve, 100));
const sessionContextString = await cache.pop(`session_${sessionId}`);
if (sessionContextString) {
sessionContext = await session.deserialize(sessionContextString);
sessionContext.httpRequest = httpRequest;
sessionContext.httpResponse = httpResponse;
}
}
const msg = `Unknown encoding '${charset}'`; const msg = `Unknown encoding '${charset}'`;
logger.accessError({ logger.accessError({
message: "XML parse error", message: "XML parse error",
@@ -1496,87 +1520,132 @@ async function listenerAsync(
httpResponse: httpResponse, httpResponse: httpResponse,
}, },
}); });
return clientError( httpResponse.setHeader("Content-Length", Buffer.byteLength(msg));
httpResponse.writeHead(400, { Connection: "close" });
if (sessionContext) {
if (sessionContext.debug) {
debug.incomingHttpRequest(
httpRequest, httpRequest,
httpResponse, sessionContext.deviceId,
sessionContext, body.toString()
body.toString(),
msg
); );
debug.outgoingHttpResponse(httpResponse, sessionContext.deviceId, msg);
}
await endSession(sessionContext);
} else {
const cacheSnapshot = await localCache.getCurrentSnapshot();
const d = !!localCache.getConfig(
cacheSnapshot,
"cwmp.debug",
{
remoteAddress: getRequestOrigin(httpRequest).remoteAddress,
},
Date.now(),
(e) => {
if (Array.isArray(e) && e[0] === "FUNC" && e[1] === "REMOTE_ADDRESS")
return getRequestOrigin(httpRequest).remoteAddress;
return e;
}
);
if (d) {
debug.incomingHttpRequest(httpRequest, null, body.toString());
debug.outgoingHttpResponse(httpResponse, null, msg);
}
}
httpResponse.end(msg);
return;
} }
const parseWarnings = []; const parseWarnings = [];
let rpc: SoapMessage; let rpc: SoapMessage;
try { try {
rpc = soap.request(bodyStr, parseWarnings); rpc = soap.request(
} catch (err) { bodyStr,
if (!sessionContext && sessionId) { sessionContext ? sessionContext.cwmpVersion : null,
await new Promise((resolve) => setTimeout(resolve, 100)); parseWarnings
const sessionContextString = await cache.pop(`session_${sessionId}`); );
if (sessionContextString) { } catch (error) {
sessionContext = await session.deserialize(sessionContextString);
sessionContext.httpRequest = httpRequest;
sessionContext.httpResponse = httpResponse;
}
}
logger.accessError({ logger.accessError({
message: "XML parse error", message: "XML parse error",
parseError: err.message, parseError: error.message.trim(),
sessionContext: sessionContext || { sessionContext: sessionContext || {
httpRequest: httpRequest, httpRequest: httpRequest,
httpResponse: httpResponse, httpResponse: httpResponse,
}, },
}); });
httpResponse.setHeader("Content-Length", Buffer.byteLength(error.message));
return clientError( httpResponse.writeHead(400, { Connection: "close" });
if (sessionContext) {
if (sessionContext.debug) {
debug.incomingHttpRequest(
httpRequest, httpRequest,
sessionContext.deviceId,
bodyStr
);
debug.outgoingHttpResponse(
httpResponse, httpResponse,
sessionContext, sessionContext.deviceId,
bodyStr, error.message
err.message
); );
} }
await endSession(sessionContext);
if (!sessionContext && sessionId && rpc.cpeRequest?.name !== "Inform") { } else {
await new Promise((resolve) => setTimeout(resolve, 100)); const cacheSnapshot = await localCache.getCurrentSnapshot();
const sessionContextString = await cache.pop(`session_${sessionId}`); const d = !!localCache.getConfig(
if (sessionContextString) { cacheSnapshot,
sessionContext = await session.deserialize(sessionContextString); "cwmp.debug",
sessionContext.httpRequest = httpRequest; {
sessionContext.httpResponse = httpResponse; remoteAddress: getRequestOrigin(httpRequest).remoteAddress,
httpRequest.socket.setTimeout(sessionContext.timeout * 1000); },
if (sessionContext.authState !== 1) sessionContext.authState = 0; Date.now(),
(e) => {
if (Array.isArray(e) && e[0] === "FUNC" && e[1] === "REMOTE_ADDRESS")
return getRequestOrigin(httpRequest).remoteAddress;
return e;
} }
);
if (d) {
debug.incomingHttpRequest(httpRequest, null, bodyStr);
debug.outgoingHttpResponse(httpResponse, null, error.message);
}
}
httpResponse.end(error.message);
return;
} }
if (sessionContext) if (sessionContext)
return processRequest(sessionContext, rpc, parseWarnings, bodyStr); return processRequest(sessionContext, rpc, parseWarnings, bodyStr);
if (rpc.cpeRequest?.name !== "Inform") { if (!(rpc.cpeRequest && rpc.cpeRequest.name === "Inform")) {
logger.accessError({ logger.accessError({
message: "Invalid session", message: "Invalid session",
sessionContext: { sessionContext: sessionContext || {
httpRequest: httpRequest, httpRequest: httpRequest,
httpResponse: httpResponse, httpResponse: httpResponse,
}, },
}); });
const _body = "Invalid session";
return clientError( httpResponse.setHeader("Content-Length", Buffer.byteLength(_body));
httpRequest, httpResponse.writeHead(400, { Connection: "close" });
httpResponse, const cacheSnapshot = await localCache.getCurrentSnapshot();
null, const d = !!localCache.getConfig(
bodyStr, cacheSnapshot,
"Invalid session" "cwmp.debug",
); {
remoteAddress: getRequestOrigin(httpRequest).remoteAddress,
},
Date.now(),
(e) => {
if (Array.isArray(e) && e[0] === "FUNC" && e[1] === "REMOTE_ADDRESS")
return getRequestOrigin(httpRequest).remoteAddress;
return e;
} }
);
if (stats.concurrentRequests > MAX_CONCURRENT_REQUESTS) { if (d) {
// Check again just in case device included old session ID debug.incomingHttpRequest(httpRequest, null, bodyStr);
// from the previous session debug.outgoingHttpResponse(httpResponse, null, _body);
httpResponse.writeHead(503, { "Retry-after": 60, Connection: "close" }); }
httpResponse.end("503 Service Unavailable"); httpResponse.end(_body);
stats.droppedRequests += 1;
return; return;
} }
+24 -19
View File
@@ -17,7 +17,7 @@
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>. * along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/ */
import { MongoClient, ObjectId, Collection, Db } from "mongodb"; import { MongoClient, ObjectID, Collection, Db } from "mongodb";
import { get } from "./config"; import { get } from "./config";
import { decodeTag, encodeTag, escapeRegExp } from "./common"; import { decodeTag, encodeTag, escapeRegExp } from "./common";
import { parse } from "./common/expression-parser"; import { parse } from "./common/expression-parser";
@@ -61,7 +61,10 @@ export function onConnect(callback: (db: Db) => Promise<void>): void {
} }
export async function connect(): Promise<void> { export async function connect(): Promise<void> {
clientPromise = MongoClient.connect("" + get("MONGODB_CONNECTION_URL")); clientPromise = MongoClient.connect("" + get("MONGODB_CONNECTION_URL"), {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const client = await clientPromise; const client = await clientPromise;
const db = client.db(); const db = client.db();
@@ -505,7 +508,7 @@ export async function saveDevice(
break; break;
case "object": case "object":
if (!diff[1]?.object || object2 !== object1) { if (!diff[1] || !diff[1].object || object2 !== object1) {
update["$set"][ update["$set"][
path.length ? path.toString() + "._object" : "_object" path.length ? path.toString() + "._object" : "_object"
] = !!object2; ] = !!object2;
@@ -513,7 +516,7 @@ export async function saveDevice(
break; break;
case "writable": case "writable":
if (!diff[1]?.writable || writable2 !== writable1) { if (!diff[1] || !diff[1].writable || writable2 !== writable1) {
update["$set"][ update["$set"][
path.length ? path.toString() + "._writable" : "_writable" path.length ? path.toString() + "._writable" : "_writable"
] = !!writable2; ] = !!writable2;
@@ -534,8 +537,9 @@ export async function saveDevice(
} }
if (attributesTimestamp2 !== attributesTimestamp1) { if (attributesTimestamp2 !== attributesTimestamp1) {
update["$set"][path.toString() + "._attributesTimestamp"] = update["$set"][
new Date(attributesTimestamp2); path.toString() + "._attributesTimestamp"
] = new Date(attributesTimestamp2);
} }
break; break;
@@ -553,8 +557,9 @@ export async function saveDevice(
} }
if (attributesTimestamp2 !== attributesTimestamp1) { if (attributesTimestamp2 !== attributesTimestamp1) {
update["$set"][path.toString() + "._attributesTimestamp"] = update["$set"][
new Date(attributesTimestamp2); path.toString() + "._attributesTimestamp"
] = new Date(attributesTimestamp2);
} }
} }
} }
@@ -564,7 +569,7 @@ export async function saveDevice(
for (const attrName of Object.keys(diff[1])) { for (const attrName of Object.keys(diff[1])) {
if ( if (
diff[1][attrName][1] != null && diff[1][attrName][1] != null &&
diff[2]?.[attrName]?.[1] == null (!diff[2] || !diff[2][attrName] || diff[2][attrName][1] == null)
) { ) {
const p = path.length ? path.toString() + "." : ""; const p = path.length ? path.toString() + "." : "";
update["$unset"][`${p}_${attrName}`] = 1; update["$unset"][`${p}_${attrName}`] = 1;
@@ -618,7 +623,7 @@ export async function saveDevice(
upsert: isNew, upsert: isNew,
}); });
if (!result.matchedCount && !result.upsertedCount) if (result.result.n !== 1)
throw new Error(`Device ${deviceId} not found in database`); throw new Error(`Device ${deviceId} not found in database`);
if (update2) { if (update2) {
@@ -642,7 +647,7 @@ export async function getFaults(
delete r.device; delete r.device;
r.timestamp = +r.timestamp; r.timestamp = +r.timestamp;
r.provisions = JSON.parse(r.provisions); r.provisions = JSON.parse(r.provisions);
faults[channel] = r as SessionFault; faults[channel] = r;
} }
return faults; return faults;
@@ -683,13 +688,13 @@ export async function getDueTasks(
if (task.timestamp >= timestamp) return [tasks, +task.timestamp]; if (task.timestamp >= timestamp) return [tasks, +task.timestamp];
task._id = String(task._id); task._id = String(task._id);
tasks.push(task as Task); tasks.push(task);
// For API compatibility // For API compatibility
if (task.name === "download" && task.file) { if (task.name === "download" && task.file) {
let q; let q;
if (ObjectId.isValid(task.file)) if (ObjectID.isValid(task.file))
q = { _id: { $in: [task.file, new ObjectId(task.file)] } }; q = { _id: { $in: [task.file, new ObjectID(task.file)] } };
else q = { _id: task.file }; else q = { _id: task.file };
const res = await filesCollection.find(q).toArray(); const res = await filesCollection.find(q).toArray();
@@ -710,7 +715,7 @@ export async function clearTasks(
taskIds: string[] taskIds: string[]
): Promise<void> { ): Promise<void> {
await tasksCollection.deleteMany({ await tasksCollection.deleteMany({
_id: { $in: taskIds.map((id) => new ObjectId(id)) }, _id: { $in: taskIds.map((id) => new ObjectID(id)) },
}); });
} }
@@ -727,14 +732,14 @@ export async function getOperations(
delete r._id; delete r._id;
// Workaround for a bug in v1.2.1 where operation object is saved without deserialization // Workaround for a bug in v1.2.1 where operation object is saved without deserialization
if (typeof r.provisions !== "string") { if (typeof r.provisions !== "string") {
operations[commandKey] = r as Operation; operations[commandKey] = r;
continue; continue;
} }
r.timestamp = +r.timestamp; r.timestamp = +r.timestamp;
if (r.args) r.args = JSON.parse(r.args); if (r.args) r.args = JSON.parse(r.args);
r.provisions = JSON.parse(r.provisions); r.provisions = JSON.parse(r.provisions);
r.retries = JSON.parse(r.retries); r.retries = JSON.parse(r.retries);
operations[commandKey] = r as Operation; operations[commandKey] = r;
} }
return operations; return operations;
} }
@@ -802,7 +807,7 @@ interface Permission {
} }
export async function getPermissions(): Promise<Permission[]> { export async function getPermissions(): Promise<Permission[]> {
return permissionsCollection.find().toArray() as unknown as Permission[]; return permissionsCollection.find().toArray();
} }
interface User { interface User {
@@ -813,5 +818,5 @@ interface User {
} }
export async function getUsers(): Promise<User[]> { export async function getUsers(): Promise<User[]> {
return usersCollection.find().toArray() as unknown as User[]; return usersCollection.find().toArray();
} }
+15 -12
View File
@@ -17,7 +17,12 @@
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>. * along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/ */
import { IncomingMessage, ServerResponse, ClientRequest } from "http"; import {
IncomingMessage,
ServerResponse,
ClientRequest,
RequestOptions,
} from "http";
import { Socket } from "net"; import { Socket } from "net";
import { appendFileSync } from "fs"; import { appendFileSync } from "fs";
import { stringify } from "./common/yaml"; import { stringify } from "./common/yaml";
@@ -94,8 +99,7 @@ export function outgoingHttpResponse(
export function outgoingHttpRequest( export function outgoingHttpRequest(
httpRequest: ClientRequest, httpRequest: ClientRequest,
deviceId: string, deviceId: string,
method: "GET" | "PUT" | "POST" | "DELETE", options: RequestOptions,
url: URL,
body: string body: string
): void { ): void {
if (!DEBUG_FILE) return; if (!DEBUG_FILE) return;
@@ -107,9 +111,9 @@ export function outgoingHttpRequest(
remoteAddress: con.remoteAddress, remoteAddress: con.remoteAddress,
deviceId: deviceId, deviceId: deviceId,
connection: getConnectionTimestamp(con), connection: getConnectionTimestamp(con),
remotePort: url.port, remotePort: options.port,
method: method, method: options.method || "GET",
url: url.pathname + url.search, url: options.path,
headers: httpRequest.getHeaders(), headers: httpRequest.getHeaders(),
body: body, body: body,
}; };
@@ -124,8 +128,7 @@ export function outgoingHttpRequest(
export function outgoingHttpRequestError( export function outgoingHttpRequestError(
httpRequest: ClientRequest, httpRequest: ClientRequest,
deviceId: string, deviceId: string,
method: "GET" | "PUT" | "POST" | "DELETE", options: RequestOptions,
url: URL,
err: Error err: Error
): void { ): void {
if (!DEBUG_FILE) return; if (!DEBUG_FILE) return;
@@ -133,12 +136,12 @@ export function outgoingHttpRequestError(
const msg = { const msg = {
event: "outgoing HTTP request", event: "outgoing HTTP request",
timestamp: now, timestamp: now,
remoteAddress: url.hostname, remoteAddress: options.hostname,
deviceId: deviceId, deviceId: deviceId,
connection: null, connection: null,
remotePort: url.port, remotePort: options.port,
method: method, method: options.method,
url: url.pathname + url.search, url: options.path,
headers: httpRequest.getHeaders(), headers: httpRequest.getHeaders(),
error: err.message, error: err.message,
}; };
+9 -2
View File
@@ -54,6 +54,13 @@ export function sanitizeParameterValue(
else parameterValue[0] = b; else parameterValue[0] = b;
} }
break; break;
case "xsd:string":
case "xsd:base64":
case "xsd:hexBinary":
if (typeof parameterValue[0] !== "string")
parameterValue[0] = "" + parameterValue[0];
break;
case "xsd:int": case "xsd:int":
case "xsd:unsignedInt": case "xsd:unsignedInt":
if (typeof parameterValue[0] !== "number") { if (typeof parameterValue[0] !== "number") {
@@ -76,7 +83,7 @@ export function sanitizeParameterValue(
} }
break; break;
default: default:
parameterValue[0] = "" + parameterValue[0]; parameterValue[0] = JSON.parse(JSON.stringify(parameterValue[0]));
break; break;
} }
} }
@@ -201,7 +208,7 @@ export function clear(
timestamp = timestamp || 0; timestamp = timestamp || 0;
let descendantsTimestamp = timestamp; let descendantsTimestamp = timestamp;
if (attributes?.object) { if (attributes && attributes.object) {
if (attributes.object > descendantsTimestamp) if (attributes.object > descendantsTimestamp)
descendantsTimestamp = attributes.object; descendantsTimestamp = attributes.object;
if (!(attributes.object <= attributes.value)) if (!(attributes.object <= attributes.value))
+5 -8
View File
@@ -18,6 +18,7 @@
*/ */
import * as url from "url"; import * as url from "url";
import * as querystring from "querystring";
import { IncomingMessage, ServerResponse } from "http"; import { IncomingMessage, ServerResponse } from "http";
import { Collection, GridFSBucket } from "mongodb"; import { Collection, GridFSBucket } from "mongodb";
import { onConnect } from "./db"; import { onConnect } from "./db";
@@ -34,7 +35,7 @@ onConnect(async (db) => {
}); });
const getFile = memoize( const getFile = memoize(
(uploadDate: number, size: number, filename: string): Promise<Buffer> => { (md5: string, size: number, filename: string): Promise<Buffer> => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const buffer = Buffer.allocUnsafe(size); const buffer = Buffer.allocUnsafe(size);
let i = 0; let i = 0;
@@ -63,7 +64,7 @@ export async function listener(
} }
const urlParts = url.parse(request.url, true); const urlParts = url.parse(request.url, true);
const filename = decodeURIComponent(urlParts.pathname.substring(1)); const filename = querystring.unescape(urlParts.pathname.substring(1));
const log = { const log = {
message: "Fetch file", message: "Fetch file",
@@ -81,14 +82,10 @@ export async function listener(
return; return;
} }
const buffer = await getFile( const buffer = await getFile(file.md5, file.length, filename);
file["uploadDate"].getTime(),
file.length,
filename
);
response.writeHead(200, { response.writeHead(200, {
"Content-Type": "application/octet-stream", "Content-Type": file.contentType || "application/octet-stream",
"Content-Length": file.length, "Content-Length": file.length,
}); });
+2 -2
View File
@@ -59,7 +59,7 @@ const INFORM_SCRIPT = `
// Device ID as user name // Device ID as user name
const username = declare("DeviceID.ID", {value: 1}).value[0] const username = declare("DeviceID.ID", {value: 1}).value[0]
// Password will be fixed for a given device because Math.random() is seeded with device ID by default. // Password will be fixed a given device because Math.random() is seeded with devcie ID by default.
const password = Math.trunc(Math.random() * Number.MAX_SAFE_INTEGER).toString(36); const password = Math.trunc(Math.random() * Number.MAX_SAFE_INTEGER).toString(36);
const informInterval = 300; const informInterval = 300;
@@ -257,7 +257,7 @@ export async function seed(options: Record<string, boolean>): Promise<void> {
"InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.KeyPassphrase", "InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.KeyPassphrase",
}, },
{ _id: "ui.device.3.type", value: "'parameter-table'" }, { _id: "ui.device.3.type", value: "'parameter-table'" },
{ _id: "ui.device.3.label", value: "'LAN hosts'" }, { _id: "ui.device.3.label", value: "'WLAN hosts'" },
{ {
_id: "ui.device.3.parameter", _id: "ui.device.3.parameter",
value: "InternetGatewayDevice.LANDevice.1.Hosts.Host", value: "InternetGatewayDevice.LANDevice.1.Hosts.Host",
+1 -1
View File
@@ -22,7 +22,7 @@ interface Instance {
} }
export default class InstanceSet { export default class InstanceSet {
private declare set: Set<Instance>; private set: Set<Instance>;
public constructor() { public constructor() {
this.set = new Set(); this.set = new Set();
+11 -5
View File
@@ -131,7 +131,7 @@ async function fetchPresets(): Promise<Preset[]> {
// If no keys are defined, consider all parameters as keys to keep the // If no keys are defined, consider all parameters as keys to keep the
// same behavior from v1.0 // same behavior from v1.0
if (!obj["_keys"]?.length) if (!obj["_keys"] || !obj["_keys"].length)
obj["_keys"] = Object.keys(obj).filter((k) => !k.startsWith("_")); obj["_keys"] = Object.keys(obj).filter((k) => !k.startsWith("_"));
return obj; return obj;
@@ -266,7 +266,9 @@ async function fetchProvisions(): Promise<Provisions> {
.createHash("md5") .createHash("md5")
.update(r["script"]) .update(r["script"])
.digest("hex"); .digest("hex");
provisions[r["_id"]].script = new vm.Script( provisions[
r["_id"]
].script = new vm.Script(
`"use strict";(function(){\n${r["script"]}\n})();`, `"use strict";(function(){\n${r["script"]}\n})();`,
{ filename: r["_id"], lineOffset: -1, timeout: 50 } { filename: r["_id"], lineOffset: -1, timeout: 50 }
); );
@@ -285,7 +287,9 @@ async function fetchVirtualParameters(): Promise<VirtualParameters> {
.createHash("md5") .createHash("md5")
.update(r["script"]) .update(r["script"])
.digest("hex"); .digest("hex");
virtualParameters[r["_id"]].script = new vm.Script( virtualParameters[
r["_id"]
].script = new vm.Script(
`"use strict";(function(){\n${r["script"]}\n})();`, `"use strict";(function(){\n${r["script"]}\n})();`,
{ filename: r["_id"], lineOffset: -1, timeout: 50 } { filename: r["_id"], lineOffset: -1, timeout: 50 }
); );
@@ -321,6 +325,8 @@ async function fetchFiles(): Promise<Files> {
const id = r["filename"] || r["_id"].toString(); const id = r["filename"] || r["_id"].toString();
files[id] = {}; files[id] = {};
files[id].length = r["length"]; files[id].length = r["length"];
files[id].md5 = r["md5"];
files[id].contentType = r["contentType"];
} }
return files; return files;
@@ -365,7 +371,7 @@ async function fetchConfig(): Promise<[Config, UiConfig]> {
let ref = ui; let ref = ui;
while (keys.length > 1) { while (keys.length > 1) {
const k = keys.shift(); const k = keys.shift();
if (ref[k] == null || typeof ref[k] !== "object") ref[k] = {}; if (typeof ref[k] !== "object") ref[k] = {};
ref = ref[k]; ref = ref[k];
} }
ref[keys[0]] = val; ref[keys[0]] = val;
@@ -495,7 +501,7 @@ export function getConfig(
if (!(key in snapshot.config)) { if (!(key in snapshot.config)) {
if (key in oldOpts) { if (key in oldOpts) {
let id; let id;
if (context?.["id"]) { if (context && context["id"]) {
id = context["id"]; id = context["id"];
} else if (cb) { } else if (cb) {
id = cb(["PARAM", "DeviceID.ID"]); id = cb(["PARAM", "DeviceID.ID"]);
+5 -3
View File
@@ -292,9 +292,11 @@ function formatSimple(
return `${severity}${remote}${details.message}${meta}${os.EOL}`; return `${severity}${remote}${details.message}${meta}${os.EOL}`;
} }
return `${details.timestamp} [${( return `${
details.severity as string details.timestamp
).toUpperCase()}] ${remote}${details.message}${meta}${os.EOL}`; } [${(details.severity as string).toUpperCase()}] ${remote}${
details.message
}${meta}${os.EOL}`;
} }
function log(details: Record<string, unknown>): void { function log(details: Record<string, unknown>): void {
+2 -2
View File
@@ -17,7 +17,7 @@
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>. * along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/ */
import { ObjectId } from "mongodb"; import { ObjectID } from "mongodb";
import { map, parse, stringify, parseList } from "./common/expression-parser"; import { map, parse, stringify, parseList } from "./common/expression-parser";
import { likePatternToRegExp } from "./common/expression"; import { likePatternToRegExp } from "./common/expression";
import { Expression, Fault, Task } from "./types"; import { Expression, Fault, Task } from "./types";
@@ -90,7 +90,7 @@ export function processTasksFilter(filter: Expression): Expression {
if (!isArray(exp)) return exp; if (!isArray(exp)) return exp;
if (["=", "<>", ">", ">=", "<", "<="].includes(exp[0])) { if (["=", "<>", ">", ">=", "<", "<="].includes(exp[0])) {
const e = exp.slice(); const e = exp.slice();
if (e[1][0] === "PARAM" && e[1][1] === "_id") e[2] = new ObjectId(e[2]); if (e[1][0] === "PARAM" && e[1][1] === "_id") e[2] = new ObjectID(e[2]);
else if (e[1][0] === "PARAM" && e[1][1] === "timestamp") else if (e[1][0] === "PARAM" && e[1][1] === "timestamp")
e[2] = new Date(e[2]); e[2] = new Date(e[2]);
else if (e[1][0] === "PARAM" && e[1][1] === "expiry") else if (e[1][0] === "PARAM" && e[1][1] === "expiry")
+40 -95
View File
@@ -19,6 +19,7 @@
import * as url from "url"; import * as url from "url";
import { Collection, GridFSBucket, ObjectId } from "mongodb"; import { Collection, GridFSBucket, ObjectId } from "mongodb";
import * as querystring from "querystring";
import * as vm from "vm"; import * as vm from "vm";
import * as config from "./config"; import * as config from "./config";
import { onConnect } from "./db"; import { onConnect } from "./db";
@@ -29,12 +30,10 @@ import * as cache from "./cache";
import { version as VERSION } from "../package.json"; import { version as VERSION } from "../package.json";
import { ping } from "./ping"; import { ping } from "./ping";
import * as logger from "./logger"; import * as logger from "./logger";
import { flattenDevice } from "./mongodb-functions";
const DEVICE_TASKS_REGEX = /^\/devices\/([a-zA-Z0-9\-_%]+)\/tasks\/?$/; const DEVICE_TASKS_REGEX = /^\/devices\/([a-zA-Z0-9\-_%]+)\/tasks\/?$/;
const TASKS_REGEX = /^\/tasks\/([a-zA-Z0-9\-_%]+)(\/[a-zA-Z_]*)?$/; const TASKS_REGEX = /^\/tasks\/([a-zA-Z0-9\-_%]+)(\/[a-zA-Z_]*)?$/;
const TAGS_REGEX = const TAGS_REGEX = /^\/devices\/([a-zA-Z0-9\-_%]+)\/tags\/([a-zA-Z0-9\-_%]+)\/?$/;
/^\/devices\/([a-zA-Z0-9\-_%]+)\/tags\/([a-zA-Z0-9\-_%]+)\/?$/;
const PRESETS_REGEX = /^\/presets\/([a-zA-Z0-9\-_%]+)\/?$/; const PRESETS_REGEX = /^\/presets\/([a-zA-Z0-9\-_%]+)\/?$/;
const OBJECTS_REGEX = /^\/objects\/([a-zA-Z0-9\-_%]+)\/?$/; const OBJECTS_REGEX = /^\/objects\/([a-zA-Z0-9\-_%]+)\/?$/;
const FILES_REGEX = /^\/files\/([a-zA-Z0-9%!*'();:@&=+$,?#[\]\-_.~]+)\/?$/; const FILES_REGEX = /^\/files\/([a-zA-Z0-9%!*'();:@&=+$,?#[\]\-_.~]+)\/?$/;
@@ -42,11 +41,10 @@ const PING_REGEX = /^\/ping\/([a-zA-Z0-9\-_.:]+)\/?$/;
const QUERY_REGEX = /^\/([a-zA-Z0-9_]+)\/?$/; const QUERY_REGEX = /^\/([a-zA-Z0-9_]+)\/?$/;
const DELETE_DEVICE_REGEX = /^\/devices\/([a-zA-Z0-9\-_%]+)\/?$/; const DELETE_DEVICE_REGEX = /^\/devices\/([a-zA-Z0-9\-_%]+)\/?$/;
const PROVISIONS_REGEX = /^\/provisions\/([a-zA-Z0-9\-_%]+)\/?$/; const PROVISIONS_REGEX = /^\/provisions\/([a-zA-Z0-9\-_%]+)\/?$/;
const VIRTUAL_PARAMETERS_REGEX = const VIRTUAL_PARAMETERS_REGEX = /^\/virtual_parameters\/([a-zA-Z0-9\-_%]+)\/?$/;
/^\/virtual_parameters\/([a-zA-Z0-9\-_%]+)\/?$/;
const FAULTS_REGEX = /^\/faults\/([a-zA-Z0-9\-_%:]+)\/?$/; const FAULTS_REGEX = /^\/faults\/([a-zA-Z0-9\-_%:]+)\/?$/;
const collections: Record<string, Collection> = { const collections = {
tasks: null as Collection, tasks: null as Collection,
devices: null as Collection, devices: null as Collection,
presets: null as Collection, presets: null as Collection,
@@ -104,24 +102,17 @@ export function listener(
logger.accessInfo( logger.accessInfo(
Object.assign({}, urlParts.query, { Object.assign({}, urlParts.query, {
remoteAddress: request.socket.remoteAddress, remoteAddress: request.connection.remoteAddress,
message: `${request.method} ${urlParts.pathname}`, message: `${request.method} ${urlParts.pathname}`,
}) })
); );
if (PRESETS_REGEX.test(urlParts.pathname)) { if (PRESETS_REGEX.test(urlParts.pathname)) {
const presetName = decodeURIComponent( const presetName = querystring.unescape(
PRESETS_REGEX.exec(urlParts.pathname)[1] PRESETS_REGEX.exec(urlParts.pathname)[1]
); );
if (request.method === "PUT") { if (request.method === "PUT") {
let preset; const preset = JSON.parse(body.toString());
try {
preset = JSON.parse(body.toString());
} catch (err) {
response.writeHead(400);
response.end(`${err.name}: ${err.message}`);
return;
}
preset._id = presetName; preset._id = presetName;
collections.presets.replaceOne( collections.presets.replaceOne(
{ _id: presetName }, { _id: presetName },
@@ -164,18 +155,11 @@ export function listener(
response.end("405 Method Not Allowed"); response.end("405 Method Not Allowed");
} }
} else if (OBJECTS_REGEX.test(urlParts.pathname)) { } else if (OBJECTS_REGEX.test(urlParts.pathname)) {
const objectName = decodeURIComponent( const objectName = querystring.unescape(
OBJECTS_REGEX.exec(urlParts.pathname)[1] OBJECTS_REGEX.exec(urlParts.pathname)[1]
); );
if (request.method === "PUT") { if (request.method === "PUT") {
let object; const object = JSON.parse(body.toString());
try {
object = JSON.parse(body.toString());
} catch (err) {
response.writeHead(400);
response.end(`${err.name}: ${err.message}`);
return;
}
object._id = objectName; object._id = objectName;
collections.objects.replaceOne( collections.objects.replaceOne(
{ _id: objectName }, { _id: objectName },
@@ -218,7 +202,7 @@ export function listener(
response.end("405 Method Not Allowed"); response.end("405 Method Not Allowed");
} }
} else if (PROVISIONS_REGEX.test(urlParts.pathname)) { } else if (PROVISIONS_REGEX.test(urlParts.pathname)) {
const provisionName = decodeURIComponent( const provisionName = querystring.unescape(
PROVISIONS_REGEX.exec(urlParts.pathname)[1] PROVISIONS_REGEX.exec(urlParts.pathname)[1]
); );
if (request.method === "PUT") { if (request.method === "PUT") {
@@ -276,7 +260,7 @@ export function listener(
response.end("405 Method Not Allowed"); response.end("405 Method Not Allowed");
} }
} else if (VIRTUAL_PARAMETERS_REGEX.test(urlParts.pathname)) { } else if (VIRTUAL_PARAMETERS_REGEX.test(urlParts.pathname)) {
const virtualParameterName = decodeURIComponent( const virtualParameterName = querystring.unescape(
VIRTUAL_PARAMETERS_REGEX.exec(urlParts.pathname)[1] VIRTUAL_PARAMETERS_REGEX.exec(urlParts.pathname)[1]
); );
if (request.method === "PUT") { if (request.method === "PUT") {
@@ -338,8 +322,8 @@ export function listener(
} }
} else if (TAGS_REGEX.test(urlParts.pathname)) { } else if (TAGS_REGEX.test(urlParts.pathname)) {
const r = TAGS_REGEX.exec(urlParts.pathname); const r = TAGS_REGEX.exec(urlParts.pathname);
const deviceId = decodeURIComponent(r[1]); const deviceId = querystring.unescape(r[1]);
const tag = decodeURIComponent(r[2]); const tag = querystring.unescape(r[2]);
if (request.method === "POST") { if (request.method === "POST") {
collections.devices.updateOne( collections.devices.updateOne(
{ _id: deviceId }, { _id: deviceId },
@@ -367,7 +351,7 @@ export function listener(
} }
} else if (FAULTS_REGEX.test(urlParts.pathname)) { } else if (FAULTS_REGEX.test(urlParts.pathname)) {
if (request.method === "DELETE") { if (request.method === "DELETE") {
const faultId = decodeURIComponent( const faultId = querystring.unescape(
FAULTS_REGEX.exec(urlParts.pathname)[1] FAULTS_REGEX.exec(urlParts.pathname)[1]
); );
const deviceId = faultId.split(":", 1)[0]; const deviceId = faultId.split(":", 1)[0];
@@ -412,31 +396,15 @@ export function listener(
} }
} else if (DEVICE_TASKS_REGEX.test(urlParts.pathname)) { } else if (DEVICE_TASKS_REGEX.test(urlParts.pathname)) {
if (request.method === "POST") { if (request.method === "POST") {
const deviceId = decodeURIComponent( const deviceId = querystring.unescape(
DEVICE_TASKS_REGEX.exec(urlParts.pathname)[1] DEVICE_TASKS_REGEX.exec(urlParts.pathname)[1]
); );
if (body.length) { if (body.length) {
let task; const task = JSON.parse(body.toString());
try {
task = JSON.parse(body.toString());
} catch (err) {
response.writeHead(400);
response.end(`${err.name}: ${err.message}`);
return;
}
task.device = deviceId; task.device = deviceId;
collections.devices apiFunctions
.findOne({ _id: deviceId }) .insertTasks(task)
.then(async (dev) => { .then(async () => {
if (!dev) {
response.writeHead(404);
response.end("No such device");
return;
}
const device = flattenDevice(dev);
await apiFunctions.insertTasks(task);
const lastInform = Date.now(); const lastInform = Date.now();
const socketTimeout: number = request.socket["timeout"]; const socketTimeout: number = request.socket["timeout"];
@@ -467,10 +435,7 @@ export function listener(
return; return;
} }
const status = await apiFunctions.connectionRequest( const status = await apiFunctions.connectionRequest(deviceId);
deviceId,
device
);
if (status) { if (status) {
if (socketTimeout) request.socket.setTimeout(socketTimeout); if (socketTimeout) request.socket.setTimeout(socketTimeout);
@@ -553,28 +518,15 @@ export function listener(
}); });
} else if (urlParts.query.connection_request != null) { } else if (urlParts.query.connection_request != null) {
// No task, send connection request only // No task, send connection request only
collections.devices apiFunctions
.findOne({ _id: deviceId }) .connectionRequest(deviceId)
.then(async (dev) => { .then(() => {
if (!dev) {
response.writeHead(404);
response.end("No such device");
return;
}
return apiFunctions.connectionRequest(deviceId).then((status) => {
if (status) {
response.writeHead(504, status);
response.end(status);
return;
}
response.writeHead(200); response.writeHead(200);
response.end(); response.end();
});
}) })
.catch((err) => { .catch((err) => {
setTimeout(() => { response.writeHead(504);
throwError(err, response); response.end(`${err.name}: ${err.message}`);
});
}); });
} else { } else {
response.writeHead(400); response.writeHead(400);
@@ -586,7 +538,7 @@ export function listener(
} }
} else if (TASKS_REGEX.test(urlParts.pathname)) { } else if (TASKS_REGEX.test(urlParts.pathname)) {
const r = TASKS_REGEX.exec(urlParts.pathname); const r = TASKS_REGEX.exec(urlParts.pathname);
const taskId = decodeURIComponent(r[1]); const taskId = querystring.unescape(r[1]);
const action = r[2]; const action = r[2];
if (!action || action === "/") { if (!action || action === "/") {
if (request.method === "DELETE") { if (request.method === "DELETE") {
@@ -672,7 +624,7 @@ export function listener(
response.end(); response.end();
} }
} else if (FILES_REGEX.test(urlParts.pathname)) { } else if (FILES_REGEX.test(urlParts.pathname)) {
const filename = decodeURIComponent( const filename = querystring.unescape(
FILES_REGEX.exec(urlParts.pathname)[1] FILES_REGEX.exec(urlParts.pathname)[1]
); );
if (request.method === "PUT") { if (request.method === "PUT") {
@@ -682,9 +634,9 @@ export function listener(
productClass: request.headers.productclass, productClass: request.headers.productclass,
version: request.headers.version, version: request.headers.version,
}; };
filesBucket.delete(filename as unknown as ObjectId, () => { filesBucket.delete((filename as unknown) as ObjectId, () => {
const uploadStream = filesBucket.openUploadStreamWithId( const uploadStream = filesBucket.openUploadStreamWithId(
filename as unknown as ObjectId, filename,
filename, filename,
{ {
metadata: metadata, metadata: metadata,
@@ -701,7 +653,7 @@ export function listener(
}); });
}); });
} else if (request.method === "DELETE") { } else if (request.method === "DELETE") {
filesBucket.delete(filename as unknown as ObjectId, (err) => { filesBucket.delete((filename as unknown) as ObjectId, (err) => {
if (err) { if (err) {
if (err.message.startsWith("FileNotFound")) { if (err.message.startsWith("FileNotFound")) {
response.writeHead(404); response.writeHead(404);
@@ -719,7 +671,7 @@ export function listener(
response.end("405 Method Not Allowed"); response.end("405 Method Not Allowed");
} }
} else if (PING_REGEX.test(urlParts.pathname)) { } else if (PING_REGEX.test(urlParts.pathname)) {
const host = decodeURIComponent(PING_REGEX.exec(urlParts.pathname)[1]); const host = querystring.unescape(PING_REGEX.exec(urlParts.pathname)[1]);
ping(host, (err, res, stdout) => { ping(host, (err, res, stdout) => {
if (err) { if (err) {
if (!res) { if (!res) {
@@ -745,7 +697,7 @@ export function listener(
return; return;
} }
const deviceId = decodeURIComponent( const deviceId = querystring.unescape(
DELETE_DEVICE_REGEX.exec(urlParts.pathname)[1] DELETE_DEVICE_REGEX.exec(urlParts.pathname)[1]
); );
apiFunctions apiFunctions
@@ -824,14 +776,7 @@ export function listener(
const cur = collection.find(q, { projection: projection }); const cur = collection.find(q, { projection: projection });
if (urlParts.query.sort) { if (urlParts.query.sort) {
let s; const s = JSON.parse(urlParts.query.sort as string);
try {
s = JSON.parse(urlParts.query.sort as string);
} catch (err) {
response.writeHead(400);
response.end(`${err.name}: ${err.message}`);
return;
}
const sort = {}; const sort = {};
for (const [k, v] of Object.entries(s)) { for (const [k, v] of Object.entries(s)) {
if (k[k.lastIndexOf(".") + 1] !== "_" && collectionName === "devices") if (k[k.lastIndexOf(".") + 1] !== "_" && collectionName === "devices")
@@ -842,7 +787,13 @@ export function listener(
cur.sort(sort); cur.sort(sort);
} }
cur.count((err, total) => { if (urlParts.query.skip)
cur.skip(parseInt(urlParts.query.skip as string));
if (urlParts.query.limit)
cur.limit(parseInt(urlParts.query.limit as string));
cur.count(false, (err, total) => {
if (err) return void throwError(err); if (err) return void throwError(err);
response.writeHead(200, { response.writeHead(200, {
@@ -855,12 +806,6 @@ export function listener(
return; return;
} }
if (urlParts.query.skip)
cur.skip(parseInt(urlParts.query.skip as string));
if (urlParts.query.limit)
cur.limit(parseInt(urlParts.query.limit as string));
response.write("[\n"); response.write("[\n");
i = 0; i = 0;
cur.forEach( cur.forEach(
+4 -8
View File
@@ -39,20 +39,16 @@ export function ping(
switch (platform()) { switch (platform()) {
case "linux": case "linux":
cmd = `ping -w 1 -i 0.2 -c 3 ${host}`; cmd = `ping -w 1 -i 0.2 -c 3 ${host}`;
parseRegExp1 = parseRegExp1 = /(\d+) packets transmitted, (\d+) received, ([\d.]+)% packet loss[^]*([\d.]+)\/([\d.]+)\/([\d.]+)\/([\d.]+)/;
/(\d+) packets transmitted, (\d+) received, ([\d.]+)% packet loss[^]*([\d.]+)\/([\d.]+)\/([\d.]+)\/([\d.]+)/; parseRegExp2 = /(\d+) packets transmitted, (\d+) received, ([\d.]+)% packet loss/;
parseRegExp2 =
/(\d+) packets transmitted, (\d+) received, ([\d.]+)% packet loss/;
break; break;
case "freebsd": case "freebsd":
// Send a single packet because on FreeBSD only superuser can send // Send a single packet because on FreeBSD only superuser can send
// packets that are only 200 ms apart. // packets that are only 200 ms apart.
cmd = `ping -t 1 -c 3 ${host}`; cmd = `ping -t 1 -c 3 ${host}`;
parseRegExp1 = parseRegExp1 = /(\d+) packets transmitted, (\d+) packets received, ([\d.]+)% packet loss\nround-trip min\/avg\/max\/stddev = ([\d.]+)\/([\d.]+)\/([\d.]+)\/([\d.]+) ms/;
/(\d+) packets transmitted, (\d+) packets received, ([\d.]+)% packet loss\nround-trip min\/avg\/max\/stddev = ([\d.]+)\/([\d.]+)\/([\d.]+)\/([\d.]+) ms/; parseRegExp2 = /(\d+) packets transmitted, (\d+) packets received, ([\d.]+)% packet loss/;
parseRegExp2 =
/(\d+) packets transmitted, (\d+) packets received, ([\d.]+)% packet loss/;
break; break;
default: default:
+474 -62
View File
@@ -57,6 +57,10 @@ import {
FactoryReset, FactoryReset,
AddObjectResponse, AddObjectResponse,
GetParameterValuesResponse, GetParameterValuesResponse,
ChangeDUStateOperation,
DownloadOperation,
DUStateChangeCompleteRequest,
ChangeDUState,
} from "./types"; } from "./types";
import { getRequestOrigin } from "./forwarded"; import { getRequestOrigin } from "./forwarded";
import * as logger from "./logger"; import * as logger from "./logger";
@@ -136,7 +140,7 @@ export function configContextCallback(
const path = paths.get(Path.parse(name)); const path = paths.get(Path.parse(name));
if (path) { if (path) {
const attrs = deviceData.attributes.get(path, 1); const attrs = deviceData.attributes.get(path, 1);
if (attrs?.value?.[1]) return attrs.value[1][0]; if (attrs && attrs.value && attrs.value[1]) return attrs.value[1][0];
} }
} else if (exp[0] === "FUNC") { } else if (exp[0] === "FUNC") {
if (exp[1] === "REMOTE_ADDRESS") if (exp[1] === "REMOTE_ADDRESS")
@@ -284,6 +288,8 @@ export async function transferComplete(
sessionContext.deviceData.attributes.revision = revision; sessionContext.deviceData.attributes.revision = revision;
const commandKey = rpcReq.commandKey; const commandKey = rpcReq.commandKey;
const operation = sessionContext.operations[commandKey]; const operation = sessionContext.operations[commandKey];
if (operation.name !== "Download")
throw new Error("Unexpected operation name");
if (!operation) { if (!operation) {
return { return {
@@ -299,7 +305,7 @@ export async function transferComplete(
if (!sessionContext.operationsTouched) sessionContext.operationsTouched = {}; if (!sessionContext.operationsTouched) sessionContext.operationsTouched = {};
sessionContext.operationsTouched[commandKey] = 1; sessionContext.operationsTouched[commandKey] = 1;
if (rpcReq.faultStruct?.faultCode !== "0") { if (rpcReq.faultStruct && rpcReq.faultStruct.faultCode !== "0") {
revertDownloadParameters(sessionContext, operation.args.instance); revertDownloadParameters(sessionContext, operation.args.instance);
const fault: Fault = { const fault: Fault = {
@@ -418,7 +424,149 @@ function revertDownloadParameters(
); );
const toClear = device.set(sessionContext.deviceData, p, timestamp, { const toClear = device.set(sessionContext.deviceData, p, timestamp, {
value: [timestamp, [lastDownload?.value[1]?.[0] || 0, "xsd:dateTime"]], value: [
timestamp,
[
lastDownload && lastDownload.value[1] ? lastDownload.value[1][0] : 0,
"xsd:dateTime",
],
],
});
if (toClear) {
for (const c of toClear)
device.clear(sessionContext.deviceData, c[0], c[1], c[2], c[3]);
}
}
export async function duStateChangeComplete(
sessionContext: SessionContext,
rpcReq: DUStateChangeCompleteRequest
): Promise<{
acsResponse: AcsResponse;
operation: ChangeDUStateOperation;
fault: Fault;
}> {
const revision =
(sessionContext.revisions[sessionContext.revisions.length - 1] || 0) + 1;
sessionContext.deviceData.timestamps.revision = revision;
sessionContext.deviceData.attributes.revision = revision;
const commandKey = rpcReq.commandKey;
const operation = sessionContext.operations[commandKey];
if (!operation) {
return {
acsResponse: { name: "DUStateChangeCompleteResponse" },
operation: null,
fault: null,
};
}
if (operation.name !== "ChangeDUState")
throw new Error("Unexpected operation name");
if (operation.args.operations.length !== rpcReq.results.length) {
return {
acsResponse: { name: "DUStateChangeCompleteResponse" },
operation: operation,
fault: {
code: "invalid_response",
message: "Unexpected number of results in DUStateChangeComplete",
timestamp: operation.timestamp,
},
};
}
delete sessionContext.operations[commandKey];
if (!sessionContext.operationsTouched) sessionContext.operationsTouched = {};
sessionContext.operationsTouched[commandKey] = 1;
let toClear = null;
const timestamp = sessionContext.timestamp + sessionContext.iteration + 1;
let fault: Fault;
for (const [i, res] of rpcReq.results.entries()) {
const op = operation.args.operations[i];
if (res.fault && res.fault.faultCode !== "0") {
revertChangeDuStateParameters(sessionContext, op.instance);
fault = {
code: `cwmp.${res.fault.faultCode}`,
message: res.fault.faultString,
timestamp: operation.timestamp,
detail: res.fault,
};
continue;
}
const toSet: Record<string, [string | number | boolean, string]> = {
LastChangeDUState: [operation.timestamp, "xsd:dateTime"],
LastOperationType: [op.operationType, "xsd:string"],
LastURL: [op.url, "xsd:string"],
LastUUID: [op.uuid, "xsd:string"],
LastExecutionEnvRef: [op.executionEnvRef, "xsd:string"],
StartTime: [+res.startTime, "xsd:dateTime"],
CompleteTime: [+res.completeTime, "xsd:dateTime"],
CurrentState: [res.currentState, "xsd:string"],
DeploymentUnitRef: [res.deploymentUnitRef, "xsd:string"],
Version: [res.version, "xsd:string"],
Resolved: [res.resolved, "xsd:string"],
ExecutionUnitRefList: [res.executionUnitRefList, "xsd:string"],
};
for (const [k, v] of Object.entries(toSet)) {
const p = sessionContext.deviceData.paths.add(
Path.parse(`DUStates.${op.instance}.${k}`)
);
toClear = device.set(
sessionContext.deviceData,
p,
timestamp,
{ value: [timestamp, v] },
toClear
);
}
}
if (toClear) {
for (const c of toClear)
device.clear(sessionContext.deviceData, c[0], c[1], c[2], c[3]);
}
return {
acsResponse: { name: "DUStateChangeCompleteResponse" },
operation: operation,
fault: fault,
};
}
function revertChangeDuStateParameters(
sessionContext: SessionContext,
instance
): void {
const timestamp = sessionContext.timestamp + sessionContext.iteration + 1;
let p;
p = sessionContext.deviceData.paths.add(
Path.parse(`DUStates.${instance}.LastChangeDUState`)
);
const lastChangeDuState = sessionContext.deviceData.attributes.get(p);
p = sessionContext.deviceData.paths.add(
Path.parse(`DUStates.${instance}.ChangeDUState`)
);
const toClear = device.set(sessionContext.deviceData, p, timestamp, {
value: [
timestamp,
[
lastChangeDuState && lastChangeDuState.value[1]
? lastChangeDuState.value[1][0]
: 0,
"xsd:dateTime",
],
],
}); });
if (toClear) { if (toClear) {
@@ -452,36 +600,7 @@ export async function timeoutOperations(
(e) => configContextCallback(sessionContext, e) (e) => configContextCallback(sessionContext, e)
) * 1000; ) * 1000;
if (sessionContext.timestamp < operation.timestamp + DOWNLOAD_TIMEOUT) if (sessionContext.timestamp > operation.timestamp + DOWNLOAD_TIMEOUT) {
continue;
logger.accessWarn({
sessionContext: sessionContext,
message: "Download operation timed out",
commandKey: commandKey,
});
const SUCCESS_ON_TIMEOUT = +localCache.getConfig(
sessionContext.cacheSnapshot,
"cwmp.downloadSuccessOnTimeout",
{},
sessionContext.timestamp,
(e) => configContextCallback(sessionContext, e)
);
if (SUCCESS_ON_TIMEOUT) {
const r = {
name: "TransferComplete" as const,
commandKey: commandKey,
startTime: 0,
completeTime: 0,
};
// Call transferComplete code and ignore the response
await transferComplete(sessionContext, r);
continue;
}
delete sessionContext.operations[commandKey]; delete sessionContext.operations[commandKey];
if (!sessionContext.operationsTouched) if (!sessionContext.operationsTouched)
sessionContext.operationsTouched = {}; sessionContext.operationsTouched = {};
@@ -497,6 +616,7 @@ export async function timeoutOperations(
revertDownloadParameters(sessionContext, operation.args.instance); revertDownloadParameters(sessionContext, operation.args.instance);
} }
}
return { faults, operations }; return { faults, operations };
} }
@@ -816,6 +936,10 @@ function runDeclarations(
downloadsToCreate: new InstanceSet(), downloadsToCreate: new InstanceSet(),
downloadsValues: new Map(), downloadsValues: new Map(),
downloadsDownload: new Map(), downloadsDownload: new Map(),
duStatesToDelete: new Set(),
duStatesToCreate: new InstanceSet(),
duStatesValues: new Map(),
duStatesChangeDUState: new Map(),
reboot: 0, reboot: 0,
factoryReset: 0, factoryReset: 0,
}; };
@@ -1035,7 +1159,7 @@ export async function rpcRequest(
if ( if (
!sessionContext.virtualParameters.length && !sessionContext.virtualParameters.length &&
!sessionContext.declarations.length && !sessionContext.declarations.length &&
!_declarations?.length && !(_declarations && _declarations.length) &&
!sessionContext.provisions.length !sessionContext.provisions.length
) )
return { fault: null, rpcId: null, rpc: null }; return { fault: null, rpcId: null, rpc: null };
@@ -1110,11 +1234,12 @@ export async function rpcRequest(
return rpcRequest(sessionContext, _declarations); return rpcRequest(sessionContext, _declarations);
} }
if (_declarations?.length) { if (_declarations && _declarations.length) {
delete sessionContext.syncState; delete sessionContext.syncState;
if (!sessionContext.declarations[0]) sessionContext.declarations[0] = []; if (!sessionContext.declarations[0]) sessionContext.declarations[0] = [];
sessionContext.declarations[0] = sessionContext.declarations[0] = sessionContext.declarations[0].concat(
sessionContext.declarations[0].concat(_declarations); _declarations
);
return rpcRequest(sessionContext, null); return rpcRequest(sessionContext, null);
} }
@@ -1445,7 +1570,148 @@ export async function rpcRequest(
for (const [p, v] of sessionContext.syncState.downloadsValues) { for (const [p, v] of sessionContext.syncState.downloadsValues) {
const attrs = sessionContext.deviceData.attributes.get(p); const attrs = sessionContext.deviceData.attributes.get(p);
if (attrs) { if (attrs) {
if (attrs.writable?.[1] && attrs.value) { if (attrs.writable && attrs.writable[1] && attrs.value) {
const val = device.sanitizeParameterValue([v, attrs.value[1][1]]);
if (val[0] !== attrs.value[1][0]) {
toClear = device.set(
sessionContext.deviceData,
p,
timestamp,
{ value: [timestamp, val] },
toClear
);
}
}
}
}
// DUState
index = null;
for (const instance of sessionContext.syncState.duStatesToCreate) {
if (index == null) {
index = 0;
for (const p of sessionContext.deviceData.paths.find(
Path.parse("DUStates.*"),
false,
true
)) {
if (
+p.segments[1] > index &&
sessionContext.deviceData.attributes.has(p)
)
index = +p.segments[1];
}
}
++index;
toClear = device.set(
sessionContext.deviceData,
Path.parse("DUStates"),
timestamp,
{ object: [timestamp, 1], writable: [timestamp, 1] },
toClear
);
toClear = device.set(
sessionContext.deviceData,
Path.parse(`DUStates.${index}`),
timestamp,
{ object: [timestamp, 1], writable: [timestamp, 1] },
toClear
);
const params = {
URL: {
writable: 1,
value: [instance.URL || "", "xsd:string"],
},
UUID: {
writable: 1,
value: [instance.UUID || "", "xsd:string"],
},
ExecutionEnvRef: {
writable: 1,
value: [instance.ExecutionEnvRef || "", "xsd:string"],
},
Username: {
writable: 1,
value: [instance.Username || "", "xsd:string"],
},
Password: {
writable: 1,
value: [instance.Password || "", "xsd:string"],
},
ChangeDUState: {
writable: 1,
value: [instance.ChangeDUState || "", "xsd:dateTime"],
},
OperationType: {
writable: 1,
value: [instance.OperationType || "", "xsd:string"],
},
LastURL: { writable: 0, value: ["", "xsd:string"] },
LastUUID: { writable: 0, value: ["", "xsd:string"] },
LastExecutionEnvRef: { writable: 0, value: ["", "xsd:string"] },
LastChangeDUState: { writable: 0, value: [0, "xsd:dateTime"] },
LastOperationType: { writable: 0, value: [0, "xsd:string"] },
Version: { writable: 0, value: [0, "xsd:string"] },
CurrentState: { writable: 0, value: [0, "xsd:string"] },
ExecutionUnitRefList: { writable: 0, value: [0, "xsd:string"] },
DeploymentUnitRef: { writable: 0, value: [0, "xsd:string"] },
Resolved: { writable: 0, value: [0, "xsd:boolean"] },
StartTime: { writable: 0, value: [0, "xsd:dateTime"] },
CompleteTime: { writable: 0, value: [0, "xsd:dateTime"] },
};
for (const [k, v] of Object.entries(params)) {
toClear = device.set(
sessionContext.deviceData,
Path.parse(`DUStates.${index}.${k}`),
timestamp,
{
object: [timestamp, 0],
writable: [timestamp, v.writable as 0 | 1],
value: [
timestamp,
v.value as [string | number | boolean, string],
],
},
toClear
);
}
toClear = device.set(
sessionContext.deviceData,
Path.parse(`DUStates.${index}.*`),
timestamp,
null,
toClear
);
}
sessionContext.syncState.duStatesToCreate.clear();
for (const instance of sessionContext.syncState.duStatesToDelete) {
toClear = device.set(
sessionContext.deviceData,
instance,
timestamp,
null,
toClear
);
for (const p of sessionContext.syncState.duStatesValues.keys()) {
if (p.segments[1] === instance.segments[1])
sessionContext.syncState.duStatesValues.delete(p);
}
}
sessionContext.syncState.duStatesToDelete.clear();
for (const [p, v] of sessionContext.syncState.duStatesValues) {
const attrs = sessionContext.deviceData.attributes.get(p);
if (attrs) {
if (attrs.writable && attrs.writable[1] && attrs.value) {
const val = device.sanitizeParameterValue([v, attrs.value[1][1]]); const val = device.sanitizeParameterValue([v, attrs.value[1][1]]);
if (val[0] !== attrs.value[1][0]) { if (val[0] !== attrs.value[1][0]) {
toClear = device.set( toClear = device.set(
@@ -1686,7 +1952,7 @@ function generateGetRpcRequest(
syncState.refreshAttributes.value.delete(path); syncState.refreshAttributes.value.delete(path);
// Need to check in case param is deleted or changed to object // Need to check in case param is deleted or changed to object
const attrs = sessionContext.deviceData.attributes.get(path); const attrs = sessionContext.deviceData.attributes.get(path);
if (attrs?.object?.[1] === 0) { if (attrs && attrs.object && attrs.object[1] === 0) {
parameterNames.push(path.toString()); parameterNames.push(path.toString());
if (parameterNames.length >= GPV_BATCH_SIZE) break; if (parameterNames.length >= GPV_BATCH_SIZE) break;
} }
@@ -1762,6 +2028,7 @@ function generateSetRpcRequest(
| FactoryReset | FactoryReset
| Reboot | Reboot
| Download | Download
| ChangeDUState
) & { next?: string } { ) & { next?: string } {
const syncState = sessionContext.syncState; const syncState = sessionContext.syncState;
if (!syncState) return null; if (!syncState) return null;
@@ -1839,7 +2106,7 @@ function generateSetRpcRequest(
for (const [k, v] of syncState.spv) { for (const [k, v] of syncState.spv) {
syncState.spv.delete(k); syncState.spv.delete(k);
const attrs = sessionContext.deviceData.attributes.get(k); const attrs = sessionContext.deviceData.attributes.get(k);
const curVal = attrs.value?.[1]; const curVal = attrs.value ? attrs.value[1] : null;
if (curVal && canWrite(attrs)) { if (curVal && canWrite(attrs)) {
const val = v.slice() as [string | number | boolean, string]; const val = v.slice() as [string | number | boolean, string];
if (!val[1]) val[1] = curVal[1]; if (!val[1]) val[1] = curVal[1];
@@ -1903,9 +2170,8 @@ function generateSetRpcRequest(
// Downloads // Downloads
for (const [p, t] of syncState.downloadsDownload) { for (const [p, t] of syncState.downloadsDownload) {
if (!(t > 0 && t <= sessionContext.timestamp)) continue;
const attrs = deviceData.attributes.get(p); const attrs = deviceData.attributes.get(p);
if (!(t <= attrs?.value?.[1]?.[0])) { if (!(attrs && attrs.value && t <= attrs.value[1][0])) {
const fileTypeAttrs = deviceData.attributes.get( const fileTypeAttrs = deviceData.attributes.get(
deviceData.paths.get(p.slice(0, -1).concat(Path.parse("FileType"))) deviceData.paths.get(p.slice(0, -1).concat(Path.parse("FileType")))
); );
@@ -1922,31 +2188,112 @@ function generateSetRpcRequest(
name: "Download", name: "Download",
commandKey: generateRpcId(sessionContext), commandKey: generateRpcId(sessionContext),
instance: p.segments[1] as string, instance: p.segments[1] as string,
fileType: fileTypeAttrs?.value?.[1][0] as string, fileType: fileTypeAttrs
fileName: fileNameAttrs?.value?.[1][0] as string, ? fileTypeAttrs.value
targetFileName: targetFileNameAttrs?.value?.[1][0] as string, ? (fileTypeAttrs.value[1][0] as string)
: null
: null,
fileName: fileNameAttrs
? fileNameAttrs.value
? (fileNameAttrs.value[1][0] as string)
: null
: null,
targetFileName: targetFileNameAttrs
? targetFileNameAttrs.value
? (targetFileNameAttrs.value[1][0] as string)
: null
: null,
}; };
} }
} }
// ChangeDUState
const duStateOps: ChangeDUState["operations"] = [];
for (const [p, t] of syncState.duStatesChangeDUState) {
const attrs = deviceData.attributes.get(p);
if (attrs && attrs.value && t <= attrs.value[1][0]) continue;
const urlAttrs = deviceData.attributes.get(
deviceData.paths.get(p.slice(0, -1).concat(Path.parse("URL")))
);
const operationTypeAttrs = deviceData.attributes.get(
deviceData.paths.get(p.slice(0, -1).concat(Path.parse("OperationType")))
);
const uuidAttrs = deviceData.attributes.get(
deviceData.paths.get(p.slice(0, -1).concat(Path.parse("UUID")))
);
const executionEnvRefAttrs = deviceData.attributes.get(
deviceData.paths.get(p.slice(0, -1).concat(Path.parse("ExecutionEnvRef")))
);
const versionAttrs = deviceData.attributes.get(
deviceData.paths.get(p.slice(0, -1).concat(Path.parse("Version")))
);
const usernameAttrs = deviceData.attributes.get(
deviceData.paths.get(p.slice(0, -1).concat(Path.parse("Username")))
);
const passwordAttrs = deviceData.attributes.get(
deviceData.paths.get(p.slice(0, -1).concat(Path.parse("Password")))
);
const operationType = operationTypeAttrs
? (operationTypeAttrs.value[1][0] as string)
: null;
const url = urlAttrs ? (urlAttrs.value[1][0] as string) : null;
const uuid = uuidAttrs ? (uuidAttrs.value[1][0] as string) : null;
const executionEnvRef = executionEnvRefAttrs
? (executionEnvRefAttrs.value[1][0] as string)
: null;
const version = versionAttrs ? (versionAttrs.value[1][0] as string) : null;
const username = usernameAttrs
? (usernameAttrs.value[1][0] as string)
: null;
const password = passwordAttrs
? (passwordAttrs.value[1][0] as string)
: null;
if (
operationType !== "install" &&
operationType !== "update" &&
operationType !== "uninstall"
)
continue;
duStateOps.push({
operationType: operationType,
instance: p.segments[1] as string,
url: url,
uuid: uuid,
executionEnvRef: executionEnvRef,
version: version,
username: username,
password: password,
});
}
if (duStateOps.length) {
return {
name: "ChangeDUState",
commandKey: generateRpcId(sessionContext),
operations: duStateOps,
};
}
// Reboot // Reboot
if (syncState.reboot > 0 && syncState.reboot <= sessionContext.timestamp) { if (syncState.reboot) {
const p = sessionContext.deviceData.paths.get(Path.parse("Reboot")); const p = sessionContext.deviceData.paths.get(Path.parse("Reboot"));
const attrs = p ? sessionContext.deviceData.attributes.get(p) : null; const attrs = p ? sessionContext.deviceData.attributes.get(p) : null;
if (!(attrs?.value?.[1][0] >= syncState.reboot)) { if (!(attrs && attrs.value && attrs.value[1][0] >= syncState.reboot)) {
delete syncState.reboot; delete syncState.reboot;
return { name: "Reboot" }; return { name: "Reboot" };
} }
} }
// Factory reset // Factory reset
if ( if (syncState.factoryReset) {
syncState.factoryReset > 0 &&
syncState.factoryReset <= sessionContext.timestamp
) {
const p = sessionContext.deviceData.paths.get(Path.parse("FactoryReset")); const p = sessionContext.deviceData.paths.get(Path.parse("FactoryReset"));
const attrs = p ? sessionContext.deviceData.attributes.get(p) : null; const attrs = p ? sessionContext.deviceData.attributes.get(p) : null;
if (!(attrs?.value?.[1][0] >= syncState.factoryReset)) { if (
!(attrs && attrs.value && attrs.value[1][0] >= syncState.factoryReset)
) {
delete syncState.factoryReset; delete syncState.factoryReset;
return { name: "FactoryReset" }; return { name: "FactoryReset" };
} }
@@ -2014,7 +2361,7 @@ function generateSetVirtualParameterProvisions(
let provisions; let provisions;
if (virtualParameterDeclarations) { if (virtualParameterDeclarations) {
for (const declaration of virtualParameterDeclarations) { for (const declaration of virtualParameterDeclarations) {
if (declaration[2]?.value != null) { if (declaration[2] && declaration[2].value != null) {
const attrs = sessionContext.deviceData.attributes.get(declaration[0]); const attrs = sessionContext.deviceData.attributes.get(declaration[0]);
if ( if (
attrs && attrs &&
@@ -2143,7 +2490,9 @@ function processDeclarations(
if (currentAttributes) { if (currentAttributes) {
leafParam = currentPath; leafParam = currentPath;
leafIsObject = currentAttributes.object?.[1]; leafIsObject = currentAttributes.object
? currentAttributes.object[1]
: null;
// Possible V8 bug causes null === 0 // Possible V8 bug causes null === 0
if (leafIsObject != null && leafIsObject === 0) if (leafIsObject != null && leafIsObject === 0)
leafTimestamp = Math.max(leafTimestamp, currentAttributes.object[0]); leafTimestamp = Math.max(leafTimestamp, currentAttributes.object[0]);
@@ -2158,13 +2507,13 @@ function processDeclarations(
) { ) {
case "Reboot": case "Reboot":
if (currentPath.length === 1) { if (currentPath.length === 1) {
if (declareAttributeValues?.value) if (declareAttributeValues && declareAttributeValues.value)
syncState.reboot = +new Date(declareAttributeValues.value[0]); syncState.reboot = +new Date(declareAttributeValues.value[0]);
} }
break; break;
case "FactoryReset": case "FactoryReset":
if (currentPath.length === 1) { if (currentPath.length === 1) {
if (declareAttributeValues?.value) if (declareAttributeValues && declareAttributeValues.value)
syncState.factoryReset = +new Date(declareAttributeValues.value[0]); syncState.factoryReset = +new Date(declareAttributeValues.value[0]);
} }
break; break;
@@ -2209,6 +2558,26 @@ function processDeclarations(
} }
} }
break; break;
case "DUStates":
if (
currentPath.length === 3 &&
currentPath.wildcard === 0 &&
declareAttributeValues &&
declareAttributeValues.value
) {
if (currentPath.segments[2] === "ChangeDUState") {
syncState.duStatesChangeDUState.set(
currentPath,
declareAttributeValues.value[0]
);
} else {
syncState.duStatesValues.set(
currentPath,
declareAttributeValues.value[0]
);
}
}
break;
case "VirtualParameters": case "VirtualParameters":
if (currentPath.length <= 2) { if (currentPath.length <= 2) {
let d; let d;
@@ -2379,6 +2748,10 @@ function processInstances(
if (parent.length !== 1) return; if (parent.length !== 1) return;
instancesToDelete = sessionContext.syncState.downloadsToDelete; instancesToDelete = sessionContext.syncState.downloadsToDelete;
instancesToCreate = sessionContext.syncState.downloadsToCreate; instancesToCreate = sessionContext.syncState.downloadsToCreate;
} else if (parent.segments[0] === "DUStates") {
if (parent.length !== 1) return;
instancesToDelete = sessionContext.syncState.duStatesToDelete;
instancesToCreate = sessionContext.syncState.duStatesToCreate;
} else { } else {
instancesToDelete = sessionContext.syncState.instancesToDelete.get(parent); instancesToDelete = sessionContext.syncState.instancesToDelete.get(parent);
if (instancesToDelete == null) { if (instancesToDelete == null) {
@@ -2667,6 +3040,7 @@ export async function rpcResponse(
"FactoryReset", "FactoryReset",
"VirtualParameters", "VirtualParameters",
"Downloads", "Downloads",
"DUStates",
]) { ]) {
const p = sessionContext.deviceData.paths.get(Path.parse(n)); const p = sessionContext.deviceData.paths.get(Path.parse(n));
if (p && sessionContext.deviceData.attributes.has(p)) if (p && sessionContext.deviceData.attributes.has(p))
@@ -2866,7 +3240,7 @@ export async function rpcResponse(
toClear toClear
); );
} else { } else {
const operation = { const operation: DownloadOperation = {
name: "Download", name: "Download",
timestamp: sessionContext.timestamp, timestamp: sessionContext.timestamp,
provisions: sessionContext.provisions, provisions: sessionContext.provisions,
@@ -2890,6 +3264,40 @@ export async function rpcResponse(
sessionContext.operationsTouched = {}; sessionContext.operationsTouched = {};
sessionContext.operationsTouched[rpcReq.commandKey] = 1; sessionContext.operationsTouched[rpcReq.commandKey] = 1;
} }
} else if (rpcRes.name === "ChangeDUStateResponse") {
if (rpcReq.name !== "ChangeDUState")
return invalidResponse("Response name does not match request name");
for (const op of rpcReq.operations) {
toClear = device.set(
sessionContext.deviceData,
Path.parse(`DUStates.${op.instance}.ChangeDUState`),
timestamp + 1,
{ value: [timestamp + 1, [sessionContext.timestamp, "xsd:dateTime"]] },
toClear
);
}
const operation = {
name: "ChangeDUState",
timestamp: sessionContext.timestamp,
provisions: sessionContext.provisions,
channels: sessionContext.channels,
retries: {},
args: {
operations: rpcReq.operations,
},
};
for (const channel of Object.keys(sessionContext.channels)) {
if (sessionContext.retries[channel] != null)
operation.retries[channel] = sessionContext.retries[channel];
}
sessionContext.operations[rpcReq.commandKey] = operation as Operation;
if (!sessionContext.operationsTouched)
sessionContext.operationsTouched = {};
sessionContext.operationsTouched[rpcReq.commandKey] = 1;
} else { } else {
return invalidResponse("Response name not recognized"); return invalidResponse("Response name not recognized");
} }
@@ -2932,9 +3340,13 @@ export async function rpcFault(
(p) => [Path.parse(p.replace(/\.$/, "")), timestamp] as Clear (p) => [Path.parse(p.replace(/\.$/, "")), timestamp] as Clear
); );
} else if (rpcReq.name === "SetParameterValues") { } else if (rpcReq.name === "SetParameterValues") {
toClear = ( toClear = (rpcReq.parameterList as [
rpcReq.parameterList as [string, string | number | boolean, string][] string,
).map((p) => [Path.parse(p[0].replace(/\.$/, "")), timestamp] as Clear); string | number | boolean,
string
][]).map(
(p) => [Path.parse(p[0].replace(/\.$/, "")), timestamp] as Clear
);
} else if (rpcReq.name === "AddObject") { } else if (rpcReq.name === "AddObject") {
toClear = [[Path.parse(rpcReq.objectName.replace(/\.$/, "")), timestamp]]; toClear = [[Path.parse(rpcReq.objectName.replace(/\.$/, "")), timestamp]];
} else if (rpcReq.name === "DeleteObject") { } else if (rpcReq.name === "DeleteObject") {
@@ -2971,10 +3383,10 @@ export async function deserialize(
const sessionContext = JSON.parse(sessionContextString) as SessionContext; const sessionContext = JSON.parse(sessionContextString) as SessionContext;
for (const decs of sessionContext.declarations) for (const decs of sessionContext.declarations)
for (const d of decs) d.path = Path.parse(d.path as unknown as string); for (const d of decs) d.path = Path.parse((d.path as unknown) as string);
const deviceData = initDeviceData(); const deviceData = initDeviceData();
for (const r of sessionContext.deviceData as unknown as any[]) { for (const r of (sessionContext.deviceData as unknown) as any[]) {
const path = deviceData.paths.add(Path.parse(r[0])); const path = deviceData.paths.add(Path.parse(r[0]));
if (r[1]) deviceData.trackers.set(path, r[1]); if (r[1]) deviceData.trackers.set(path, r[1]);
+132 -2
View File
@@ -48,6 +48,9 @@ import {
GetRPCMethodsRequest, GetRPCMethodsRequest,
RequestDownloadRequest, RequestDownloadRequest,
AcsResponse, AcsResponse,
ChangeDUState,
ChangeDUStateResponse,
DUStateChangeCompleteRequest,
} from "./types"; } from "./types";
import Path from "./common/path"; import Path from "./common/path";
@@ -573,6 +576,29 @@ function DownloadResponse(xml: Element): DownloadResponse {
}; };
} }
function ChangeDUState(methodRequest: ChangeDUState): string {
const operations = methodRequest.operations.map((op) => {
if (op.operationType === "install")
return `<Operations xsi:type="cwmp:InstallOpStruct"><UUID>${op.uuid}</UUID><URL>${op.url}</URL><ExecutionEnvRef>${op.executionEnvRef}</ExecutionEnvRef><Username>${op.username}</Username><Password>${op.password}</Password></Operations>`;
else if (op.operationType === "update")
return `<Operations xsi:type="cwmp:UpdateOpStruct"><UUID>${op.uuid}</UUID><URL>${op.url}</URL><Version>${op.version}</Version><Username>${op.username}</Username><Password>${op.password}</Password></Operations>`;
else if (op.operationType === "uninstall")
return `<Operations xsi:type="cwmp:UninstallOpStruct"><UUID>${op.uuid}</UUID><Version>${op.version}</Version><ExecutionEnvRef>${op.executionEnvRef}</ExecutionEnvRef></Operations>`;
return "";
});
return `<cwmp:ChangeDUState><CommandKey>${
methodRequest.commandKey || ""
}</CommandKey>${operations.join("")}</cwmp:ChangeDUState>`;
}
function ChangeDUStateResponse(): ChangeDUStateResponse {
return {
name: "ChangeDUStateResponse",
};
}
function Inform(xml: Element): InformRequest { function Inform(xml: Element): InformRequest {
let retryCount: number, evnt: string[]; let retryCount: number, evnt: string[];
let parameterList: [Path, string | number | boolean, string][]; let parameterList: [Path, string | number | boolean, string][];
@@ -719,6 +745,97 @@ function TransferCompleteResponse(): string {
return "<cwmp:TransferCompleteResponse></cwmp:TransferCompleteResponse>"; return "<cwmp:TransferCompleteResponse></cwmp:TransferCompleteResponse>";
} }
function DUStateChangeComplete(xml: Element): DUStateChangeCompleteRequest {
let commandKey: string,
results: DUStateChangeCompleteRequest["results"],
res: DUStateChangeCompleteRequest["results"][0];
for (const c of xml.children) {
switch (c.localName) {
case "CommandKey":
commandKey = c.text;
break;
case "Results":
if (!results) results = [];
res = {
uuid: null,
deploymentUnitRef: null,
version: null,
currentState: null,
resolved: true,
executionUnitRefList: null,
startTime: null,
completeTime: null,
fault: null,
};
for (const cc of c.children) {
switch (cc.localName) {
case "UUID":
res.uuid = cc.text;
break;
case "DeploymentUnitRef":
res.deploymentUnitRef = cc.text;
break;
case "Version":
res.version = cc.text;
break;
case "CurrentState":
res.currentState = cc.text;
break;
case "Resolved":
res.resolved = parseBool(cc.text);
break;
case "ExecutionUnitRefList":
res.executionUnitRefList = cc.text;
break;
case "StartTime":
if (c.text) res.startTime = Date.parse(c.text);
break;
case "CompleteTime":
if (c.text) res.completeTime = Date.parse(c.text);
break;
case "Fault":
res.fault = {
faultCode: cc.children.find((n) => n.localName === "FaultCode")
.text,
faultString: cc.children.find(
(n) => n.localName === "FaultString"
).text,
};
break;
}
}
results.push(res);
break;
}
}
if (commandKey == null) {
warnings.push({
message: "Missing or invalid XML node",
element: "CommandKey",
});
commandKey = "";
}
if (!results) {
warnings.push({
message: "Missing or invalid XML node",
element: "Results",
});
results = [];
}
return {
name: "DUStateChangeComplete",
commandKey: commandKey,
results: results,
};
}
function DUStateChangeCompleteResponse(): string {
return "<cwmp:DUStateChangeCompleteResponse></cwmp:DUStateChangeCompleteResponse>";
}
function RequestDownload(xml: Element): RequestDownloadRequest { function RequestDownload(xml: Element): RequestDownloadRequest {
return { return {
name: "RequestDownload", name: "RequestDownload",
@@ -827,13 +944,14 @@ function fault(xml: Element): CpeFault {
export function request( export function request(
body: string, body: string,
cwmpVersion: string,
warn: Record<string, unknown>[] warn: Record<string, unknown>[]
): SoapMessage { ): SoapMessage {
warnings = warn; warnings = warn;
const rpc = { const rpc = {
id: null, id: null,
cwmpVersion: null, cwmpVersion: cwmpVersion,
sessionTimeout: null, sessionTimeout: null,
cpeRequest: null, cpeRequest: null,
cpeFault: null, cpeFault: null,
@@ -876,7 +994,7 @@ export function request(
const methodElement = bodyElement.children[0]; const methodElement = bodyElement.children[0];
if (methodElement.localName === "Inform") { if (!rpc.cwmpVersion && methodElement.localName !== "Fault") {
let namespace, namespaceHref; let namespace, namespaceHref;
for (const e of [methodElement, bodyElement, envelope]) { for (const e of [methodElement, bodyElement, envelope]) {
namespace = namespace || e.namespace; namespace = namespace || e.namespace;
@@ -922,6 +1040,9 @@ export function request(
case "TransferComplete": case "TransferComplete":
rpc.cpeRequest = TransferComplete(methodElement); rpc.cpeRequest = TransferComplete(methodElement);
break; break;
case "DUStateChangeComplete":
rpc.cpeRequest = DUStateChangeComplete(methodElement);
break;
case "RequestDownload": case "RequestDownload":
rpc.cpeRequest = RequestDownload(methodElement); rpc.cpeRequest = RequestDownload(methodElement);
break; break;
@@ -955,6 +1076,9 @@ export function request(
case "DownloadResponse": case "DownloadResponse":
rpc.cpeResponse = DownloadResponse(methodElement); rpc.cpeResponse = DownloadResponse(methodElement);
break; break;
case "ChangeDUStateResponse":
rpc.cpeResponse = ChangeDUStateResponse();
break;
case "Fault": case "Fault":
rpc.cpeFault = fault(methodElement); rpc.cpeFault = fault(methodElement);
break; break;
@@ -1008,6 +1132,9 @@ export function response(rpc: {
case "TransferCompleteResponse": case "TransferCompleteResponse":
body = TransferCompleteResponse(); body = TransferCompleteResponse();
break; break;
case "DUStateChangeCompleteResponse":
body = DUStateChangeCompleteResponse();
break;
case "RequestDownloadResponse": case "RequestDownloadResponse":
body = RequestDownloadResponse(); body = RequestDownloadResponse();
break; break;
@@ -1050,6 +1177,9 @@ export function response(rpc: {
case "Download": case "Download":
body = Download(rpc.acsRequest); body = Download(rpc.acsRequest);
break; break;
case "ChangeDUState":
body = ChangeDUState(rpc.acsRequest);
break;
default: default:
throw new Error( throw new Error(
`Unknown method request ${(rpc.acsRequest as AcsRequest).name}` `Unknown method request ${(rpc.acsRequest as AcsRequest).name}`
+66 -6
View File
@@ -121,6 +121,10 @@ export interface SyncState {
downloadsToCreate: InstanceSet; downloadsToCreate: InstanceSet;
downloadsValues: Map<Path, string | number>; downloadsValues: Map<Path, string | number>;
downloadsDownload: Map<Path, number>; downloadsDownload: Map<Path, number>;
duStatesToDelete: Set<Path>;
duStatesToCreate: InstanceSet;
duStatesValues: Map<Path, string | number>;
duStatesChangeDUState: Map<Path, number>;
reboot: number; reboot: number;
factoryReset: number; factoryReset: number;
} }
@@ -183,12 +187,17 @@ export interface Task {
provisions?: (string | number | boolean)[][]; provisions?: (string | number | boolean)[][];
} }
export interface Operation { interface OperationBase {
name: string; name: string;
timestamp: number; timestamp: number;
provisions: string[][]; provisions: string[][];
channels: { [channel: string]: number }; channels: { [channel: string]: number };
retries: { [channel: string]: number }; retries: { [channel: string]: number };
args: any;
}
export interface DownloadOperation extends OperationBase {
name: "Download";
args: { args: {
instance: string; instance: string;
fileType: string; fileType: string;
@@ -197,6 +206,15 @@ export interface Operation {
}; };
} }
export interface ChangeDUStateOperation extends OperationBase {
name: "ChangeDUState";
args: {
operations: ChangeDUState["operations"];
};
}
export type Operation = DownloadOperation | ChangeDUStateOperation;
export type AcsRequest = export type AcsRequest =
| GetParameterNames | GetParameterNames
| GetParameterValues | GetParameterValues
@@ -207,7 +225,8 @@ export type AcsRequest =
| DeleteObject | DeleteObject
| FactoryReset | FactoryReset
| Reboot | Reboot
| Download; | Download
| ChangeDUState;
export interface GetParameterNames { export interface GetParameterNames {
name: "GetParameterNames"; name: "GetParameterNames";
@@ -277,6 +296,21 @@ export interface Download {
failureUrl?: string; failureUrl?: string;
} }
export interface ChangeDUState {
name: "ChangeDUState";
commandKey: string;
operations: {
operationType: "install" | "update" | "uninstall";
instance: string;
url: string;
uuid: string;
version: string;
executionEnvRef: string;
username: string;
password: string;
}[];
}
export interface SpvFault { export interface SpvFault {
parameterName: string; parameterName: string;
faultCode: string; faultCode: string;
@@ -305,7 +339,8 @@ export type CpeResponse =
| DeleteObjectResponse | DeleteObjectResponse
| RebootResponse | RebootResponse
| FactoryResetResponse | FactoryResetResponse
| DownloadResponse; | DownloadResponse
| ChangeDUStateResponse;
export interface GetParameterNamesResponse { export interface GetParameterNamesResponse {
name: "GetParameterNamesResponse"; name: "GetParameterNamesResponse";
@@ -342,6 +377,10 @@ export interface DeleteObjectResponse {
status: number; status: number;
} }
export interface ChangeDUStateResponse {
name: "ChangeDUStateResponse";
}
export interface RebootResponse { export interface RebootResponse {
name: "RebootResponse"; name: "RebootResponse";
} }
@@ -361,7 +400,8 @@ export type CpeRequest =
| InformRequest | InformRequest
| TransferCompleteRequest | TransferCompleteRequest
| GetRPCMethodsRequest | GetRPCMethodsRequest
| RequestDownloadRequest; | RequestDownloadRequest
| DUStateChangeCompleteRequest;
export interface InformRequest { export interface InformRequest {
name: "Inform"; name: "Inform";
@@ -384,6 +424,22 @@ export interface TransferCompleteRequest {
completeTime?: number; completeTime?: number;
} }
export interface DUStateChangeCompleteRequest {
name: "DUStateChangeComplete";
commandKey: string;
results: {
uuid: string;
deploymentUnitRef: string;
version: string;
currentState: string;
resolved: boolean;
executionUnitRefList: string;
fault?: { faultCode: string; faultString: string };
startTime?: number;
completeTime?: number;
}[];
}
export interface GetRPCMethodsRequest { export interface GetRPCMethodsRequest {
name: "GetRPCMethods"; name: "GetRPCMethods";
} }
@@ -397,7 +453,8 @@ export type AcsResponse =
| InformResponse | InformResponse
| GetRPCMethodsResponse | GetRPCMethodsResponse
| TransferCompleteResponse | TransferCompleteResponse
| RequestDownloadResponse; | RequestDownloadResponse
| DUStateChangeCompleteResponse;
export interface InformResponse { export interface InformResponse {
name: "InformResponse"; name: "InformResponse";
@@ -415,6 +472,9 @@ export interface TransferCompleteResponse {
export interface RequestDownloadResponse { export interface RequestDownloadResponse {
name: "RequestDownloadResponse"; name: "RequestDownloadResponse";
} }
export interface DUStateChangeCompleteResponse {
name: "DUStateChangeCompleteResponse";
}
export interface QueryOptions { export interface QueryOptions {
projection?: any; projection?: any;
@@ -477,7 +537,7 @@ export interface VirtualParameters {
} }
export interface Files { export interface Files {
[name: string]: { length: number }; [name: string]: { length: number; md5: string; contentType: string };
} }
export interface Users { export interface Users {
+3 -4
View File
@@ -62,7 +62,7 @@ const getAuthorizer = memoize(
koa.on("error", async (err, ctx) => { koa.on("error", async (err, ctx) => {
setTimeout(() => { setTimeout(() => {
// Ignored errors resulting from aborted requests // Ignored errors resulting from aborted requests
if (ctx?.req.aborted) return; if (ctx && ctx.req.aborted) return;
throw err; throw err;
}); });
}); });
@@ -95,7 +95,7 @@ koa.use(
koa.use(async (ctx, next) => { koa.use(async (ctx, next) => {
let roles: string[] = []; let roles: string[] = [];
if (ctx.state.user?.username) { if (ctx.state.user && ctx.state.user.username) {
let user; let user;
if (ctx.state.user.authMethod === "local") { if (ctx.state.user.authMethod === "local") {
user = localCache.getUsers(ctx.state.configSnapshot)[ user = localCache.getUsers(ctx.state.configSnapshot)[
@@ -221,8 +221,7 @@ router.post("/init", async (ctx) => {
}); });
router.get("/", async (ctx) => { router.get("/", async (ctx) => {
const permissionSets: PermissionSet[] = const permissionSets: PermissionSet[] = ctx.state.authorizer.getPermissionSets();
ctx.state.authorizer.getPermissionSets();
let wizard = ""; let wizard = "";
if (!Object.keys(localCache.getUsers(ctx.state.configSnapshot)).length) if (!Object.keys(localCache.getUsers(ctx.state.configSnapshot)).length)
+8 -8
View File
@@ -17,7 +17,7 @@
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>. * along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/ */
import { ObjectId } from "mongodb"; import { ObjectID } from "mongodb";
import * as db from "./db"; import * as db from "./db";
import { del } from "../cache"; import { del } from "../cache";
import { getUsers } from "../local-cache"; import { getUsers } from "../local-cache";
@@ -38,7 +38,7 @@ async function deleteFault(id): Promise<void> {
await Promise.all([ await Promise.all([
db.deleteFault(id), db.deleteFault(id),
channel.startsWith("task_") channel.startsWith("task_")
? db.deleteTask(new ObjectId(channel.slice(5))) ? db.deleteTask(new ObjectID(channel.slice(5)))
: null, : null,
]); ]);
@@ -108,7 +108,7 @@ export async function postTasks(
const lastInform = Date.now(); const lastInform = Date.now();
const notInSession = await awaitSessionEnd(deviceId, 30000); const notInSession = await awaitSessionEnd(deviceId, 30000);
if (!notInSession) { if (!notInSession) {
await Promise.all(statuses.map((t) => db.deleteTask(new ObjectId(t._id)))); await Promise.all(statuses.map((t) => db.deleteTask(new ObjectID(t._id))));
return { return {
connectionRequest: "Session took too long to complete", connectionRequest: "Session took too long to complete",
tasks: statuses, tasks: statuses,
@@ -120,7 +120,7 @@ export async function postTasks(
const status = await connectionRequest(deviceId, device); const status = await connectionRequest(deviceId, device);
if (status) { if (status) {
await Promise.all(statuses.map((t) => db.deleteTask(new ObjectId(t._id)))); await Promise.all(statuses.map((t) => db.deleteTask(new ObjectID(t._id))));
return { return {
connectionRequest: status, connectionRequest: status,
tasks: statuses, tasks: statuses,
@@ -129,7 +129,7 @@ export async function postTasks(
const sessionStarted = await awaitSessionStart(deviceId, lastInform, timeout); const sessionStarted = await awaitSessionStart(deviceId, lastInform, timeout);
if (!sessionStarted) { if (!sessionStarted) {
await Promise.all(statuses.map((t) => db.deleteTask(new ObjectId(t._id)))); await Promise.all(statuses.map((t) => db.deleteTask(new ObjectID(t._id))));
return { return {
connectionRequest: "No contact from CPE", connectionRequest: "No contact from CPE",
tasks: statuses, tasks: statuses,
@@ -138,7 +138,7 @@ export async function postTasks(
const sessionEnded = await awaitSessionEnd(deviceId, 120000); const sessionEnded = await awaitSessionEnd(deviceId, 120000);
if (!sessionEnded) { if (!sessionEnded) {
await Promise.all(statuses.map((t) => db.deleteTask(new ObjectId(t._id)))); await Promise.all(statuses.map((t) => db.deleteTask(new ObjectID(t._id))));
return { return {
connectionRequest: "Session took too long to complete", connectionRequest: "Session took too long to complete",
tasks: statuses, tasks: statuses,
@@ -163,7 +163,7 @@ export async function postTasks(
} }
} }
await Promise.all(statuses.map((t) => db.deleteTask(new ObjectId(t._id)))); await Promise.all(statuses.map((t) => db.deleteTask(new ObjectID(t._id))));
return { connectionRequest: "OK", tasks: statuses }; return { connectionRequest: "OK", tasks: statuses };
} }
@@ -200,7 +200,7 @@ export function authLocal(
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const users = getUsers(snapshot); const users = getUsers(snapshot);
const user = users[username]; const user = users[username];
if (!user?.password) return void resolve(null); if (!user || !user.password) return void resolve(null);
hashPassword(password, user.salt) hashPassword(password, user.salt)
.then((hash) => { .then((hash) => {
if (hash === user.password) resolve(true); if (hash === user.password) resolve(true);
+7 -6
View File
@@ -30,6 +30,7 @@ import { generateSalt, hashPassword } from "../auth";
import { del } from "../cache"; import { del } from "../cache";
import Authorizer from "../common/authorizer"; import Authorizer from "../common/authorizer";
import { ping } from "../ping"; import { ping } from "../ping";
import * as url from "url";
import { decodeTag } from "../common"; import { decodeTag } from "../common";
import { stringify as yamlStringify } from "../common/yaml"; import { stringify as yamlStringify } from "../common/yaml";
@@ -508,11 +509,11 @@ router.put("/files/:id", async (ctx) => {
} }
const metadata = { const metadata = {
fileType: singleParam(ctx.request.headers["metadata-filetype"]) || "", fileType: singleParam(ctx.request.headers["metadata.filetype"]) || "",
oui: singleParam(ctx.request.headers["metadata-oui"]) || "", oui: singleParam(ctx.request.headers["metadata.oui"]) || "",
productClass: productClass:
singleParam(ctx.request.headers["metadata-productclass"]) || "", singleParam(ctx.request.headers["metadata.productclass"]) || "",
version: singleParam(ctx.request.headers["metadata-version"]) || "", version: singleParam(ctx.request.headers["metadata.version"]) || "",
}; };
const validate = authorizer.getValidator(resource, metadata); const validate = authorizer.getValidator(resource, metadata);
@@ -573,12 +574,12 @@ router.post("/devices/:id/tasks", async (ctx) => {
if (!Array.isArray(exp)) return exp; if (!Array.isArray(exp)) return exp;
if (exp[0] === "PARAM") { if (exp[0] === "PARAM") {
const p = device[exp[1]]; const p = device[exp[1]];
if (p?.value) return p.value[0]; if (p && p.value) return p.value[0];
} else if (exp[0] === "FUNC") { } else if (exp[0] === "FUNC") {
if (exp[1] === "REMOTE_ADDRESS") { if (exp[1] === "REMOTE_ADDRESS") {
for (const root of ["InternetGatewayDevice", "Device"]) { for (const root of ["InternetGatewayDevice", "Device"]) {
const p = device[`${root}.ManagementServer.ConnectionRequestURL`]; const p = device[`${root}.ManagementServer.ConnectionRequestURL`];
if (p?.value) return new URL(p.value[0]).hostname; if (p && p.value) return url.parse(p.value[0]).host;
} }
return null; return null;
} }
+11 -15
View File
@@ -17,13 +17,13 @@
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>. * along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/ */
import { Db, GridFSBucket, ObjectId } from "mongodb"; import { Db, GridFSBucket, ObjectID } from "mongodb";
import { Script } from "vm"; import { Script } from "vm";
import { onConnect } from "../db"; import { onConnect } from "../db";
import * as mongodbFunctions from "../mongodb-functions"; import * as mongodbFunctions from "../mongodb-functions";
import * as expression from "../common/expression"; import * as expression from "../common/expression";
import { QueryOptions, Expression } from "../types"; import { QueryOptions, Expression } from "../types";
import { Readable, Writable } from "stream"; import { Readable } from "stream";
import { minimize } from "../common/boolean-expression"; import { minimize } from "../common/boolean-expression";
const RESOURCE_COLLECTION = { const RESOURCE_COLLECTION = {
@@ -190,8 +190,8 @@ export async function updateDeviceTags(
const collection = db.collection("devices"); const collection = db.collection("devices");
const object = {}; const object = {};
if (add?.length) object["$addToSet"] = { _tags: { $each: add } }; if (add && add.length) object["$addToSet"] = { _tags: { $each: add } };
if (pull?.length) object["$pullAll"] = { _tags: pull }; if (pull && pull.length) object["$pullAll"] = { _tags: pull };
await collection.updateOne({ _id: deviceId }, object); await collection.updateOne({ _id: deviceId }, object);
} }
@@ -208,7 +208,7 @@ function putResource(resource, id, object): Promise<void> {
function deleteResource( function deleteResource(
resource: string, resource: string,
id: string | ObjectId id: string | ObjectID
): Promise<void> { ): Promise<void> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const collection = db.collection(RESOURCE_COLLECTION[resource] || resource); const collection = db.collection(RESOURCE_COLLECTION[resource] || resource);
@@ -242,7 +242,7 @@ export function putProvision(
lineOffset: -1, lineOffset: -1,
}); });
} catch (err) { } catch (err) {
if (err.stack?.startsWith(`${id}:`)) { if (err.stack && err.stack.startsWith(`${id}:`)) {
return Promise.reject( return Promise.reject(
new Error(`${err.name} at ${err.stack.split("\n", 1)[0]}`) new Error(`${err.name} at ${err.stack.split("\n", 1)[0]}`)
); );
@@ -267,7 +267,7 @@ export function putVirtualParameter(
lineOffset: -1, lineOffset: -1,
}); });
} catch (err) { } catch (err) {
if (err.stack?.startsWith(`${id}:`)) { if (err.stack && err.stack.startsWith(`${id}:`)) {
return Promise.reject( return Promise.reject(
new Error(`${err.name} at ${err.stack.split("\n", 1)[0]}`) new Error(`${err.name} at ${err.stack.split("\n", 1)[0]}`)
); );
@@ -338,17 +338,13 @@ export function putFile(
): Promise<void> { ): Promise<void> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const bucket = new GridFSBucket(db); const bucket = new GridFSBucket(db);
const uploadStream = bucket.openUploadStreamWithId( const uploadStream = bucket.openUploadStreamWithId(filename, filename, {
filename as unknown as ObjectId,
filename,
{
metadata: metadata, metadata: metadata,
} });
);
uploadStream.on("error", reject); uploadStream.on("error", reject);
contentStream.on("error", reject); contentStream.on("error", reject);
uploadStream.on("finish", resolve); uploadStream.on("finish", resolve);
contentStream.pipe(uploadStream as Writable); contentStream.pipe(uploadStream);
}); });
} }
@@ -366,6 +362,6 @@ export function deleteFault(id: string): Promise<void> {
return deleteResource("faults", id); return deleteResource("faults", id);
} }
export function deleteTask(id: ObjectId): Promise<void> { export function deleteTask(id: ObjectID): Promise<void> {
return deleteResource("tasks", id); return deleteResource("tasks", id);
} }
+4 -4
View File
@@ -26,10 +26,10 @@ interface Revisions<V> {
} }
export default class VersionedMap<K, V> { export default class VersionedMap<K, V> {
private declare _sizeDiff: number[]; private _sizeDiff: number[];
private declare _revision: number; private _revision: number;
private declare map: Map<K, (V | symbol)[]>; private map: Map<K, (V | symbol)[]>;
public declare dirty: number; public dirty: number;
public constructor() { public constructor() {
this._sizeDiff = [0]; this._sizeDiff = [0];
+4264 -1115
View File
File diff suppressed because it is too large. Load diff
+36 -36
View File
@@ -1,6 +1,6 @@
{ {
"name": "genieacs", "name": "genieacs",
"version": "1.2.6", "version": "1.2.5",
"description": "A TR-069 Auto Configuration Server (ACS)", "description": "A TR-069 Auto Configuration Server (ACS)",
"repository": { "repository": {
"type": "git", "type": "git",
@@ -19,56 +19,56 @@
"bin": "bin" "bin": "bin"
}, },
"dependencies": { "dependencies": {
"espresso-iisojs": "^1.0.4", "espresso-iisojs": "^1.0.3",
"iconv-lite": "^0.6.3", "iconv-lite": "^0.6.2",
"ipaddr.js": "^2.0.1", "ipaddr.js": "^2.0.0",
"jsonwebtoken": "^8.5.1", "jsonwebtoken": "^8.5.1",
"koa": "^2.13.1", "koa": "^2.13.1",
"koa-bodyparser": "^4.3.0", "koa-bodyparser": "^4.3.0",
"koa-compress": "^5.1.0", "koa-compress": "^5.0.1",
"koa-jwt": "^4.0.1", "koa-jwt": "^4.0.0",
"koa-router": "^10.1.1", "koa-router": "^10.0.0",
"koa-static": "^5.0.0", "koa-static": "^5.0.0",
"later": "^1.2.0", "later": "^1.2.0",
"mongodb": "^4.1.2", "mongodb": "^3.6.3",
"parsimmon": "^1.18.0", "parsimmon": "^1.16.0",
"seedrandom": "^3.0.5" "seedrandom": "^3.0.5"
}, },
"devDependencies": { "devDependencies": {
"@rollup/plugin-commonjs": "^20.0.0", "@rollup/plugin-commonjs": "^17.1.0",
"@rollup/plugin-json": "^4.1.0", "@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.0.4", "@rollup/plugin-node-resolve": "^11.2.0",
"@rollup/plugin-typescript": "^8.2.5", "@rollup/plugin-typescript": "^8.2.0",
"@types/codemirror": "^5.60.2", "@types/codemirror": "0.0.108",
"@types/jsonwebtoken": "^8.5.5", "@types/jsonwebtoken": "^8.5.0",
"@types/koa": "^2.13.4", "@types/koa": "^2.13.1",
"@types/koa-compress": "^4.0.3", "@types/koa-compress": "^4.0.1",
"@types/koa-router": "^7.4.4", "@types/koa-router": "^7.4.1",
"@types/koa-static": "^4.0.2", "@types/koa-static": "^4.0.1",
"@types/mithril": "^2.0.8", "@types/mithril": "^2.0.6",
"@types/node": "^16.9.1", "@types/mongodb": "^3.6.9",
"@types/node": "^14.14.34",
"@types/parsimmon": "^1.10.6", "@types/parsimmon": "^1.10.6",
"@types/seedrandom": "^3.0.1", "@types/seedrandom": "^3.0.0",
"@typescript-eslint/eslint-plugin": "^4.31.1", "@typescript-eslint/eslint-plugin": "^4.17.0",
"@typescript-eslint/parser": "^4.31.1", "@typescript-eslint/parser": "^4.17.0",
"ava": "^3.15.0", "ava": "^3.15.0",
"codemirror": "^5.62.3", "codemirror": "^5.59.4",
"cssnano": "^5.0.8", "cssnano": "^4.1.10",
"eslint": "^7.32.0", "eslint": "^7.22.0",
"eslint-config-prettier": "^8.3.0", "eslint-config-prettier": "^8.1.0",
"esm": "^3.2.25", "esm": "^3.2.25",
"mithril": "^2.0.4", "mithril": "^2.0.4",
"postcss": "^8.3.6", "postcss": "^8.2.8",
"postcss-import": "^14.0.2", "postcss-import": "^14.0.0",
"postcss-preset-env": "^6.7.0", "postcss-preset-env": "^6.7.0",
"prettier": "^2.4.1", "prettier": "^2.2.1",
"rollup": "^2.56.3", "rollup": "^2.41.2",
"rollup-plugin-terser": "^7.0.2", "rollup-plugin-terser": "^7.0.2",
"sql.js": "^1.6.1", "svgo": "^2.2.2",
"svgo": "^2.6.1", "ts-node": "^9.1.1",
"ts-node": "^10.2.1", "typescript": "^4.2.3",
"typescript": "^4.4.3", "yaml": "^1.10.0"
"yaml": "^1.10.2"
}, },
"engines": { "engines": {
"node": ">=12.13.0" "node": ">=12.13.0"
-118
View File
@@ -1,118 +0,0 @@
import ava, { TestInterface } from "ava";
import { minimize, unionDiff } from "../lib/common/boolean-expression";
import { parse, stringify } from "../lib/common/expression-parser";
import initSqlJs from "sql.js";
const test = ava as TestInterface<{
query: (filter: string) => Promise<Set<number>>;
}>;
const STRING_VALUES = [null, "", "a", "ab", "ab10", "ab-10"];
const DECIMAL_VALUES = [null, 0, -10, 10];
test.before(async (t) => {
const sql = await initSqlJs();
const db = new sql.Database();
t.context.query = async (filter: string): Promise<Set<number>> => {
const res = db.exec(`SELECT id FROM test WHERE ${filter}`);
if (!res.length) return new Set();
return new Set(res[0].values.flat());
};
db.run(
"CREATE TABLE test (id INTEGER PRIMARY KEY, string STRING, decimal DECIMAL(4,2))"
);
const stmt = db.prepare("INSERT INTO test (string, decimal) VALUES (?, ?)");
const count = STRING_VALUES.length * DECIMAL_VALUES.length;
for (let i = 0; i < count; ++i) {
const str = i % STRING_VALUES.length;
const dec = Math.trunc(i / STRING_VALUES.length) % DECIMAL_VALUES.length;
stmt.run([STRING_VALUES[str], DECIMAL_VALUES[dec]]);
}
stmt.free();
});
function setsEqual(set1: Set<number>, set2: Set<number>): boolean {
if (set1.size !== set2.size) return false;
for (const s of set1) if (!set2.has(s)) return false;
return true;
}
function getPermutations(...arrs: any[][]): any[][] {
const count = arrs.reduce((total, arr) => total * arr.length, 1);
const res = [];
for (let i = 0; i < count; ++i) {
let j = i;
const row = [];
for (const arr of arrs) {
const v = arr[j % arr.length];
j = Math.trunc(j / arr.length);
row.push(v);
}
res.push(row);
}
return res;
}
test("minimize", async (t) => {
const cases: string[] = [];
cases.push("null");
cases.push("false");
cases.push("true");
cases.push("string");
cases.push("(string + decimal) IS NULL");
cases.push("(string + decimal) = NULL");
cases.push("COALESCE(string, decimal) = 0");
for (const [s1, s2, s3, op1, op2] of getPermutations(
STRING_VALUES.filter((s) => s),
STRING_VALUES.filter((s) => s),
STRING_VALUES.filter((s) => s),
[">", "=", "<"],
["<>", ">="]
)) {
cases.push(
`string ${op1} "${s1}" OR string ='${s2}' OR NOT string ${op2} '${s3}'`
);
}
for (const [s1, s2] of getPermutations(
STRING_VALUES.filter((s) => s),
STRING_VALUES.filter((s) => s)
))
cases.push(`string > "${s1}" AND string < '${s2}'`);
for (const c of cases) {
const res1 = await t.context.query(c);
const min = stringify(minimize(parse(c), true));
const res2 = await t.context.query(min);
t.true(setsEqual(res1, res2));
}
});
test("unionDiff", async (t) => {
const cases = [
"true",
"decimal > 0",
"decimal > 10",
"UPPER(string || decimal) LIKE 'AB10'",
"COALESCE(string, decimal) = 0",
];
for (const [c1, c2] of getPermutations(cases, cases)) {
const res1 = await t.context.query(c1);
const res2 = await t.context.query(c2);
const [union, diff] = unionDiff(parse(c1), parse(c2));
const res3 = await t.context.query(stringify(union));
const res4 = await t.context.query(stringify(diff));
const unionSet = new Set([...res1, ...res2]);
const diffSet = new Set(Array.from(res2).filter((r) => !res1.has(r)));
t.true(setsEqual(res3, unionSet));
t.true(setsEqual(res4, diffSet));
}
});
+2 -10
View File
@@ -49,16 +49,8 @@ ava("convertOldPrecondition", (t) => {
]; ];
t.plan(tests.length + 2 * shouldFailTests.length); t.plan(tests.length + 2 * shouldFailTests.length);
for (const test of tests) { for (const test of tests)
t.is( t.is(stringify(mongodbFunctions.convertOldPrecondition(test[0])), test[1]);
stringify(
mongodbFunctions.convertOldPrecondition(
test[0] as Record<string, unknown>
)
),
test[1]
);
}
for (const test of shouldFailTests) { for (const test of shouldFailTests) {
const func = (): void => { const func = (): void => {
+3 -3
View File
@@ -1,9 +1,9 @@
{ {
"compilerOptions": { "compilerOptions": {
"lib": ["ES2019", "dom"], "lib": ["esnext", "dom"],
"module": "ES2020", "module": "esnext",
"moduleResolution": "node", "moduleResolution": "node",
"target": "ES2019", "target": "esnext",
"resolveJsonModule": true, "resolveJsonModule": true,
"allowSyntheticDefaultImports": true, "allowSyntheticDefaultImports": true,
"alwaysStrict": true, "alwaysStrict": true,
+1 -1
View File
@@ -69,7 +69,7 @@ function pagify(pageName, page): RouteResolver {
render: () => { render: () => {
const lastRenderTimestamp = Date.now(); const lastRenderTimestamp = Date.now();
let p; let p;
if (state?.error) p = m(errorPage.component, state); if (state && state.error) p = m(errorPage.component, state);
else p = m(contextifyComponent(page.component), state); else p = m(contextifyComponent(page.component), state);
const attrs = {}; const attrs = {};
attrs["page"] = pageName; attrs["page"] = pageName;
+12 -24
View File
@@ -23,13 +23,13 @@ type AutocompleteCallback = (
) => void; ) => void;
export default class Autocomplete { export default class Autocomplete {
private declare callback: AutocompleteCallback; private callback: AutocompleteCallback;
private declare element: HTMLInputElement; private element: HTMLInputElement;
private declare hideTimeout: NodeJS.Timeout; private hideTimeout: NodeJS.Timeout;
private declare visible: boolean; private visible: boolean;
private declare default: string; private default: string;
private declare selection: number; private selection: number;
private declare container: HTMLElement; private container: HTMLElement;
public constructor(className: string, callback: AutocompleteCallback) { public constructor(className: string, callback: AutocompleteCallback) {
this.callback = callback; this.callback = callback;
@@ -51,8 +51,11 @@ export default class Autocomplete {
el.addEventListener("focus", () => { el.addEventListener("focus", () => {
this.element = el; this.element = el;
const domRect = el.getBoundingClientRect();
this.container.style.left = `${domRect.left + window.pageXOffset}px`;
this.container.style.width = `${domRect.width}px`;
this.container.style.top = `${domRect.bottom + window.pageYOffset}px`;
this.update(); this.update();
this.reposition();
}); });
el.addEventListener("blur", () => { el.addEventListener("blur", () => {
@@ -68,8 +71,7 @@ export default class Autocomplete {
} else if (e.key === "Enter") { } else if (e.key === "Enter") {
if (this.default != null) { if (this.default != null) {
el.value = this.default; el.value = this.default;
e.stopImmediatePropagation(); e.preventDefault();
el.dispatchEvent(new InputEvent("input"));
this.update(); this.update();
} }
} else if (e.key === "ArrowDown") { } else if (e.key === "ArrowDown") {
@@ -91,19 +93,6 @@ export default class Autocomplete {
}); });
} }
public reposition(): void {
if (!this.element) return;
const domRect = this.element.getBoundingClientRect();
if (!domRect.width) {
// Element has been removed
if (this.visible) this.hide();
return;
}
this.container.style.left = `${domRect.left + window.pageXOffset}px`;
this.container.style.width = `${domRect.width}px`;
this.container.style.top = `${domRect.bottom + window.pageYOffset}px`;
}
private hide(): void { private hide(): void {
this.container.style.opacity = "0"; this.container.style.opacity = "0";
this.visible = false; this.visible = false;
@@ -170,7 +159,6 @@ export default class Autocomplete {
e.addEventListener("mousedown", (ev) => { e.addEventListener("mousedown", (ev) => {
ev.preventDefault(); ev.preventDefault();
el.value = suggestion.value; el.value = suggestion.value;
el.dispatchEvent(new InputEvent("input"));
if (this.element === el) this.update(); if (this.element === el) this.update();
}); });
this.container.appendChild(e); this.container.appendChild(e);
+5 -11
View File
@@ -17,23 +17,17 @@
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>. * along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/ */
import { VnodeDOM, ClosureComponent } from "mithril"; import { VnodeDOM, ClosureComponent, Component } from "mithril";
import * as notifications from "./notifications"; import * as notifications from "./notifications";
import { changePassword } from "./store"; import { changePassword } from "./store";
import { m } from "./components"; import { m } from "./components";
interface Attrs { const component: ClosureComponent = (): Component => {
noAuth?: boolean;
username?: string;
onPasswordChange: () => void;
}
const component: ClosureComponent<Attrs> = () => {
return { return {
view: (vnode) => { view: (vnode) => {
const onPasswordChange = vnode.attrs.onPasswordChange; const onPasswordChange = vnode.attrs["onPasswordChange"];
const enforceAuth = !vnode.attrs.noAuth; const enforceAuth = !vnode.attrs["noAuth"];
const username = vnode.attrs.username; const username = vnode.attrs["username"];
if (username) vnode.state["username"] = username; if (username) vnode.state["username"] = username;
+14 -24
View File
@@ -17,54 +17,44 @@
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>. * along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/ */
import { Editor } from "codemirror"; import { ClosureComponent, Component } from "mithril";
import { ClosureComponent } from "mithril";
import { m } from "./components"; import { m } from "./components";
import { codeMirror } from "./dynamic-loader"; import { codeMirror } from "./dynamic-loader";
interface Attrs { const component: ClosureComponent = (): Component => {
id: string;
value: string;
mode: string;
readOnly?: boolean;
focus?: boolean;
onSubmit?: (dom: Element) => void;
onChange?: (value: string) => void;
onReady?: (editor: Editor) => void;
}
const component: ClosureComponent<Attrs> = () => {
return { return {
view: (vnode) => { view: (vnode) => {
return m("textarea", { return m("textarea", {
name: vnode.attrs.id, name: vnode.attrs["id"],
value: vnode.attrs.value, value: vnode.attrs["value"],
oncreate: (_vnode) => { oncreate: (_vnode) => {
const editor = codeMirror.fromTextArea( const editor = codeMirror.fromTextArea(
_vnode.dom as HTMLTextAreaElement, _vnode.dom as HTMLTextAreaElement,
{ {
mode: vnode.attrs.mode, mode: vnode.attrs["mode"],
lineNumbers: true, lineNumbers: true,
readOnly: vnode.attrs.readOnly, readOnly: vnode.attrs["readOnly"],
extraKeys: { extraKeys: {
"Ctrl-Enter": () => { "Ctrl-Enter": () => {
if (vnode.attrs.onSubmit) vnode.attrs.onSubmit(_vnode.dom); if (vnode.attrs["onSubmit"])
vnode.attrs["onSubmit"](_vnode.dom);
}, },
"Cmd-Enter": () => { "Cmd-Enter": () => {
if (vnode.attrs.onSubmit) vnode.attrs.onSubmit(_vnode.dom); if (vnode.attrs["onSubmit"])
vnode.attrs["onSubmit"](_vnode.dom);
}, },
}, },
} }
); );
if (vnode.attrs.onChange) { if (vnode.attrs["onChange"]) {
editor.on("change", (e) => { editor.on("change", (e) => {
vnode.attrs.onChange(e.getValue()); vnode.attrs["onChange"](e.getValue());
}); });
} }
if (vnode.attrs.focus) editor.focus(); if (vnode.attrs["focus"]) editor.focus();
if (vnode.attrs.onReady) vnode.attrs.onReady(editor); if (vnode.attrs["onReady"]) vnode.attrs["onReady"](editor);
}, },
}); });
}, },
+5 -12
View File
@@ -22,7 +22,7 @@ import m, {
Attributes, Attributes,
Children, Children,
ComponentTypes, ComponentTypes,
CommonAttributes, Lifecycle,
ClosureComponent, ClosureComponent,
Vnode, Vnode,
} from "mithril"; } from "mithril";
@@ -81,7 +81,7 @@ interface MC extends Static {
<Attrs, State>( <Attrs, State>(
ctx: Attributes, ctx: Attributes,
component: ComponentTypes<Attrs, State>, component: ComponentTypes<Attrs, State>,
attributes: Attrs & CommonAttributes<Attrs, State>, attributes: Attrs & Lifecycle<Attrs, State> & { key?: string | number },
...args: Children[] ...args: Children[]
): Vnode<Attrs, State>; ): Vnode<Attrs, State>;
}; };
@@ -116,7 +116,7 @@ function applyContext(vnode, parentContext): void {
vnodeContext.set(vnode, vc); vnodeContext.set(vnode, vc);
vnode.attrs = Object.assign({}, vc, vnode.attrs); vnode.attrs = Object.assign({}, vc, vnode.attrs);
} }
if (vnode.children?.length) if (vnode.children && vnode.children.length)
for (const c of vnode.children) applyContext(c, vc); for (const c of vnode.children) applyContext(c, vc);
} }
} }
@@ -133,7 +133,7 @@ export function contextifyComponent(component: ComponentTypes): ComponentTypes {
applyContext(res, context); applyContext(res, context);
return res; return res;
}; };
} else if (!component.prototype?.view) { } else if (!component.prototype || !component.prototype.view) {
c = (initialNode) => { c = (initialNode) => {
const state = (component as ClosureComponent)(initialNode); const state = (component as ClosureComponent)(initialNode);
const view = state.view; const view = state.view;
@@ -144,14 +144,7 @@ export function contextifyComponent(component: ComponentTypes): ComponentTypes {
applyContext(res, context); applyContext(res, context);
return res; return res;
} catch (err) { } catch (err) {
return m( return m("p.error", { title: err.message }, "Error!");
"p.error",
{
title: "Click to print stack trace to console",
onclick: () => console.error(err),
},
"Error!"
);
} }
}; };
return state; return state;
+1 -1
View File
@@ -85,7 +85,7 @@ const component: ClosureComponent = (): Component => {
let c = 0; let c = 0;
for (const k of keys) { for (const k of keys) {
const p = device[k]; const p = device[k];
const str = p.value?.[0] ? `${k} ${p.value[0]}` : k; const str = p.value && p.value[0] ? `${k} ${p.value[0]}` : k;
if (re && !re.test(str)) continue; if (re && !re.test(str)) continue;
++c; ++c;
if (count < limit) filteredKeys.push(k); if (count < limit) filteredKeys.push(k);
+3 -3
View File
@@ -38,7 +38,7 @@ const evaluateParam = memoize((exp, obj, now: number) => {
) { ) {
let v = null; let v = null;
const p = obj[e[i][1]]; const p = obj[e[i][1]];
if (p?.value) { if (p && p.value) {
v = p.value[0]; v = p.value[0];
timestamp = Math.min(timestamp, p.valueTimestamp); timestamp = Math.min(timestamp, p.valueTimestamp);
} }
@@ -58,7 +58,7 @@ const evaluateParam = memoize((exp, obj, now: number) => {
value = exp; value = exp;
} else if (exp[0] === "PARAM") { } else if (exp[0] === "PARAM") {
const p = obj[exp[1]]; const p = obj[exp[1]];
if (p?.value) { if (p && p.value) {
timestamp = p.valueTimestamp; timestamp = p.valueTimestamp;
value = p.value[0]; value = p.value[0];
parameter = exp[1]; parameter = exp[1];
@@ -84,7 +84,7 @@ const component: ClosureComponent = (): Component => {
if (value == null) return null; if (value == null) return null;
let edit; let edit;
if (device[parameter]?.writable) { if (device[parameter] && device[parameter].writable) {
edit = m( edit = m(
"button", "button",
{ {
+3 -3
View File
@@ -45,7 +45,7 @@ export function flattenConfig(config: Record<string, unknown>): any {
// Order keys such that nested objects come last // Order keys such that nested objects come last
function orderKeys(config: any): number { function orderKeys(config: any): number {
let res = 1; let res = 1;
if (config == null || typeof config !== "object") return res; if (typeof config !== "object") return res;
if (Array.isArray(config)) { if (Array.isArray(config)) {
for (const c of config) res += orderKeys(c); for (const c of config) res += orderKeys(c);
return res; return res;
@@ -79,7 +79,7 @@ export function structureConfig(config: Config[]): any {
let ref = _config; let ref = _config;
while (keys.length > 1) { while (keys.length > 1) {
const k = keys.shift(); const k = keys.shift();
if (ref[k] == null || typeof ref[k] !== "object") ref[k] = {}; if (typeof ref[k] !== "object") ref[k] = {};
ref = ref[k]; ref = ref[k];
} }
ref[keys[0]] = c.value; ref[keys[0]] = c.value;
@@ -137,7 +137,7 @@ export function diffConfig(
}; };
for (const [k, v] of Object.entries(target)) for (const [k, v] of Object.entries(target))
if (v && current[k] !== v) diff.add.push({ _id: k, value: v }); if (!current[k] || current[k] !== v) diff.add.push({ _id: k, value: v });
for (const k of Object.keys(current)) if (!target[k]) diff.remove.push(k); for (const k of Object.keys(current)) if (!target[k]) diff.remove.push(k);
+4 -4
View File
@@ -154,7 +154,7 @@ function renderTable(confsResponse, searchString): Children {
{ {
base: conf, base: conf,
actionHandler: (action, object) => { actionHandler: (action, object) => {
return new Promise<void>((resolve) => { return new Promise((resolve) => {
putActionHandler(action, object, false) putActionHandler(action, object, false)
.then((errors) => { .then((errors) => {
const ErrorList = errors ? Object.values(errors) : []; const ErrorList = errors ? Object.values(errors) : [];
@@ -164,11 +164,11 @@ function renderTable(confsResponse, searchString): Children {
} else { } else {
overlay.close(cb); overlay.close(cb);
} }
resolve(); resolve(null);
}) })
.catch((err) => { .catch((err) => {
notifications.push("error", err.message); notifications.push("error", err.message);
resolve(); resolve(null);
}); });
}); });
}, },
@@ -322,7 +322,7 @@ export const component: ClosureComponent = (): Component => {
uiConfigComponent, uiConfigComponent,
Object.assign( Object.assign(
{ {
onUpdate: (errs: Record<string, string>) => { onUpdate: (errs: string[]) => {
const errors = errs ? Object.values(errs) : []; const errors = errs ? Object.values(errs) : [];
if (errors.length) { if (errors.length) {
for (const err of errors) for (const err of errors)
+4 -5
View File
@@ -328,11 +328,10 @@ export const component: ClosureComponent = (): Component => {
if (window.authorizer.hasAccess("devices", 3)) if (window.authorizer.hasAccess("devices", 3))
attrs["actionsCallback"] = renderActions; attrs["actionsCallback"] = renderActions;
const filterAttrs = { const filterAttrs = {};
resource: "devices", filterAttrs["resource"] = "devices";
filter: vnode.attrs["filter"], filterAttrs["filter"] = vnode.attrs["filter"];
onChange: onFilterChanged, filterAttrs["onChange"] = onFilterChanged;
};
return [ return [
m("h1", "Listing devices"), m("h1", "Listing devices"),
+4 -5
View File
@@ -200,11 +200,10 @@ export const component: ClosureComponent = (): Component => {
}; };
} }
const filterAttrs = { const filterAttrs = {};
resource: "faults", filterAttrs["resource"] = "faults";
filter: vnode.attrs["filter"], filterAttrs["filter"] = vnode.attrs["filter"];
onChange: onFilterChanged, filterAttrs["onChange"] = onFilterChanged;
};
return [ return [
m("h1", "Listing faults"), m("h1", "Listing faults"),
+11 -18
View File
@@ -204,20 +204,14 @@ export const component: ClosureComponent = (): Component => {
putFormComponent, putFormComponent,
Object.assign( Object.assign(
{ {
actionHandler: async (action, obj) => { actionHandler: async (action, _object) => {
if (action !== "save") if (action !== "save")
throw new Error("Undefined action"); throw new Error("Undefined action");
const file = obj["file"]?.[0]; const object = Object.assign({}, _object);
const file = object["file"]
// nginx strips out headers with dot, so replace with dash ? object["file"][0]
const headers = { : null;
"metadata-fileType": obj["metadata.fileType"] || "", delete object["file"];
"metadata-oui": obj["metadata.oui"] || "",
"metadata-productclass":
obj["metadata.productClass"] || "",
"metadata-version": obj["metadata.version"] || "",
};
if (!file) { if (!file) {
notifications.push("error", "File not selected"); notifications.push("error", "File not selected");
return; return;
@@ -238,7 +232,7 @@ export const component: ClosureComponent = (): Component => {
try { try {
await upload( await upload(
file, file,
headers, object,
abortController.signal, abortController.signal,
progressListener progressListener
); );
@@ -316,11 +310,10 @@ export const component: ClosureComponent = (): Component => {
}; };
} }
const filterAttrs = { const filterAttrs = {};
resource: "files", filterAttrs["resource"] = "files";
filter: vnode.attrs["filter"], filterAttrs["filter"] = vnode.attrs["filter"];
onChange: onFilterChanged, filterAttrs["onChange"] = onFilterChanged;
};
return [ return [
m("h1", "Listing files"), m("h1", "Listing files"),
+47 -60
View File
@@ -17,7 +17,7 @@
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>. * along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/ */
import m, { ClosureComponent } from "mithril"; import m, { ClosureComponent, Component } from "mithril";
import { parse, stringify, map } from "../lib/common/expression-parser"; import { parse, stringify, map } from "../lib/common/expression-parser";
import memoize from "../lib/common/memoize"; import memoize from "../lib/common/memoize";
import Autocomplete from "./autocomplete-compnent"; import Autocomplete from "./autocomplete-compnent";
@@ -82,9 +82,9 @@ function stringifyFilter(f: Expression): string {
return stringify(f); return stringify(f);
} }
function splitFilter(filter: string): string[] { const splitFilter = memoize((filter) => {
if (!filter) return [""]; if (!filter) return [""];
const list: string[] = []; const list = [];
const f = parse(filter); const f = parse(filter);
if (Array.isArray(f) && f[0] === "AND") if (Array.isArray(f) && f[0] === "AND")
for (const ff of f.slice(1)) list.push(stringifyFilter(ff)); for (const ff of f.slice(1)) list.push(stringifyFilter(ff));
@@ -92,82 +92,69 @@ function splitFilter(filter: string): string[] {
list.push(""); list.push("");
return list; return list;
} });
interface Attrs { const component: ClosureComponent = (): Component => {
resource: string; return {
filter: string; view: (vnode) => {
onChange: (filter: string) => void; if (
} !vnode.state["filterList"] ||
vnode.attrs["filter"] !== vnode.state["filter"]
const component: ClosureComponent<Attrs> = (initialVnode) => { ) {
let filterList = splitFilter(initialVnode.attrs.filter); vnode.state["filterInvalid"] = 0;
let filterInvalid = 0; vnode.state["filter"] = vnode.attrs["filter"];
let filterTouched = false; vnode.state["filterList"] = splitFilter(vnode.attrs["filter"]);
let attrs: Attrs = initialVnode.attrs; }
function onChange(): void { function onChange(): void {
filterTouched = false; vnode.state["filterInvalid"] = 0;
filterInvalid = 0; vnode.state["filterList"] = vnode.state["filterList"].filter((f) => f);
filterList = filterList.filter((f) => f); let filter = vnode.state["filterList"].map((f, idx) => {
const list = filterList.map((f, idx) => {
try { try {
return parseFilter(attrs.resource, f); return parseFilter(vnode.attrs["resource"], f);
} catch (err) { } catch (err) {
filterInvalid |= 1 << idx; vnode.state["filterInvalid"] |= 1 << idx;
} }
return null; return null;
}); });
filterList.push(""); vnode.state["filterList"].push("");
if (filterInvalid) { if (!vnode.state["filterInvalid"]) {
m.redraw(); delete vnode.state["filter"];
return; if (filter.length === 0) {
vnode.attrs["onChange"]("");
} else {
if (filter.length > 1) filter = ["AND"].concat(filter);
else filter = filter[0];
vnode.attrs["onChange"](stringify(filter));
}
} }
if (list.length === 0) attrs.onChange("");
else if (list.length > 1) attrs.onChange(stringify(["AND", ...list]));
else attrs.onChange(stringify(list[0]));
} }
return { return m(
onupdate: (vnode) => { "div.filter",
getAutocomplete(vnode.attrs.resource).reposition(); [m("b", "Filter")].concat(
}, vnode.state["filterList"].map((fltr, idx) => {
view: (vnode) => {
if (attrs.filter !== vnode.attrs.filter) {
filterInvalid = 0;
filterList = splitFilter(vnode.attrs.filter);
}
attrs = vnode.attrs;
return m("div.filter", [
m("b", "Filter"),
...filterList.map((fltr, idx) => {
return m("input", { return m("input", {
type: "text", type: "text",
class: `${(filterInvalid >> idx) & 1 ? "error" : ""}`, class: `${
(vnode.state["filterInvalid"] >> idx) & 1 ? "error" : ""
}`,
value: fltr, value: fltr,
oninput: (e) => { onchange: (e) => {
e.redraw = false; vnode.state["filterList"] = vnode.state["filterList"].slice();
filterList[idx] = e.target.value; vnode.state["filterList"][idx] = e.target.value.trim();
filterTouched = true; onChange();
}, },
oncreate: (vn) => { oncreate: (vn) => {
const el = vn.dom as HTMLInputElement; getAutocomplete(vnode.attrs["resource"]).attach(
getAutocomplete(vnode.attrs.resource).attach(el); vn.dom as HTMLInputElement
);
el.addEventListener("blur", () => {
if (filterTouched) onChange();
});
el.addEventListener("keydown", (e) => {
if (e.key === "Enter" && filterTouched) onChange();
});
}, },
}); });
}), })
]); )
);
}, },
}; };
}; };
+4 -5
View File
@@ -365,11 +365,10 @@ export const component: ClosureComponent = (): Component => {
}; };
} }
const filterAttrs = { const filterAttrs = {};
resource: "permissions", filterAttrs["resource"] = "permissions";
filter: vnode.attrs["filter"], filterAttrs["filter"] = vnode.attrs["filter"];
onChange: onFilterChanged, filterAttrs["onChange"] = onFilterChanged;
};
return [ return [
m("h1", "Listing permissions"), m("h1", "Listing permissions"),
+6 -7
View File
@@ -323,7 +323,7 @@ export const component: ClosureComponent = (): Component => {
return m( return m(
"div", "div",
{ style: "margin:20px" }, { style: "margin:20px" },
"This UI only supports presets with a single 'provision' configuration. If this preset was originally created from the old UI (genieacs-gui), you must edit it there." "This UI only supports presets with a single 'provision' configuraiton. If this preset was originally created from the old UI (genieacs-gui), you must edit it there."
); );
} }
return comp; return comp;
@@ -331,7 +331,7 @@ export const component: ClosureComponent = (): Component => {
overlay.open( overlay.open(
cb, cb,
() => () =>
!comp.state?.["current"]["modified"] || !comp.state["current"]["modified"] ||
confirm("You have unsaved changes. Close anyway?") confirm("You have unsaved changes. Close anyway?")
); );
}, },
@@ -427,11 +427,10 @@ export const component: ClosureComponent = (): Component => {
}; };
} }
const filterAttrs = { const filterAttrs = {};
resource: "presets", filterAttrs["resource"] = "presets";
filter: vnode.attrs["filter"], filterAttrs["filter"] = vnode.attrs["filter"];
onChange: onFilterChanged, filterAttrs["onChange"] = onFilterChanged;
};
return [ return [
m("h1", "Listing presets"), m("h1", "Listing presets"),
+4 -5
View File
@@ -346,11 +346,10 @@ export const component: ClosureComponent = (): Component => {
}; };
} }
const filterAttrs = { const filterAttrs = {};
resource: "provisions", filterAttrs["resource"] = "provisions";
filter: vnode.attrs["filter"], filterAttrs["filter"] = vnode.attrs["filter"];
onChange: onFilterChanged, filterAttrs["onChange"] = onFilterChanged;
};
return [ return [
m("h1", "Listing provisions"), m("h1", "Listing provisions"),
+8 -20
View File
@@ -17,7 +17,7 @@
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>. * along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/ */
import { VnodeDOM, ClosureComponent, Children } from "mithril"; import { VnodeDOM, ClosureComponent, Component, Children } from "mithril";
import { m } from "./components"; import { m } from "./components";
import codeEditorComponent from "./code-editor-component"; import codeEditorComponent from "./code-editor-component";
import { getDatalistId } from "./datalist"; import { getDatalistId } from "./datalist";
@@ -175,25 +175,13 @@ function createField(current, attr, focus): Children {
}); });
} }
interface Attrs { const component: ClosureComponent = (): Component => {
base?: Record<string, any>;
actionHandler: (action: string, object: any) => Promise<void>;
resource: string;
attributes: {
id: string;
label: string;
type?: string;
options?: string[];
}[];
}
const component: ClosureComponent<Attrs> = () => {
return { return {
view: (vnode) => { view: (vnode) => {
const actionHandler = vnode.attrs.actionHandler; const actionHandler = vnode.attrs["actionHandler"];
const attributes = vnode.attrs.attributes; const attributes = vnode.attrs["attributes"];
const resource = vnode.attrs.resource; const resource = vnode.attrs["resource"];
const base = vnode.attrs.base || {}; const base = vnode.attrs["base"] || {};
if (!vnode.state["current"]) { if (!vnode.state["current"]) {
vnode.state["current"] = { vnode.state["current"] = {
isNew: !base["_id"], isNew: !base["_id"],
@@ -238,7 +226,7 @@ const component: ClosureComponent<Attrs> = () => {
onclick: (e) => { onclick: (e) => {
e.redraw = false; e.redraw = false;
e.target.disabled = true; e.target.disabled = true;
actionHandler("delete", current.object).finally(() => { actionHandler("delete", current.object).then(() => {
e.target.disabled = false; e.target.disabled = false;
}); });
}, },
@@ -267,7 +255,7 @@ const component: ClosureComponent<Attrs> = () => {
// e.target.onsubmit = null; // e.target.onsubmit = null;
(submit.dom as HTMLFormElement).disabled = true; (submit.dom as HTMLFormElement).disabled = true;
// submit.dom.textContent = "Loading ..."; // submit.dom.textContent = "Loading ...";
actionHandler("save", current.object).finally(() => { actionHandler("save", current.object).then(() => {
// submit.dom.textContent = "Save"; // submit.dom.textContent = "Save";
// e.target.onsubmit = onsubmit; // e.target.onsubmit = onsubmit;
(submit.dom as HTMLFormElement).disabled = false; (submit.dom as HTMLFormElement).disabled = false;
+1 -1
View File
@@ -145,7 +145,7 @@ function queryTag(tag: string): Expression {
export function getTip(resource: string, label: string): string { export function getTip(resource: string, label: string): string {
let tip; let tip;
if (resources[resource]?.[label]) { if (resources[resource] && resources[resource][label]) {
const param = resources[resource][label]; const param = resources[resource][label];
const types = const types =
resource === "devices" ? param["type"] : param["type"].split(","); resource === "devices" ? param["type"] : param["type"].split(",");
+3 -2
View File
@@ -609,8 +609,9 @@ export function postTasks(
if (xhr.status === 403) throw new Error("Not authorized"); if (xhr.status === 403) throw new Error("Not authorized");
if (!xhr.status) throw new Error("Server is unreachable"); if (!xhr.status) throw new Error("Server is unreachable");
if (xhr.status !== 200) throw new Error(xhr.response); if (xhr.status !== 200) throw new Error(xhr.response);
const connectionRequestStatus = const connectionRequestStatus = xhr.getResponseHeader(
xhr.getResponseHeader("Connection-Request"); "Connection-Request"
);
const st = JSON.parse(xhr.response); const st = JSON.parse(xhr.response);
for (const [i, t] of st.entries()) { for (const [i, t] of st.entries()) {
tasks[i]._id = t._id; tasks[i]._id = t._id;
+8 -16
View File
@@ -17,7 +17,7 @@
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>. * along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
*/ */
import { ClosureComponent } from "mithril"; import { ClosureComponent, Component } from "mithril";
import { m } from "./components"; import { m } from "./components";
import * as store from "./store"; import * as store from "./store";
import { yaml } from "./dynamic-loader"; import { yaml } from "./dynamic-loader";
@@ -65,7 +65,7 @@ function putActionHandler(prefix: string[], dataYaml: string): Promise<any> {
store.putResource( store.putResource(
"config", "config",
obj._id, obj._id,
obj as unknown as Record<string, unknown> (obj as unknown) as Record<string, unknown>
) )
); );
} }
@@ -86,20 +86,12 @@ function putActionHandler(prefix: string[], dataYaml: string): Promise<any> {
}); });
} }
interface Attrs { const component: ClosureComponent = (): Component => {
prefix: string;
name: string;
data: { _id: string; value: string }[];
onUpdate: (errs: Record<string, string>) => void;
onError: (err: Error) => void;
}
const component: ClosureComponent<Attrs> = () => {
return { return {
view: (vnode) => { view: (vnode) => {
const prefix = vnode.attrs.prefix.split("."); const prefix = vnode.attrs["prefix"].split(".");
const name = vnode.attrs.name; const name = vnode.attrs["name"];
const data = vnode.attrs.data; const data = vnode.attrs["data"];
if (prefix[prefix.length - 1] === "") prefix.pop(); if (prefix[prefix.length - 1] === "") prefix.pop();
@@ -143,8 +135,8 @@ const component: ClosureComponent<Attrs> = () => {
vnode.state["updatedYaml"] = yamlString; vnode.state["updatedYaml"] = yamlString;
putActionHandler(prefix, vnode.state["updatedYaml"]) putActionHandler(prefix, vnode.state["updatedYaml"])
.then(vnode.attrs.onUpdate) .then(vnode.attrs["onUpdate"])
.catch(vnode.attrs.onError); .catch(vnode.attrs["onError"]);
}, },
}, },
[code, m(".actions-bar", [submit])] [code, m(".actions-bar", [submit])]
+4 -5
View File
@@ -401,11 +401,10 @@ export const component: ClosureComponent = (): Component => {
}; };
} }
const filterAttrs = { const filterAttrs = {};
resource: "users", filterAttrs["resource"] = "users";
filter: vnode.attrs["filter"], filterAttrs["filter"] = vnode.attrs["filter"];
onChange: onFilterChanged, filterAttrs["onChange"] = onFilterChanged;
};
return [ return [
m("h1", "Listing users"), m("h1", "Listing users"),
+4 -5
View File
@@ -346,11 +346,10 @@ export const component: ClosureComponent = (): Component => {
}; };
} }
const filterAttrs = { const filterAttrs = {};
resource: "virtualParameters", filterAttrs["resource"] = "virtualParameters";
filter: vnode.attrs["filter"], filterAttrs["filter"] = vnode.attrs["filter"];
onChange: onFilterChanged, filterAttrs["onChange"] = onFilterChanged;
};
return [ return [
m("h1", "Listing virtual parameters"), m("h1", "Listing virtual parameters"),