chore: fix undiallable api and gateway files
Fixes #9232
Jorropo committed
Aug 29, 2022 at 17:51 UTC
954c6cc52eef7eae551b547c9c9932e7422b1047
3 files changed
+45
-7
cmd/ipfs/daemon.go
+21
-4
@@ -673,8 +673,8 @@ func serveHTTPApi(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, error
673
return nil, fmt.Errorf("serveHTTPApi: ConstructNode() failed: %s", err)
674
}
675
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)
678
}
679
680
errc := make(chan error)
@@ -695,6 +695,19 @@ func serveHTTPApi(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, error
695
return errc, nil
696
}
697
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
+
711
// printSwarmAddrs prints the addresses of the host
712
func printSwarmAddrs(node *core.IpfsNode) {
713
if !node.IsOnline {
@@ -808,7 +821,11 @@ func serveHTTPGateway(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, e
821
}
822
823
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 {
829
return nil, fmt.Errorf("serveHTTPGateway: SetGatewayAddr() failed: %w", err)
830
}
831
}
@@ -831,7 +848,7 @@ func serveHTTPGateway(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, e
848
return errc, nil
849
}
850
834
-//collects options and opens the fuse mountpoint
851
+// collects options and opens the fuse mountpoint
852
func mountFuse(req *cmds.Request, cctx *oldcmds.Context) error {
853
cfg, err := cctx.GetConfig()
854
if err != nil {
test/sharness/t0064-api-file.sh
+12
@@ -97,4 +97,16 @@ test_expect_success "pin ls fails when daemon is running but API file is missing
97
98
test_kill_ipfs_daemon
99
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 "/ip4/127.0.0.1/tcp/$APIPORT" > api_file_expected &&
107
+ test_cmp api_file_expected api_file_actual
108
+'
109
+
110
+test_kill_ipfs_daemon
111
+
112
test_done
test/sharness/t0110-gateway.sh
+12
-3
@@ -288,16 +288,25 @@ test_expect_success "GET compact blocks succeeds" '
288
'
289
290
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 &&
293
test_cmp gateway_daemon_actual gateway_file_actual
294
'
295
296
test_kill_ipfs_daemon
297
298
-
298
GWPORT=32563
299
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
+
310
test_expect_success "set up iptb testbed" '
311
iptb testbed create -type localipfs -count 5 -force -init &&
312
ipfsi 0 config Addresses.Gateway /ip4/127.0.0.1/tcp/$GWPORT &&