Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-validate auto announcement #250

Merged
merged 2 commits into from Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion host/settings/announce.go
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
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