123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- // 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
- import (
- "errors"
- "net"
- "strconv"
- "strings"
- )
- // GetIpArray retrieves and returns all the ip of current host.
- func GetIpArray() (ips []string, err error) {
- interfaceAddr, err := net.InterfaceAddrs()
- if err != nil {
- return nil, err
- }
- for _, address := range interfaceAddr {
- ipNet, isValidIpNet := address.(*net.IPNet)
- if isValidIpNet && !ipNet.IP.IsLoopback() {
- if ipNet.IP.To4() != nil {
- ips = append(ips, ipNet.IP.String())
- }
- }
- }
- return ips, nil
- }
- // GetIntranetIp retrieves and returns the first intranet ip of current machine.
- func GetIntranetIp() (ip string, err error) {
- ips, err := GetIntranetIpArray()
- if err != nil {
- return "", err
- }
- if len(ips) == 0 {
- return "", errors.New("no intranet ip found")
- }
- return ips[0], nil
- }
- // GetIntranetIpArray retrieves and returns the intranet ip list of current machine.
- func GetIntranetIpArray() (ips []string, err error) {
- interFaces, e := net.Interfaces()
- if e != nil {
- return ips, e
- }
- for _, interFace := range interFaces {
- if interFace.Flags&net.FlagUp == 0 {
- // interface down
- continue
- }
- if interFace.Flags&net.FlagLoopback != 0 {
- // loopback interface
- continue
- }
- // ignore warden bridge
- if strings.HasPrefix(interFace.Name, "w-") {
- continue
- }
- addresses, e := interFace.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
- }
|