5 Star 12 Fork 5

joymufeng / cn-ip-recognizer

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

介绍

本项目包含一个工具类CNIPRecognizer,用于判断某个ip是否为国内ip。判断原理很简单,如果该ip存在于apnic公开的cn地址段中则认为它是国内ip。示例代码:

System.out.println("8.8.8.8: " + CNIPRecognizer.isCNIP("8.8.8.8"));
System.out.println("114.114.114.114: " + CNIPRecognizer.isCNIP("114.114.114.114"));

如何更新apnic地址段?

通过如下Scala代码生成相应的Java代码,然后将生成的代码覆盖CNIPRecognizer类中相应部分:

Source.fromFile("D:/delegated-apnic-latest", "utf-8").getLines()
  .filter(_.startsWith("apnic|CN|ipv4|"))
  .map(_.split("\\|")).filter(_.length == 7)
  .map(arr => (IPUtil.ipToLong(arr(3)), arr(4).toLong))
  .toList.sortBy(_._1)
  .foldLeft(List.empty[(Long, Long)]){ (list, t) =>
    list match {
      case head :: tail =>
        if(head._1 + head._2 == t._1){ head.copy(_2 = head._2 + t._2) :: tail } else { t :: list }
      case Nil =>
        t :: Nil
    }
  }.reverse.zipWithIndex.foreach{ case (t, i) =>{
  if(i % 1000 == 0){
    if(i > 0){
      println("}")
    }
    println(s"private static void init${i/1000}(List<CNRecord> list){")
  }
  println(s"list.add(new CNRecord(${t._1}L, ${t._2}));")
}}
println("}")

为了减少生成的代码行数,针对原始的apnic进行了ip地址段合并,合并前的地址段有7637条记录,合并后剩余3365条。

MIT License Copyright (c) 2016 mufeng Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

根据apnic公开的cn地址段,判断某个ip地址是否为国内ip地址。 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/joymufeng/cn-ip-recognizer.git
git@gitee.com:joymufeng/cn-ip-recognizer.git
joymufeng
cn-ip-recognizer
cn-ip-recognizer
master

搜索帮助