2019-02-02 16:18:25 +08:00
|
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2017-12-29 16:03:30 +08:00
|
|
|
|
//
|
|
|
|
|
// 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,
|
2019-02-02 16:18:25 +08:00
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2017-12-29 16:03:30 +08:00
|
|
|
|
//
|
2017-12-31 18:19:58 +08:00
|
|
|
|
|
2019-01-15 23:27:47 +08:00
|
|
|
|
// Package gipv4 provides useful API for IPv4 address handling.
|
2017-12-14 17:32:51 +08:00
|
|
|
|
package gipv4
|
2017-11-23 10:21:28 +08:00
|
|
|
|
|
|
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
|
"encoding/binary"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net"
|
|
|
|
|
"regexp"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
|
|
|
|
|
"github.com/gogf/gf/text/gregex"
|
2017-11-23 10:21:28 +08:00
|
|
|
|
)
|
|
|
|
|
|
2017-12-29 15:42:42 +08:00
|
|
|
|
// 判断所给地址是否是一个IPv4地址
|
|
|
|
|
func Validate(ip string) bool {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
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)
|
2017-12-29 15:42:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-30 21:27:03 +08:00
|
|
|
|
// Get the IPv4 address corresponding to a given Internet host name.
|
|
|
|
|
func GetHostByName(hostname string) (string, error) {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
ips, err := net.LookupIP(hostname)
|
|
|
|
|
if ips != nil {
|
|
|
|
|
for _, v := range ips {
|
|
|
|
|
if v.To4() != nil {
|
|
|
|
|
return v.String(), nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return "", nil
|
|
|
|
|
}
|
|
|
|
|
return "", err
|
2019-01-30 21:27:03 +08:00
|
|
|
|
}
|
2017-11-23 10:21:28 +08:00
|
|
|
|
|
2019-01-30 21:27:03 +08:00
|
|
|
|
// Get a list of IPv4 addresses corresponding to a given Internet host name.
|
|
|
|
|
func GetHostsByName(hostname string) ([]string, error) {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
ips, err := net.LookupIP(hostname)
|
|
|
|
|
if ips != nil {
|
|
|
|
|
var ipStrs []string
|
|
|
|
|
for _, v := range ips {
|
|
|
|
|
if v.To4() != nil {
|
|
|
|
|
ipStrs = append(ipStrs, v.String())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ipStrs, nil
|
|
|
|
|
}
|
|
|
|
|
return nil, err
|
2019-01-30 21:27:03 +08:00
|
|
|
|
}
|
2017-11-23 10:21:28 +08:00
|
|
|
|
|
2019-01-30 21:27:03 +08:00
|
|
|
|
// Get the Internet host name corresponding to a given IP address.
|
|
|
|
|
func GetNameByAddr(ipAddress string) (string, error) {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
names, err := net.LookupAddr(ipAddress)
|
|
|
|
|
if names != nil {
|
|
|
|
|
return strings.TrimRight(names[0], "."), nil
|
|
|
|
|
}
|
|
|
|
|
return "", err
|
2019-01-30 21:27:03 +08:00
|
|
|
|
}
|
2017-11-23 10:21:28 +08:00
|
|
|
|
|
2019-01-30 21:27:03 +08:00
|
|
|
|
// IP字符串转为整形.
|
|
|
|
|
func Ip2long(ipAddress string) uint32 {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
ip := net.ParseIP(ipAddress)
|
|
|
|
|
if ip == nil {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
return binary.BigEndian.Uint32(ip.To4())
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ip整形转为字符串
|
2019-01-30 21:27:03 +08:00
|
|
|
|
func Long2ip(properAddress uint32) string {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
ipByte := make([]byte, 4)
|
|
|
|
|
binary.BigEndian.PutUint32(ipByte, properAddress)
|
|
|
|
|
return net.IP(ipByte).String()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获得ip的网段,例如:192.168.2.102 -> 192.168.2
|
|
|
|
|
func GetSegment(ip string) string {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
r := `^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$`
|
|
|
|
|
reg, err := regexp.Compile(r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
ips := reg.FindStringSubmatch(ip)
|
|
|
|
|
if ips == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return fmt.Sprintf("%s.%s.%s", ips[1], ips[2], ips[3])
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 解析地址,形如:192.168.1.1:80 -> 192.168.1.1, 80
|
|
|
|
|
func ParseAddress(addr string) (string, int) {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
r := `^(.+):(\d+)$`
|
|
|
|
|
reg, err := regexp.Compile(r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", 0
|
|
|
|
|
}
|
|
|
|
|
result := reg.FindStringSubmatch(addr)
|
|
|
|
|
if result != nil {
|
|
|
|
|
i, _ := strconv.Atoi(result[2])
|
|
|
|
|
return result[1], i
|
|
|
|
|
}
|
|
|
|
|
return "", 0
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取本地局域网ip列表
|
|
|
|
|
func IntranetIP() (ips []string, err error) {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
ips = make([]string, 0)
|
|
|
|
|
ifaces, e := net.Interfaces()
|
|
|
|
|
if e != nil {
|
|
|
|
|
return ips, e
|
|
|
|
|
}
|
|
|
|
|
for _, iface := range ifaces {
|
|
|
|
|
if iface.Flags&net.FlagUp == 0 {
|
|
|
|
|
continue // interface down
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if iface.Flags&net.FlagLoopback != 0 {
|
|
|
|
|
continue // loopback interface
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ignore warden bridge
|
|
|
|
|
if strings.HasPrefix(iface.Name, "w-") {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addrs, e := iface.Addrs()
|
|
|
|
|
if e != nil {
|
|
|
|
|
return ips, e
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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() {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ip = ip.To4()
|
|
|
|
|
if ip == nil {
|
|
|
|
|
continue // not an ipv4 address
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ipStr := ip.String()
|
|
|
|
|
if IsIntranet(ipStr) {
|
|
|
|
|
ips = append(ips, ipStr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ips, nil
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 判断所给ip是否为局域网ip
|
|
|
|
|
// 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(ipStr string) bool {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
// ip协议保留的局域网ip
|
|
|
|
|
if strings.HasPrefix(ipStr, "10.") || strings.HasPrefix(ipStr, "192.168.") {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
if strings.HasPrefix(ipStr, "172.") {
|
|
|
|
|
// 172.16.0.0 - 172.31.255.255
|
|
|
|
|
arr := strings.Split(ipStr, ".")
|
|
|
|
|
if len(arr) != 4 {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
second, err := strconv.ParseInt(arr[1], 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if second >= 16 && second <= 31 {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|