Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Emulator for Ngrok v3 Support #2399

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions packages/app/main/src/ngrok.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ import { dispatch, store } from './state';
export interface NgrokOptions {
addr: number;
name: string;
path: string;
port: number;
proto: 'http' | 'https' | 'tcp' | 'tls';
region: 'us' | 'eu' | 'au' | 'ap';
inspect: boolean;
host_header?: string;
bind_tls?: boolean | 'both';
Expand All @@ -68,7 +66,6 @@ const defaultOptions: Partial<NgrokOptions> = {
addr: 80,
name: uniqueId(),
proto: 'http',
region: 'us',
inspect: true,
};

Expand All @@ -86,14 +83,16 @@ export class NgrokInstance {
public ngrokEmitter = new EventEmitter().on('error', this.kill);
private pendingConnection: Promise<{ url; inspectUrl }>;
private ngrokProcess: ChildProcess;
private ngrokFilePath = '';
private tunnels = {};
private inspectUrl = '';
private intervalForHealthCheck: NodeJS.Timer = null;
private ws: FileWriteStream = null;
private boundCheckTunnelStatus = null;

constructor() {
constructor(newNgrokPath) {
this.boundCheckTunnelStatus = this.checkTunnelStatus.bind(this);
this.ngrokFilePath = newNgrokPath;
}

public running(): boolean {
Expand Down Expand Up @@ -263,11 +262,12 @@ export class NgrokInstance {
}

private spawnNgrok(opts: NgrokOptions): ChildProcess {
const filename = `${opts.path ? path.basename(opts.path) : bin}`;
const folder = opts.path ? path.dirname(opts.path) : path.join(__dirname, 'bin');
const filename = `${this.ngrokFilePath ? path.basename(this.ngrokFilePath) : bin}`;
const folder = this.ngrokFilePath ? path.dirname(this.ngrokFilePath) : path.join(__dirname, 'bin');

try {
this.ws.write('Ngrok Logger starting');
const args = ['start', '--none', `--log=stdout`, `--region=${opts.region}`];
const args = ['start', '--none', `--log=stdout`];
const ngrokPath = path.join(folder, filename);
if (!existsSync(ngrokPath)) {
throw new Error(
Expand All @@ -276,6 +276,7 @@ export class NgrokInstance {
`Ngrok is required to receive a token from the Bot Framework token service.`
);
}

const ngrok = spawn(ngrokPath, args, { cwd: folder });
// Errors are emitted instead of throwing since ngrok is a long running process
ngrok.on('error', e => this.ngrokEmitter.emit('error', e));
Expand Down
9 changes: 4 additions & 5 deletions packages/app/main/src/ngrokService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ import { getSettings } from './state/store';
let ngrokServiceInstance: NgrokService;

export class NgrokService {
private ngrok = new NgrokInstance();
// Pass ngrok path to create instance without adding path to
// Ngrok Options for compatibility with Ngrok 3
private ngrok = new NgrokInstance(getSettings().framework.ngrokPath);
private ngrokPath: string;
private serviceUrl: string;
private inspectUrl: string;
Expand Down Expand Up @@ -96,14 +98,12 @@ export class NgrokService {
return this.serviceUrl;
}
// otherwise, we need to spin up an auxillary ngrok instance that we can tear down when the token response comes back
this.oauthNgrokInstance = new NgrokInstance();
this.oauthNgrokInstance = new NgrokInstance(getSettings().framework.ngrokPath);
const port = Emulator.getInstance().server.serverPort;
const ngrokPath = getSettings().framework.ngrokPath;
const inspectUrl = new Promise<string>(async (resolve, reject) => {
try {
const { url } = await this.oauthNgrokInstance.connect({
addr: port,
path: ngrokPath,
});
resolve(url);
} catch (e) {
Expand Down Expand Up @@ -152,7 +152,6 @@ export class NgrokService {
this.triedToSpawn = true;
const { inspectUrl, url } = await this.ngrok.connect({
addr: port,
path: this.ngrokPath,
});

this.serviceUrl = url;
Expand Down