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

Commit

Permalink
feat: webui v2.7.5, with feeling (#2984)
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel authored Apr 28, 2020
1 parent b1fc655 commit 2e0a114
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 28 deletions.
10 changes: 6 additions & 4 deletions packages/ipfs/src/http/api/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

const Boom = require('@hapi/boom')

const METHODS = [
// RPC API requires POST, we block every other method
const BLOCKED_METHODS = [
'GET',
'PUT',
'PATCH',
Expand All @@ -27,7 +28,6 @@ const routes = [
...require('./files'),
...require('./pubsub'),
require('./debug'),
...require('./webui'),
...require('./dag'),
require('./dns'),
...require('./key'),
Expand All @@ -37,13 +37,15 @@ const routes = [
...require('./dht')
]

const extraRoutes = []
// webui is loaded from API port, but works over GET (not a part of RPC API)
const extraRoutes = [...require('./webui')]

const handler = () => {
throw Boom.methodNotAllowed()
}

METHODS.forEach(method => {
// add routes that return HTTP 504 for non-POST requests to RPC API
BLOCKED_METHODS.forEach(method => {
routes.forEach(route => {
extraRoutes.push({
method,
Expand Down
43 changes: 19 additions & 24 deletions packages/ipfs/src/http/api/routes/webui.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
'use strict'

const multiaddr = require('multiaddr')
const debug = require('debug')
const { gateway } = require('../../gateway/resources')
const log = debug('ipfs:webui:info')
log.error = debug('ipfs:webui:error')

const webuiCid = 'bafybeidatpz2hli6fgu3zul5woi27ujesdf5o5a7bu622qj6ugharciwjq' // v2.7.5

module.exports = [
{
method: '*',
path: '/webui',
async handler (request, h) {
let scheme = 'http'
let port
let host

try {
const { ipfs } = request.server.app
const gateway = await ipfs.config.get('Addresses.Gateway')
const address = multiaddr(gateway).nodeAddress()

port = address.port
host = address.address
} catch (err) {
// may not have gateway configured
log.error(err)

scheme = 'https'
port = 443
host = 'gateway.ipfs.io'
method: 'GET',
path: `/ipfs/${webuiCid}/{path*}`, // only the whitelisted webui is allowed on API port
options: {
handler: gateway.handler,
response: {
ranges: false // disable built-in support, handler does it manually
},
ext: {
onPostHandler: { method: gateway.afterHandler }
}

return h.redirect(`${scheme}://${host}:${port}/ipfs/Qmexhq2sBHnXQbvyP2GfUdbnY7HCagH2Mw5vUNSBn2nxip`)
}
},
{
method: 'GET',
path: '/webui/{slug?}', // optional slug makes it work with and without slash
handler (request, h) {
return h.redirect(`/ipfs/${webuiCid}/`)
}
}
]

0 comments on commit 2e0a114

Please sign in to comment.