| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {ServerChallengeResponse} from './server'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import * as http from 'node:http'; |
| b69ab31 | | | 11 | import {type ExistingServerInfo, readExistingServerFile} from './existingServerStateFiles'; |
| b69ab31 | | | 12 | import {areTokensEqual} from './proxyUtils'; |
| b69ab31 | | | 13 | |
| b69ab31 | | | 14 | /** |
| b69ab31 | | | 15 | * If it looks like something is serving on `localhost` on the same port, |
| b69ab31 | | | 16 | * send it a request to verify it's actually ISL. |
| b69ab31 | | | 17 | * Send it the token we recovered and then validate it responds with |
| b69ab31 | | | 18 | * the same challenge token we recovered. |
| b69ab31 | | | 19 | * |
| b69ab31 | | | 20 | * If the challenge is successful, returns the PID of the server; otherwise, |
| b69ab31 | | | 21 | * returns null. |
| b69ab31 | | | 22 | */ |
| b69ab31 | | | 23 | export async function checkIfServerIsAliveAndIsISL( |
| b69ab31 | | | 24 | info: typeof console.info, |
| b69ab31 | | | 25 | port: number, |
| b69ab31 | | | 26 | existingServerInfo: ExistingServerInfo, |
| b69ab31 | | | 27 | silent = false, |
| b69ab31 | | | 28 | ): Promise<number | null> { |
| b69ab31 | | | 29 | let response: unknown; |
| b69ab31 | | | 30 | try { |
| b69ab31 | | | 31 | const result = await Promise.race<string>([ |
| b69ab31 | | | 32 | new Promise<string>((res, rej) => { |
| b69ab31 | | | 33 | const req = http.request( |
| b69ab31 | | | 34 | { |
| b69ab31 | | | 35 | hostname: 'localhost', |
| b69ab31 | | | 36 | port, |
| b69ab31 | | | 37 | path: `/challenge_authenticity?token=${existingServerInfo.sensitiveToken}`, |
| b69ab31 | | | 38 | method: 'GET', |
| b69ab31 | | | 39 | }, |
| b69ab31 | | | 40 | response => { |
| b69ab31 | | | 41 | response.on('data', d => { |
| b69ab31 | | | 42 | res(d); |
| b69ab31 | | | 43 | }); |
| b69ab31 | | | 44 | response.on('error', e => { |
| b69ab31 | | | 45 | rej(e); |
| b69ab31 | | | 46 | }); |
| b69ab31 | | | 47 | }, |
| b69ab31 | | | 48 | ); |
| b69ab31 | | | 49 | req.on('error', rej); |
| b69ab31 | | | 50 | req.end(); |
| b69ab31 | | | 51 | }), |
| b69ab31 | | | 52 | // Timeout so we don't wait around forever for it. |
| b69ab31 | | | 53 | // This should always be on localhost and therefore quite fast. |
| b69ab31 | | | 54 | new Promise<never>((_, rej) => setTimeout(() => rej('timeout'), 500)), |
| b69ab31 | | | 55 | ]); |
| b69ab31 | | | 56 | |
| b69ab31 | | | 57 | response = JSON.parse(result); |
| b69ab31 | | | 58 | } catch (error) { |
| b69ab31 | | | 59 | if (!silent) { |
| b69ab31 | | | 60 | info(`error checking if existing Sapling Web server on port ${port} is authentic: `, error); |
| b69ab31 | | | 61 | } |
| b69ab31 | | | 62 | // if the request fails for any reason, we don't think it's an ISL server. |
| b69ab31 | | | 63 | return null; |
| b69ab31 | | | 64 | } |
| b69ab31 | | | 65 | |
| b69ab31 | | | 66 | if (!validateServerChallengeResponse(response)) { |
| b69ab31 | | | 67 | return null; |
| b69ab31 | | | 68 | } |
| b69ab31 | | | 69 | |
| b69ab31 | | | 70 | const {challengeToken, pid} = response; |
| b69ab31 | | | 71 | return areTokensEqual(challengeToken, existingServerInfo.challengeToken) ? pid : null; |
| b69ab31 | | | 72 | } |
| b69ab31 | | | 73 | |
| b69ab31 | | | 74 | /** |
| b69ab31 | | | 75 | * Try multiple times to read the server data, in case we try to read during the time between the server |
| b69ab31 | | | 76 | * starting and it writing to the token file. |
| b69ab31 | | | 77 | */ |
| b69ab31 | | | 78 | export async function readExistingServerFileWithRetries( |
| b69ab31 | | | 79 | port: number, |
| b69ab31 | | | 80 | ): Promise<ExistingServerInfo | undefined> { |
| b69ab31 | | | 81 | let tries = 3; |
| b69ab31 | | | 82 | while (tries > 0) { |
| b69ab31 | | | 83 | try { |
| b69ab31 | | | 84 | // eslint-disable-next-line no-await-in-loop |
| b69ab31 | | | 85 | return await readExistingServerFile(port); |
| b69ab31 | | | 86 | } catch (error) { |
| b69ab31 | | | 87 | sleepMs(500); |
| b69ab31 | | | 88 | } |
| b69ab31 | | | 89 | tries--; |
| b69ab31 | | | 90 | } |
| b69ab31 | | | 91 | return undefined; |
| b69ab31 | | | 92 | } |
| b69ab31 | | | 93 | |
| b69ab31 | | | 94 | function sleepMs(timeMs: number): Promise<void> { |
| b69ab31 | | | 95 | return new Promise(res => setTimeout(res, timeMs)); |
| b69ab31 | | | 96 | } |
| b69ab31 | | | 97 | |
| b69ab31 | | | 98 | export function validateServerChallengeResponse(v: unknown): v is ServerChallengeResponse { |
| b69ab31 | | | 99 | return ( |
| b69ab31 | | | 100 | !!v && |
| b69ab31 | | | 101 | typeof v === 'object' && |
| b69ab31 | | | 102 | typeof (v as ServerChallengeResponse).challengeToken === 'string' && |
| b69ab31 | | | 103 | typeof (v as ServerChallengeResponse).pid === 'number' |
| b69ab31 | | | 104 | ); |
| b69ab31 | | | 105 | } |