Skip to content
This repository has been archived by the owner on Jun 17, 2022. It is now read-only.

Commit

Permalink
native fetch with proxy support on node api
Browse files Browse the repository at this point in the history
  • Loading branch information
kspearrin committed Jun 24, 2019
1 parent 6d82fb5 commit bc5a6e0
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 14 deletions.
47 changes: 35 additions & 12 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"electron-store": "1.3.0",
"electron-updater": "4.0.6",
"form-data": "2.3.2",
"https-proxy-agent": "2.2.1",
"inquirer": "6.2.0",
"jsdom": "13.2.0",
"keytar": "4.4.1",
Expand Down
1 change: 1 addition & 0 deletions src/abstractions/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,5 @@ export abstract class ApiService {

getActiveBearerToken: () => Promise<string>;
fetch: (request: Request) => Promise<Response>;
nativeFetch: (request: Request) => Promise<Response>;
}
25 changes: 25 additions & 0 deletions src/globals.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
declare function escape(s: string): string;
declare function unescape(s: string): string;
declare module 'duo_web_sdk';

// From: https://github.com/TooTallNate/node-https-proxy-agent/issues/27
declare module 'https-proxy-agent' {
import * as https from 'https'

namespace HttpsProxyAgent {
interface HttpsProxyAgentOptions {
host: string
port: number
secureProxy?: boolean
headers?: {
[key: string]: string
}
[key: string]: any
}
}

// HttpsProxyAgent doesnt *actually* extend https.Agent, but for my purposes I want it to pretend that it does
class HttpsProxyAgent extends https.Agent {
constructor(opts: string)
constructor(opts: HttpsProxyAgent.HttpsProxyAgentOptions)
}

export = HttpsProxyAgent
}
4 changes: 4 additions & 0 deletions src/services/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,10 @@ export class ApiService implements ApiServiceAbstraction {
request.headers.set('Cache-Control', 'no-cache');
request.headers.set('Pragma', 'no-cache');
}
return this.nativeFetch(request);
}

nativeFetch(request: Request): Promise<Response> {
return fetch(request);
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/audit.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class AuditService implements AuditServiceAbstraction {
const hashStart = hash.substr(0, 5);
const hashEnding = hash.substr(5);

const response = await fetch(PwnedPasswordsApi + hashStart);
const response = await this.apiService.nativeFetch(new Request(PwnedPasswordsApi + hashStart));
const leakedHashes = await response.text();
const match = leakedHashes.split(/\r?\n/).find((v) => {
return v.split(':')[0] === hashEnding;
Expand Down
3 changes: 2 additions & 1 deletion src/services/cipher.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,8 @@ export class CipherService implements CipherServiceAbstraction {

private async shareAttachmentWithServer(attachmentView: AttachmentView, cipherId: string,
organizationId: string): Promise<any> {
const attachmentResponse = await fetch(new Request(attachmentView.url, { cache: 'no-cache' }));
const attachmentResponse = await this.apiService.nativeFetch(
new Request(attachmentView.url, { cache: 'no-cache' }));
if (attachmentResponse.status !== 200) {
throw Error('Failed to download attachment: ' + attachmentResponse.status.toString());
}
Expand Down
9 changes: 9 additions & 0 deletions src/services/nodeApi.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as FormData from 'form-data';
import * as HttpsProxyAgent from 'https-proxy-agent';
import * as fe from 'node-fetch';

import { ApiService } from './api.service';
Expand All @@ -17,4 +18,12 @@ export class NodeApiService extends ApiService {
logoutCallback: (expired: boolean) => Promise<void>) {
super(tokenService, platformUtilsService, logoutCallback);
}

nativeFetch(request: Request): Promise<Response> {
const proxy = process.env.http_proxy || process.env.https_proxy;
if (proxy) {
(request as any).agent = new HttpsProxyAgent(proxy);
}
return fetch(request);
}
}

0 comments on commit bc5a6e0

Please sign in to comment.