geolocation - 如何在不使用 API 的情况下查找 IP 地址的位置?

标签 geolocation ip

更多的是一个理论问题。是的,我知道这些类型的问题在这里不受欢迎。但是假设像 MaxMind GeoIP 这样的 API 不存在,并且您想构建一个服务,将 IP 地址转换为它所在的城市。您会怎么做?你会如何着手解决这个问题?

最佳答案

  • 使用 whois 数据库找出 IP 分配给谁。
  • 使用 DNS 对 IP 地址进行反向查找。
  • 使用 traceroute 根据通向主机的路由器猜测位置

  • 例如,让我们看看 stackoverflow.com (64.34.119.12)
    $ whois 64.34.119.12 | egrep "(OrgName)|(City)|(State)|(Country)"
    OrgName:        Peer 1 Network Inc.
    City:           New York
    StateProv:      NY
    Country:        US
    

    whois 向我们表明他们可能在纽约,纽约。
    $ host 64.34.119.12
    12.119.34.64.in-addr.arpa domain name pointer stackoverflow.com.
    

    反向 DNS(PTR 记录)在这里没有帮助。但是,许多 ISP 确实具有区域特定的反向 DNS 条目,可以帮助完成此过程(特别是对于动态分配的主机)。举个例子:
    $ host 24.25.195.1
    1.195.25.24.in-addr.arpa domain name pointer sndgca-dns-cac-01.san.rr.com.
    

    该主机显然是通过它的 PTR 记录来识别和定位的:位于加利福尼亚州圣地亚哥的时代华纳(Road Runner)缓存 DNS 服务器。

    现在,回到 stackoverflow.com 的定位过程:
    $ traceroute stackoverflow.com
    traceroute to stackoverflow.com (64.34.119.12), 64 hops max, 40 byte packets
    ms
     (... removed the first few hops ...)
     8  ae-6-6.ebr1.Chicago1.Level3.net (4.69.140.189)  30.9 ms  28.855 ms  25.183 ms
     9  ae-2-2.ebr2.NewYork2.Level3.net (4.69.132.66)  54.373 ms  54.197 ms  48.171 ms
    10  ae-6-6.ebr2.NewYork1.Level3.net (4.69.141.21)  48.250 ms  50.712 ms  51.293 ms
    11  ae-62-62.csw1.NewYork1.Level3.net (4.69.148.34)  59.493 ms ae-82-82.csw3.NewYork1.Level3.net (4.69.148.42)  51.694 ms ae-62-62.csw1.NewYork1.Level3.net (4.69.148.34)  58.315 ms
    12  ae-3-89.edge1.NewYork1.Level3.net (4.68.16.142)  51.529 ms ae-2-79.edge1.NewYork1.Level3.net (4.68.16.78)  50.77 ms ae-3-89.edge1.NewYork1.Level3.net (4.68.16.142)  50.567 ms
    13  10ge-te-1-1.nyc-75bre-dis-2.peer1.net (216.187.115.77)  50.776 ms  51.193 ms  56.166 ms
    14  gig2-0.nyc-gsr-b.peer1.net (216.187.123.5)  54.994 ms  58.813 ms  51.794 ms
    15  gwny01.stackoverflow.com (64.34.41.58)  54.121 ms  52.65 ms  54.166 ms
    16  * * *
    (... remaining time outs removed ...)
    

    traceroute 输出证实了我们怀疑 stackoverflow.com 托管在纽约州纽约市。

    这是一种无需 API 即可解决地理定位问题的方法。

    关于geolocation - 如何在不使用 API 的情况下查找 IP 地址的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5516761/

    相关文章:

    javascript - 移动浏览器处于非事件状态时地理定位失败

    Docker for Desktop 运行 Kubernetes - IP 地址不起作用

    sockets - 用于发送控制台消息的 TCP/IP 默认端口?

    python - Tor、Stem 和套接字 - 使用 TOR 改变身份

    php - 在 MySQL 数据库中存储 IP 地址(IPv4 和 IPv6)

    geolocation - 通过IP地址识别国家

    postgresql - 包含覆盖世界的六边形网格坐标的表格

    c# - unity C# 如何获取玩家的国家、城市或位置

    android - 从时间轴中提取谷歌位置历史

    android - 如何授予调试的android应用程序权限?