diff --git a/src/tailscale/cli.ts b/src/tailscale/cli.ts index f393888..637f652 100644 --- a/src/tailscale/cli.ts +++ b/src/tailscale/cli.ts @@ -44,29 +44,34 @@ export class Tailscale { return ts; } - async init(port?: string, nonce?: string) { + defaultArgs() { + const args = []; + if (this._vscode.env.logLevel === LogLevel.Debug) { + args.push('-v'); + } + if (this.port) { + args.push(`-port=${this.port}`); + } + if (this.nonce) { + args.push(`-nonce=${this.nonce}`); + } + if (this.socket) { + args.push(`-socket=${this.socket}`); + } + return args; + } + + async init() { return new Promise((resolve) => { this.socket = vscode.workspace.getConfiguration(EXTENSION_NS).get('socketPath'); let binPath = this.tsrelayPath(); - let args = []; - if (this._vscode.env.logLevel === LogLevel.Debug) { - args.push('-v'); - } + let args = this.defaultArgs(); let cwd = __dirname; if (process.env.NODE_ENV === 'development') { binPath = '../tool/go'; args = ['run', '.', ...args]; cwd = path.join(cwd, '../tsrelay'); } - if (port) { - args.push(`-port=${this.port}`); - } - if (nonce) { - args.push(`-nonce=${this.nonce}`); - } - if (this.socket) { - args.push(`-socket=${this.socket}`); - } Logger.debug(`path: ${binPath}`, LOG_COMPONENT); Logger.debug(`args: ${args.join(' ')}`, LOG_COMPONENT); @@ -109,48 +114,11 @@ export class Tailscale { this.processStderr(this.childProcess); }); } - - processStderr(childProcess: cp.ChildProcess) { - if (!childProcess.stderr) { - Logger.error('childProcess.stderr is null', LOG_COMPONENT); - throw new Error('childProcess.stderr is null'); - } - let buffer = ''; - childProcess.stderr.on('data', (data: Buffer) => { - buffer += data.toString(); // Append the data to the buffer - - const lines = buffer.split('\n'); // Split the buffer into lines - - // Process all complete lines except the last one - for (let i = 0; i < lines.length - 1; i++) { - const line = lines[i].trim(); - if (line.length > 0) { - Logger.info(line, LOG_COMPONENT); - } - } - - buffer = lines[lines.length - 1]; - }); - - childProcess.stderr.on('end', () => { - // Process the remaining data in the buffer after the stream ends - const line = buffer.trim(); - if (line.length > 0) { - Logger.info(line, LOG_COMPONENT); - } - }); - } - async initSudo() { return new Promise((resolve, err) => { const binPath = this.tsrelayPath(); - const args = [`-nonce=${this.nonce}`, `-port=${this.port}`]; - if (this._vscode.env.logLevel === LogLevel.Debug) { - args.push('-v'); - } - if (this.socket) { - args.push(`-socket=${this.socket}`); - } + const args = this.defaultArgs(); + Logger.info(`path: ${binPath}`, LOG_COMPONENT); this.notifyExit = () => { Logger.info('starting sudo tsrelay'); @@ -177,7 +145,7 @@ export class Tailscale { } else { this._vscode.window.showErrorMessage('Could not run authenticator, please check logs'); } - await this.init(this.port, this.nonce); + await this.init(); err('unauthenticated'); }); childProcess.on('error', (err) => { @@ -200,6 +168,37 @@ export class Tailscale { }); } + processStderr(childProcess: cp.ChildProcess) { + if (!childProcess.stderr) { + Logger.error('childProcess.stderr is null', LOG_COMPONENT); + throw new Error('childProcess.stderr is null'); + } + let buffer = ''; + childProcess.stderr.on('data', (data: Buffer) => { + buffer += data.toString(); // Append the data to the buffer + + const lines = buffer.split('\n'); // Split the buffer into lines + + // Process all complete lines except the last one + for (let i = 0; i < lines.length - 1; i++) { + const line = lines[i].trim(); + if (line.length > 0) { + Logger.info(line, LOG_COMPONENT); + } + } + + buffer = lines[lines.length - 1]; + }); + + childProcess.stderr.on('end', () => { + // Process the remaining data in the buffer after the stream ends + const line = buffer.trim(); + if (line.length > 0) { + Logger.info(line, LOG_COMPONENT); + } + }); + } + tsrelayPath(): string { let arch = process.arch; let platform: string = process.platform;