Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(desktop): add windows code signing server config #1942

Merged
merged 2 commits into from
May 24, 2023
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
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