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

发布于 2024-10-29 08:07:18 字数 118 浏览 5 评论 0原文

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

More of a theoretical question. Yes, I know those types of questions are frowned upon here. But suppose APIs like MaxMind GeoIP's didn't exist, and you wanted to build a service that translated IP addresses to the city it's in. What would you do? How would you go about solving that problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏末染殇 2024-11-05 08:07:18
  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

whois 向我们显示他们可能位于 NY, NY。

$ 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 即可解决地理定位问题的一种方法。

  1. Use the whois database to find out who the IP is allocated to.
  2. Use DNS to do a reverse lookup on the IP address.
  3. Use traceroute to guess the location based on the routers leading up to the host

As an example, lets look at 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 shows us that they're probably in NY, NY.

$ host 64.34.119.12
12.119.34.64.in-addr.arpa domain name pointer stackoverflow.com.

Reverse DNS (PTR record) doesn't help here. However, a lot of ISPs do have region specific reverse DNS entries that can help with this process (especially for dynamically assigned hosts). As an example:

$ host 24.25.195.1
1.195.25.24.in-addr.arpa domain name pointer sndgca-dns-cac-01.san.rr.com.

This host is obviously identified and located by it's PTR record: a Time Warner (Road Runner) caching DNS server in San Diego, CA.

Now, back to the stackoverflow.com locating process:

$ 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 ...)

The traceroute output confirms our suspicion that stackoverflow.com is hosted in NY, NY.

That's one way to tackle geolocation without an API.

情场扛把子 2024-11-05 08:07:18

我真的在这里猜测,但这可能有效。

IP 范围由 IANA 分配给区域提供商。他们有五个。反过来,每个区域提供商将 IP 范围分配给最终用户和互联网服务提供商。
如果您可以从区域提供商及其 ISP 客户处获取/购买信息,那么您就可以开展业务。

Im really guessing here but this may work.

IP ranges are assigned by IANA to regional providers. There are five of them. In turn each regional provider assign IP ranges to end users and internet service provides.
If you can get/buy the information from the regional providers and their ISP clients you are in business.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文