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

Commit

Permalink
feat: act as HTTP PROXY for http://<cidv1b32>.ipfs.localhost
Browse files Browse the repository at this point in the history
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 <alan.shaw@protocol.ai>
  • Loading branch information
Alan Shaw committed Jul 12, 2019
1 parent 26b92a1 commit 573fa4b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
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

0 comments on commit 573fa4b

Please sign in to comment.