mirror of
https://gitee.com/johng/gf.git
synced 2024-12-04 13:18:01 +08:00
198 lines
4.5 KiB
Go
198 lines
4.5 KiB
Go
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
|
//
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
// If a copy of the MIT was not distributed with this file,
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
//
|
|
|
|
// Package gipv4 provides useful API for IPv4 address handling.
|
|
package gipv4
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/gogf/gf/text/gregex"
|
|
)
|
|
|
|
// Validate checks whether given <ip> a valid IPv4 address.
|
|
func Validate(ip string) bool {
|
|
return gregex.IsMatchString(`^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$`, ip)
|
|
}
|
|
|
|
// GetHostByName returns the IPv4 address corresponding to a given Internet host name.
|
|
func GetHostByName(hostname string) (string, error) {
|
|
ips, err := net.LookupIP(hostname)
|
|
if ips != nil {
|
|
for _, v := range ips {
|
|
if v.To4() != nil {
|
|
return v.String(), nil
|
|
}
|
|
}
|
|
return "", nil
|
|
}
|
|
return "", err
|
|
}
|
|
|
|
// GetHostsByName returns a list of IPv4 addresses corresponding to a given Internet host name.
|
|
func GetHostsByName(hostname string) ([]string, error) {
|
|
ips, err := net.LookupIP(hostname)
|
|
if ips != nil {
|
|
var ipStrings []string
|
|
for _, v := range ips {
|
|
if v.To4() != nil {
|
|
ipStrings = append(ipStrings, v.String())
|
|
}
|
|
}
|
|
return ipStrings, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
// GetNameByAddr returns the Internet host name corresponding to a given IP address.
|
|
func GetNameByAddr(ipAddress string) (string, error) {
|
|
names, err := net.LookupAddr(ipAddress)
|
|
if names != nil {
|
|
return strings.TrimRight(names[0], "."), nil
|
|
}
|
|
return "", err
|
|
}
|
|
|
|
// Ip2long converts ip address to an uint32 integer.
|
|
func Ip2long(ip string) uint32 {
|
|
netIp := net.ParseIP(ip)
|
|
if netIp == nil {
|
|
return 0
|
|
}
|
|
return binary.BigEndian.Uint32(netIp.To4())
|
|
}
|
|
|
|
// Long2ip converts an uint32 integer ip address to its string type address.
|
|
func Long2ip(long uint32) string {
|
|
ipByte := make([]byte, 4)
|
|
binary.BigEndian.PutUint32(ipByte, long)
|
|
return net.IP(ipByte).String()
|
|
}
|
|
|
|
// GetSegment returns the segment of given ip address.
|
|
// Eg: 192.168.2.102 -> 192.168.2
|
|
func GetSegment(ip string) string {
|
|
match, err := gregex.MatchString(`^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$`, ip)
|
|
if err != nil || len(match) < 4 {
|
|
return ""
|
|
}
|
|
return fmt.Sprintf("%s.%s.%s", match[1], match[2], match[3])
|
|
}
|
|
|
|
// ParseAddress parses <address> to its ip and port.
|
|
// Eg: 192.168.1.1:80 -> 192.168.1.1, 80
|
|
func ParseAddress(address string) (string, int) {
|
|
match, err := gregex.MatchString(`^(.+):(\d+)$`, address)
|
|
if err == nil {
|
|
i, _ := strconv.Atoi(match[2])
|
|
return match[1], i
|
|
}
|
|
return "", 0
|
|
}
|
|
|
|
// IntranetIP returns the first intranet ip of current machine.
|
|
func IntranetIP() (ip string, err error) {
|
|
ips, err := IntranetIPArray()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if len(ips) == 0 {
|
|
return "", errors.New("no intranet ip found")
|
|
}
|
|
return ips[0], nil
|
|
}
|
|
|
|
// IntranetIPArray returns the intranet ip list of current machine.
|
|
func IntranetIPArray() (ips []string, err error) {
|
|
ips = make([]string, 0)
|
|
ifaces, e := net.Interfaces()
|
|
if e != nil {
|
|
return ips, e
|
|
}
|
|
for _, iface := range ifaces {
|
|
if iface.Flags&net.FlagUp == 0 {
|
|
// interface down
|
|
continue
|
|
}
|
|
if iface.Flags&net.FlagLoopback != 0 {
|
|
// loopback interface
|
|
continue
|
|
}
|
|
// ignore warden bridge
|
|
if strings.HasPrefix(iface.Name, "w-") {
|
|
continue
|
|
}
|
|
addresses, e := iface.Addrs()
|
|
if e != nil {
|
|
return ips, e
|
|
}
|
|
for _, addr := range addresses {
|
|
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() {
|
|
continue
|
|
}
|
|
ip = ip.To4()
|
|
if ip == nil {
|
|
// not an ipv4 address
|
|
continue
|
|
}
|
|
ipStr := ip.String()
|
|
if IsIntranet(ipStr) {
|
|
ips = append(ips, ipStr)
|
|
}
|
|
}
|
|
}
|
|
return ips, nil
|
|
}
|
|
|
|
// IsIntranet checks and returns whether given ip an intranet ip.
|
|
//
|
|
// Local: 127.0.0.1
|
|
// A: 10.0.0.0--10.255.255.255
|
|
// B: 172.16.0.0--172.31.255.255
|
|
// C: 192.168.0.0--192.168.255.255
|
|
func IsIntranet(ip string) bool {
|
|
if ip == "127.0.0.1" {
|
|
return true
|
|
}
|
|
array := strings.Split(ip, ".")
|
|
if len(array) != 4 {
|
|
return false
|
|
}
|
|
// A
|
|
if array[0] == "10" || (array[0] == "192" && array[1] == "168") {
|
|
return true
|
|
}
|
|
// C
|
|
if array[0] == "192" && array[1] == "168" {
|
|
return true
|
|
}
|
|
// B
|
|
if array[0] == "172" {
|
|
second, err := strconv.ParseInt(array[1], 10, 64)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
if second >= 16 && second <= 31 {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|