-
-
Save ezekg/b7e9144ce9107d7ea64fdab571c87e12 to your computer and use it in GitHub Desktop.
Example update proxy for electron
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment