Skip to content

Commit

Permalink
Tiny refactor for common tsrelay args (#87)
Browse files Browse the repository at this point in the history
We run tsrelay in sudo and non-sudo, this PR abstracts the common args
passed to both class methods.

(cherry picked from commit f3a5ab0)
  • Loading branch information
marwan-at-work authored and tylersmalley committed Jun 29, 2023
1 parent 079a553 commit 3c6d291
Showing 1 changed file with 53 additions and 54 deletions.
107 changes: 53 additions & 54 deletions src/tailscale/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<null>((resolve) => {
this.socket = vscode.workspace.getConfiguration(EXTENSION_NS).get<string>('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);

Expand Down Expand Up @@ -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<null>((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');
Expand All @@ -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) => {
Expand All @@ -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;
Expand Down

0 comments on commit 3c6d291

Please sign in to comment.