Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

feat: act as HTTP PROXY for http://<cidv1b32>.ipfs.localhost #2246

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"dependencies": {
"@hapi/ammo": "^3.1.0",
"@hapi/boom": "^7.4.2",
"@hapi/h2o2": "^8.3.0",
"@hapi/hapi": "^18.3.1",
"@hapi/joi": "^15.0.1",
"array-shuffle": "^1.0.1",
Expand Down
25 changes: 25 additions & 0 deletions src/http/gateway/routes/gateway.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict'

const Joi = require('@hapi/joi')
const Boom = require('@hapi/boom')
const resources = require('../resources')
const isIpfs = require('is-ipfs')

module.exports = [
{
Expand Down Expand Up @@ -39,5 +41,28 @@ module.exports = [
onPostHandler: { method: resources.gateway.afterHandler }
}
}
},
{
method: '*',
path: '/{path*}',
handler: {
proxy: {
mapUri: request => {
if (!isIpfs.ipfsSubdomain(request.url.toString())) {
throw Boom.notFound()
}

const cid = request.url.hostname.split('.')[0]
let uri = `${request.server.info.uri}/ipfs/${cid}`

if (request.url.pathname !== '/') {
uri += request.url.pathname
}

console.log(`${request.url} -> ${uri}`)
return { uri }
}
}
}
}
]
3 changes: 3 additions & 0 deletions src/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const Hapi = require('@hapi/hapi')
const Pino = require('hapi-pino')
const H2o2 = require('@hapi/h2o2')
const debug = require('debug')
const multiaddr = require('multiaddr')
const toMultiaddr = require('uri-to-multiaddr')
Expand Down Expand Up @@ -133,6 +134,8 @@ class HttpApi {
}
})

await server.register(H2o2)

server.route(require('./gateway/routes'))

return server
Expand Down