网络相关
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
4.3 KiB

package network
import (
"errors"
"net"
"net/http"
"strconv"
"strings"
)
/**
* 取本地IP
*/
func GetLocalIps() []string {
addrs, err := net.InterfaceAddrs()
var list []string
if err != nil {
return list
}
for _, address := range addrs {
// 检查ip地址判断是否回环地址
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil && !ipnet.IP.IsLinkLocalUnicast() {
if IsPrivateIp(ipnet.IP.String()) {
list = append(list, ipnet.IP.String())
}
}
}
}
return list
}
/**
* 取外网IP
*/
func GetInternetIps() []string {
addrs, err := net.InterfaceAddrs()
var list []string
if err != nil {
return list
}
for _, address := range addrs {
// 检查ip地址判断是否回环地址
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil && !ipnet.IP.IsLinkLocalUnicast() {
if !IsPrivateIp(ipnet.IP.String()) {
list = append(list, ipnet.IP.String())
}
}
}
}
return list
}
/**
* 是否本地(局域网)IP
*/
func IsPrivateIp(ip string) bool {
process_ip := getIpNumber(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
* D类 127.0.0.0 -127.255.255.255(环回地址)
*/
a_begin := getIpNumber("10.0.0.0")
a_end := getIpNumber("10.255.255.255")
if process_ip >= a_begin && process_ip <= a_end {
return true
}
b_begin := getIpNumber("172.16.0.0")
b_end := getIpNumber("172.31.255.255")
if process_ip >= b_begin && process_ip <= b_end {
return true
}
c_begin := getIpNumber("192.168.0.0")
c_end := getIpNumber("192.168.255.255")
if process_ip >= c_begin && process_ip <= c_end {
return true
}
d_begin := getIpNumber("127.0.0.0")
d_end := getIpNumber("127.255.255.255")
if process_ip >= d_begin && process_ip <= d_end {
return true
}
return false
}
func getIpNumber(ip string) int64 {
ip_segment := strings.Split(ip, ".")
if len(ip_segment) != 4 {
return -1
}
segment_1, _ := strconv.ParseInt(ip_segment[0], 10, 64)
segment_2, _ := strconv.ParseInt(ip_segment[1], 10, 64)
segment_3, _ := strconv.ParseInt(ip_segment[2], 10, 64)
segment_4, _ := strconv.ParseInt(ip_segment[3], 10, 64)
ip_num := segment_1*256*256*256 + segment_2*256*256 + segment_3*256 + segment_4
return ip_num
}
/**
* 取来源地址
*/
func GetRemoteIp(req *http.Request) string {
forward_ip := req.Header.Get("X-Forwarded-For") //使用代理
if forward_ip != "" {
forward_ips := strings.Split(forward_ip, ",")
if len(forward_ips) > 0 && forward_ips[0] != "" {
remoteIp, _, err := net.SplitHostPort(forward_ips[0])
if err != nil {
remoteIp = forward_ips[0]
}
return remoteIp
}
}
if remoteIp, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
return remoteIp
}
return req.RemoteAddr
}
/**
* ip是否正确
*/
func GetNumber(ip, mask string) (string, error) {
a := net.ParseIP(ip)
if a == nil {
return "", errors.New("150057")
}
b := net.ParseIP(mask)
if b == nil {
return "", errors.New("150058")
}
ips := strings.Split(ip, ".")
a1, _ := strconv.Atoi(ips[0])
a2, _ := strconv.Atoi(ips[1])
a3, _ := strconv.Atoi(ips[2])
a4, _ := strconv.Atoi(ips[3])
masks := strings.Split(mask, ".")
m1, _ := strconv.Atoi(masks[0])
m2, _ := strconv.Atoi(masks[1])
m3, _ := strconv.Atoi(masks[2])
m4, _ := strconv.Atoi(masks[3])
n1 := a1 & m1
n2 := a2 & m2
n3 := a3 & m3
n4 := a4 & m4
return strconv.Itoa(n1) + "." + strconv.Itoa(n2) + "." + strconv.Itoa(n3) + "." + strconv.Itoa(n4), nil
}
/*
* CIDR取IP mask
* 2019/03/07
*/
func GetIpMaskFromCIDR(ip string) (string, string, error) {
ips, ipNet, err := net.ParseCIDR(ip)
if err != nil {
return "", "", err
}
val := make([]byte, len(ipNet.Mask))
copy(val, ipNet.Mask)
var s []string
for _, i := range val[:] {
s = append(s, strconv.Itoa(int(i)))
}
return ips.String(), strings.Join(s, "."), nil
}
/*
* net mask转换, 255.255.255.0 -> 24
* 2019/03/07
*/
func ChangeMask(mask string) string {
s := strings.Split(mask, ".")
s1, _ := strconv.Atoi(s[0])
s2, _ := strconv.Atoi(s[1])
s3, _ := strconv.Atoi(s[2])
s4, _ := strconv.Atoi(s[3])
ipmask := net.IPv4Mask(byte(s1), byte(s2), byte(s3), byte(s4))
ones, _ := ipmask.Size()
return strconv.Itoa(ones)
}