2019-05-30 23:52:10 +01:00
|
|
|
package addr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
2021-04-27 08:32:47 +03:00
|
|
|
var privateBlocks []*net.IPNet
|
2019-05-30 23:52:10 +01:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
for _, b := range []string{"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "100.64.0.0/10", "fd00::/8"} {
|
|
|
|
if _, block, err := net.ParseCIDR(b); err == nil {
|
|
|
|
privateBlocks = append(privateBlocks, block)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-17 00:05:42 +08:00
|
|
|
// AppendPrivateBlocks append private network blocks
|
|
|
|
func AppendPrivateBlocks(bs ...string) {
|
|
|
|
for _, b := range bs {
|
|
|
|
if _, block, err := net.ParseCIDR(b); err == nil {
|
|
|
|
privateBlocks = append(privateBlocks, block)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-30 23:52:10 +01:00
|
|
|
func isPrivateIP(ipAddr string) bool {
|
|
|
|
ip := net.ParseIP(ipAddr)
|
|
|
|
for _, priv := range privateBlocks {
|
|
|
|
if priv.Contains(ip) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-01-14 13:23:16 +00:00
|
|
|
// IsLocal tells us whether an ip is local
|
|
|
|
func IsLocal(addr string) bool {
|
|
|
|
// extract the host
|
|
|
|
host, _, err := net.SplitHostPort(addr)
|
|
|
|
if err == nil {
|
|
|
|
addr = host
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if its localhost
|
|
|
|
if addr == "localhost" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// check against all local ips
|
|
|
|
for _, ip := range IPs() {
|
|
|
|
if addr == ip {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-05-30 23:52:10 +01:00
|
|
|
// Extract returns a real ip
|
2021-02-14 16:16:01 +03:00
|
|
|
//nolint:gocyclo
|
2019-05-30 23:52:10 +01:00
|
|
|
func Extract(addr string) (string, error) {
|
|
|
|
// if addr specified then its returned
|
2019-07-17 10:38:50 +03:00
|
|
|
if len(addr) > 0 && (addr != "0.0.0.0" && addr != "[::]" && addr != "::") {
|
2019-05-30 23:52:10 +01:00
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ifaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to get interfaces! Err: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-12-03 22:59:44 +03:00
|
|
|
//nolint:prealloc
|
2019-05-30 23:52:10 +01:00
|
|
|
var addrs []net.Addr
|
2019-08-27 23:33:30 +08:00
|
|
|
var loAddrs []net.Addr
|
2019-05-30 23:52:10 +01:00
|
|
|
for _, iface := range ifaces {
|
|
|
|
ifaceAddrs, err := iface.Addrs()
|
|
|
|
if err != nil {
|
2020-05-30 00:49:22 +08:00
|
|
|
// ignore error, interface can disappear from system
|
2019-05-30 23:52:10 +01:00
|
|
|
continue
|
|
|
|
}
|
2019-08-26 14:37:49 +08:00
|
|
|
if iface.Flags&net.FlagLoopback != 0 {
|
2019-08-27 23:33:30 +08:00
|
|
|
loAddrs = append(loAddrs, ifaceAddrs...)
|
2019-08-26 14:37:49 +08:00
|
|
|
continue
|
|
|
|
}
|
2019-05-30 23:52:10 +01:00
|
|
|
addrs = append(addrs, ifaceAddrs...)
|
|
|
|
}
|
2019-08-27 23:33:30 +08:00
|
|
|
addrs = append(addrs, loAddrs...)
|
2019-05-30 23:52:10 +01:00
|
|
|
|
2020-03-07 21:05:00 +03:00
|
|
|
var ipAddr string
|
|
|
|
var publicIP string
|
2019-05-30 23:52:10 +01:00
|
|
|
|
|
|
|
for _, rawAddr := range addrs {
|
|
|
|
var ip net.IP
|
|
|
|
switch addr := rawAddr.(type) {
|
|
|
|
case *net.IPAddr:
|
|
|
|
ip = addr.IP
|
|
|
|
case *net.IPNet:
|
|
|
|
ip = addr.IP
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isPrivateIP(ip.String()) {
|
2020-03-07 21:05:00 +03:00
|
|
|
publicIP = ip.String()
|
2019-05-30 23:52:10 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-03-07 21:05:00 +03:00
|
|
|
ipAddr = ip.String()
|
2019-05-30 23:52:10 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// return private ip
|
2020-03-07 21:05:00 +03:00
|
|
|
if len(ipAddr) > 0 {
|
|
|
|
a := net.ParseIP(ipAddr)
|
|
|
|
if a == nil {
|
|
|
|
return "", fmt.Errorf("ip addr %s is invalid", ipAddr)
|
|
|
|
}
|
|
|
|
return a.String(), nil
|
2019-05-30 23:52:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// return public or virtual ip
|
2020-03-07 21:05:00 +03:00
|
|
|
if len(publicIP) > 0 {
|
|
|
|
a := net.ParseIP(publicIP)
|
|
|
|
if a == nil {
|
|
|
|
return "", fmt.Errorf("ip addr %s is invalid", publicIP)
|
|
|
|
}
|
|
|
|
return a.String(), nil
|
2019-05-30 23:52:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("No IP address found, and explicit IP not provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
// IPs returns all known ips
|
|
|
|
func IPs() []string {
|
|
|
|
ifaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var ipAddrs []string
|
|
|
|
|
|
|
|
for _, i := range ifaces {
|
|
|
|
addrs, err := i.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-07-17 10:38:50 +03:00
|
|
|
// dont skip ipv6 addrs
|
|
|
|
/*
|
|
|
|
ip = ip.To4()
|
|
|
|
if ip == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
*/
|
2019-05-30 23:52:10 +01:00
|
|
|
|
|
|
|
ipAddrs = append(ipAddrs, ip.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ipAddrs
|
|
|
|
}
|