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

Hen*_*hiu 4 ip geolocation

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

chu*_*ckx 6

  1. 使用whois数据库找出IP分配给谁.
  2. 使用DNS对IP地址执行反向查找.
  3. 使用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
Run Code Online (Sandbox Code Playgroud)

whois告诉我们他们可能在纽约州纽约市.

$ host 64.34.119.12
12.119.34.64.in-addr.arpa domain name pointer stackoverflow.com.
Run Code Online (Sandbox Code Playgroud)

反向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.
Run Code Online (Sandbox Code Playgroud)

该主机显然是由它的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 ...)
Run Code Online (Sandbox Code Playgroud)

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

这是在没有API的情况下解决地理定位的一种方法.