Skip to content

Commit

Permalink
Update ECC Library and packer.
Browse files Browse the repository at this point in the history
  • Loading branch information
h5mcbox committed Apr 7, 2024
1 parent 539312b commit d28c10b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 45 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ jobs:
run: |
cd /opt/
wget https://nodejs.org/dist/v16.16.0/node-v16.16.0-linux-x64.tar.xz
ls -l
tar -Jxvf node-v16.16.0-linux-x64.tar.xz
ls -l
sudo ln -s /opt/node-v16.16.0-linux-x64/bin/node /usr/bin/node
sudo ln -s /opt/node-v16.16.0-linux-x64/bin/npm /usr/bin/npm
wget https://github.com/tj/node-prune/releases/download/v1.0.1/node-prune_1.0.1_linux_amd64.tar.gz
Expand Down
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const VERSION = 0;
const PACKAGEFILE = "./app.apkg";
const BACKUPPACKAGEFILE = "./app.backup.apkg";
const PUBLICKEY = "AtUOYcgW3LSjxqEOCSWGtpw8xu1YoZDMudoMBehZK76O";
const PUBLICKEY = "";
function moduleEntry(returnMethod) {
//unpacker
let unpack = (function () {
Expand Down
12 changes: 7 additions & 5 deletions core/simpleecc.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,11 @@
return BigInt("0x" + HEX);
}
function randomBuffer(l) {
const crypto = (typeof require !== "object") ? require("crypto").webcrypto : window.crypto;
let buffer = new Uint8Array(l);
return crypto.getRandomValues(buffer);
const buffer = new Uint8Array(l);
if (globalThis.crypto) globalThis.crypto.getRandomValues(buffer);
else if (typeof require === "function") require("crypto").randomFillSync(buffer);
else throw "There aren't any secure source to generate random buffer.";
return buffer;
}
function BigIntToBuffer(n = 0n) {
let l = Math.ceil(n.toString(16).length / 2);
Expand Down Expand Up @@ -273,7 +275,7 @@
return new cryptoKey("public", exportable, pointMul(entry.key, BasePoint));
}
}
function genKeyPair(exportable = true) {
function generateKeyPair(exportable = true) {
let p = generatePrivateKey(exportable);
return [p, getPublicKey(p, exportable)];
}
Expand Down Expand Up @@ -309,7 +311,7 @@
return {
generatePrivateKey,
getPublicKey,
generateKeyPair: genKeyPair,
generateKeyPair,
importKey,
cryptoKey,
ECDSA: {
Expand Down
58 changes: 21 additions & 37 deletions helper/packer/packer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { signKey, packerNoRestart } = process.env;
const { signKey, publicKey: CLIPublicKey } = process.env;
const { brotliCompressSync, constants } = require("node:zlib");
let readdir = require("./readdirRecurively");
let ECC = require("../../core/simpleecc")("secp256k1");
Expand All @@ -7,6 +7,7 @@ const toHEXString = bytes =>
bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');
const now = Math.floor(Date.now() / 1000)
let privateKey = ECC.importKey(false, Buffer.from(signKey, "base64"));
let publicKey = Buffer.from(ECC.getPublicKey(privateKey, true).exportKey()).toString("base64");
let fs = require("fs");
let path = require("path");
let Filenames = readdir(".");
Expand All @@ -17,6 +18,7 @@ function normalize(_path) {
let exceptions = [
".git",
".github",
"app.js",
"server",
"tmp_upload",
"logs",
Expand All @@ -33,24 +35,22 @@ let exceptionsEnd = [
"package-lock.json",
".md"
];
let bufs = [];
let cursor = 0;
let entries = [];
Filenames.forEach(e => {
let bufs = [], entries = [], cursor = 0;
for (let filename of Filenames) {
let skip = false;
exceptions.forEach(f => normalize(e).startsWith(f) ? skip = true : false);
exceptionsEnd.forEach(f => normalize(e).endsWith(f) ? skip = true : false);
exceptions.forEach(f => normalize(filename).startsWith(f) ? skip = true : false);
exceptionsEnd.forEach(f => normalize(filename).endsWith(f) ? skip = true : false);
if (skip) {
console.log("skip:" + e);
return false;
console.log("skip:" + filename);
continue;
}
console.log("pack:" + e);
let filebuf = fs.readFileSync(e);
let fileEntry = [e, hash(filebuf), cursor, filebuf.length];
console.log("pack:" + filename);
let filebuf = fs.readFileSync(filename);
let fileEntry = [filename, hash(filebuf), cursor, filebuf.length];
entries.push(fileEntry);
bufs.push(filebuf);
cursor += filebuf.length;
});
};
function addFile(filename, data, first) {
let targetFilename = normalize(filename);
let fileEntry = [targetFilename, hash(data), cursor, data.length];
Expand All @@ -64,36 +64,20 @@ function addFile(filename, data, first) {
bufs.push(data);
cursor += data.length;
}
function moveFile(_startsWith, first) {
_startsWith = normalize(_startsWith);
let foundFiles = entries.filter(e => e[0].startsWith(_startsWith));
for (let i of foundFiles) {
let index = entries.indexOf(i);
entries.splice(index, 1);
if (first) {
console.log("move to head:" + i[0]);
entries.unshift(i);
} else {
console.log("move to end:" + i[0]);
entries.push(i);
}
}
}
const AppEntry = fs.readFileSync("app.js").toString();
const AppEntryPatched = AppEntry.replaceAll("const VERSION = 0;", `const VERSION = ${now};`) //修改硬编码时间
const AppEntryPatchedSecond = Buffer.from(AppEntryPatched.replaceAll("./helper/packer/packer.js", "./app.js"));
addFile("app.js", AppEntryPatchedSecond, false); //修改入口
const AppEntryPatched = AppEntry
.replaceAll("const VERSION = 0;", `const VERSION = ${now};`) //修改硬编码时间
.replaceAll(`const PUBLICKEY = "";`, `const PUBLICKEY = "${CLIPublicKey ?? publicKey}";`) //修改公钥
.replaceAll("./helper/packer/packer.js", "./app.js") //修改入口
const AppEntryBuffer = Buffer.from(AppEntryPatched);
addFile("app.js", AppEntryBuffer, false);
let databuf = Buffer.concat(bufs);
let Header = {
version: now,
entries: entries
}
let Header = { version: now, entries: entries };
Header.sign = toHEXString(new Uint8Array(ECC.ECDSA.sign(`${Header.version}:${JSON.stringify(Header.entries)}`, privateKey)));
let headerbuf = (new TextEncoder).encode(JSON.stringify(Header));
let packagebuf = brotliCompressSync(Buffer.concat([databuf, Buffer.from("\n"), headerbuf]), { params: { [constants.BROTLI_PARAM_QUALITY]: 4 } });
fs.mkdirSync("./dist", { recursive: true });
fs.writeFileSync("./dist/app.apkg", packagebuf);
fs.writeFileSync("./dist/app.js", AppEntryPatchedSecond);
fs.writeFileSync("./dist/app.js", AppEntryBuffer);
console.log("写入完成");
if (!packerNoRestart) process.send({ restart: "./_app.js" });
process.exit();

0 comments on commit d28c10b

Please sign in to comment.