5 Star 9 Fork 4

joymufeng / cn-ip-recognizer

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
Clone or download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.md

介绍

本项目包含一个工具类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条。

Comments ( 0 )

Sign in for post a comment

About

根据apnic公开的cn地址段,判断某个ip地址是否为国内ip地址。 spread retract
Java
MIT
Cancel

Releases

No release

cn-ip-recognizer

Contributors

All

Activities

load more
can not load any more
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

Search

101046 36f486cf 1899542 101055 4cb66026 1899542