Skip to content

Commit cf362a7

Browse files
committed
chore: fix undiallable api and gateway files
Fixes #9232
1 parent 9241813 commit cf362a7

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

cmd/ipfs/daemon.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -673,8 +673,8 @@ func serveHTTPApi(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, error
673673
return nil, fmt.Errorf("serveHTTPApi: ConstructNode() failed: %s", err)
674674
}
675675

676-
if err := node.Repo.SetAPIAddr(listeners[0].Multiaddr()); err != nil {
677-
return nil, fmt.Errorf("serveHTTPApi: SetAPIAddr() failed: %s", err)
676+
if err := node.Repo.SetAPIAddr(rewriteMaddrToUseLocalhostIfItsAny(listeners[0].Multiaddr())); err != nil {
677+
return nil, fmt.Errorf("serveHTTPApi: SetAPIAddr() failed: %w", err)
678678
}
679679

680680
errc := make(chan error)
@@ -695,6 +695,19 @@ func serveHTTPApi(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, error
695695
return errc, nil
696696
}
697697

698+
func rewriteMaddrToUseLocalhostIfItsAny(maddr ma.Multiaddr) ma.Multiaddr {
699+
first, rest := ma.SplitFirst(maddr)
700+
701+
switch {
702+
case first.Equal(manet.IP4Unspecified):
703+
return manet.IP4Loopback.Encapsulate(rest)
704+
case first.Equal(manet.IP6Unspecified):
705+
return manet.IP6Loopback.Encapsulate(rest)
706+
default:
707+
return maddr // not ip
708+
}
709+
}
710+
698711
// printSwarmAddrs prints the addresses of the host
699712
func printSwarmAddrs(node *core.IpfsNode) {
700713
if !node.IsOnline {
@@ -808,7 +821,11 @@ func serveHTTPGateway(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, e
808821
}
809822

810823
if len(listeners) > 0 {
811-
if err := node.Repo.SetGatewayAddr(listeners[0].Addr()); err != nil {
824+
addr, err := manet.ToNetAddr(rewriteMaddrToUseLocalhostIfItsAny(listeners[0].Multiaddr()))
825+
if err != nil {
826+
return nil, fmt.Errorf("serveHTTPGateway: manet.ToIP() failed: %w", err)
827+
}
828+
if err := node.Repo.SetGatewayAddr(addr); err != nil {
812829
return nil, fmt.Errorf("serveHTTPGateway: SetGatewayAddr() failed: %w", err)
813830
}
814831
}
@@ -831,7 +848,7 @@ func serveHTTPGateway(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, e
831848
return errc, nil
832849
}
833850

834-
//collects options and opens the fuse mountpoint
851+
// collects options and opens the fuse mountpoint
835852
func mountFuse(req *cmds.Request, cctx *oldcmds.Context) error {
836853
cfg, err := cctx.GetConfig()
837854
if err != nil {

test/sharness/t0064-api-file.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,16 @@ test_expect_success "pin ls fails when daemon is running but API file is missing
9797

9898
test_kill_ipfs_daemon
9999

100+
APIPORT=32563
101+
102+
test_expect_success "Verify gateway file diallable while on unspecified" '
103+
ipfs config Addresses.API /ip4/0.0.0.0/tcp/$APIPORT &&
104+
test_launch_ipfs_daemon &&
105+
cat "$IPFS_PATH/api" > api_file_actual &&
106+
echo -n "http://127.0.0.1:$APIPORT" > api_file_expected &&
107+
test_cmp api_file_expected api_file_actual
108+
'
109+
110+
test_kill_ipfs_daemon
111+
100112
test_done

test/sharness/t0110-gateway.sh

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,16 +288,25 @@ test_expect_success "GET compact blocks succeeds" '
288288
'
289289

290290
test_expect_success "Verify gateway file" '
291-
cat "$IPFS_PATH/gateway" >> gateway_file_actual &&
292-
echo -n "http://$GWAY_ADDR" >> gateway_daemon_actual &&
291+
cat "$IPFS_PATH/gateway" > gateway_file_actual &&
292+
echo -n "http://$GWAY_ADDR" > gateway_daemon_actual &&
293293
test_cmp gateway_daemon_actual gateway_file_actual
294294
'
295295

296296
test_kill_ipfs_daemon
297297

298-
299298
GWPORT=32563
300299

300+
test_expect_success "Verify gateway file diallable while on unspecified" '
301+
ipfs config Addresses.Gateway /ip4/0.0.0.0/tcp/$GWPORT &&
302+
test_launch_ipfs_daemon &&
303+
cat "$IPFS_PATH/gateway" > gateway_file_actual &&
304+
echo -n "http://127.0.0.1:$GWPORT" > gateway_file_expected &&
305+
test_cmp gateway_file_expected gateway_file_actual
306+
'
307+
308+
test_kill_ipfs_daemon
309+
301310
test_expect_success "set up iptb testbed" '
302311
iptb testbed create -type localipfs -count 5 -force -init &&
303312
ipfsi 0 config Addresses.Gateway /ip4/127.0.0.1/tcp/$GWPORT &&

0 commit comments

Comments
 (0)