github.com/whatap/golib@v0.0.22/util/iputil/IPUtil.go (about)

     1  package iputil
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/hex"
     6  	"net"
     7  	"strconv"
     8  	"strings"
     9  
    10  	"github.com/whatap/golib/io"
    11  )
    12  
    13  func ToStringFrInt(ip int32) string {
    14  	return ToString(io.ToBytesInt(ip))
    15  }
    16  
    17  func ToStringInt(ip int32) string {
    18  	return ToString(io.ToBytesInt(ip))
    19  }
    20  
    21  func ToString(ip []byte) string {
    22  	if ip == nil || len(ip) == 0 {
    23  		return "0.0.0.0"
    24  	}
    25  	var buffer bytes.Buffer
    26  	buffer.WriteString(strconv.Itoa(int(uint(ip[0]))))
    27  	buffer.WriteString(".")
    28  	buffer.WriteString(strconv.Itoa(int(uint(ip[1]))))
    29  	buffer.WriteString(".")
    30  	buffer.WriteString(strconv.Itoa(int(uint(ip[2]))))
    31  	buffer.WriteString(".")
    32  	buffer.WriteString(strconv.Itoa(int(uint(ip[3]))))
    33  	return buffer.String()
    34  }
    35  
    36  func ToBytes(ip string) []byte {
    37  	if ip == "" {
    38  		return []byte{0, 0, 0, 0}
    39  	}
    40  	result := []byte{0, 0, 0, 0}
    41  	s := strings.Split(ip, ".")
    42  	if len(s) != 4 {
    43  		return []byte{0, 0, 0, 0}
    44  	}
    45  	for i := 0; i < 4; i++ {
    46  		if val, err := strconv.Atoi(s[i]); err == nil {
    47  			result[i] = (byte)(val & 0xff)
    48  		}
    49  	}
    50  	return result
    51  }
    52  
    53  func ToBytesFrInt(ip int32) []byte {
    54  	return io.ToBytesInt(ip)
    55  }
    56  func ToInt(ip []byte) int32 {
    57  	return io.ToInt(ip, 0)
    58  }
    59  
    60  func IsOK(ip []byte) bool {
    61  	return ip != nil && len(ip) == 4
    62  }
    63  
    64  func IsNotLocal(ip []byte) bool {
    65  	return IsOK(ip) && uint(ip[0]) != 127
    66  }
    67  
    68  func ParseHexString(ipport string) ([]byte, error) {
    69  	words := strings.Split(ipport, ":")
    70  	parsedbytes, err := hex.DecodeString(words[0])
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  	parsedLength := len(parsedbytes)
    75  	ipbytes := make([]byte, 6)
    76  	ipbytes[3] = parsedbytes[parsedLength-4]
    77  	ipbytes[2] = parsedbytes[parsedLength-3]
    78  	ipbytes[1] = parsedbytes[parsedLength-2]
    79  	ipbytes[0] = parsedbytes[parsedLength-1]
    80  
    81  	portbytes, err := hex.DecodeString(words[1])
    82  	ipbytes[4] = portbytes[0]
    83  	ipbytes[5] = portbytes[1]
    84  
    85  	return ipbytes, nil
    86  }
    87  
    88  func GetIPsToString() string {
    89  	rt := make([]string, 0)
    90  
    91  	ifaces, err := net.Interfaces()
    92  	if err != nil {
    93  		return ""
    94  	}
    95  
    96  	for _, i := range ifaces {
    97  		if i.Flags&net.FlagUp != 1 {
    98  			continue
    99  		}
   100  
   101  		addrs, err := i.Addrs()
   102  		if err != nil {
   103  			continue
   104  		}
   105  
   106  		for _, a := range addrs {
   107  			switch v := a.(type) {
   108  			case *net.IPAddr:
   109  				if !v.IP.IsLoopback() {
   110  					rt = append(rt, v.IP.String())
   111  				}
   112  			case *net.IPNet:
   113  				if !v.IP.IsLoopback() {
   114  					rt = append(rt, v.IP.String())
   115  				}
   116  			}
   117  		}
   118  	}
   119  
   120  	return strings.Join(rt, ",")
   121  }
   122  
   123  func LocalAddresses() (rt []net.IP) {
   124  	rt = make([]net.IP, 0)
   125  
   126  	ifaces, err := net.Interfaces()
   127  	if err != nil {
   128  		return
   129  	}
   130  	for _, i := range ifaces {
   131  		if i.Flags&net.FlagUp != 1 {
   132  			continue
   133  		}
   134  
   135  		addrs, err := i.Addrs()
   136  		if err != nil {
   137  			continue
   138  		}
   139  
   140  		for _, a := range addrs {
   141  			switch v := a.(type) {
   142  			case *net.IPAddr:
   143  				//logutil.Printf("WAIPUtil005", "----- %v : %s (%s) %t %d [%v,%v]\n", i.Name, v, v.IP.DefaultMask(), v.IP.IsLoopback(), len(v.IP), v.IP.To4(), v.IP.To16)
   144  				if !v.IP.IsLoopback() && v.IP.To4() != nil {
   145  					//logutil.Println("WAIPUtil006", v.IP)
   146  					rt = append(rt, v.IP)
   147  				}
   148  
   149  			case *net.IPNet:
   150  				//logutil.Printf("WAIPUtil007", "------ %v : %s [%v/%v] %t %d [%v,%v]\n", i.Name, v, v.IP, v.Mask, v.IP.IsLoopback(), len(v.IP), v.IP.To4(), v.IP.To16())
   151  				if !v.IP.IsLoopback() && v.IP.To4() != nil {
   152  					//logutil.Println("WAIPUtil008", v.IP, v.IP[12], v.IP[13], v.IP[14], v.IP[15])
   153  					rt = append(rt, v.IP)
   154  				}
   155  			}
   156  		}
   157  	}
   158  
   159  	return rt
   160  }
   161  
   162  func lookup(ip net.IP) bool {
   163  	_, err := net.LookupHost(ip.String())
   164  	//arr, err := net.LookupAddr(it.String())
   165  	if err != nil {
   166  		return false
   167  	}
   168  	return true
   169  }