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

Commit 573fa4b

Browse files
author
Alan Shaw
committed
feat: act as HTTP PROXY for http://<cidv1b32>.ipfs.localhost
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 ipfs/kubo#5982 License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent 26b92a1 commit 573fa4b

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"dependencies": {
6060
"@hapi/ammo": "^3.1.0",
6161
"@hapi/boom": "^7.4.2",
62+
"@hapi/h2o2": "^8.3.0",
6263
"@hapi/hapi": "^18.3.1",
6364
"@hapi/joi": "^15.0.1",
6465
"array-shuffle": "^1.0.1",

src/http/gateway/routes/gateway.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
'use strict'
22

33
const Joi = require('@hapi/joi')
4+
const Boom = require('@hapi/boom')
45
const resources = require('../resources')
6+
const isIpfs = require('is-ipfs')
57

68
module.exports = [
79
{
@@ -39,5 +41,28 @@ module.exports = [
3941
onPostHandler: { method: resources.gateway.afterHandler }
4042
}
4143
}
44+
},
45+
{
46+
method: '*',
47+
path: '/{path*}',
48+
handler: {
49+
proxy: {
50+
mapUri: request => {
51+
if (!isIpfs.ipfsSubdomain(request.url.toString())) {
52+
throw Boom.notFound()
53+
}
54+
55+
const cid = request.url.hostname.split('.')[0]
56+
let uri = `${request.server.info.uri}/ipfs/${cid}`
57+
58+
if (request.url.pathname !== '/') {
59+
uri += request.url.pathname
60+
}
61+
62+
console.log(`${request.url} -> ${uri}`)
63+
return { uri }
64+
}
65+
}
66+
}
4267
}
4368
]

src/http/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const Hapi = require('@hapi/hapi')
44
const Pino = require('hapi-pino')
5+
const H2o2 = require('@hapi/h2o2')
56
const debug = require('debug')
67
const multiaddr = require('multiaddr')
78
const toMultiaddr = require('uri-to-multiaddr')
@@ -133,6 +134,8 @@ class HttpApi {
133134
}
134135
})
135136

137+
await server.register(H2o2)
138+
136139
server.route(require('./gateway/routes'))
137140

138141
return server

0 commit comments

Comments
 (0)