Skip to content

Commit

Permalink
chore: fix undiallable api and gateway files
Browse files Browse the repository at this point in the history
Fixes ipfs#9232
  • Loading branch information
Jorropo committed Aug 30, 2022
1 parent 9241813 commit cf362a7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
25 changes: 21 additions & 4 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -673,8 +673,8 @@ func serveHTTPApi(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, error
return nil, fmt.Errorf("serveHTTPApi: ConstructNode() failed: %s", err)
}

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

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

func rewriteMaddrToUseLocalhostIfItsAny(maddr ma.Multiaddr) ma.Multiaddr {
first, rest := ma.SplitFirst(maddr)

switch {
case first.Equal(manet.IP4Unspecified):
return manet.IP4Loopback.Encapsulate(rest)
case first.Equal(manet.IP6Unspecified):
return manet.IP6Loopback.Encapsulate(rest)
default:
return maddr // not ip
}
}

// printSwarmAddrs prints the addresses of the host
func printSwarmAddrs(node *core.IpfsNode) {
if !node.IsOnline {
Expand Down Expand Up @@ -808,7 +821,11 @@ func serveHTTPGateway(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, e
}

if len(listeners) > 0 {
if err := node.Repo.SetGatewayAddr(listeners[0].Addr()); err != nil {
addr, err := manet.ToNetAddr(rewriteMaddrToUseLocalhostIfItsAny(listeners[0].Multiaddr()))
if err != nil {
return nil, fmt.Errorf("serveHTTPGateway: manet.ToIP() failed: %w", err)
}
if err := node.Repo.SetGatewayAddr(addr); err != nil {
return nil, fmt.Errorf("serveHTTPGateway: SetGatewayAddr() failed: %w", err)
}
}
Expand All @@ -831,7 +848,7 @@ func serveHTTPGateway(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, e
return errc, nil
}

//collects options and opens the fuse mountpoint
// collects options and opens the fuse mountpoint
func mountFuse(req *cmds.Request, cctx *oldcmds.Context) error {
cfg, err := cctx.GetConfig()
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions test/sharness/t0064-api-file.sh
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,16 @@ test_expect_success "pin ls fails when daemon is running but API file is missing

test_kill_ipfs_daemon

APIPORT=32563

test_expect_success "Verify gateway file diallable while on unspecified" '
ipfs config Addresses.API /ip4/0.0.0.0/tcp/$APIPORT &&
test_launch_ipfs_daemon &&
cat "$IPFS_PATH/api" > api_file_actual &&
echo -n "http://127.0.0.1:$APIPORT" > api_file_expected &&
test_cmp api_file_expected api_file_actual
'

test_kill_ipfs_daemon

test_done
15 changes: 12 additions & 3 deletions test/sharness/t0110-gateway.sh
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,25 @@ test_expect_success "GET compact blocks succeeds" '
'

test_expect_success "Verify gateway file" '
cat "$IPFS_PATH/gateway" >> gateway_file_actual &&
echo -n "http://$GWAY_ADDR" >> gateway_daemon_actual &&
cat "$IPFS_PATH/gateway" > gateway_file_actual &&
echo -n "http://$GWAY_ADDR" > gateway_daemon_actual &&
test_cmp gateway_daemon_actual gateway_file_actual
'

test_kill_ipfs_daemon


GWPORT=32563

test_expect_success "Verify gateway file diallable while on unspecified" '
ipfs config Addresses.Gateway /ip4/0.0.0.0/tcp/$GWPORT &&
test_launch_ipfs_daemon &&
cat "$IPFS_PATH/gateway" > gateway_file_actual &&
echo -n "http://127.0.0.1:$GWPORT" > gateway_file_expected &&
test_cmp gateway_file_expected gateway_file_actual
'

test_kill_ipfs_daemon

test_expect_success "set up iptb testbed" '
iptb testbed create -type localipfs -count 5 -force -init &&
ipfsi 0 config Addresses.Gateway /ip4/127.0.0.1/tcp/$GWPORT &&
Expand Down

0 comments on commit cf362a7

Please sign in to comment.