mirror of
https://github.com/luxfi/node.git
synced 2026-10-30 15:24:05 +00:00
Two packages were named for the same word and meant different things. network/ dialed peers, exchanged keys, throttled and tracked them; nets/ tracked which chains share a validator set and when they have all bootstrapped. One letter apart, and neither name said which was which. The peer layer is mesh — the word this estate already uses for it, in lux-rs/node's own mesh.rs. That frees network for the thing that is actually a network: a set of chains sharing a validator set. A network holds chains; a chain belongs to one network. github.com/luxfi/p2p stays what it is, the gossip library the VMs use. Three concerns, three names, none of them borrowed from another. Co-authored-by: Hanzo Dev <[email protected]>
71 lines
1.7 KiB
Go
71 lines
1.7 KiB
Go
// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package peer
|
|
|
|
import (
|
|
"errors"
|
|
"net"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
ErrInvalidHost = errors.New("invalid host")
|
|
ErrIPLiteralNotAllowed = errors.New("IP literals are not allowed")
|
|
)
|
|
|
|
// UnsignedHost represents a hostname-based endpoint claim.
|
|
type UnsignedHost struct {
|
|
Host string
|
|
Port uint16
|
|
Timestamp uint64
|
|
}
|
|
|
|
// CanonicalHost normalizes and validates a hostname.
|
|
// It rejects IP literals, trims whitespace and trailing dots, and lowercases.
|
|
func CanonicalHost(host string) (string, error) {
|
|
canonical := strings.TrimSpace(host)
|
|
if canonical == "" {
|
|
return "", ErrInvalidHost
|
|
}
|
|
|
|
// Strip IPv6 brackets if present.
|
|
if strings.HasPrefix(canonical, "[") && strings.HasSuffix(canonical, "]") {
|
|
canonical = strings.TrimPrefix(canonical, "[")
|
|
canonical = strings.TrimSuffix(canonical, "]")
|
|
}
|
|
|
|
// Reject IP literals (IPv4 or IPv6).
|
|
if ip := net.ParseIP(canonical); ip != nil {
|
|
return "", ErrIPLiteralNotAllowed
|
|
}
|
|
|
|
// Trim trailing dot for FQDNs.
|
|
canonical = strings.TrimSuffix(canonical, ".")
|
|
if canonical == "" || canonical == "." {
|
|
return "", ErrInvalidHost
|
|
}
|
|
|
|
labels := strings.Split(canonical, ".")
|
|
for _, label := range labels {
|
|
if label == "" {
|
|
return "", ErrInvalidHost
|
|
}
|
|
if len(label) > 63 {
|
|
return "", ErrInvalidHost
|
|
}
|
|
if label[0] == '-' || label[len(label)-1] == '-' {
|
|
return "", ErrInvalidHost
|
|
}
|
|
for i := 0; i < len(label); i++ {
|
|
ch := label[i]
|
|
if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '-' {
|
|
continue
|
|
}
|
|
return "", ErrInvalidHost
|
|
}
|
|
}
|
|
|
|
return strings.ToLower(canonical), nil
|
|
}
|