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

Commit 95cc1c2

Browse files
committed
fix: ipv6 multiaddr in stdout
Fixes #1853 License: MIT Signed-off-by: Marcin Rataj <[email protected]>
1 parent bbe561b commit 95cc1c2

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@
146146
"mime-types": "^2.1.21",
147147
"mkdirp": "~0.5.1",
148148
"multiaddr": "^6.0.0",
149-
"multiaddr-to-uri": "^4.0.0",
149+
"multiaddr-to-uri": "^4.0.1",
150150
"multibase": "~0.6.0",
151151
"multihashes": "~0.4.14",
152152
"multihashing-async": "~0.5.1",
@@ -175,6 +175,7 @@
175175
"tar-stream": "^1.6.2",
176176
"temp": "~0.9.0",
177177
"update-notifier": "^2.5.0",
178+
"uri-to-multiaddr": "^3.0.1",
178179
"varint": "^5.0.0",
179180
"yargs": "^12.0.5",
180181
"yargs-promise": "^1.1.0"

src/http/index.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
const series = require('async/series')
44
const Hapi = require('hapi')
55
const debug = require('debug')
6-
const multiaddr = require('multiaddr')
6+
const toUri = require('multiaddr-to-uri')
7+
const toMultiaddr = require('uri-to-multiaddr')
78
const setHeader = require('hapi-set-header')
89
const once = require('once')
910

@@ -15,9 +16,17 @@ const WS = require('libp2p-websockets')
1516
const Bootstrap = require('libp2p-bootstrap')
1617
const errorHandler = require('./error-handler')
1718

18-
function uriToMultiaddr (uri) {
19-
const ipPort = uri.split('/')[2].split(':')
20-
return `/ip4/${ipPort[0]}/tcp/${ipPort[1]}`
19+
function serverInfoToMultiaddr (info) {
20+
let hostname = info.host
21+
let uri = info.uri
22+
// ipv6 fix
23+
if (hostname.includes(':') && !hostname.startsWith('[')) {
24+
// hapi 16 produces invalid URI for ipv6
25+
// we fix it here by restoring missing square brackets
26+
hostname = `[${hostname}]`
27+
uri = uri.replace(`://${info.host}`, `://${hostname}`)
28+
}
29+
return toMultiaddr(uri)
2130
}
2231

2332
function HttpApi (repo, config, cliArgs) {
@@ -162,13 +171,12 @@ function HttpApi (repo, config, cliArgs) {
162171
(cb) => {
163172
const api = this.server.select('API')
164173
const gateway = this.server.select('Gateway')
165-
this.apiMultiaddr = multiaddr('/ip4/127.0.0.1/tcp/' + api.info.port)
166-
api.info.ma = uriToMultiaddr(api.info.uri)
167-
gateway.info.ma = uriToMultiaddr(gateway.info.uri)
174+
api.info.ma = serverInfoToMultiaddr(api.info)
175+
gateway.info.ma = serverInfoToMultiaddr(gateway.info)
168176

169177
this.node._print('API listening on %s', api.info.ma)
170178
this.node._print('Gateway (read only) listening on %s', gateway.info.ma)
171-
this.node._print('Web UI available at %s', api.info.uri + '/webui')
179+
this.node._print('Web UI available at %s', toUri(api.info.ma) + '/webui')
172180

173181
// for the CLI to know the where abouts of the API
174182
this.node._repo.apiAddr.set(api.info.ma, cb)

0 commit comments

Comments
 (0)