Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: localhost subdomain redirection #136

Merged
merged 11 commits into from
Sep 22, 2022
31 changes: 24 additions & 7 deletions src/background/listener/bee-api.listener.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { 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'
Expand Down Expand Up @@ -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('/')

Expand Down Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you do that? cid.bzz.link also should be mapped to {cid}.swarm.localhost

},
{ url: [{ hostSuffix: '.bzz.link' }] },
)
Expand All @@ -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

Expand Down Expand Up @@ -276,8 +276,25 @@ 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) {
const url = `${this._beeApiUrl}/bzz/${bzzReference}`
private redirectToBzzReference(bzzReference: string, tabId: number, preventSubdomainRedirection = false) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove `preventSubdomainRedirection

Suggested change
private redirectToBzzReference(bzzReference: string, tabId: number, preventSubdomainRedirection = false) {
private redirectToBzzReference(bzzReference: string, tabId: number) {

let url: string

if (preventSubdomainRedirection || !isLocalhost(this._beeApiUrl)) {
url = `${this._beeApiUrl}/bzz/${bzzReference}`
} else {
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here the subdomain is a swarm hash, but u don't transform it into a CID that the swarm.localhost subdomain requires.


if (path) {
url += `/${path}`
}
}

console.log(`Fake URL redirection to ${url} on tabId ${tabId}`)

Expand Down
20 changes: 20 additions & 0 deletions src/utils/bzz-link.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -62,6 +64,24 @@ export function subdomainToBzzResource(subdomain: string): string {
return `${subdomain}.eth`
}

export function isLocalhost(url: string): boolean {
const { host } = new URL(url)

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('://')

return `${protocol}://${subdomain}.swarm.${host}`
}

export function bzzResourceToSubdomain(
bzzReference: string,
type: swarmCid.ReferenceType = swarmCid.ReferenceType.FEED,
Expand Down