// Bambu Connect extracted main.js by https://www.reddit.com/user/hWuxH
async function are(t) {
const {
encAppKey: e,
aes256: r
} = process.report.certManager.encryptAppKey();
const n = new URL(t.url.replace(jL, e));
n.searchParams.set("aes256", r);
return new Request(n, t);
}
async function cre(t) {
if (t.status !== 200) {
return t;
}
const e = await t.json();
process.report.certManager.updateAppCert(e);
return new Response(JSON.stringify({
code: e.code
}), {
headers: t.headers
});
}
async function lre(t) {
t.headers.set("x-bbl-app-certification-id", process.report.certManager.getSignCertId());
t.headers.set("x-bbl-device-security-sign", process.report.certManager.privateEncrypt(`${new Date().getTime()}`));
return t;
}
const Are = 'GLOF3813734089-524a37c80000c6a6a274a47b3281'
const fre = '-----BEGIN CERTIFICATE-----\nMIIDeTCCAmGgAwIBAgIUGpX26HEycYnaaCUFwJr8F/UPCZIwDQYJKoZIhvcNAQEL\nBQAwKDEmMCQGA1UEAwwdYXBwbGljYXRpb25fcm9vdC5iYW1idWxhYi5jb20wHhcN\nMjQwNzI2MDE1MjI3WhcNMzQwNzI0MDE1MjI3WjAfMR0wGwYDVQQDDBRzZXJ2aWNl\nLmJhbWJ1bGFiLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAJ2z\nwQvNAjA9lQe06o/Vv6JULHLn25NK706myDa4i9lmb3wqARNVi6aSWfxO3Cz9NklC\n4dYAdFbgcEUXuJIcJBBZdgPO2GhnaeCUVWz/ucW2GCdPaBAkXZNDK56aPqfYwltI\nsNJcUIvq0OmbFsI90l8zKTQzd8/zJcfm8lOBpAberwbEeHCQfDxUWpNyrAuGJlkD\ncmxraAjzBXQjAyMT+kK+KPOqhJICu5SXP87WGlzw8sfrdLl7J2vhtXpzRC67vtv6\nuD6aqq5f2OaRxHRUyqeDY/adiWE9Qeb9amkRMcQiUJ0IQJZ4wDSMNpZe0guXLkl5\nLPdPU249Bu86H5eDf5ECAwEAAaOBozCBoDAJBgNVHRMEAjAAMAsGA1UdDwQEAwID\nuDAdBgNVHQ4EFgQ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46fkuM\nkJ7UeO17FddDtfCDqxIvheo/RPvZPoiNuCpUQuGAI59O3kFqNkv6VsZlk+7E/D1Q\naSiKr+bk6+hWslSLtenA4rxZNcL8cq7AYijLPlE2HTN6ASCMx/bBZXzm28KHDyeR\nFtfnJsmWXBbeOqHmR9/JpSbJdXRD6jvXF2nQVgQcAqv3DZcOhov0ah+31foAe2/e\naRANWl5wMJ5nUd5UFCk=\n-----END X509 CRL-----\n']
};
class dre {
constructor() {
Ee(this, "store");
Ee(this, "randomKey");
Ee(this, "mainBridge");
Ee(this, "crypto");
Ee(this, "deviceCerts", new Map());
this.crypto = process.report.sec.crypto;
this.randomKey = this.crypto.randomBytes(32);
this.mainBridge = process.report.sec.mainBridge;
const e = process.report.sec.Store;
this.store = new e({
name: "store",
defaults: {
serverCertificate: fre,
embedAppCert: j_,
appCert: j_
},
encryptionKey: 'DvyI8DTMqidJ+rH6w+wYg8LzD747kdW+bj8O8wDOczNMgHK93yOJqepuH3197qLS'
});
this.mainBridge.handle('isAppCertInstalled', r => ({
installed: r.certIds.includes(this.getCertId())
}));
}
addDeviceCert(e, r) {
this.deviceCerts.set(e, r);
}
signMessage(e, r) {
if (!this.deviceCerts.get(e)) {
return r;
}
const i = JSON.stringify(r);
const s = this.crypto.sign('RSA-SHA256', Buffer.from(i), this.store.get('appCert').privateKey).toString('base64');
return {
...r,
header: {
sign_ver: "v1.0",
sign_alg: 'RSA_SHA256',
sign_string: s,
cert_id: this.getCertId(),
payload_len: new TextEncoder().encode(i).length
}
};
}
devicePublicEncrypt(e, r) {
const n = this.deviceCerts.get(e);
if (!n) {
return r;
}
console.log({
deviceCert: n
});
const i = new this.crypto.X509Certificate(n);
return this.crypto.publicEncrypt({
key: i.publicKey,
padding: this.crypto.constants.RSA_PKCS1_PADDING
}, r).toString("base64");
}
updateAppCert(e) {
const r = e.key;
const n = Buffer.from(r, "base64");
const i = n.subarray(0, 12);
const s = n.subarray(-16);
const o = n.subarray(12, -16);
const l = this.crypto.createDecipheriv('aes-256-gcm', this.randomKey, i);
l.setAuthTag(s);
const a = Buffer.concat([l.update(o), l.final()]);
this.store.set("appCert", {
privateKey: a.toString("utf-8"),
cert: e.cert,
crl: e.crl
});
}
privateEncrypt(e) {
return this.crypto.privateEncrypt({
key: this.store.get("appCert").privateKey
}, e).toString("base64");
}
getCert() {
return this.store.get("appCert").cert;
}
getCertId() {
const e = new this.crypto.X509Certificate(this.store.get("appCert").cert);
return `${e.serialNumber.toLowerCase()}${e.issuer}`;
}
getSignCertId() {
const e = new this.crypto.X509Certificate(this.store.get("appCert").cert);
return `${e.issuer}:${e.serialNumber.toLowerCase()}`;
}
getFirstCrl() {
return this.store.get("appCert").crl[0];
}
encryptAppKey() {
const e = this.crypto.randomBytes(12);
const r = this.crypto.createCipheriv('aes-256-gcm', this.randomKey, e);
const n = Buffer.concat([e, r.update(Are), r.final(), r.getAuthTag()]);
const i = Y_(n);
const s = new this.crypto.X509Certificate(this.store.get('serverCertificate'));
const o = this.crypto.publicEncrypt({
key: s.publicKey,
padding: this.crypto.constants.RSA_PKCS1_PADDING
}, this.randomKey);
const l = Y_(o);
return {
encAppKey: i,
aes256: l
};
}
}
function Y_(t) {
return (Buffer.isBuffer(t) ? t : Buffer.from(t)).toString("base64").replace(/\+/g, "-").replace(/\//g, "_");
}
function hre() {
process.report.certManager = new dre();
}
function pre() {
process.report.sec = {
crypto: JN,
mainBridge: vt,
Store: UV
};
hre();
Oe.protocol.handle("https", ore);
const t = new HW();
t.connect();
const e = new JO();
e.start();
ZW();
vt.handle("openFileDialog", async () => {
const {
canceled: r,
filePaths: n
} = await Oe.dialog.showOpenDialog({});
return {
path: r ? undefined : n[0]
};
});
vt.handle("readFile", async r => ({
file: await WN.readFile(r.path)
}));
return () => {
e.destroy();
t.destroy();
};
}
function gre(t) {
Oe.Menu.setApplicationMenu(null);
if (ML) {
Oe.app.quit();
}
if (process.defaultApp) {
if (process.argv.length >= 2) {
Oe.app.setAsDefaultProtocolClient("bambu-connect", process.execPath, [fn.resolve(process.argv[1])]);
}
} else {
Oe.app.setAsDefaultProtocolClient("bambu-connect");
}
if (Oe.app.requestSingleInstanceLock()) {
Oe.app.on("second-instance", (i, s, o) => {
if (r) {
if (r.isMinimized()) {
r.restore();
}
r.focus();
const l = [...s].pop();
if (l) {
r.webContents.send(Uu, l);
}
}
});
Oe.app.on(Ny, (i, s) => {
const o = s;
if (r) {
r.show();
r.focus();
r.webContents.send(Uu, o);
} else {
n(o);
}
});
} else {
Oe.app.quit();
}
let r = null;
const n = async i => {
console.log("create window");
r = new Oe.BrowserWindow({
width: 1000,
height: 800,
minWidth: 1000,
minHeight: 800,
webPreferences: {
sandbox: false,
webSecurity: false,
preload: fn.join(__dirname, "preload.js"),
devTools: !GL
}
});
vt.connect(Oe.ipcMain, r);
const s = pre();
r.loadFile(fn.join(__dirname, "../renderer/main_window/index.html"));
r.webContents.openDevTools();
LL(r);
r.on("closed", () => {
r = null;
vt.removeAllHandlers();
vt.disconnect();
UL();
Oe.protocol.unhandle("https");
s();
});
r.webContents.once("did-finish-load", () => {
setTimeout(() => {
if (i) {
if (r != null) {
r.webContents.send(Uu, i);
}
}
}, 1000);
});
r.webContents.setWindowOpenHandler(o => {
if (r != null) {
r.loadURL(o.url);
}
return {
action: "deny"
};
});
};
Oe.app.on("ready", () => {
n();
const i = process.argv.find(s => s.startsWith("bambu-connect://"));
if (i) {
if (r != null) {
r.webContents.once("did-finish-load", () => {
setTimeout(() => {
if (r != null) {
r.webContents.send(Uu, i);
}
}, 1000);
});
}
}
});
Oe.app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
Oe.app.quit();
}
});
Oe.app.on("activate", () => {
if (Oe.BrowserWindow.getAllWindows().length === 0) {
n();
}
});
}
module.exports = gre;