Skip to content

Commit

Permalink
Merge pull request #228 from SiaFoundation/nate/api-listen-localhost
Browse files Browse the repository at this point in the history
Listen on localhost for API
  • Loading branch information
n8maninger committed Dec 6, 2023
2 parents 8bc3737 + 632d009 commit 696e1cd
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/hostd/consts_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const (
logPathEnvVariable = "HOSTD_LOG_PATH"
configPathEnvVariable = "HOSTD_CONFIG_FILE"

defaultAPIAddr = ":9980"
defaultAPIAddr = "localhost:9980"
defaultGatewayAddr = ":9981"
defaultRHP2Addr = ":9982"
defaultRHP3TCPAddr = ":9983"
Expand Down
2 changes: 1 addition & 1 deletion cmd/hostd/consts_testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const (
logPathEnvVariable = "HOSTD_ZEN_LOG_PATH"
configPathEnvVariable = "HOSTD_ZEN_CONFIG_FILE"

defaultAPIAddr = ":9880"
defaultAPIAddr = "localhost:9880"
defaultGatewayAddr = ":9881"
defaultRHP2Addr = ":9882"
defaultRHP3TCPAddr = ":9883"
Expand Down
31 changes: 30 additions & 1 deletion cmd/hostd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,35 @@ func mustGetSeedPhrase(log *zap.Logger) string {
}
}

func startAPIListener(log *zap.Logger) (l net.Listener, err error) {
addr, port, err := net.SplitHostPort(cfg.HTTP.Address)
if err != nil {
return nil, fmt.Errorf("failed to parse API address: %w", err)
}

// if the address is not localhost, listen on the address as-is
if addr != "localhost" {
return net.Listen("tcp", cfg.HTTP.Address)
}

// localhost fails on some new installs of Windows 11, so try a few
// different addresses
tryAddresses := []string{
net.JoinHostPort("localhost", port), // original address
net.JoinHostPort("127.0.0.1", port), // IPv4 loopback
net.JoinHostPort("::1", port), // IPv6 loopback
}

for _, addr := range tryAddresses {
l, err = net.Listen("tcp", addr)
if err == nil {
return
}
log.Warn("failed to listen on address", zap.String("address", addr), zap.Error(err))
}
return
}

// mustSetWalletkey prompts the user to enter a wallet seed phrase if one is not
// already set via environment variable or config file.
func mustSetWalletkey(log *zap.Logger) {
Expand Down Expand Up @@ -311,7 +340,7 @@ func main() {
log.Fatal("unable to create config directory", zap.Error(err))
}

apiListener, err := net.Listen("tcp", cfg.HTTP.Address)
apiListener, err := startAPIListener(log)
if err != nil {
log.Fatal("failed to listen on API address", zap.Error(err), zap.String("address", cfg.HTTP.Address))
}
Expand Down
17 changes: 10 additions & 7 deletions host/settings/announce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,27 @@ func TestAutoAnnounce(t *testing.T) {
time.Sleep(time.Second)

// confirm the announcement
if err := node.MineBlocks(node.Address(), 2); err != nil {
if err := node.MineBlocks(node.Address(), 5); err != nil {
t.Fatal(err)
}
time.Sleep(time.Second)

lastAnnouncement, err := manager.LastAnnouncement()
if err != nil {
t.Fatal(err)
} else if lastAnnouncement.Index.Height != 101 {
t.Fatalf("expected height 100, got %v", lastAnnouncement.Index.Height)
} else if lastAnnouncement.Index.Height == 0 {
t.Fatalf("expected an announcement, got %v", lastAnnouncement.Index.Height)
} else if lastAnnouncement.Address != "foo.bar:1234" {
t.Fatal("announcement not updated")
}
lastHeight := lastAnnouncement.Index.Height

remainingBlocks := lastHeight + 100 - node.ChainManager().TipState().Index.Height
t.Log("remaining blocks:", remainingBlocks)

// mine until right before the next announcement to ensure that the
// announcement is not triggered early
if err := node.MineBlocks(node.Address(), 99); err != nil {
if err := node.MineBlocks(node.Address(), int(remainingBlocks-1)); err != nil {
t.Fatal(err)
}
time.Sleep(time.Second)
Expand All @@ -94,12 +97,12 @@ func TestAutoAnnounce(t *testing.T) {
}
time.Sleep(time.Second)

nextHeight := lastHeight + 1 + 100 // off-by-one because the announcement is mined in the next block
prevHeight := lastAnnouncement.Index.Height
lastAnnouncement, err = manager.LastAnnouncement()
if err != nil {
t.Fatal(err)
} else if lastAnnouncement.Index.Height != nextHeight {
t.Fatalf("expected height %v, got %v", nextHeight, lastAnnouncement.Index.Height)
} else if lastAnnouncement.Index.Height <= prevHeight {
t.Fatalf("expected a new announcement after %v, got %v", prevHeight, lastAnnouncement.Index.Height)
} else if lastAnnouncement.Address != "foo.bar:1234" {
t.Fatal("announcement not updated")
}
Expand Down
2 changes: 2 additions & 0 deletions internal/test/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"sync"
"time"

"go.sia.tech/core/types"
"go.sia.tech/siad/crypto"
Expand Down Expand Up @@ -135,6 +136,7 @@ func (m *Miner) Mine(addr types.Address, n int) error {
return fmt.Errorf("failed to mine block %v: %w", mined, errFailedToSolve)
}
mined++
time.Sleep(time.Millisecond)
}
return nil
}
Expand Down

0 comments on commit 696e1cd

Please sign in to comment.