Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Node.js CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x, 20.x]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm test
75 changes: 58 additions & 17 deletions package-lock.json

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

12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"description": "TPLink Router API",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "node --test"
},
"files": [
"src/*"
Expand All @@ -20,17 +20,23 @@
"api",
"gdpr",
"cgi_gdpr",
"archer",
"tl-archer",
"vr900v",
"mr6400",
"tl-mr6400",
"archer-vr900v",
"tl-archer-vr900v",
"tl-vr900v",
"tplink sms",
"sms"
],
"author": "George Kotchlamazashvili <[email protected]>",
"license": "MIT",
"dependencies": {
"undici": "5.10.0"
"undici": "5.22.1"
},
"devDependencies": {
"prettier": "^2.7.1"
"prettier": "2.8.8"
}
}
33 changes: 29 additions & 4 deletions src/client/fetchPublicKey.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,42 @@
import { fetch } from "./fetch.js";

export const _extractVariables = (js) => {
return js
.split("\n")
.map((line) => line.trim())
.filter((line) => line.length)
.reduce(
(acc, line) => {
switch (true) {
case line.startsWith("var ee="):
acc.exponent = line.slice(8, -2);
break;
case line.startsWith("var nn="):
acc.modulus = line.slice(8, -2);
break;
case line.startsWith("var seq="):
acc.sequence = line.slice(9, -2);
break;
}
return acc;
},
{ exponent: "", modulus: "", sequence: "" }
);
};

export const fetchPublicKey = async (baseUrl) => {
const res = await fetch(new URL("cgi/getParm", baseUrl).href, {
method: "POST",
headers: { Referer: new URL("/", baseUrl).href },
});

const js = await res.text();
const [eeLine, nnLine, , seqLine] = js.split("\n").map((s) => s.trim());

const { exponent, modulus, sequence } = _extractVariables(js);

return {
exponent: Buffer.from(eeLine.slice(8, -2), "hex"),
modulus: Buffer.from(nnLine.slice(8, -2), "hex"),
sequence: Number(seqLine.slice(9, -2)),
exponent: Buffer.from(exponent, "hex"),
modulus: Buffer.from(modulus, "hex"),
sequence: Number(sequence),
};
};
64 changes: 64 additions & 0 deletions test/fetchPublicKey.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import test from "node:test";
import assert from "node:assert";
import { _extractVariables } from "../src/client/fetchPublicKey.js";

test("cgi/getParm samples where all 3 required fields are present", () => {
const samples = [
[
"var userSetting=1;",
'var ee="010100";',
'var nn="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";',
'var seq="111111111";',
"$.ret=0;",
].join("\n"),
[
'var ee="010100";',
'var nn="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";',
'var seq="111111111";',
"$.ret=0;",
].join("\n"),
[
'var ee="010100";',
"var userSetting=1;",
'var seq="111111111";',
'var nn="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";',
"$.ret=0;",
].join("\n"),
[
'var ee="010100";',
'var seq="111111111";',
'var nn="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";',
].join("\n"),
[
'var ee="010100";',
'var nn="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";',
'',
'var seq="111111111";',
"$.ret=0;",
].join("\n"),
[
'var ee="010100";',
'',
'var nn="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";',
'',
'var seq="111111111";',
'',
'',
'',
'',
'',
"$.ret=0;",
].join("\n"),
];

const expected = {
exponent: "010100",
modulus:
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
sequence: "111111111",
};

for (const sample of samples) {
assert.deepStrictEqual(_extractVariables(sample), expected);
}
});