1 import { type IncomingMessage
, Server
, type ServerResponse
} from
'node:http'
2 import { type Http2Server
, createServer
} from
'node:http2'
4 import type { WebSocket
} from
'ws'
6 import type { AbstractUIService
} from
'./ui-services/AbstractUIService.js'
7 import { UIServiceFactory
} from
'./ui-services/UIServiceFactory.js'
8 import { getUsernameAndPasswordFromAuthorizationToken
} from
'./UIServerUtils.js'
9 import { BaseError
} from
'../../exception/index.js'
11 ApplicationProtocolVersion
,
13 type ChargingStationData
,
16 type ProtocolResponse
,
20 type UIServerConfiguration
21 } from
'../../types/index.js'
22 import { logger
} from
'../../utils/index.js'
24 const moduleName
= 'AbstractUIServer'
26 export abstract class AbstractUIServer
{
27 public readonly chargingStations
: Map
<string, ChargingStationData
>
28 public readonly chargingStationTemplates
: Set
<string>
29 protected readonly httpServer
: Server
| Http2Server
30 protected readonly responseHandlers
: Map
<string, ServerResponse
| WebSocket
>
31 protected readonly uiServices
: Map
<ProtocolVersion
, AbstractUIService
>
33 public constructor (protected readonly uiServerConfiguration
: UIServerConfiguration
) {
34 this.chargingStations
= new Map
<string, ChargingStationData
>()
35 this.chargingStationTemplates
= new Set
<string>()
36 switch (this.uiServerConfiguration
.version
) {
37 case ApplicationProtocolVersion
.VERSION_11
:
38 this.httpServer
= new Server()
40 case ApplicationProtocolVersion
.VERSION_20
:
41 this.httpServer
= createServer()
45 `Unsupported application protocol version ${this.uiServerConfiguration.version}`
48 this.responseHandlers
= new Map
<string, ServerResponse
| WebSocket
>()
49 this.uiServices
= new Map
<ProtocolVersion
, AbstractUIService
>()
52 public buildProtocolRequest (
54 procedureName
: ProcedureName
,
55 requestPayload
: RequestPayload
57 return [id
, procedureName
, requestPayload
]
60 public buildProtocolResponse (id
: string, responsePayload
: ResponsePayload
): ProtocolResponse
{
61 return [id
, responsePayload
]
64 public stop (): void {
66 for (const uiService
of this.uiServices
.values()) {
72 public clearCaches (): void {
73 this.chargingStations
.clear()
74 this.chargingStationTemplates
.clear()
77 public async sendInternalRequest (request
: ProtocolRequest
): Promise
<ProtocolResponse
> {
78 const protocolVersion
= ProtocolVersion
['0.0.1']
79 this.registerProtocolVersionUIService(protocolVersion
)
80 return await (this.uiServices
82 ?.requestHandler(request
) as Promise
<ProtocolResponse
>)
85 public hasResponseHandler (id
: string): boolean {
86 return this.responseHandlers
.has(id
)
89 protected startHttpServer (): void {
90 this.httpServer
.on('error', error
=> {
92 `${this.logPrefix(moduleName, 'start.httpServer.on.error')} HTTP server error:`,
96 if (!this.httpServer
.listening
) {
97 this.httpServer
.listen(this.uiServerConfiguration
.options
)
101 protected registerProtocolVersionUIService (version
: ProtocolVersion
): void {
102 if (!this.uiServices
.has(version
)) {
103 this.uiServices
.set(version
, UIServiceFactory
.getUIServiceImplementation(version
, this))
107 protected authenticate (req
: IncomingMessage
, next
: (err
?: Error) => void): void {
108 const authorizationError
= new BaseError('Unauthorized')
109 if (this.isBasicAuthEnabled()) {
110 if (!this.isValidBasicAuth(req
, next
)) {
111 next(authorizationError
)
114 } else if (this.isProtocolBasicAuthEnabled()) {
115 if (!this.isValidProtocolBasicAuth(req
, next
)) {
116 next(authorizationError
)
119 } else if (this.uiServerConfiguration
.authentication
?.enabled
=== true) {
120 next(authorizationError
)
125 private stopHttpServer (): void {
126 if (this.httpServer
.listening
) {
127 this.httpServer
.close()
128 this.httpServer
.removeAllListeners()
132 private isBasicAuthEnabled (): boolean {
134 this.uiServerConfiguration
.authentication
?.enabled
=== true &&
135 this.uiServerConfiguration
.authentication
.type === AuthenticationType
.BASIC_AUTH
139 private isProtocolBasicAuthEnabled (): boolean {
141 this.uiServerConfiguration
.authentication
?.enabled
=== true &&
142 this.uiServerConfiguration
.authentication
.type === AuthenticationType
.PROTOCOL_BASIC_AUTH
146 private isValidBasicAuth (req
: IncomingMessage
, next
: (err
?: Error) => void): boolean {
147 const [username
, password
] = getUsernameAndPasswordFromAuthorizationToken(
148 req
.headers
.authorization
?.split(/\s
+/).pop() ?? '',
151 return this.isValidUsernameAndPassword(username
, password
)
154 private isValidProtocolBasicAuth (req
: IncomingMessage
, next
: (err
?: Error) => void): boolean {
155 const authorizationProtocol
= req
.headers
['sec-websocket-protocol']?.split(/,\s
+/).pop()
156 const [username
, password
] = getUsernameAndPasswordFromAuthorizationToken(
157 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
158 `${authorizationProtocol}${Array(((4 - (authorizationProtocol!.length % 4)) % 4) + 1).join('=')}`
163 return this.isValidUsernameAndPassword(username
, password
)
166 private isValidUsernameAndPassword (username
: string, password
: string): boolean {
168 this.uiServerConfiguration
.authentication
?.username
=== username
&&
169 this.uiServerConfiguration
.authentication
.password
=== password
173 public abstract start (): void
174 public abstract sendRequest (request
: ProtocolRequest
): void
175 public abstract sendResponse (response
: ProtocolResponse
): void
176 public abstract logPrefix (moduleName
?: string, methodName
?: string, prefixSuffix
?: string): string