Skip to content

Commit

Permalink
config: Add warning if EndpointAddress and NetAddress ports are equal (
Browse files Browse the repository at this point in the history
…#6006)

Co-authored-by: Pavel Zbitskiy <[email protected]>
  • Loading branch information
hsoerensen and algorandskiy authored Jun 12, 2024
1 parent dbe60ee commit 4e116cb
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions daemon/algod/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"net"
"net/http"
_ "net/http/pprof" // net/http/pprof is for registering the pprof URLs with the web server, so http://localhost:8080/debug/pprof/ works.
"net/url"
"os"
"os/signal"
"path/filepath"
Expand Down Expand Up @@ -269,6 +270,19 @@ func makeListener(addr string) (net.Listener, error) {
return net.Listen("tcp", addr)
}

// helper to get port from an address
func getPortFromAddress(addr string) (string, error) {
u, err := url.Parse(addr)
if err == nil && u.Scheme != "" {
addr = u.Host
}
_, port, err := net.SplitHostPort(addr)
if err != nil {
return "", fmt.Errorf("Error parsing address: %v", err)
}
return port, nil
}

// Start starts a Node instance and its network services
func (s *Server) Start() {
s.log.Info("Trying to start an Algorand node")
Expand Down Expand Up @@ -359,6 +373,20 @@ func (s *Server) Start() {
fmt.Printf("netlistenfile error: %v\n", err)
os.Exit(1)
}

addrPort, err := getPortFromAddress(addr)
if err != nil {
s.log.Warnf("Error getting port from EndpointAddress: %v", err)
}

listenAddrPort, err := getPortFromAddress(listenAddr)
if err != nil {
s.log.Warnf("Error getting port from NetAddress: %v", err)
}

if addrPort == listenAddrPort {
s.log.Warnf("EndpointAddress port %v matches NetAddress port %v. This may lead to unexpected results when accessing endpoints.", addrPort, listenAddrPort)
}
}

errChan := make(chan error, 1)
Expand Down

0 comments on commit 4e116cb

Please sign in to comment.