-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(repeater): restore remote network diagnostic and scripts
closes #436
- Loading branch information
Showing
18 changed files
with
338 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { Event } from '../../Bus/Event'; | ||
import { Event } from '../../Bus'; | ||
|
||
export class RegisterScripts implements Event { | ||
constructor(public readonly script: string | Record<string, string>) {} | ||
constructor(public readonly script?: string | Record<string, string>) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,35 @@ | ||
import { bind, Handler } from '../Bus'; | ||
import { NetworkTest } from './Events'; | ||
import { NetworkTestResult } from '../Integrations'; | ||
import { ReadlinePlatform } from '../Wizard'; | ||
import { Helpers, logger } from '../Utils'; | ||
import { injectable } from 'tsyringe'; | ||
import { EOL } from 'os'; | ||
import { URL } from 'url'; | ||
import { CommandHub } from '../Repeater'; | ||
import { inject, injectable } from 'tsyringe'; | ||
|
||
type NetworkTestResult = | ||
| { | ||
output: string; | ||
} | ||
| { | ||
error: string; | ||
}; | ||
|
||
@injectable() | ||
@bind(NetworkTest) | ||
export class NetworkTestHandler | ||
implements Handler<NetworkTest, NetworkTestResult> | ||
{ | ||
public async handle(config: NetworkTest): Promise<NetworkTestResult> { | ||
const output = await this.getOutput(config); | ||
|
||
return { output, repeaterId: config.repeaterId }; | ||
} | ||
|
||
private async getOutput(config: NetworkTest): Promise<string> { | ||
return new Promise((resolve, reject) => { | ||
const args = ['configure', `--${config.type}`]; | ||
|
||
logger.debug('Launching "Network Diagnostic" process with cmd: %j', args); | ||
|
||
const child = Helpers.spawn({ | ||
include: args, | ||
exclude: ['repeater'] | ||
}); | ||
|
||
child.unref(); | ||
|
||
const stdout: string[] = []; | ||
|
||
child.stdout.on('data', (data: Buffer) => { | ||
const chunk = data.toString(); | ||
const lines: string[] = chunk | ||
.split('\n') | ||
.filter((line: string) => line.length > 0); | ||
|
||
stdout.push(...lines); | ||
constructor(@inject(CommandHub) private readonly commandHub: CommandHub) {} | ||
|
||
const [first, ...rest]: string[] = [].concat(config.input); | ||
|
||
if (chunk.indexOf(ReadlinePlatform.URLS_QUESTION) > -1) { | ||
child.stdin.write(`${[first, ...rest].join(',')}${EOL}`); | ||
} | ||
|
||
if (chunk.indexOf(ReadlinePlatform.HOST_OR_IP_QUESTION) > -1) { | ||
child.stdin.write(`${new URL(first).hostname}${EOL}`); | ||
} | ||
|
||
if (chunk.indexOf(ReadlinePlatform.COMPELED_MESSAGE) > -1) { | ||
child.stdin.end(); | ||
} | ||
}); | ||
|
||
child.once('error', (err: Error) => { | ||
logger.warn( | ||
`Failed to start "Network Diagnostic" due to %s`, | ||
err.message | ||
); | ||
reject(err); | ||
}); | ||
|
||
child.on('close', (code: number) => { | ||
if (code !== 0 || stdout.length === 0) { | ||
const msg = `"Network Diagnostic" did not start successfully. Process exited with code ${code}`; | ||
|
||
logger.warn(msg); | ||
|
||
return reject(new Error(msg)); | ||
} | ||
|
||
resolve(this.processOutput(stdout)); | ||
}); | ||
}); | ||
} | ||
|
||
// this is workaround of \x1B[1G control code that retype string in console | ||
private processOutput(input: string[]): string { | ||
return input | ||
.filter( | ||
(element, index, arr) => | ||
!( | ||
element.endsWith('\u001b[1G') || | ||
(!!arr[index + 1] && arr[index + 1] === '\u001b[1G') | ||
) | ||
) | ||
.filter((x) => !x.startsWith(ReadlinePlatform.URLS_QUESTION)) | ||
.join('\n'); | ||
public async handle(config: NetworkTest): Promise<NetworkTestResult> { | ||
try { | ||
const output = await this.commandHub.testNetwork( | ||
config.type, | ||
config.input | ||
); | ||
|
||
return { output }; | ||
} catch (e) { | ||
return { | ||
error: typeof e === 'string' ? e : (e as Error).message | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,17 @@ | ||
import { bind, Handler } from '../Bus'; | ||
import { VirtualScripts, VirtualScriptType } from '../Scripts'; | ||
import { RegisterScripts } from './Events'; | ||
import { CommandHub } from '../Repeater'; | ||
import { inject, injectable } from 'tsyringe'; | ||
|
||
@injectable() | ||
@bind(RegisterScripts) | ||
export class RegisterScriptsHandler implements Handler<RegisterScripts> { | ||
constructor( | ||
@inject(VirtualScripts) private readonly virtualScripts: VirtualScripts | ||
) {} | ||
constructor(@inject(CommandHub) private readonly commandHub: CommandHub) {} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
public async handle(event: RegisterScripts): Promise<void> { | ||
this.virtualScripts.clear(VirtualScriptType.REMOTE); | ||
|
||
const { script } = event; | ||
|
||
if (typeof script === 'string') { | ||
this.virtualScripts.set('*', VirtualScriptType.REMOTE, script); | ||
} else if (script) { | ||
Object.entries(script).map(([wildcard, code]: [string, string]) => | ||
this.virtualScripts.set(wildcard, VirtualScriptType.REMOTE, code) | ||
); | ||
if (event.script) { | ||
this.commandHub.compileScripts(event.script); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Request, Response } from '../RequestExecutor'; | ||
import { NetworkTestType } from './NetworkTestType'; | ||
|
||
export interface CommandHub { | ||
compileScripts(script: string | Record<string, string>): void; | ||
testNetwork(type: NetworkTestType, input: string | string[]): Promise<string>; | ||
sendRequest(request: Request): Promise<Response>; | ||
} | ||
|
||
export const CommandHub: unique symbol = Symbol('CommandHub'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { CommandHub } from './CommandHub'; | ||
import { NetworkTestType } from './NetworkTestType'; | ||
import { VirtualScripts, VirtualScriptType } from '../Scripts'; | ||
import { Helpers, logger } from '../Utils'; | ||
import { ReadlinePlatform } from '../Wizard'; | ||
import { Request, RequestExecutor, Response } from '../RequestExecutor'; | ||
import { inject, injectAll } from 'tsyringe'; | ||
import { EOL } from 'os'; | ||
import { URL } from 'url'; | ||
|
||
export class DefaultCommandHub implements CommandHub { | ||
constructor( | ||
@inject(VirtualScripts) private readonly virtualScripts: VirtualScripts, | ||
@injectAll(RequestExecutor) | ||
private readonly requestExecutors: RequestExecutor[] | ||
) {} | ||
|
||
public compileScripts(script: string | Record<string, string>): void { | ||
this.virtualScripts.clear(VirtualScriptType.REMOTE); | ||
|
||
if (this.virtualScripts.size) { | ||
logger.warn( | ||
'Error Loading Script: Cannot accept scripts from the cloud when a local script is already loaded' | ||
); | ||
|
||
return; | ||
} | ||
|
||
if (typeof script === 'string') { | ||
this.virtualScripts.set('*', VirtualScriptType.REMOTE, script); | ||
} else { | ||
Object.entries(script).map(([wildcard, code]: [string, string]) => | ||
this.virtualScripts.set(wildcard, VirtualScriptType.REMOTE, code) | ||
); | ||
} | ||
} | ||
|
||
public sendRequest(request: Request): Promise<Response> { | ||
const { protocol } = request; | ||
|
||
const requestExecutor = this.requestExecutors.find( | ||
(x) => x.protocol === protocol | ||
); | ||
|
||
if (!requestExecutor) { | ||
throw new Error(`Unsupported protocol "${protocol}"`); | ||
} | ||
|
||
return requestExecutor.execute(request); | ||
} | ||
|
||
public testNetwork( | ||
type: NetworkTestType, | ||
input: string | string[] | ||
): Promise<string> { | ||
return new Promise((resolve, reject) => { | ||
const args = ['configure', `--${type}`]; | ||
|
||
logger.debug('Launching "Network Diagnostic" process with cmd: %j', args); | ||
|
||
const child = Helpers.spawn({ | ||
include: args, | ||
exclude: ['repeater'] | ||
}); | ||
|
||
child.unref(); | ||
|
||
const stdout: string[] = []; | ||
|
||
child.stdout.on('data', (data: Buffer) => { | ||
const chunk = data.toString(); | ||
const lines: string[] = chunk | ||
.split('\n') | ||
.filter((line: string) => line.length > 0); | ||
|
||
stdout.push(...lines); | ||
|
||
const [first, ...rest]: string[] = [].concat(input); | ||
|
||
if (chunk.indexOf(ReadlinePlatform.URLS_QUESTION) > -1) { | ||
child.stdin.write(`${[first, ...rest].join(',')}${EOL}`); | ||
} | ||
|
||
if (chunk.indexOf(ReadlinePlatform.HOST_OR_IP_QUESTION) > -1) { | ||
child.stdin.write(`${new URL(first).hostname}${EOL}`); | ||
} | ||
|
||
if (chunk.indexOf(ReadlinePlatform.COMPELED_MESSAGE) > -1) { | ||
child.stdin.end(); | ||
} | ||
}); | ||
|
||
child.once('error', (err: Error) => { | ||
logger.warn( | ||
`Failed to start "Network Diagnostic" due to %s`, | ||
err.message | ||
); | ||
reject(err); | ||
}); | ||
|
||
child.on('close', (code: number) => { | ||
if (code !== 0 || stdout.length === 0) { | ||
const msg = `"Network Diagnostic" did not start successfully. Process exited with code ${code}`; | ||
|
||
logger.warn(msg); | ||
|
||
return reject(new Error(msg)); | ||
} | ||
|
||
resolve(this.processOutput(stdout)); | ||
}); | ||
}); | ||
} | ||
|
||
// this is workaround of \x1B[1G control code that retype string in console | ||
private processOutput(input: string[]): string { | ||
return input | ||
.filter( | ||
(element, index, arr) => | ||
!( | ||
element.endsWith('\u001b[1G') || | ||
(!!arr[index + 1] && arr[index + 1] === '\u001b[1G') | ||
) | ||
) | ||
.filter((x) => !x.startsWith(ReadlinePlatform.URLS_QUESTION)) | ||
.join('\n'); | ||
} | ||
} |
Oops, something went wrong.