Refactor server implementation for WebSockets

Summary:
Standardize WS implementation for JS environments.

Why do we need a separate server implementation for browsers?
Browser targets cannot authenticate via the default certificate exchange flow. For browser targets we verify the origin instead.
Moreover, for already forgotten reasons the initial implementation of the WS server for browsers used a different kind of message structure and added extra `connect`/`disconnect` messages. After examination, it seems the `connect`/`disconnect` flow is redundant.

Major changes:
1. Updated class hierarchy for WS server implementations.
2. Updated browser WS server to support the modern and the legacy protocols.
3. Now a websocket connection with the device is closed on error. The idea is it is highly unlikely to handle any subsequent messages properly once we observe an error. It is better to bail and reconnect. What do you think?

Reviewed By: mweststrate

Differential Revision: D31532172

fbshipit-source-id: f86aa63a40efe4d5263353cc124fac8c63b80e45
This commit is contained in:
Andrey Goncharov
2021-10-21 03:30:41 -07:00
committed by Facebook GitHub Bot
parent 6bd4a07286
commit 37498ad5a9
24 changed files with 1584 additions and 725 deletions

View File

@@ -25,7 +25,7 @@ export type FlipperServerState =
export type DeviceType = PluginDeviceType;
export type DeviceOS = PluginOS | 'Windows' | 'MacOS';
export type DeviceOS = PluginOS | 'Windows' | 'MacOS' | 'Browser' | 'Linux';
export type DeviceDescription = {
readonly os: DeviceOS;
@@ -83,11 +83,13 @@ export type ClientErrorType = {
name: string;
};
export type ClientResponseType = {
success?: Object;
error?: ClientErrorType;
length: number;
};
export type ClientResponseType =
| {
success: object | string | number | boolean | null;
error?: never;
length: number;
}
| {success?: never; error: ClientErrorType; length: number};
export type FlipperServerEvents = {
'server-state': {state: FlipperServerState; error?: Error};
@@ -258,3 +260,38 @@ export type MetroReportableEvent =
| 'debug';
data: Array<any>;
};
// TODO: Complete message list
export type SignCertificateMessage = {
method: 'signCertificate';
csr: string;
destination: string;
medium: number | undefined;
};
export type GetPluginsMessage = {
id: number;
method: 'getPlugins';
};
export type GetBackgroundPluginsMessage = {
id: number;
method: 'getBackgroundPlugins';
};
export type ExecuteMessage = {
method: 'execute';
params: {
method: string;
api: string;
params?: unknown;
};
};
export type ResponseMessage =
| {
id: number;
success: object | string | number | boolean | null;
error?: never;
}
| {
id: number;
success?: never;
error: ClientErrorType;
};