Skip to content

Commit

Permalink
feat: localhost subdomain redirection (#136)
Browse files Browse the repository at this point in the history
* feat: redirect requests to subdomain based urls

* fix: fix subdomain redirection

* feat: do redirection for localhost only

* chore: remove unused code

* fix: fix bzz redirection

* fix: fix check for localhost

* fix: fix subdomain

* fix: fix subdomain redirection

* fix: fix subdomain redirection
  • Loading branch information
tomicvladan authored Sep 22, 2022
1 parent 871f09c commit 082f053
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
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

0 comments on commit 082f053

Please sign in to comment.