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

feat: webui v2.7.5, with feeling #2984

Merged
merged 3 commits into from Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions packages/ipfs/src/http/api/routes/index.js
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
@@ -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}/`)
}
}
]