71 lines
1.9 KiB
Go
71 lines
1.9 KiB
Go
package peerdiscovery
|
|
|
|
import (
|
|
math_rand "math/rand"
|
|
"net"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// GetLocalIP returns the local ip address
|
|
func GetLocalIP() string {
|
|
addrs, err := net.InterfaceAddrs()
|
|
if err != nil {
|
|
return "localhost"
|
|
}
|
|
bestIP := "localhost"
|
|
for _, address := range addrs {
|
|
// check the address type and if it is not a loopback the display it
|
|
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
|
if ipnet.IP.To4() != nil && (strings.Contains(ipnet.IP.String(), "192.168.1") || strings.Contains(ipnet.IP.String(), "192.168")) {
|
|
return ipnet.IP.String()
|
|
}
|
|
}
|
|
}
|
|
return bestIP
|
|
}
|
|
|
|
// GetLocalIPs returns the local ip address
|
|
func GetLocalIPs() (ips map[string]struct{}) {
|
|
ips = make(map[string]struct{})
|
|
ips["localhost"] = struct{}{}
|
|
ips["127.0.0.1"] = struct{}{}
|
|
addrs, err := net.InterfaceAddrs()
|
|
if err != nil {
|
|
return
|
|
}
|
|
for _, address := range addrs {
|
|
ips[strings.Split(address.String(), "/")[0]] = struct{}{}
|
|
}
|
|
return
|
|
}
|
|
|
|
// src is seeds the random generator for generating random strings
|
|
var src = math_rand.NewSource(time.Now().UnixNano())
|
|
|
|
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
const (
|
|
letterIdxBits = 6 // 6 bits to represent a letter index
|
|
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
|
|
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
|
|
)
|
|
|
|
// RandStringBytesMaskImprSrc prints a random string
|
|
func RandStringBytesMaskImprSrc(n int) string {
|
|
b := make([]byte, n)
|
|
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
|
|
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
|
|
if remain == 0 {
|
|
cache, remain = src.Int63(), letterIdxMax
|
|
}
|
|
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
|
|
b[i] = letterBytes[idx]
|
|
i--
|
|
}
|
|
cache >>= letterIdxBits
|
|
remain--
|
|
}
|
|
|
|
return string(b)
|
|
}
|