Skip to content

Commit

Permalink
fix: proxy config from npm electron-userland#585
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed Sep 11, 2016
1 parent dd61408 commit fedb12c
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 23 deletions.
1 change: 1 addition & 0 deletions .idea/dictionaries/develar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion nsis-auto-updater/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"dependencies": {
"bluebird": "^3.4.6",
"fs-extra-p": "^1.1.8",
"semver": "^5.3.0"
"semver": "^5.3.0",
"ini": "^1.3.4",
"tunnel-agent": "^0.4.3"
},
"bundledDependencies": [
"fs-extra-p",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"fs-extra-p": "^1.1.8",
"hosted-git-info": "^2.1.5",
"image-size": "^0.5.0",
"ini": "^1.3.4",
"isbinaryfile": "^3.0.1",
"lodash.template": "^4.4.0",
"mime": "^1.3.4",
Expand Down Expand Up @@ -122,7 +123,7 @@
"json8": "^0.9.2",
"path-sort": "^0.1.0",
"pre-git": "^3.10.0",
"ts-babel": "^1.0.7",
"ts-babel": "^1.0.9",
"tslint": "^3.15.1",
"typescript": "^2.0.2",
"whitespace": "^2.1.0"
Expand Down
2 changes: 1 addition & 1 deletion src/publish/restApiRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function doApiRequest<T>(options: RequestOptions, token: string | null, r
// error is clear, we don't need to read detailed error description
reject(new HttpError(response, `method: ${options.method} url: https://${options.hostname}${options.path}
Please double check that your GitHub Token is correct. Due to security reasons GitHub doesn't report actual status, but 404.
Please double check that your authentication token is correct. Due to security reasons actual status maybe not reported, but 404.
`))
}
else if (response.statusCode === 204) {
Expand Down
2 changes: 1 addition & 1 deletion src/util/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { copy, Stats } from "fs-extra-p"
import { Minimatch } from "minimatch"
import * as path from "path"
import { Promise as BluebirdPromise } from "bluebird"
const readInstalled = require("read-installed")

//noinspection JSUnusedLocalSymbols
const __awaiter = require("./awaiter")
const readInstalled = require("read-installed")

// we use relative path to avoid canonical path issue - e.g. /tmp vs /private/tmp
export function copyFiltered(src: string, destination: string, filter: Filter, dereference: boolean): Promise<any> {
Expand Down
77 changes: 58 additions & 19 deletions src/util/httpRequest.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { Socket } from "net"
import { IncomingMessage, ClientRequest } from "http"
import { IncomingMessage, ClientRequest, Agent } from "http"
import * as https from "https"
import { createWriteStream, ensureDir } from "fs-extra-p"
import { createWriteStream, ensureDir, readFile } from "fs-extra-p"
import { parse as parseUrl } from "url"
import { Promise as BluebirdPromise } from "bluebird"
import * as path from "path"
import { createHash } from "crypto"
import { Transform } from "stream"
import { homedir } from "os"

//noinspection JSUnusedLocalSymbols
const __awaiter = require("./awaiter")

const maxRedirects = 10

Expand All @@ -15,14 +19,20 @@ export interface DownloadOptions {
sha2?: string
}

export const download = <(url: string, destination: string, options?: DownloadOptions) => BluebirdPromise<any>>(BluebirdPromise.promisify(_download))
let httpsAgent: Promise<Agent> | null = null

function _download(url: string, destination: string, options: DownloadOptions | null | undefined, callback: (error: Error) => void): void {
if (callback == null) {
callback = <any>options
options = null
}
doDownload(url, destination, 0, options || {}, callback)
export function download(url: string, destination: string, options?: DownloadOptions | null): BluebirdPromise<any> {
return <BluebirdPromise<Agent>>(httpsAgent || (httpsAgent = createAgent()))
.then(it => new BluebirdPromise(function (resolve, reject) {
doDownload(url, destination, 0, options || {}, it, error => {
if (error == null) {
resolve()
}
else {
reject(error)
}
})
}))
}

export function addTimeOutHandler(request: ClientRequest, callback: (error: Error) => void) {
Expand All @@ -34,7 +44,7 @@ export function addTimeOutHandler(request: ClientRequest, callback: (error: Erro
})
}

function doDownload(url: string, destination: string, redirectCount: number, options: DownloadOptions, callback: (error: Error | null) => void) {
function doDownload(url: string, destination: string, redirectCount: number, options: DownloadOptions, agent: Agent, callback: (error: Error | null) => void) {
const ensureDirPromise = options.skipDirCreation ? BluebirdPromise.resolve() : ensureDir(path.dirname(destination))

const parsedUrl = parseUrl(url)
Expand All @@ -45,7 +55,7 @@ function doDownload(url: string, destination: string, redirectCount: number, opt
headers: {
"User-Agent": "electron-builder"
},
agent: createAgent("https"),
agent: agent,
}, (response: IncomingMessage) => {
if (response.statusCode >= 400) {
callback(new Error(`Cannot download "${url}", status ${response.statusCode}: ${response.statusMessage}`))
Expand All @@ -55,7 +65,7 @@ function doDownload(url: string, destination: string, redirectCount: number, opt
const redirectUrl = response.headers.location
if (redirectUrl != null) {
if (redirectCount < maxRedirects) {
doDownload(redirectUrl, destination, redirectCount++, options, callback)
doDownload(redirectUrl, destination, redirectCount++, options, agent, callback)
}
else {
callback(new Error("Too many redirects (> " + maxRedirects + ")"))
Expand Down Expand Up @@ -124,20 +134,49 @@ class DigestTransform extends Transform {
}
}

function createAgent(uriProtocol: string) {
const proxyString: string = process.env.HTTPS_PROXY ||
process.env.https_proxy ||
process.env.HTTP_PROXY ||
process.env.http_proxy
// only https proxy
async function proxyFromNpm() {
let data = ""
try {
data = await readFile(path.join(homedir(), ".npmrc"), "utf-8")
}
catch (ignored) {
return null
}

if (!proxyString) {
if (!data) {
return null
}

try {
const config = require("ini").parse(data)
return config["https-proxy"] || config.proxy
}
catch (e) {
// used in nsis auto-updater, do not use .util.warn here
console.warn(e)
return null
}
}

// only https url
async function createAgent() {
let proxyString: string =
process.env.npm_config_https_proxy ||
process.env.HTTPS_PROXY || process.env.https_proxy ||
process.env.npm_config_proxy

if (!proxyString) {
proxyString = await proxyFromNpm()
if (!proxyString) {
return null
}
}

const proxy = parseUrl(proxyString)

const proxyProtocol = proxy.protocol === "https:" ? "Https" : "Http"
return require("tunnel-agent")[`${uriProtocol}Over${proxyProtocol}`]({
return require("tunnel-agent")[`httpsOver${proxyProtocol}`]({
proxy: {
port: proxy.port || (proxyProtocol === "Https" ? 443 : 80),
host: proxy.hostname,
Expand Down

0 comments on commit fedb12c

Please sign in to comment.