14

Go IP 段范围校验

 3 years ago
source link: https://studygolang.com/articles/30798
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

友情提示:此篇文章大约需要阅读 3分钟21秒,不足之处请多指教,感谢你的阅读。 订阅本站

背景

近期做了一个需求,是检测某个 IP 是否在若干 IP 段内,做固定地点 IP 筛查,满足特定业务需求。

解决方案

PLAN A 点分十进制范围区分

简单来讲,就是将 IPv4 原有的四段,分别对比 IP 地址,查看每一段是否在 IP 段范围内,可以用于段控制在每一个特定段 0 ~ 255 内筛选,例如:

192.123.1.0 ~ 192.123.156.255

这样的比较规范的特定段可以实现简单的筛选,但是问题来了,不规则的连续 IP 段怎么排除? 如下:

IP段:192.168.1.0 ~ 192.172.3.255
IP: 192.160.0.255

这样就会出现问题,可以看到按照简单的分段对比,很明显校验不通过,但是这个 IP 还是存在在 IP 段中,方案只能针对统一分段下规则的IP段才可以区分。

PLAN B 转整型对别

IP 地址可以转换为整数,可以将 IP 范围化整为 整数范围进行排查。

这种方式只需要将授为范围内的地址转换为整数,就可以将 IP 排查在外了。

代码

以下是示例代码:

package main

import (
    "fmt"
    "strconv"
    "strings"
)

func main() {
    ipVerifyList := "192.168.1.0-192.172.3.255"
    ip := "192.170.223.1"
    ipSlice := strings.Split(ipVerifyList, `-`)
    if len(ipSlice) < 0 {
        return
    }
    if ip2Int(ip) >= ip2Int(ipSlice[0]) && ip2Int(ip) <= ip2Int(ipSlice[1]) {
        fmt.Println("ip in iplist")
        return
    }
    fmt.Println("ip not in iplist")
}

func ip2Int(ip string) int64 {
    if len(ip) == 0 {
        return 0
    }
    bits := strings.Split(ip, ".")
    if len(bits) < 4 {
        return 0
    }
    b0 := string2Int(bits[0])
    b1 := string2Int(bits[1])
    b2 := string2Int(bits[2])
    b3 := string2Int(bits[3])

    var sum int64
    sum += int64(b0) << 24
    sum += int64(b1) << 16
    sum += int64(b2) << 8
    sum += int64(b3)

    return sum
}

func string2Int(in string) (out int) {
    out, _ = strconv.Atoi(in)
    return
}

有疑问加站长微信联系

iiUfA3j.png!mobile

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK