3
3
const series = require ( 'async/series' )
4
4
const Hapi = require ( 'hapi' )
5
5
const debug = require ( 'debug' )
6
- const multiaddr = require ( 'multiaddr' )
6
+ const toUri = require ( 'multiaddr-to-uri' )
7
+ const toMultiaddr = require ( 'uri-to-multiaddr' )
7
8
const setHeader = require ( 'hapi-set-header' )
8
9
const once = require ( 'once' )
9
10
@@ -15,9 +16,17 @@ const WS = require('libp2p-websockets')
15
16
const Bootstrap = require ( 'libp2p-bootstrap' )
16
17
const errorHandler = require ( './error-handler' )
17
18
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 )
21
30
}
22
31
23
32
function HttpApi ( repo , config , cliArgs ) {
@@ -162,13 +171,12 @@ function HttpApi (repo, config, cliArgs) {
162
171
( cb ) => {
163
172
const api = this . server . select ( 'API' )
164
173
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 )
168
176
169
177
this . node . _print ( 'API listening on %s' , api . info . ma )
170
178
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' )
172
180
173
181
// for the CLI to know the where abouts of the API
174
182
this . node . _repo . apiAddr . set ( api . info . ma , cb )
0 commit comments