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

Commit 83f0129

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

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
"mime-types": "^2.1.21",
148148
"mkdirp": "~0.5.1",
149149
"multiaddr": "^6.0.0",
150-
"multiaddr-to-uri": "^4.0.0",
150+
"multiaddr-to-uri": "^4.0.1",
151151
"multibase": "~0.6.0",
152152
"multihashes": "~0.4.14",
153153
"multihashing-async": "~0.5.1",
@@ -175,6 +175,7 @@
175175
"tar-stream": "^2.0.0",
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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const Pino = require('hapi-pino')
55
const debug = require('debug')
66
const multiaddr = require('multiaddr')
77
const promisify = require('promisify-es6')
8+
const toUri = require('multiaddr-to-uri')
9+
const toMultiaddr = require('uri-to-multiaddr')
810

911
const IPFS = require('../core')
1012
const WStar = require('libp2p-webrtc-star')
@@ -14,9 +16,17 @@ const WS = require('libp2p-websockets')
1416
const Bootstrap = require('libp2p-bootstrap')
1517
const 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

2232
class 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

Comments
 (0)