2021-08-03 22:02:59 +03:00
|
|
|
//
|
|
|
|
// Copyright © 2020, 2021 Anticrm Platform Contributors.
|
|
|
|
// Copyright © 2021 Hardcore Engineering Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License. You may
|
|
|
|
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
//
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
2021-08-04 19:17:01 +03:00
|
|
|
import { readRequest, serialize, Response } from '@anticrm/platform'
|
2021-08-03 22:02:59 +03:00
|
|
|
import { createServer, IncomingMessage } from 'http'
|
|
|
|
import WebSocket, { Server } from 'ws'
|
|
|
|
import { decode } from 'jwt-simple'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export interface _Token {
|
|
|
|
workspace: string
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handleRequest<S> (service: S, ws: WebSocket, msg: string): Promise<void> {
|
|
|
|
const request = readRequest(msg)
|
|
|
|
const f = (service as any)[request.method]
|
|
|
|
const result = await f.apply(null, request.params)
|
|
|
|
ws.send(serialize({
|
|
|
|
id: request.id,
|
|
|
|
result
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
2021-08-04 19:17:01 +03:00
|
|
|
*/
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
|
|
export interface Session {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface JsonRpcServer {
|
|
|
|
broadcast: (from: Session, resp: Response<any>) => void
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* @param sessionFactory -
|
2021-08-04 12:10:22 +03:00
|
|
|
* @param port -
|
|
|
|
* @param host -
|
2021-08-03 22:02:59 +03:00
|
|
|
*/
|
2021-08-04 19:17:01 +03:00
|
|
|
export function start (sessionFactory: (server: JsonRpcServer) => Session, port: number, host?: string): void {
|
2021-08-04 23:47:15 +03:00
|
|
|
console.log(`starting server on port ${port} ...`)
|
2021-08-03 22:02:59 +03:00
|
|
|
|
2021-08-04 19:17:01 +03:00
|
|
|
const sessions: [Session, WebSocket][] = []
|
|
|
|
|
|
|
|
const jsonServer: JsonRpcServer = {
|
|
|
|
broadcast (from: Session, resp: Response<[]>) {
|
|
|
|
const msg = serialize(resp)
|
|
|
|
for (const session of sessions) {
|
|
|
|
if (session[0] !== from) { session[1].send(msg) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-03 22:02:59 +03:00
|
|
|
|
|
|
|
const wss = new Server({ noServer: true })
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
2021-08-04 19:17:01 +03:00
|
|
|
wss.on('connection', (ws: WebSocket, request: any, token: _Token) => {
|
|
|
|
const service = sessionFactory(jsonServer)
|
|
|
|
sessions.push([service, ws])
|
2021-08-03 22:02:59 +03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
|
|
ws.on('message', async (msg: string) => await handleRequest(service, ws, msg))
|
|
|
|
})
|
|
|
|
|
|
|
|
const server = createServer()
|
2021-08-07 08:58:28 +03:00
|
|
|
server.on('upgrade', (request: IncomingMessage, socket: any, head: Buffer) => {
|
2021-08-03 22:02:59 +03:00
|
|
|
const token = request.url?.substring(1) // remove leading '/'
|
2021-08-04 10:56:34 +03:00
|
|
|
try {
|
|
|
|
const payload = decode(token ?? '', 'secret', false)
|
|
|
|
wss.handleUpgrade(request, socket, head, ws => wss.emit('connection', ws, request, payload))
|
|
|
|
} catch (err) {
|
2021-08-04 23:47:15 +03:00
|
|
|
console.log('unauthorized')
|
2021-08-03 22:02:59 +03:00
|
|
|
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n')
|
|
|
|
socket.destroy()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
server.listen(port, host)
|
|
|
|
}
|