@@ -5,6 +5,8 @@ const Pino = require('hapi-pino')
55const debug = require ( 'debug' )
66const multiaddr = require ( 'multiaddr' )
77const promisify = require ( 'promisify-es6' )
8+ const toUri = require ( 'multiaddr-to-uri' )
9+ const toMultiaddr = require ( 'uri-to-multiaddr' )
810
911const IPFS = require ( '../core' )
1012const WStar = require ( 'libp2p-webrtc-star' )
@@ -14,9 +16,17 @@ const WS = require('libp2p-websockets')
1416const Bootstrap = require ( 'libp2p-bootstrap' )
1517const errorHandler = require ( './error-handler' )
1618
17- function uriToMultiaddr ( uri ) {
18- const ipPort = uri . split ( '/' ) [ 2 ] . split ( ':' )
19- return `/ip4/${ ipPort [ 0 ] } /tcp/${ ipPort [ 1 ] } `
19+ function hapiInfoToMultiaddr ( 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 )
2030}
2131
2232class HttpApi {
@@ -82,7 +92,7 @@ class HttpApi {
8292 const apiAddr = config . Addresses . API . split ( '/' )
8393 const apiServer = await this . _createApiServer ( apiAddr [ 2 ] , apiAddr [ 4 ] , ipfs )
8494 await apiServer . start ( )
85- apiServer . info . ma = uriToMultiaddr ( apiServer . info . uri )
95+ apiServer . info . ma = hapiInfoToMultiaddr ( apiServer . info )
8696 this . _apiServer = apiServer
8797
8898 // for the CLI to know the where abouts of the API
@@ -91,12 +101,12 @@ class HttpApi {
91101 const gatewayAddr = config . Addresses . Gateway . split ( '/' )
92102 const gatewayServer = await this . _createGatewayServer ( gatewayAddr [ 2 ] , gatewayAddr [ 4 ] , ipfs )
93103 await gatewayServer . start ( )
94- gatewayServer . info . ma = uriToMultiaddr ( gatewayServer . info . uri )
104+ gatewayServer . info . ma = hapiInfoToMultiaddr ( gatewayServer . info )
95105 this . _gatewayServer = gatewayServer
96106
97107 ipfs . _print ( 'API listening on %s' , apiServer . info . ma )
98108 ipfs . _print ( 'Gateway (read only) listening on %s' , gatewayServer . info . ma )
99- ipfs . _print ( 'Web UI available at %s' , apiServer . info . uri + '/webui' )
109+ ipfs . _print ( 'Web UI available at %s' , toUri ( apiServer . info . ma ) + '/webui' )
100110 this . _log ( 'started' )
101111 return this
102112 }
0 commit comments