diff --git a/package.json b/package.json index 7846878991..5ecc4dcb35 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/http/gateway/routes/gateway.js b/src/http/gateway/routes/gateway.js index 57d563aa6b..fdabcdaffa 100644 --- a/src/http/gateway/routes/gateway.js +++ b/src/http/gateway/routes/gateway.js @@ -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 = [ { @@ -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 } + } + } + } } ] diff --git a/src/http/index.js b/src/http/index.js index 38a9982e67..ea4de62e3d 100644 --- a/src/http/index.js +++ b/src/http/index.js @@ -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') @@ -133,6 +134,8 @@ class HttpApi { } }) + await server.register(H2o2) + server.route(require('./gateway/routes')) return server