Skip to content

Commit

Permalink
(fix) Use the initial user agent header within the agent api call (#15)
Browse files Browse the repository at this point in the history
* use the current request user agent
* update the alpha release
* update local dependency versions
  • Loading branch information
keplervital authored Jun 1, 2023
1 parent 7357126 commit 2bb92a4
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 25 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"license": "Apache-2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions packages/daemon/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 5 additions & 3 deletions packages/server/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/commons/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
32 changes: 28 additions & 4 deletions packages/server/src/servers/icp/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Actor, ActorSubclass, HttpAgent, concat } from '@dfinity/agent';
import { logger } from '@dfinity/http-proxy-core';
import { Principal } from '@dfinity/principal';
import {
getMaxVerificationVersion,
getMinVerificationVersion,
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,
Expand All @@ -22,17 +25,35 @@ 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()];

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<Response> => {
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();
}
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -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",
Expand Down
56 changes: 48 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ __metadata:
languageName: node
linkType: hard

"@dfinity/[email protected].1-alpha, @dfinity/http-proxy-core@workspace:packages/core":
"@dfinity/[email protected].2-alpha, @dfinity/http-proxy-core@workspace:packages/core":
version: 0.0.0-use.local
resolution: "@dfinity/http-proxy-core@workspace:packages/core"
dependencies:
Expand All @@ -138,11 +138,11 @@ __metadata:
languageName: unknown
linkType: soft

"@dfinity/[email protected].1-alpha, @dfinity/http-proxy-daemon@workspace:packages/daemon":
"@dfinity/[email protected].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
Expand All @@ -166,16 +166,17 @@ __metadata:
languageName: unknown
linkType: soft

"@dfinity/[email protected].1-alpha, @dfinity/http-proxy-server@workspace:packages/server":
"@dfinity/[email protected].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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 2bb92a4

Please sign in to comment.