flipper-server refactor

Summary:
This changes moves most of the functionality found in flipper-server to flipper-server-core.

flipper-server will mostly be a package that wraps around flipper-server-core. Staying in flipper-server:
- Command line args
- Orchestration to start the necessary servers

Reviewed By: aigoncharov

Differential Revision: D36807087

fbshipit-source-id: f29002c7cc5d08b8c5184fdaaa02ba22562a9f45
This commit is contained in:
Lorenzo Blasa
2022-06-07 02:42:16 -07:00
committed by Facebook GitHub Bot
parent c88e769013
commit 9cc8e4076f
9 changed files with 78 additions and 53 deletions

View File

@@ -1,225 +0,0 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import chalk from 'chalk';
import {
ClientWebSocketMessage,
ExecResponseWebSocketMessage,
ExecResponseErrorWebSocketMessage,
ServerEventWebSocketMessage,
GenericWebSocketError,
UserError,
SystemError,
getLogger,
CompanionEventWebSocketMessage,
} from 'flipper-common';
import {FlipperServerImpl} from 'flipper-server-core';
import {WebSocketServer} from 'ws';
import {
FlipperServerCompanion,
FlipperServerCompanionEnv,
} from 'flipper-server-companion';
import {URLSearchParams} from 'url';
/**
* Attach and handle incoming messages from clients.
* @param flipperServer A FlipperServer instance.
* @param socket A ws socket on which to listen for events.
*/
export function attachSocketServer(
flipperServer: FlipperServerImpl,
socket: WebSocketServer,
companionEnv: FlipperServerCompanionEnv,
) {
socket.on('connection', (client, req) => {
const clientAddress =
(req.socket.remoteAddress &&
` ${req.socket.remoteAddress}:${req.socket.remotePort}`) ||
'';
console.log(chalk.green(`Client connected${clientAddress}`));
let connected = true;
let flipperServerCompanion: FlipperServerCompanion | undefined;
if (req.url) {
const params = new URLSearchParams(req.url.slice(1));
if (params.get('server_companion')) {
flipperServerCompanion = new FlipperServerCompanion(
flipperServer,
getLogger(),
companionEnv.pluginInitializer.loadedPlugins,
);
}
}
async function onServerEvent(event: string, payload: any) {
if (flipperServerCompanion) {
switch (event) {
case 'client-message': {
const client = flipperServerCompanion.getClient(payload.id);
if (!client) {
console.warn(
'flipperServerCompanion.handleClientMessage -> unknown client',
event,
payload,
);
return;
}
client.onMessage(payload.message);
return;
}
case 'client-disconnected': {
if (flipperServerCompanion.getClient(payload.id)) {
flipperServerCompanion.destroyClient(payload.id);
}
// We use "break" here instead of "return" because a flipper desktop client still might be interested in the "client-disconnect" event to update its list of active clients
break;
}
case 'device-disconnected': {
if (flipperServerCompanion.getDevice(payload.id)) {
flipperServerCompanion.destroyDevice(payload.id);
}
// We use "break" here instead of "return" because a flipper desktop client still might be interested in the "device-disconnect" event to update its list of active devices
break;
}
}
}
const message = {
event: 'server-event',
payload: {
event,
data: payload,
},
} as ServerEventWebSocketMessage;
client.send(JSON.stringify(message));
}
flipperServer.onAny(onServerEvent);
async function onServerCompanionEvent(event: string, payload: any) {
const message = {
event: 'companion-event',
payload: {
event,
data: payload,
},
} as CompanionEventWebSocketMessage;
client.send(JSON.stringify(message));
}
flipperServerCompanion?.onAny(onServerCompanionEvent);
client.on('message', (data) => {
let [event, payload]: [event: string | null, payload: any | null] = [
null,
null,
];
try {
({event, payload} = JSON.parse(
data.toString(),
) as ClientWebSocketMessage);
} catch (err) {
console.warn('flipperServer -> onMessage: failed to parse JSON', err);
const response: GenericWebSocketError = {
event: 'error',
payload: {message: `Failed to parse JSON request: ${err}`},
};
client.send(JSON.stringify(response));
return;
}
switch (event) {
case 'exec': {
const {id, command, args} = payload;
if (typeof args[Symbol.iterator] !== 'function') {
console.warn(
'flipperServer -> exec: args argument in payload is not iterable',
);
const responseError: ExecResponseErrorWebSocketMessage = {
event: 'exec-response-error',
payload: {
id,
data: 'Payload args argument is not an iterable.',
},
};
client.send(JSON.stringify(responseError));
return;
}
const execRes = flipperServerCompanion?.canHandleCommand(command)
? flipperServerCompanion.exec(command, ...args)
: flipperServer.exec(command, ...args);
execRes
.then((result: any) => {
if (connected) {
const response: ExecResponseWebSocketMessage = {
event: 'exec-response',
payload: {
id,
data: result,
},
};
client.send(JSON.stringify(response));
}
})
.catch((error: any) => {
if (error instanceof UserError) {
console.warn(
`flipper-server.startSocketServer.exec: ${error.message}`,
error.context,
error.stack,
);
}
if (error instanceof SystemError) {
console.error(
`flipper-server.startSocketServer.exec: ${error.message}`,
error.context,
error.stack,
);
}
if (connected) {
// TODO: Serialize error
// TODO: log if verbose console.warn('Failed to handle response', error);
const responseError: ExecResponseErrorWebSocketMessage = {
event: 'exec-response-error',
payload: {
id,
data:
error.toString() +
(error.stack ? `\n${error.stack}` : ''),
},
};
client.send(JSON.stringify(responseError));
}
});
}
}
});
client.on('close', () => {
console.log(chalk.red(`Client disconnected ${clientAddress}`));
connected = false;
flipperServer.offAny(onServerEvent);
flipperServerCompanion?.destroyAll();
});
client.on('error', (e) => {
console.error(chalk.red(`Socket error ${clientAddress}`), e);
connected = false;
flipperServer.offAny(onServerEvent);
flipperServerCompanion?.destroyAll();
});
});
}

