Skip to content

Commit

Permalink
Merge pull request #1 from Microsoft/pr/yoh1496/590
Browse files Browse the repository at this point in the history
Pr/yoh1496/590
  • Loading branch information
yoh1496 authored Oct 29, 2018
2 parents 8d21c6b + 86202d5 commit 45e2723
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
22 changes: 4 additions & 18 deletions src/authentication/githubServer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as vscode from 'vscode';
import { IHostConfiguration, HostHelper } from './configuration';
import * as https from 'https';
import axios from 'axios';
import Logger from '../common/logger';
import { handler as uriHandler } from '../common/uri';
import { PromiseAdapter, promiseFromEmitter } from '../common/utils';
import axios from 'axios';
import { httpsOverHttp } from 'tunnel';
import { URL } from 'url';
import { agent } from '../common/net';


const SCOPES: string = 'read:user user:email repo write:discussion';
const GHE_OPTIONAL_SCOPES: object = {'write:discussion': true};
Expand Down Expand Up @@ -74,27 +74,13 @@ export class GitHubManager {
headers.authorization = `token ${token}`;
}

let proxy: object | undefined;
try {
const proxyURL = new URL(process.env.HTTPS_PROXY);
proxy = {
host: proxyURL.hostname,
port: proxyURL.port,
proxyAuth: (proxyURL.username && proxyURL.password) ?
`${proxyURL.username}:${proxyURL.password}` : null,
};
} catch(e) {
vscode.window.showErrorMessage('Given `HTTPS_PROXY` is not valid URL.');
Logger.appendLine(e.toString());
}

return {
host: HostHelper.getApiHost(hostUri).authority,
port: 443,
method,
path: HostHelper.getApiPath(hostUri, path),
headers,
agent: proxy ? httpsOverHttp({proxy}) : null,
agent,
};
}

Expand Down
25 changes: 25 additions & 0 deletions src/common/net.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { window } from 'vscode';
import { Agent, globalAgent } from 'https';
import { URL } from 'url';
import { httpsOverHttp } from 'tunnel';

export const agent = getAgent();

/**
* Return an https agent for the given proxy URL, or return the
* global https agent if the URL was empty or invalid.
*
* @param {string} url the proxy URL, (default: `process.env.HTTPS_PROXY`)
* @returns {https.Agent}
*/
function getAgent(url: string = process.env.HTTPS_PROXY): Agent {
if (!url) { return globalAgent; }
try {
const { hostname, port, username, password } = new URL(url);
const auth = username && password && `${username}:${password}`;
return httpsOverHttp({hostname, port, auth});
} catch (e) {
window.showErrorMessage(`HTTPS_PROXY environment variable ignored: ${e.message}`);
return globalAgent;
}
}
5 changes: 2 additions & 3 deletions src/github/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

import * as Octokit from '@octokit/rest';
import * as vscode from 'vscode';
import { httpsOverHttp } from 'tunnel';
import { URL } from 'url';
import { agent } from '../common/net';
import { IHostConfiguration, HostHelper } from '../authentication/configuration';
import { GitHubServer } from '../authentication/githubServer';
import { Remote } from '../common/remote';
Expand Down Expand Up @@ -162,7 +161,7 @@ export class CredentialStore {
Logger.appendLine(e.toString());
}
const octokit = new Octokit({
agent: proxy ? httpsOverHttp({proxy}) : null,
agent,
baseUrl: `${HostHelper.getApiHost(creds).toString().slice(0, -1)}${HostHelper.getApiPath(creds, '')}`,
headers: { 'user-agent': 'GitHub VSCode Pull Requests' }
});
Expand Down

0 comments on commit 45e2723

Please sign in to comment.