feat: add initial HTTP/2 support to ui server (mutually exclusive for now)
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / AbstractUIServer.ts
1 import { type IncomingMessage, Server, type ServerResponse } from 'node:http';
2 import { type Http2Server, createServer } from 'node:http2';
3
4 import type { WebSocket } from 'ws';
5
6 import type { AbstractUIService } from './ui-services/AbstractUIService';
7 import { UIServiceFactory } from './ui-services/UIServiceFactory';
8 import { BaseError } from '../../exception';
9 import {
10 ApplicationProtocolVersion,
11 AuthenticationType,
12 type ChargingStationData,
13 type ProcedureName,
14 type ProtocolRequest,
15 type ProtocolResponse,
16 ProtocolVersion,
17 type RequestPayload,
18 type ResponsePayload,
19 type UIServerConfiguration,
20 } from '../../types';
21
22 export abstract class AbstractUIServer {
23 public readonly chargingStations: Map<string, ChargingStationData>;
24 protected readonly httpServer: Server | Http2Server;
25 protected readonly responseHandlers: Map<string, ServerResponse | WebSocket>;
26 protected readonly uiServices: Map<ProtocolVersion, AbstractUIService>;
27
28 public constructor(protected readonly uiServerConfiguration: UIServerConfiguration) {
29 this.chargingStations = new Map<string, ChargingStationData>();
30 switch (this.uiServerConfiguration.version) {
31 case ApplicationProtocolVersion.VERSION_11:
32 this.httpServer = new Server();
33 break;
34 case ApplicationProtocolVersion.VERSION_20:
35 this.httpServer = createServer();
36 break;
37 default:
38 throw new BaseError(
39 `Unsupported application protocol version ${this.uiServerConfiguration.version}`,
40 );
41 }
42 this.responseHandlers = new Map<string, ServerResponse | WebSocket>();
43 this.uiServices = new Map<ProtocolVersion, AbstractUIService>();
44 }
45
46 public buildProtocolRequest(
47 id: string,
48 procedureName: ProcedureName,
49 requestPayload: RequestPayload,
50 ): ProtocolRequest {
51 return [id, procedureName, requestPayload];
52 }
53
54 public buildProtocolResponse(id: string, responsePayload: ResponsePayload): ProtocolResponse {
55 return [id, responsePayload];
56 }
57
58 public stop(): void {
59 this.chargingStations.clear();
60 }
61
62 public async sendInternalRequest(request: ProtocolRequest): Promise<ProtocolResponse> {
63 const protocolVersion = ProtocolVersion['0.0.1'];
64 this.registerProtocolVersionUIService(protocolVersion);
65 return this.uiServices
66 .get(protocolVersion)
67 ?.requestHandler(request) as Promise<ProtocolResponse>;
68 }
69
70 public hasResponseHandler(id: string): boolean {
71 return this.responseHandlers.has(id);
72 }
73
74 protected startHttpServer(): void {
75 if (this.httpServer.listening === false) {
76 this.httpServer.listen(this.uiServerConfiguration.options);
77 }
78 }
79
80 protected registerProtocolVersionUIService(version: ProtocolVersion): void {
81 if (this.uiServices.has(version) === false) {
82 this.uiServices.set(version, UIServiceFactory.getUIServiceImplementation(version, this));
83 }
84 }
85
86 protected authenticate(req: IncomingMessage, next: (err?: Error) => void): void {
87 if (this.isBasicAuthEnabled() === true) {
88 if (this.isValidBasicAuth(req) === false) {
89 next(new BaseError('Unauthorized'));
90 }
91 next();
92 }
93 next();
94 }
95
96 private isBasicAuthEnabled(): boolean {
97 return (
98 this.uiServerConfiguration.authentication?.enabled === true &&
99 this.uiServerConfiguration.authentication?.type === AuthenticationType.BASIC_AUTH
100 );
101 }
102
103 private isValidBasicAuth(req: IncomingMessage): boolean {
104 const authorizationHeader = req.headers.authorization ?? '';
105 const authorizationToken = authorizationHeader.split(/\s+/).pop() ?? '';
106 const authentication = Buffer.from(authorizationToken, 'base64').toString();
107 const authenticationParts = authentication.split(/:/);
108 const username = authenticationParts.shift();
109 const password = authenticationParts.join(':');
110 return (
111 this.uiServerConfiguration.authentication?.username === username &&
112 this.uiServerConfiguration.authentication?.password === password
113 );
114 }
115
116 public abstract start(): void;
117 public abstract sendRequest(request: ProtocolRequest): void;
118 public abstract sendResponse(response: ProtocolResponse): void;
119 public abstract logPrefix(
120 moduleName?: string,
121 methodName?: string,
122 prefixSuffix?: string,
123 ): string;
124 }