1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-18 09:13:33 +00:00

native fetch with proxy support on node api

This commit is contained in:
Kyle Spearrin
2019-06-24 11:07:26 -04:00
parent 6d82fb5bbc
commit bc5a6e02c1
8 changed files with 78 additions and 14 deletions

View File

@@ -266,4 +266,5 @@ export abstract class ApiService {
getActiveBearerToken: () => Promise<string>;
fetch: (request: Request) => Promise<Response>;
nativeFetch: (request: Request) => Promise<Response>;
}

25
src/globals.d.ts vendored
View File

@@ -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
}

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -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());
}

View File

@@ -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';
@@ -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);
}
}