From 7079bb6a8806ec36d6863819bbf770d4da17740f Mon Sep 17 00:00:00 2001 From: Vladan Date: Tue, 21 Jun 2022 14:09:45 +0200 Subject: [PATCH 1/9] feat: redirect requests to subdomain based urls --- src/background/listener/bee-api.listener.ts | 17 +++++++++++++++-- src/utils/bzz-link.ts | 14 ++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/background/listener/bee-api.listener.ts b/src/background/listener/bee-api.listener.ts index 289c84aa..5ebfc8f9 100644 --- a/src/background/listener/bee-api.listener.ts +++ b/src/background/listener/bee-api.listener.ts @@ -1,4 +1,4 @@ -import { subdomainToBzzResource } from '../../utils/bzz-link' +import { createSubdomainUrl, isHostIpAddress, subdomainToBzzResource } from '../../utils/bzz-link' import { fakeUrl } from '../../utils/fake-url' import { getItem, StoreObserver } from '../../utils/storage' import { SWARM_SESSION_ID_KEY, unpackSwarmSessionIdFromUrl } from '../../utils/swarm-session-id' @@ -276,7 +276,20 @@ export class BeeApiListener { * @param tabId the tab will be navigated to the dApp page */ private redirectToBzzReference(bzzReference: string, tabId: number) { - const url = `${this._beeApiUrl}/bzz/${bzzReference}` + let url: string + + if (isHostIpAddress(this._beeApiUrl)) { + const [hash, path] = bzzReference.split(/\/(.*)/s) + let subdomain = hash + + if (subdomain.endsWith('.eth')) { + subdomain = subdomain.substring(0, subdomain.length - 4) + } + + url = createSubdomainUrl(this._beeApiUrl, subdomain) + path + } else { + url = `${this._beeApiUrl}/bzz/${bzzReference}` + } console.log(`Fake URL redirection to ${url} on tabId ${tabId}`) diff --git a/src/utils/bzz-link.ts b/src/utils/bzz-link.ts index 8cb25c88..76d5434f 100644 --- a/src/utils/bzz-link.ts +++ b/src/utils/bzz-link.ts @@ -1,6 +1,10 @@ /** bzz.link CID implementaion */ import * as swarmCid from '@ethersphere/swarm-cid' +const ipAsHostRegex = RegExp( + '^http[s]?://((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])', +) + export function hashToCid( input: string, type: swarmCid.ReferenceType = swarmCid.ReferenceType.FEED, @@ -62,6 +66,16 @@ export function subdomainToBzzResource(subdomain: string): string { return `${subdomain}.eth` } +export function isHostIpAddress(url: string): boolean { + return ipAsHostRegex.test(url) +} + +export function createSubdomainUrl(beeApiUrl: string, subdomain: string): string { + const [protocol, host] = beeApiUrl.split('://') + + return `${protocol}://${subdomain}.${host}` +} + export function bzzResourceToSubdomain( bzzReference: string, type: swarmCid.ReferenceType = swarmCid.ReferenceType.FEED, From be1cb61d5b824177cd82875a81c74c7a3d98f1da Mon Sep 17 00:00:00 2001 From: tomicvladan Date: Fri, 5 Aug 2022 13:02:17 +0200 Subject: [PATCH 2/9] fix: fix subdomain redirection --- src/background/listener/bee-api.listener.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/background/listener/bee-api.listener.ts b/src/background/listener/bee-api.listener.ts index dab904d3..a93c9189 100644 --- a/src/background/listener/bee-api.listener.ts +++ b/src/background/listener/bee-api.listener.ts @@ -280,6 +280,8 @@ export class BeeApiListener { let url: string if (isHostIpAddress(this._beeApiUrl)) { + url = `${this._beeApiUrl}/bzz/${bzzReference}` + } else { const [hash, path] = bzzReference.split(/\/(.*)/s) let subdomain = hash @@ -287,9 +289,11 @@ export class BeeApiListener { subdomain = subdomain.substring(0, subdomain.length - 4) } - url = createSubdomainUrl(this._beeApiUrl, subdomain) + path - } else { - url = `${this._beeApiUrl}/bzz/${bzzReference}` + url = createSubdomainUrl(this._beeApiUrl, subdomain) + + if (path) { + url += `/${path}` + } } console.log(`Fake URL redirection to ${url} on tabId ${tabId}`) From 2a73ffc50d996970573442b8655d296afcf88536 Mon Sep 17 00:00:00 2001 From: tomicvladan Date: Wed, 7 Sep 2022 16:35:40 +0200 Subject: [PATCH 3/9] feat: do redirection for localhost only --- src/background/listener/bee-api.listener.ts | 4 ++-- src/utils/bzz-link.ts | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/background/listener/bee-api.listener.ts b/src/background/listener/bee-api.listener.ts index a93c9189..51e996ff 100644 --- a/src/background/listener/bee-api.listener.ts +++ b/src/background/listener/bee-api.listener.ts @@ -1,4 +1,4 @@ -import { createSubdomainUrl, isHostIpAddress, subdomainToBzzResource } from '../../utils/bzz-link' +import { createSubdomainUrl, isLocalhost, subdomainToBzzResource } from '../../utils/bzz-link' import { fakeUrl } from '../../utils/fake-url' import { getItem, StoreObserver } from '../../utils/storage' import { SWARM_SESSION_ID_KEY, unpackSwarmSessionIdFromUrl } from '../../utils/swarm-session-id' @@ -279,7 +279,7 @@ export class BeeApiListener { private redirectToBzzReference(bzzReference: string, tabId: number) { let url: string - if (isHostIpAddress(this._beeApiUrl)) { + if (!isLocalhost(this._beeApiUrl)) { url = `${this._beeApiUrl}/bzz/${bzzReference}` } else { const [hash, path] = bzzReference.split(/\/(.*)/s) diff --git a/src/utils/bzz-link.ts b/src/utils/bzz-link.ts index 76d5434f..fc539315 100644 --- a/src/utils/bzz-link.ts +++ b/src/utils/bzz-link.ts @@ -70,6 +70,12 @@ export function isHostIpAddress(url: string): boolean { return ipAsHostRegex.test(url) } +export function isLocalhost(url: string): boolean { + const urlObject = new URL(url) + + return urlObject.host === 'localhost' +} + export function createSubdomainUrl(beeApiUrl: string, subdomain: string): string { const [protocol, host] = beeApiUrl.split('://') From 24e678f3e2ba78edda6cd0e0ee5260bb0797fdb9 Mon Sep 17 00:00:00 2001 From: tomicvladan Date: Fri, 9 Sep 2022 12:54:55 +0200 Subject: [PATCH 4/9] chore: remove unused code --- src/utils/bzz-link.ts | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/utils/bzz-link.ts b/src/utils/bzz-link.ts index fc539315..22518548 100644 --- a/src/utils/bzz-link.ts +++ b/src/utils/bzz-link.ts @@ -1,10 +1,6 @@ /** bzz.link CID implementaion */ import * as swarmCid from '@ethersphere/swarm-cid' -const ipAsHostRegex = RegExp( - '^http[s]?://((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])', -) - export function hashToCid( input: string, type: swarmCid.ReferenceType = swarmCid.ReferenceType.FEED, @@ -66,10 +62,6 @@ export function subdomainToBzzResource(subdomain: string): string { return `${subdomain}.eth` } -export function isHostIpAddress(url: string): boolean { - return ipAsHostRegex.test(url) -} - export function isLocalhost(url: string): boolean { const urlObject = new URL(url) From e67c0aa5a6500fad9d4348c7557eb6cdc6c4ab4b Mon Sep 17 00:00:00 2001 From: tomicvladan Date: Fri, 9 Sep 2022 13:07:39 +0200 Subject: [PATCH 5/9] fix: fix bzz redirection --- src/background/listener/bee-api.listener.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/background/listener/bee-api.listener.ts b/src/background/listener/bee-api.listener.ts index 51e996ff..a2f71844 100644 --- a/src/background/listener/bee-api.listener.ts +++ b/src/background/listener/bee-api.listener.ts @@ -159,8 +159,8 @@ export class BeeApiListener { chrome.webRequest.onBeforeRequest.addListener( (details: chrome.webRequest.WebRequestBodyDetails) => { console.log('Original BZZ Url', details.url) - const urlParams = new URLSearchParams(details.url) - const query = urlParams.get('oq') + const urlParams = new URLSearchParams(new URL(details.url).search) + const query = decodeURI(urlParams.get('oq') || urlParams.get('q') || '') if (!query || !query.startsWith('bzz://')) return From 554c9ff971673c9375fd612eb3d8385187bb4670 Mon Sep 17 00:00:00 2001 From: tomicvladan Date: Fri, 9 Sep 2022 16:02:18 +0200 Subject: [PATCH 6/9] fix: fix check for localhost --- src/utils/bzz-link.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/bzz-link.ts b/src/utils/bzz-link.ts index 22518548..f5c15ea5 100644 --- a/src/utils/bzz-link.ts +++ b/src/utils/bzz-link.ts @@ -63,9 +63,9 @@ export function subdomainToBzzResource(subdomain: string): string { } export function isLocalhost(url: string): boolean { - const urlObject = new URL(url) + const { host } = new URL(url) - return urlObject.host === 'localhost' + return host === 'localhost' || host.startsWith('localhost:') } export function createSubdomainUrl(beeApiUrl: string, subdomain: string): string { From 0ed7ad51b7cf8ffd8b66ecffdace30da27cfac91 Mon Sep 17 00:00:00 2001 From: tomicvladan Date: Fri, 9 Sep 2022 17:07:00 +0200 Subject: [PATCH 7/9] fix: fix subdomain --- src/utils/bzz-link.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/bzz-link.ts b/src/utils/bzz-link.ts index f5c15ea5..73f0f8cd 100644 --- a/src/utils/bzz-link.ts +++ b/src/utils/bzz-link.ts @@ -71,7 +71,7 @@ export function isLocalhost(url: string): boolean { export function createSubdomainUrl(beeApiUrl: string, subdomain: string): string { const [protocol, host] = beeApiUrl.split('://') - return `${protocol}://${subdomain}.${host}` + return `${protocol}://${subdomain}.swarm.${host}` } export function bzzResourceToSubdomain( From 4ab717bc165609eee05bcc91deec4cb774ded663 Mon Sep 17 00:00:00 2001 From: tomicvladan Date: Mon, 12 Sep 2022 13:59:41 +0200 Subject: [PATCH 8/9] fix: fix subdomain redirection --- src/background/listener/bee-api.listener.ts | 10 +++++----- src/utils/bzz-link.ts | 8 ++++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/background/listener/bee-api.listener.ts b/src/background/listener/bee-api.listener.ts index a2f71844..7c3535ee 100644 --- a/src/background/listener/bee-api.listener.ts +++ b/src/background/listener/bee-api.listener.ts @@ -1,4 +1,4 @@ -import { createSubdomainUrl, isLocalhost, subdomainToBzzResource } from '../../utils/bzz-link' +import { createSubdomainUrl, isLocalhost, isSubdomainUsed, subdomainToBzzResource } from '../../utils/bzz-link' import { fakeUrl } from '../../utils/fake-url' import { getItem, StoreObserver } from '../../utils/storage' import { SWARM_SESSION_ID_KEY, unpackSwarmSessionIdFromUrl } from '../../utils/swarm-session-id' @@ -58,7 +58,7 @@ export class BeeApiListener { ): void | chrome.webRequest.BlockingResponse => { console.log('web2OriginEnabled', this._web2OriginEnabled) - if (this._web2OriginEnabled) return { responseHeaders: details.responseHeaders } + if (this._web2OriginEnabled || isSubdomainUsed(details.url)) return { responseHeaders: details.responseHeaders } const urlArray = details.url.toString().split('/') @@ -149,7 +149,7 @@ export class BeeApiListener { const bzzReference = subdomainToBzzResource(subdomain) + pathWithParams console.log('bzz link redirect', bzzReference, url, pathWithParams) - this.redirectToBzzReference(bzzReference, tabId) + this.redirectToBzzReference(bzzReference, tabId, true) }, { url: [{ hostSuffix: '.bzz.link' }] }, ) @@ -276,10 +276,10 @@ export class BeeApiListener { * @param bzzReference in form of $ROOT_HASH<$PATH><$QUERY> * @param tabId the tab will be navigated to the dApp page */ - private redirectToBzzReference(bzzReference: string, tabId: number) { + private redirectToBzzReference(bzzReference: string, tabId: number, preventSubdomainRedirection = false) { let url: string - if (!isLocalhost(this._beeApiUrl)) { + if (preventSubdomainRedirection || !isLocalhost(this._beeApiUrl)) { url = `${this._beeApiUrl}/bzz/${bzzReference}` } else { const [hash, path] = bzzReference.split(/\/(.*)/s) diff --git a/src/utils/bzz-link.ts b/src/utils/bzz-link.ts index 73f0f8cd..21e0c8ff 100644 --- a/src/utils/bzz-link.ts +++ b/src/utils/bzz-link.ts @@ -1,6 +1,8 @@ /** bzz.link CID implementaion */ import * as swarmCid from '@ethersphere/swarm-cid' +const subdomainUsedRegex = RegExp('^.*\\.swarm\\.localhost(:\\d+)?$') + export function hashToCid( input: string, type: swarmCid.ReferenceType = swarmCid.ReferenceType.FEED, @@ -68,6 +70,12 @@ export function isLocalhost(url: string): boolean { return host === 'localhost' || host.startsWith('localhost:') } +export function isSubdomainUsed(url: string): boolean { + const { host } = new URL(url) + + return subdomainUsedRegex.test(host) +} + export function createSubdomainUrl(beeApiUrl: string, subdomain: string): string { const [protocol, host] = beeApiUrl.split('://') From 402a78dfb69d7121049900380934bbf2af677a4f Mon Sep 17 00:00:00 2001 From: tomicvladan Date: Tue, 13 Sep 2022 10:13:04 +0200 Subject: [PATCH 9/9] fix: fix subdomain redirection --- src/background/listener/bee-api.listener.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/background/listener/bee-api.listener.ts b/src/background/listener/bee-api.listener.ts index 7c3535ee..29db256d 100644 --- a/src/background/listener/bee-api.listener.ts +++ b/src/background/listener/bee-api.listener.ts @@ -1,4 +1,10 @@ -import { createSubdomainUrl, isLocalhost, isSubdomainUsed, subdomainToBzzResource } from '../../utils/bzz-link' +import { + createSubdomainUrl, + hashToCid, + isLocalhost, + isSubdomainUsed, + subdomainToBzzResource, +} from '../../utils/bzz-link' import { fakeUrl } from '../../utils/fake-url' import { getItem, StoreObserver } from '../../utils/storage' import { SWARM_SESSION_ID_KEY, unpackSwarmSessionIdFromUrl } from '../../utils/swarm-session-id' @@ -149,7 +155,7 @@ export class BeeApiListener { const bzzReference = subdomainToBzzResource(subdomain) + pathWithParams console.log('bzz link redirect', bzzReference, url, pathWithParams) - this.redirectToBzzReference(bzzReference, tabId, true) + this.redirectToBzzReference(bzzReference, tabId) }, { url: [{ hostSuffix: '.bzz.link' }] }, ) @@ -276,10 +282,10 @@ export class BeeApiListener { * @param bzzReference in form of $ROOT_HASH<$PATH><$QUERY> * @param tabId the tab will be navigated to the dApp page */ - private redirectToBzzReference(bzzReference: string, tabId: number, preventSubdomainRedirection = false) { + private redirectToBzzReference(bzzReference: string, tabId: number) { let url: string - if (preventSubdomainRedirection || !isLocalhost(this._beeApiUrl)) { + if (!isLocalhost(this._beeApiUrl)) { url = `${this._beeApiUrl}/bzz/${bzzReference}` } else { const [hash, path] = bzzReference.split(/\/(.*)/s) @@ -289,7 +295,7 @@ export class BeeApiListener { subdomain = subdomain.substring(0, subdomain.length - 4) } - url = createSubdomainUrl(this._beeApiUrl, subdomain) + url = createSubdomainUrl(this._beeApiUrl, hashToCid(subdomain).toString()) if (path) { url += `/${path}`