Skip to content

Commit

Permalink
chore(desktop): add windows code signing server config (#1942)
Browse files Browse the repository at this point in the history
* chore(desktop): add windows code signing server config

* fix(desktop): fix windows build
  • Loading branch information
hyrious authored May 24, 2023
1 parent af2dc62 commit 61e0a25
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
1 change: 1 addition & 0 deletions config/CN/.env.development
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ APPLE_API_KEY=

WINDOWS_CODE_SIGNING_CA_PATH=
WINDOWS_CODE_SIGNING_CA_PASSWORD=
WINDOWS_CODE_SIGNING_SERVER=

ARTIFACTS_ALIBABA_CLOUD_OSS_BUCKET=
ARTIFACTS_ALIBABA_CLOUD_OSS_REGION=
Expand Down
1 change: 1 addition & 0 deletions config/CN/.env.production
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ APPLE_API_KEY=

WINDOWS_CODE_SIGNING_CA_PATH=
WINDOWS_CODE_SIGNING_CA_PASSWORD=
WINDOWS_CODE_SIGNING_SERVER=

ARTIFACTS_ALIBABA_CLOUD_OSS_BUCKET=
ARTIFACTS_ALIBABA_CLOUD_OSS_REGION=
Expand Down
8 changes: 7 additions & 1 deletion desktop/main-app/scripts/pack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ const buildElectron = async () => {
}

if (buildType === "win") {
if (
if (process.env.WINDOWS_CODE_SIGNING_SERVER) {
config.win = {
...config.win,
sign: "./scripts/pack/sign.js",
signDlls: true,
};
} else if (
process.env.WINDOWS_CODE_SIGNING_CA_PATH &&
process.env.WINDOWS_CODE_SIGNING_CA_PASSWORD
) {
Expand Down
55 changes: 55 additions & 0 deletions desktop/main-app/scripts/pack/sign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const fs = require("fs");
const crypto = require("crypto");
const { basename } = require("path");

const API = process.env.WINDOWS_CODE_SIGNING_SERVER;
if (!API) {
throw new Error('please set process.env.SIGN_SERVER before signing');
}

/** @type {import('app-builder-lib').CustomWindowsSign} */
module.exports = async function sign({ path, hash, isNest }) {
let resp;

const fileHash = await computeHash(path);
resp = await fetch(`${API}/exists`, { method: "POST", body: fileHash });
if (!resp.ok) {
throw new Error(await resp.text());
}

const exist = await resp.json();
const body = new FormData();
if (exist) {
body.append("file", fileHash);
} else {
body.append("file", await fileAsBlob(path), basename(path));
}
body.append("hash", hash);
body.append("isNest", isNest ? "1" : "");

resp = await fetch(`${API}/sign`, { method: "POST", body });

if (!resp.ok) {
throw new Error(await resp.text());
}

const arrayBuffer = await resp.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
await fs.promises.writeFile(path, buffer);
};

async function fileAsBlob(path) {
const buffer = await fs.promises.readFile(path);
return new Blob([buffer]);
}

function computeHash(path) {
return new Promise(resolve => {
const hash = crypto.createHash("md5");
const input = fs.createReadStream(path);
input.on("readable", () => {
const data = input.read();
data ? hash.update(data) : resolve(hash.digest("hex"));
});
});
}
6 changes: 5 additions & 1 deletion desktop/main-app/src/utils/ipc-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ const windowActionAsync = (customWindow: CustomWindow): ipc.WindowActionAsync =>
}

window.setSize(args.width, args.height);
window.setTrafficLightPosition(args.trafficLightPosition || { x: 5, y: 12 });

// There's no such method on Windows.
if (window.setTrafficLightPosition) {
window.setTrafficLightPosition(args.trafficLightPosition || { x: 5, y: 12 });
}

if (args.autoCenter) {
window.center();
Expand Down

0 comments on commit 61e0a25

Please sign in to comment.