Skip to content

Instantly share code, notes, and snippets.

@sploders101
Last active August 6, 2021 13:58
Show Gist options
  • Save sploders101/23719534c683216baf76b816098e614b to your computer and use it in GitHub Desktop.
Save sploders101/23719534c683216baf76b816098e614b to your computer and use it in GitHub Desktop.
Example update proxy for electron
import http from "http";
import https from "https";
import {
AddressInfo,
} from "net";
import { URL } from "url";
const updateServer = "https://updates.yourserver.com/path/to/update/dir/without/trailing/slash";
const updateAuth = `Basic ${Buffer.from("username:password").toString("base64")}`
export function startUpdateProxy() {
return new Promise<{ url: string; server: http.Server}>((res, rej) => {
const server = http.createServer((request, response) => {
const requestURL = new URL(updateServer + request.url);
const endReq = https.request(requestURL, {
headers: {
...request.headers,
Authorization: updateAuth,
Host: requestURL.host,
},
}, (endRes) => {
response.writeHead(endRes.statusCode || 500, endRes.headers);
endRes.pipe(response);
endRes.on("error", (err) => response.destroy(err));
});
request.pipe(endReq);
endReq.on("error", (err) => response.writeHead(500, `${String(err)} when connecting to ${updateServer + request.url}`).end());
});
server.listen({
host: "127.0.0.1",
port: 0,
});
server.on("listening", () => {
res({
url: `http://127.0.0.1:${(server.address() as AddressInfo).port}`,
server,
});
});
server.on("error", rej);
});
}
@sploders101
Copy link
Author

This code was inspired by this issue thread, and partly by the specific issue linked because I still wanted delta updates and didn't want to have to write my own manifest parser. Unfortunately, these requirements complicate matters quite a bit, so why not let Squirrel do the heavy lifting (that's what it was made for anyways, wasn't it?) and just intercept requests?

electron/electron#5020 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment