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

fix: print valid ipv6 multiaddrs in terminal #1854

Merged
merged 1 commit into from
Feb 11, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
"mime-types": "^2.1.21",
"mkdirp": "~0.5.1",
"multiaddr": "^6.0.0",
"multiaddr-to-uri": "^4.0.0",
"multiaddr-to-uri": "^4.0.1",
"multibase": "~0.6.0",
"multihashes": "~0.4.14",
"multihashing-async": "~0.5.1",
Expand Down Expand Up @@ -175,6 +175,7 @@
"tar-stream": "^2.0.0",
"temp": "~0.9.0",
"update-notifier": "^2.5.0",
"uri-to-multiaddr": "^3.0.1",
"varint": "^5.0.0",
"yargs": "^12.0.5",
"yargs-promise": "^1.1.0"
Expand Down
22 changes: 16 additions & 6 deletions src/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const Pino = require('hapi-pino')
const debug = require('debug')
const multiaddr = require('multiaddr')
const promisify = require('promisify-es6')
const toUri = require('multiaddr-to-uri')
const toMultiaddr = require('uri-to-multiaddr')

const IPFS = require('../core')
const WStar = require('libp2p-webrtc-star')
Expand All @@ -14,9 +16,17 @@ const WS = require('libp2p-websockets')
const Bootstrap = require('libp2p-bootstrap')
const errorHandler = require('./error-handler')

function uriToMultiaddr (uri) {
const ipPort = uri.split('/')[2].split(':')
return `/ip4/${ipPort[0]}/tcp/${ipPort[1]}`
function hapiInfoToMultiaddr (info) {
let hostname = info.host
let uri = info.uri
// ipv6 fix
if (hostname.includes(':') && !hostname.startsWith('[')) {
// hapi 16 produces invalid URI for ipv6
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still needed? We use hapi 18 now...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly still needed: I tried with vanilla hapi 18 and uri inside of apiServer.info was still broken (missing brackets), which produced broken ma:

{
  "created": 1549909555055,
  "started": 1549909555222,
  "host": "::1",
  "port": 5002,
  "protocol": "http",
  "id": "foo:5989:js0o4qjz",
  "uri": "http://::1:5002",
  "address": "::1",
  "ma": "/ip4//tcp/"
}

Due to this the line should stay :)

Suggested change
// hapi 16 produces invalid URI for ipv6
// hapi 18 produces invalid URI for ipv6

// we fix it here by restoring missing square brackets
hostname = `[${hostname}]`
uri = uri.replace(`://${info.host}`, `://${hostname}`)
}
return toMultiaddr(uri)
}

class HttpApi {
Expand Down Expand Up @@ -82,7 +92,7 @@ class HttpApi {
const apiAddr = config.Addresses.API.split('/')
const apiServer = await this._createApiServer(apiAddr[2], apiAddr[4], ipfs)
await apiServer.start()
apiServer.info.ma = uriToMultiaddr(apiServer.info.uri)
apiServer.info.ma = hapiInfoToMultiaddr(apiServer.info)
this._apiServer = apiServer

// for the CLI to know the where abouts of the API
Expand All @@ -91,12 +101,12 @@ class HttpApi {
const gatewayAddr = config.Addresses.Gateway.split('/')
const gatewayServer = await this._createGatewayServer(gatewayAddr[2], gatewayAddr[4], ipfs)
await gatewayServer.start()
gatewayServer.info.ma = uriToMultiaddr(gatewayServer.info.uri)
gatewayServer.info.ma = hapiInfoToMultiaddr(gatewayServer.info)
this._gatewayServer = gatewayServer

ipfs._print('API listening on %s', apiServer.info.ma)
ipfs._print('Gateway (read only) listening on %s', gatewayServer.info.ma)
ipfs._print('Web UI available at %s', apiServer.info.uri + '/webui')
ipfs._print('Web UI available at %s', toUri(apiServer.info.ma) + '/webui')
this._log('started')
return this
}
Expand Down