From 2bb92a446ea705e5f571901fd69abe606db380f5 Mon Sep 17 00:00:00 2001 From: Kepler <119848388+keplervital@users.noreply.github.com> Date: Thu, 1 Jun 2023 11:58:25 +0200 Subject: [PATCH] (fix) Use the initial user agent header within the agent api call (#15) * use the current request user agent * update the alpha release * update local dependency versions --- README.md | 4 +- package.json | 2 +- packages/core/package.json | 2 +- packages/daemon/package.json | 4 +- packages/server/package.json | 8 ++-- packages/server/src/commons/configs.ts | 2 +- packages/server/src/servers/icp/utils.ts | 32 ++++++++++++-- packages/ui/package.json | 6 +-- yarn.lock | 56 ++++++++++++++++++++---- 9 files changed, 91 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 4bf9223..5cadb7b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ [![GitHub license](https://img.shields.io/badge/license-Apache%202.0-blue.svg?style=for-the-badge)](LICENSE) -[![GitHub license](https://img.shields.io/badge/install-MacOSX-blue.svg?style=for-the-badge&logo=apple)](https://github.com/dfinity/http-proxy/releases/download/0.0.1-alpha/ic-http-proxy-mac-universal-0.0.1-alpha.dmg) -[![GitHub license](https://img.shields.io/badge/install-Windows-blue.svg?style=for-the-badge&logo=windows)](https://github.com/dfinity/http-proxy/releases/download/0.0.1-alpha/ic-http-proxy-win-x64-0.0.1-alpha.exe) +[![GitHub license](https://img.shields.io/badge/install-MacOSX-blue.svg?style=for-the-badge&logo=apple)](https://github.com/dfinity/http-proxy/releases/download/0.0.2-alpha/ic-http-proxy-mac-universal-0.0.2-alpha.dmg) +[![GitHub license](https://img.shields.io/badge/install-Windows-blue.svg?style=for-the-badge&logo=windows)](https://github.com/dfinity/http-proxy/releases/download/0.0.2-alpha/ic-http-proxy-win-x64-0.0.2-alpha.exe) # IC HTTP Proxy > This application is currently only a proof of concept implementation and should be used at your own risk. diff --git a/package.json b/package.json index bb986d2..0d469f9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dfinity/http-proxy", - "version": "0.0.1-alpha", + "version": "0.0.2-alpha", "description": "HTTP Proxy to enable trustless access to the Internet Computer.", "author": "Kepler Vital ", "license": "Apache-2.0", diff --git a/packages/core/package.json b/packages/core/package.json index f1edd6b..f1ceef2 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@dfinity/http-proxy-core", - "version": "0.0.1-alpha", + "version": "0.0.2-alpha", "description": "Gateway server to enable trustless access to the Internet Computer.", "main": "built/main.js", "types": "built/main.d.ts", diff --git a/packages/daemon/package.json b/packages/daemon/package.json index 4d203de..c084913 100644 --- a/packages/daemon/package.json +++ b/packages/daemon/package.json @@ -1,6 +1,6 @@ { "name": "@dfinity/http-proxy-daemon", - "version": "0.0.1-alpha", + "version": "0.0.2-alpha", "description": "Daemon process to enable trustless access to the Internet Computer.", "main": "built/main.js", "types": "built/main.d.ts", @@ -59,7 +59,7 @@ "typescript": "^4.9.5" }, "dependencies": { - "@dfinity/http-proxy-core": "0.0.1-alpha", + "@dfinity/http-proxy-core": "0.0.2-alpha", "http-proxy": "^1.18.1", "node-cache": "^5.1.2", "node-forge": "^1.3.1", diff --git a/packages/server/package.json b/packages/server/package.json index 9311a1b..394a2aa 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -1,6 +1,6 @@ { "name": "@dfinity/http-proxy-server", - "version": "0.0.1-alpha", + "version": "0.0.2-alpha", "description": "Gateway server to enable trustless access to the Internet Computer.", "main": "built/main.js", "types": "built/main.d.ts", @@ -34,6 +34,7 @@ "yarn": "~3" }, "devDependencies": { + "@types/isomorphic-fetch": "^0.0.36", "@types/node": "^18.14.0", "@types/node-forge": "^1.3.1", "@types/pako": "^2.0.0", @@ -50,11 +51,12 @@ "dependencies": { "@dfinity/agent": "^0.15.6", "@dfinity/candid": "^0.15.6", - "@dfinity/http-proxy-core": "0.0.1-alpha", - "@dfinity/http-proxy-daemon": "0.0.1-alpha", + "@dfinity/http-proxy-core": "0.0.2-alpha", + "@dfinity/http-proxy-daemon": "0.0.2-alpha", "@dfinity/principal": "^0.15.6", "@dfinity/response-verification": "^0.2.1", "http-proxy": "^1.18.1", + "isomorphic-fetch": "^3.0.0", "node-cache": "^5.1.2", "node-forge": "^1.3.1", "pako": "^2.1.0", diff --git a/packages/server/src/commons/configs.ts b/packages/server/src/commons/configs.ts index d51fdcf..10d5414 100644 --- a/packages/server/src/commons/configs.ts +++ b/packages/server/src/commons/configs.ts @@ -3,7 +3,7 @@ import { EnvironmentConfiguration } from './typings'; const environment: EnvironmentConfiguration = { platform: os.platform(), - userAgent: 'ICHttpProxy/0.0.1', + userAgent: 'ICHttpProxy/0.0.2-alpha', certificate: { storage: { folder: 'certs', diff --git a/packages/server/src/servers/icp/utils.ts b/packages/server/src/servers/icp/utils.ts index 7126c23..c597d12 100644 --- a/packages/server/src/servers/icp/utils.ts +++ b/packages/server/src/servers/icp/utils.ts @@ -1,4 +1,5 @@ import { Actor, ActorSubclass, HttpAgent, concat } from '@dfinity/agent'; +import { logger } from '@dfinity/http-proxy-core'; import { Principal } from '@dfinity/principal'; import { getMaxVerificationVersion, @@ -6,7 +7,9 @@ import { verifyRequestResponsePair, } from '@dfinity/response-verification/nodejs/nodejs.js'; import { IncomingMessage } from 'http'; +import fetch from 'isomorphic-fetch'; import { inflate, ungzip } from 'pako'; +import { environment } from '~src/commons'; import { idlFactory } from '~src/commons/http-interface/canister_http_interface'; import { HttpRequest, @@ -22,7 +25,6 @@ import { HTTPMethods, HttpResponse, } from './typings'; -import { logger } from '@dfinity/http-proxy-core'; export const maxCertTimeOffsetNs = BigInt.asUintN(64, BigInt(300_000_000_000)); export const cacheHeaders = [HTTPHeaders.CacheControl.toString()]; @@ -30,9 +32,28 @@ export const cacheHeaders = [HTTPHeaders.CacheControl.toString()]; export async function createAgentAndActor( gatewayUrl: URL, canisterId: Principal, - fetchRootKey: boolean + fetchRootKey: boolean, + userAgent: string ): Promise<[HttpAgent, ActorSubclass<_SERVICE>]> { - const agent = new HttpAgent({ host: gatewayUrl.toString() }); + // agent-js currently does not allow changing the user agent of the request with the fetchOptions, + // with this change the initial user agent will be mantained with the addition of the ic http proxy info + const customFetch: typeof fetch = ( + input: RequestInfo | URL, + opts?: RequestInit + ): Promise => { + if (opts) { + opts.headers = { + ...(opts.headers ?? {}), + [HTTPHeaders.UserAgent]: userAgent, + }; + } + + return fetch(input, opts); + }; + const agent = new HttpAgent({ + host: gatewayUrl.toString(), + fetch: customFetch, + }); if (fetchRootKey) { await agent.fetchRootKey(); } @@ -257,10 +278,13 @@ export const fetchFromInternetComputer = async ( const minAllowedVerificationVersion = getMinVerificationVersion(); const desiredVerificationVersion = getMaxVerificationVersion(); + const agentUserAgent = + request.headers.get(HTTPHeaders.UserAgent) ?? environment.userAgent; const [agent, actor] = await createAgentAndActor( DEFAULT_GATEWAY, canister, - shouldFetchRootKey + shouldFetchRootKey, + agentUserAgent ); const result = await fetchAsset({ agent, diff --git a/packages/ui/package.json b/packages/ui/package.json index 10e9b9b..b3b8a16 100644 --- a/packages/ui/package.json +++ b/packages/ui/package.json @@ -1,6 +1,6 @@ { "name": "@dfinity/http-proxy-ui", - "version": "0.0.1-alpha", + "version": "0.0.2-alpha", "description": "Desktop interface to facilitate user interaction with the HTTP Proxy server.", "main": "built/main.js", "scripts": { @@ -37,8 +37,8 @@ }, "homepage": "https://github.com/dfinity/http-proxy/tree/main/packages/ui#readme", "dependencies": { - "@dfinity/http-proxy-core": "0.0.1-alpha", - "@dfinity/http-proxy-server": "0.0.1-alpha" + "@dfinity/http-proxy-core": "0.0.2-alpha", + "@dfinity/http-proxy-server": "0.0.2-alpha" }, "devDependencies": { "@types/node": "^18.14.0", diff --git a/yarn.lock b/yarn.lock index f24e092..e98b34a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -113,7 +113,7 @@ __metadata: languageName: node linkType: hard -"@dfinity/http-proxy-core@0.0.1-alpha, @dfinity/http-proxy-core@workspace:packages/core": +"@dfinity/http-proxy-core@0.0.2-alpha, @dfinity/http-proxy-core@workspace:packages/core": version: 0.0.0-use.local resolution: "@dfinity/http-proxy-core@workspace:packages/core" dependencies: @@ -138,11 +138,11 @@ __metadata: languageName: unknown linkType: soft -"@dfinity/http-proxy-daemon@0.0.1-alpha, @dfinity/http-proxy-daemon@workspace:packages/daemon": +"@dfinity/http-proxy-daemon@0.0.2-alpha, @dfinity/http-proxy-daemon@workspace:packages/daemon": version: 0.0.0-use.local resolution: "@dfinity/http-proxy-daemon@workspace:packages/daemon" dependencies: - "@dfinity/http-proxy-core": 0.0.1-alpha + "@dfinity/http-proxy-core": 0.0.2-alpha "@types/node": ^18.14.0 "@types/node-forge": ^1.3.1 "@types/pako": ^2.0.0 @@ -166,16 +166,17 @@ __metadata: languageName: unknown linkType: soft -"@dfinity/http-proxy-server@0.0.1-alpha, @dfinity/http-proxy-server@workspace:packages/server": +"@dfinity/http-proxy-server@0.0.2-alpha, @dfinity/http-proxy-server@workspace:packages/server": version: 0.0.0-use.local resolution: "@dfinity/http-proxy-server@workspace:packages/server" dependencies: "@dfinity/agent": ^0.15.6 "@dfinity/candid": ^0.15.6 - "@dfinity/http-proxy-core": 0.0.1-alpha - "@dfinity/http-proxy-daemon": 0.0.1-alpha + "@dfinity/http-proxy-core": 0.0.2-alpha + "@dfinity/http-proxy-daemon": 0.0.2-alpha "@dfinity/principal": ^0.15.6 "@dfinity/response-verification": ^0.2.1 + "@types/isomorphic-fetch": ^0.0.36 "@types/node": ^18.14.0 "@types/node-forge": ^1.3.1 "@types/pako": ^2.0.0 @@ -185,6 +186,7 @@ __metadata: eslint-config-prettier: ^8.7.0 eslint-plugin-prettier: ^4.2.1 http-proxy: ^1.18.1 + isomorphic-fetch: ^3.0.0 node-cache: ^5.1.2 node-forge: ^1.3.1 nodemon: ^2.0.20 @@ -200,8 +202,8 @@ __metadata: version: 0.0.0-use.local resolution: "@dfinity/http-proxy-ui@workspace:packages/ui" dependencies: - "@dfinity/http-proxy-core": 0.0.1-alpha - "@dfinity/http-proxy-server": 0.0.1-alpha + "@dfinity/http-proxy-core": 0.0.2-alpha + "@dfinity/http-proxy-server": 0.0.2-alpha "@types/node": ^18.14.0 "@typescript-eslint/eslint-plugin": ^5.54.1 "@typescript-eslint/parser": ^5.54.1 @@ -579,6 +581,13 @@ __metadata: languageName: node linkType: hard +"@types/isomorphic-fetch@npm:^0.0.36": + version: 0.0.36 + resolution: "@types/isomorphic-fetch@npm:0.0.36" + checksum: 1ffaa1c97a019b1f7a682e2ae31bd4cc3f324198b422c62cece51b42d2750e0dde0c4df2320a52874d6c6019ee4b00ca2e736010d34b206a82e63ef82ef85bbe + languageName: node + linkType: hard + "@types/json-schema@npm:^7.0.9": version: 7.0.11 resolution: "@types/json-schema@npm:7.0.11" @@ -2923,6 +2932,16 @@ __metadata: languageName: node linkType: hard +"isomorphic-fetch@npm:^3.0.0": + version: 3.0.0 + resolution: "isomorphic-fetch@npm:3.0.0" + dependencies: + node-fetch: ^2.6.1 + whatwg-fetch: ^3.4.1 + checksum: e5ab79a56ce5af6ddd21265f59312ad9a4bc5a72cebc98b54797b42cb30441d5c5f8d17c5cd84a99e18101c8af6f90c081ecb8d12fd79e332be1778d58486d75 + languageName: node + linkType: hard + "jake@npm:^10.8.5": version: 10.8.5 resolution: "jake@npm:10.8.5" @@ -3439,6 +3458,20 @@ __metadata: languageName: node linkType: hard +"node-fetch@npm:^2.6.1": + version: 2.6.11 + resolution: "node-fetch@npm:2.6.11" + dependencies: + whatwg-url: ^5.0.0 + peerDependencies: + encoding: ^0.1.0 + peerDependenciesMeta: + encoding: + optional: true + checksum: 249d0666a9497553384d46b5ab296ba223521ac88fed4d8a17d6ee6c2efb0fc890f3e8091cafe7f9fba8151a5b8d925db2671543b3409a56c3cd522b468b47b3 + languageName: node + linkType: hard + "node-fetch@npm:^2.6.6": version: 2.6.9 resolution: "node-fetch@npm:2.6.9" @@ -4880,6 +4913,13 @@ __metadata: languageName: node linkType: hard +"whatwg-fetch@npm:^3.4.1": + version: 3.6.2 + resolution: "whatwg-fetch@npm:3.6.2" + checksum: ee976b7249e7791edb0d0a62cd806b29006ad7ec3a3d89145921ad8c00a3a67e4be8f3fb3ec6bc7b58498724fd568d11aeeeea1f7827e7e1e5eae6c8a275afed + languageName: node + linkType: hard + "whatwg-url@npm:^5.0.0": version: 5.0.0 resolution: "whatwg-url@npm:5.0.0"