View File

@@ -10,15 +10,18 @@
import process from 'process';
import chalk from 'chalk';
import path from 'path';
import {startFlipperServer} from './startFlipperServer';
import {startServer} from './startServer';
import {attachSocketServer} from './attachSocketServer';
import {attachDevServer} from './attachDevServer';
import {initializeLogger} from './logger';
import fs from 'fs-extra';
import yargs from 'yargs';
import open from 'open';
import {initCompanionEnv} from 'flipper-server-companion';
import {
attachSocketServer,
startFlipperServer,
startServer,
} from 'flipper-server-core';
import {isTest} from 'flipper-common';
const argv = yargs
.usage('yarn flipper-server [args]')
@@ -75,6 +78,25 @@ const staticDir = path.join(rootDir, 'static');
async function start() {
initializeLogger(staticDir);
let keytar: any = undefined;
try {
if (!isTest()) {
const keytarPath = path.join(
staticDir,
'native-modules',
`keytar-${process.platform}-${process.arch}.node`,
);
if (!(await fs.pathExists(keytarPath))) {
throw new Error(
`Keytar binary does not exist for platform ${process.platform}-${process.arch}`,
);
}
keytar = electronRequire(keytarPath);
}
} catch (e) {
console.error('Failed to load keytar:', e);
}
const {app, server, socket} = await startServer({
port: argv.port,
staticDir,
@@ -86,6 +108,7 @@ async function start() {
staticDir,
argv.settingsString,
argv.launcherSettings,
keytar,
);
const companionEnv = await initCompanionEnv(flipperServer);
if (argv.failFast) {

View File

@@ -1,94 +0,0 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import os from 'os';
import {
FlipperServerImpl,
getGatekeepers,
loadLauncherSettings,
loadProcessConfig,
loadSettings,
getEnvironmentInfo,
} from 'flipper-server-core';
import {parseEnvironmentVariables, isTest, getLogger} from 'flipper-common';
import path from 'path';
import fs from 'fs-extra';
/**
* Creates an instance of FlipperServer (FlipperServerImpl). This is the
* server used by clients to connect to.
* @param rootDir Application path.
* @param staticPath Static assets path.
* @param settingsString Optional settings used to override defaults.
* @param enableLauncherSettings Optional launcher settings used to override defaults.
* @returns
*/
export async function startFlipperServer(
rootDir: string,
staticPath: string,
settingsString: string,
enableLauncherSettings: boolean,
): Promise<FlipperServerImpl> {
const execPath = process.execPath;
const appPath = rootDir;
const isProduction =
process.env.NODE_ENV !== 'development' && process.env.NODE_ENV !== 'test';
const env = process.env;
let desktopPath = path.resolve(os.homedir(), 'Desktop');
// eslint-disable-next-line node/no-sync
if (!fs.existsSync(desktopPath)) {
console.warn('Failed to find desktop path, falling back to homedir');
desktopPath = os.homedir();
}
let keytar: any = undefined;
try {
if (!isTest()) {
const keytarPath = path.join(
staticPath,
'native-modules',
`keytar-${process.platform}-${process.arch}.node`,
);
if (!(await fs.pathExists(keytarPath))) {
throw new Error(
`Keytar binary does not exist for platform ${process.platform}-${process.arch}`,
);
}
keytar = electronRequire(keytarPath);
}
} catch (e) {
console.error('Failed to load keytar:', e);
}
const environmentInfo = await getEnvironmentInfo(appPath, isProduction, true);
return new FlipperServerImpl(
{
environmentInfo,
env: parseEnvironmentVariables(process.env),
// TODO: make username parameterizable
gatekeepers: getGatekeepers(environmentInfo.os.unixname),
paths: {
appPath,
homePath: os.homedir(),
execPath,
staticPath: staticPath,
tempPath: os.tmpdir(),
desktopPath: desktopPath,
},
launcherSettings: await loadLauncherSettings(enableLauncherSettings),
processConfig: loadProcessConfig(env),
settings: await loadSettings(settingsString),
validWebSocketOrigins: ['localhost:', 'http://localhost:'],
},
getLogger(),
keytar,
);
}

View File

@@ -1,219 +0,0 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import os from 'os';
import express, {Express} from 'express';
import http, {ServerResponse} from 'http';
import path from 'path';
import fs from 'fs-extra';
import {VerifyClientCallbackSync, WebSocketServer} from 'ws';
import {WEBSOCKET_MAX_MESSAGE_SIZE} from 'flipper-server-core';
import {parse} from 'url';
import {makeSocketPath, checkSocketInUse} from './utilities';
import proxy from 'http-proxy';
import exitHook from 'exit-hook';
type Config = {
port: number;
staticDir: string;
entry: string;
};
/**
* Orchestrates the creation of the HTTP server, proxy, and web socket.
* @param config Server configuration.
* @returns Returns a promise to the created server, proxy and web socket.
*/
export async function startServer(config: Config): Promise<{
app: Express;
server: http.Server;
socket: WebSocketServer;
}> {
const {app, server} = await startHTTPServer(config);
const socket = addWebsocket(server, config);
return {
app,
server,
socket,
};
}
/**
* Creates an express app with configured routing and creates
* a proxy server.
* @param config Server configuration.
* @returns A promise to both app and HTTP server.
*/
async function startHTTPServer(
config: Config,
): Promise<{app: Express; server: http.Server}> {
const app = express();
app.use((_req, res, next) => {
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
next();
});
app.get('/', (_req, res) => {
fs.readFile(path.join(config.staticDir, config.entry), (_err, content) => {
res.end(content);
});
});
app.get('/health', (_req, res) => {
res.end('flipper-ok');
});
app.use(express.static(config.staticDir));
return startProxyServer(config, app);
}
/**
* Creates and starts the HTTP and proxy servers.
* @param config Server configuration.
* @param app Express app.
* @returns Returns both the app and server configured and
* listening.
*/
async function startProxyServer(
config: Config,
app: Express,
): Promise<{app: Express; server: http.Server}> {
const server = http.createServer(app);
// For now, we only support domain socket access on POSIX-like systems.
// On Windows, a proxy is not created and the server starts
// listening at the specified port.
if (os.platform() === 'win32') {
return new Promise((resolve) => {
console.log(`Starting server on http://localhost:${config.port}`);
server.listen(config.port, undefined, () => resolve({app, server}));
});
}
const socketPath = await makeSocketPath();
if (await checkSocketInUse(socketPath)) {
console.warn(
`Cannot start flipper-server because socket ${socketPath} is in use.`,
);
} else {
console.info(`Cleaning up stale socket ${socketPath}`);
await fs.rm(socketPath, {force: true});
}
const proxyServer = proxy.createProxyServer({
target: {host: 'localhost', port: 0, socketPath},
autoRewrite: true,
ws: true,
});
console.log('Starting socket server on ', socketPath);
console.log(`Starting proxy server on http://localhost:${config.port}`);
exitHook(() => {
console.log('Cleaning up socket on exit:', socketPath);
// This *must* run synchronously and we're not blocking any UI loop by definition.
// eslint-disable-next-line node/no-sync
fs.rmSync(socketPath, {force: true});
});
proxyServer.on('error', (err, _req, res) => {
console.warn('Error in proxy server:', err);
if (res instanceof ServerResponse) {
res.writeHead(502, 'Failed to proxy request');
}
res.end('Failed to proxy request: ' + err);
});
return new Promise((resolve) => {
proxyServer.listen(config.port);
server.listen(socketPath, undefined, () => resolve({app, server}));
});
}
/**
* Adds a WS to the existing HTTP server.
* @param server HTTP server.
* @param config Server configuration. Port is used to verify
* incoming connections origin.
* @returns Returns the created WS.
*/
function addWebsocket(server: http.Server, config: Config) {
const localhostIPV4 = `localhost:${config.port}`;
const localhostIPV6 = `[::1]:${config.port}`;
const localhostIPV6NoBrackets = `::1:${config.port}`;
const localhostIPV4Electron = 'localhost:3000';
const possibleHosts = [
localhostIPV4,
localhostIPV6,
localhostIPV6NoBrackets,
localhostIPV4Electron,
];
const possibleOrigins = possibleHosts.map((host) => `http://${host}`);
const verifyClient: VerifyClientCallbackSync = ({origin, req}) => {
const noOriginHeader = origin === undefined;
if (
(noOriginHeader || possibleOrigins.includes(origin)) &&
req.headers.host &&
possibleHosts.includes(req.headers.host)
) {
// no origin header? The request is not originating from a browser, so should be OK to pass through
// If origin matches our own address, it means we are serving the page
return true;
} else {
// for now we don't allow cross origin request, so that an arbitrary website cannot try to
// connect a socket to localhost:serverport, and try to use the all powerful Flipper APIs to read
// for example files.
// Potentially in the future we do want to allow this, e.g. if we want to connect to a local flipper-server
// directly from intern. But before that, we should either authenticate the request somehow,
// and discuss security impact and for example scope the files that can be read by Flipper.
console.warn(
`Refused socket connection from cross domain request, origin: ${origin}, host: ${
req.headers.host
}. Expected origins: ${possibleOrigins.join(
' or ',
)}. Expected hosts: ${possibleHosts.join(' or ')}`,
);
return false;
}
};
const wss = new WebSocketServer({
noServer: true,
maxPayload: WEBSOCKET_MAX_MESSAGE_SIZE,
verifyClient,
});
server.on('upgrade', function upgrade(request, socket, head) {
const {pathname} = parse(request.url!);
// Handled by Metro
if (pathname === '/hot') {
return;
}
if (pathname === '/') {
wss.handleUpgrade(request, socket, head, function done(ws) {
wss.emit('connection', ws, request);
});
return;
}
console.error('addWebsocket.upgrade -> unknown pathname', pathname);
socket.destroy();
});
return wss;
}

View File

@@ -1,73 +0,0 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import os from 'os';
import xdgBasedir from 'xdg-basedir';
import net from 'net';
import fs from 'fs-extra';
/**
* Checks if a socket is in used for given path.
* If the path doesn't exist then is not in use. If it does,
* create a socket client and attempt to connect to it.
* If the connection is established, then there's a process
* already listening. Otherwise, it kind of indicates the
* contrary.
* @param path Path used instead of port number.
* @returns True or false dependning on whether the socket is in
* use or not.
*/
export async function checkSocketInUse(path: string): Promise<boolean> {
if (!(await fs.pathExists(path))) {
return false;
}
return new Promise((resolve, _reject) => {
const client = net
.createConnection(path, () => {
resolve(true);
client.destroy();
})
.on('error', (e) => {
if (e.message.includes('ECONNREFUSED')) {
resolve(false);
} else {
console.warn(
`[conn] Socket ${path} is in use, but we don't know why.`,
e,
);
resolve(false);
}
client.destroy();
});
});
}
/**
* Creates a socket path. Used to open the socket at location.
* Format: flipper-server-${userInfo}.sock
* @returns The created socket path.
*/
export async function makeSocketPath(): Promise<string> {
const runtimeDir = xdgBasedir.runtime || '/tmp';
await fs.mkdirp(runtimeDir);
const filename = `flipper-server-${os.userInfo().uid}.sock`;
const path = `${runtimeDir}/${filename}`;
// Depending on the OS this is between 104 and 108:
// https://unix.stackexchange.com/a/367012/10198
if (path.length >= 104) {
console.warn(
'Ignoring XDG_RUNTIME_DIR as it would exceed the total limit for domain socket paths. See man 7 unix.',
);
// Even with the INT32_MAX userid, we should have plenty of room.
return `/tmp/${filename}`;
}
return path;
}