UNPKG

@ajejoseph22/proxx

Version:

A lightweight HTTPS/HTTP proxy server with bandwidth tracking, basic auth and real-time analytics.

92 lines (91 loc) 3.93 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AuthService = void 0; const bcrypt_1 = __importDefault(require("bcrypt")); class AuthService { constructor(dbService) { this.statusCode = { UNAUTHORIZED: 401, PROXY_AUTH_REQUIRED: 407, FORBIDDEN: 403, OK: 200, }; this.databaseService = dbService; } addUser(username, password) { return __awaiter(this, void 0, void 0, function* () { const salt = yield bcrypt_1.default.genSalt(10); const auth = this.databaseService.getData().auth; auth[username] = { passwordHash: yield bcrypt_1.default.hash(password, salt), }; yield this.databaseService.save({ auth }); }); } isAuthenticated(authHeader) { return __awaiter(this, void 0, void 0, function* () { var _a; const [method, credentials] = authHeader.split(" "); if (method !== "Basic") return false; const [username, password] = Buffer.from(credentials, "base64") .toString() .split(":"); const user = (_a = this.databaseService.getData().auth) === null || _a === void 0 ? void 0 : _a[username]; if (!user) return false; return bcrypt_1.default.compare(password, user.passwordHash); }); } handleAuth(authHeader, isProxyAuth) { return __awaiter(this, void 0, void 0, function* () { if (!authHeader) { return { isAuthenticated: false, code: isProxyAuth ? this.statusCode.PROXY_AUTH_REQUIRED : this.statusCode.UNAUTHORIZED, message: "Authentication Required\r\n\r\n", }; } const isAuthenticated = yield this.isAuthenticated(authHeader); return { isAuthenticated, code: isAuthenticated ? this.statusCode.OK : this.statusCode.FORBIDDEN, message: `${isAuthenticated ? "OK" : "Invalid credentials"}\r\n\r\n}`, }; }); } initialize() { return __awaiter(this, void 0, void 0, function* () { if (process.env.ADMIN_USERNAME && process.env.ADMIN_PASSWORD) { yield this.addUser(process.env.ADMIN_USERNAME, process.env.ADMIN_PASSWORD); } }); } proxyAuth(req, res) { return __awaiter(this, void 0, void 0, function* () { res === null || res === void 0 ? void 0 : res.setHeader("Proxy-Authenticate", "Basic"); return this.handleAuth(req.headers["proxy-authorization"], true); }); } endpointAuth(req, res) { return __awaiter(this, void 0, void 0, function* () { res.setHeader("WWW-Authenticate", "Basic"); return this.handleAuth(req.headers["authorization"], false); }); } } exports.AuthService = AuthService;