edge-core-js
Version:
Edge account & wallet management library
36 lines (27 loc) • 946 B
JavaScript
// @flow
import { Buffer } from "buffer";
import http from "http";
import { createProxyServer } from "http-proxy";
const proxy = createProxyServer({});
// Rust WebDAV server running on port 3000
const WEBDAV_TARGET = "http://localhost:3000";
const OTHER_TARGET = "http://localhost:4000";
const AUTH_USER = "william";
const AUTH_PASS = "hunter2";
const mainServer = http.createServer((req, res) => {
if (isAuthorized(req)) {
proxy.web(req, res, { target: WEBDAV_TARGET });
} else {
proxy.web(req, res, { target: OTHER_TARGET });
}
});
mainServer.listen(8080, () => {
console.log("Node proxy running on port 8080");
});
function isAuthorized(req) {
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith("Basic ")) return false;
const encoded = authHeader.slice(6);
const decoded = Buffer.from(encoded, "base64").toString("utf8");
return decoded === `${AUTH_USER}:${AUTH_PASS}`;
}