From 573fa4b1cf6462c2c7095a33f3f0c5faeb92ec46 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Fri, 12 Jul 2019 15:25:25 +0100 Subject: [PATCH] feat: act as HTTP PROXY for http://.ipfs.localhost MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PoC right now, but you get the idea. ``` ⨎ curl -v --proxy 'http://127.0.0.1:8180' 'http://bafybeih4mncb4apdnrvbnqkicldf74sfstykoryzi7noaf4njxkiuflnay.ipfs.localhost/658.png' > 658.png % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying 127.0.0.1... * TCP_NODELAY set * Connected to 127.0.0.1 (127.0.0.1) port 8180 (#0) > GET http://bafybeih4mncb4apdnrvbnqkicldf74sfstykoryzi7noaf4njxkiuflnay.ipfs.localhost/658.png HTTP/1.1 > Host: bafybeih4mncb4apdnrvbnqkicldf74sfstykoryzi7noaf4njxkiuflnay.ipfs.localhost > User-Agent: curl/7.54.0 > Accept: */* > Proxy-Connection: Keep-Alive > < HTTP/1.1 200 OK < content-type: application/octet-stream < cache-control: no-cache < Date: Fri, 12 Jul 2019 14:20:44 GMT < Connection: keep-alive < Transfer-Encoding: chunked < { [16211 bytes data] 100 853k 0 853k 0 0 30.0M 0 --:--:-- --:--:-- --:--:-- 30.8M * Connection #0 to host 127.0.0.1 left intact ``` refs https://github.com/ipfs/go-ipfs/issues/5982 License: MIT Signed-off-by: Alan Shaw --- package.json | 1 + src/http/gateway/routes/gateway.js | 25 +++++++++++++++++++++++++ src/http/index.js | 3 +++ 3 files changed, 29 insertions(+) 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