Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x | /** * HTTPS server * @module Https * @category IO * @internal */ import * as https from "https"; import { parse } from "url"; import { IncomingMessage, ServerResponse, OutgoingHttpHeaders } from "http"; import { WebSockets } from "./Wss"; import { Socket } from "net"; import { Files } from "./Files"; const files = new Files(); import { FixedSizeArray } from "../../Utility/DataStructures"; const mimiTypes = { txt: "text/text", html: "text/html", css: "text/css", js: "application/javascript", ico: "image/x-icon", png: "image/png", svg: "image/svg+xml", }; /** * Class representing the HTTPS server * */ export class Https { /** * The HTTPS server */ private server: https.Server | undefined; private wssPaths = new Map<string | null, WebSockets>(); private requestLog = new FixedSizeArray<OutgoingHttpHeaders>(10); private errorLog = new FixedSizeArray<Error>(10); /** * Creates a new instance of the HTTPS server */ constructor() {} /** * Starts the HTTPS server * * @throws {Error} If an error occurs while starting the server */ public async start() { try { this.server = https.createServer({ key: await files.returnFile("./config/key.pem"), cert: await files.returnFile("./config/cert.pem"), }); this.server.on("request", this.handleRequest.bind(this)); this.server.on("upgrade", this.handleUpgrade.bind(this)); this.server.on("error", this.handleError.bind(this)); this.server.listen(443); } catch (error) { throw error; } } /** * Stops the HTTPS server * * @throws {Error} If an error occurs while starting the server */ public async stop() { Eif (this.server !== undefined) { this.server.close(); } } /** * Handles incoming requests from listening server * */ private handleRequest(request: IncomingMessage, response: ServerResponse) { this.requestLog.add(request.headers); const url = parse(request.url || ""); const pathname = url.pathname || ""; if (pathname.startsWith("/api/")) { let result = "<pre>"; this.requestLog.forEach((entry) => { result += `${JSON.stringify(entry, null, 2)}\n`; }); this.errorLog.forEach((entry) => { result += `${JSON.stringify(entry, null, 2)}\n`; }); result += "</pre>"; this.sendContent(response, mimiTypes.html, result); } else { let result = "FORBIDDEN"; this.sendContent(response, mimiTypes.html, result); } } /** * Handles incoming Wss Upgrade requests from listening server * */ private handleUpgrade( request: IncomingMessage, socket: Socket, head: Buffer ) { const { pathname } = parse(request.url || ""); const webSockets = this.wssPaths.get(pathname); if (webSockets) { webSockets.handleUpgrade(request, socket, head); } else E{ socket.destroy(); } } /** * Handles incoming requests from listening server * */ private handleError(error: Error) { this.errorLog.add(error); console.error(error); } /** * Sends content in the response. * @param response - The response to send the content to. * @param contentType - The type of content being sent (e.g. "text/html"). * @param contentData - The data of the content being sent. */ private sendContent( response: ServerResponse, contentType: string, contentData: string ) { let content_length = contentData.length; response.writeHead(200, { "Content-Length": content_length, "Content-Type": contentType, }); response.end(contentData); } /** * Adds a new WebSocket server for a specific path * @param {WebSockets} webSockets - The WebSockets instance to add * @param {string} path - The path for which the WebSockets instance will be added */ public addWebSocketServer(webSockets: WebSockets, path: string) { this.wssPaths.set(path, webSockets); } /** * Returns a boolean indicating whether the required files for the HTTPS server are ready * * @returns {Promise<boolean>} True */ async isReady() { return true; } } |