Skip to content

Commit

Permalink
Merge pull request #250 from SiaFoundation/nate/auto-announce-validate
Browse files Browse the repository at this point in the history
Pre-validate auto announcement
  • Loading branch information
n8maninger authored Dec 29, 2023
2 parents ee75b69 + 70a5347 commit 71eeb1e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 7 additions & 1 deletion host/settings/announce.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ func (cm *ConfigManager) ProcessConsensusChange(cc modules.ConsensusChange) {
// get the current net address
cm.mu.Lock()
defer cm.mu.Unlock()

if err := validateNetAddress(cm.settings.NetAddress); err != nil {
log.Debug("skipping auto announcement for invalid net address", zap.Error(err))
return
}

currentNetAddress := cm.settings.NetAddress
cm.scanHeight = uint64(cc.BlockHeight)
timestamp := time.Unix(int64(cc.AppliedBlocks[len(cc.AppliedBlocks)-1].Timestamp), 0)
Expand All @@ -168,7 +174,7 @@ func (cm *ConfigManager) ProcessConsensusChange(cc modules.ConsensusChange) {

// if the address hasn't changed, don't reannounce
if cm.scanHeight < nextAnnounceHeight && currentNetAddress == lastAnnouncement.Address {
log.Debug("skipping announcement")
log.Debug("skipping announcement for unchanged address")
return
}

Expand Down
5 changes: 5 additions & 0 deletions host/settings/netaddress_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package settings

import (
"errors"
"fmt"
"net"
)
Expand All @@ -11,6 +12,10 @@ func validateNetAddress(netaddress string) error {
addr, _, err := net.SplitHostPort(netaddress)
if err != nil {
return fmt.Errorf("invalid net address %q: net addresses must contain an IP and port: %w", netaddress, err)
} else if addr == "" {
return errors.New("empty net address")
} else if addr == "localhost" {
return errors.New("net address cannot be localhost")
}

ip := net.ParseIP(addr)
Expand Down

0 comments on commit 71eeb1e

Please sign in to comment.