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
33 changes: 28 additions & 5 deletions src/background/listener/bee-api.listener.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { 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'
Expand Down Expand Up @@ -58,7 +64,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 @@ -159,8 +165,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 @@ -277,7 +283,24 @@ 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 (!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, hashToCid(subdomain).toString())

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