mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-03 00:27:38 +00:00
7cc32c4dda
* remove unused code * remove defer use in loop * Remove unused methods and constants * gofmt and gaz * nilness check * remove unused args * Add TODO for refactoring subscribeWithBase to remove unused arg. It seems too involved to include in this sweeping PR. https://github.com/prysmaticlabs/prysm/issues/7437 * replace empty slice declaration * Remove unnecessary type conversions * remove redundant type declaration * rename receivers to be consistent * Remove bootnode query tool. It is now obsolete by discv5 * Remove relay node. It is no longer used or supported * Revert "Remove relay node. It is no longer used or supported" This reverts commit 4bd7717334dad85ef4766ed9bc4da711fb5fa810. * Delete unused test directory * Delete unsupported gcp startup script * Delete old k8s script * build fixes * fix build * go mod tidy * revert slasher/db/kv/block_header.go * fix build * remove redundant nil check * combine func args Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> Co-authored-by: Victor Farazdagi <simple.square@gmail.com>
105 lines
2.1 KiB
Go
105 lines
2.1 KiB
Go
// Package iputils contains useful functions for ip address formatting.
|
|
package iputils
|
|
|
|
import (
|
|
"net"
|
|
"sort"
|
|
)
|
|
|
|
// ExternalIPv4 returns the first IPv4 available.
|
|
func ExternalIPv4() (string, error) {
|
|
ips, err := retrieveIPAddrs()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if len(ips) == 0 {
|
|
return "127.0.0.1", nil
|
|
}
|
|
for _, ip := range ips {
|
|
ip = ip.To4()
|
|
if ip == nil {
|
|
continue // not an ipv4 address
|
|
}
|
|
return ip.String(), nil
|
|
}
|
|
return "127.0.0.1", nil
|
|
}
|
|
|
|
// ExternalIPv6 retrieves any allocated IPv6 addresses
|
|
// from the accessible network interfaces.
|
|
func ExternalIPv6() (string, error) {
|
|
ips, err := retrieveIPAddrs()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if len(ips) == 0 {
|
|
return "127.0.0.1", nil
|
|
}
|
|
for _, ip := range ips {
|
|
if ip.To4() != nil {
|
|
continue // not an ipv6 address
|
|
}
|
|
if ip.To16() == nil {
|
|
continue
|
|
}
|
|
return ip.String(), nil
|
|
}
|
|
return "127.0.0.1", nil
|
|
}
|
|
|
|
// ExternalIP returns the first IPv4/IPv6 available.
|
|
func ExternalIP() (string, error) {
|
|
ips, err := retrieveIPAddrs()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if len(ips) == 0 {
|
|
return "127.0.0.1", nil
|
|
}
|
|
return ips[0].String(), nil
|
|
}
|
|
|
|
// retrieveIP returns all the valid IPs available.
|
|
func retrieveIPAddrs() ([]net.IP, error) {
|
|
ifaces, err := net.Interfaces()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var ipAddrs []net.IP
|
|
for _, iface := range ifaces {
|
|
if iface.Flags&net.FlagUp == 0 {
|
|
continue // interface down
|
|
}
|
|
if iface.Flags&net.FlagLoopback != 0 {
|
|
continue // loopback interface
|
|
}
|
|
addrs, err := iface.Addrs()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, addr := range addrs {
|
|
var ip net.IP
|
|
switch v := addr.(type) {
|
|
case *net.IPNet:
|
|
ip = v.IP
|
|
case *net.IPAddr:
|
|
ip = v.IP
|
|
}
|
|
if ip == nil || ip.IsLoopback() || ip.IsLinkLocalUnicast() {
|
|
continue
|
|
}
|
|
ipAddrs = append(ipAddrs, ip)
|
|
}
|
|
}
|
|
return SortAddresses(ipAddrs), nil
|
|
}
|
|
|
|
// SortAddresses sorts a set of addresses in the order of
|
|
// ipv4 -> ipv6.
|
|
func SortAddresses(ipAddrs []net.IP) []net.IP {
|
|
sort.Slice(ipAddrs, func(i, j int) bool {
|
|
return ipAddrs[i].To4() != nil && ipAddrs[j].To4() == nil
|
|
})
|
|
return ipAddrs
|
|
}
|