Geolocate any IP using latency

Published: (December 15, 2025 at 02:24 PM EST)
4 min read
Source: Dev.to

Source: Dev.to

TL;DR

I built a CLI tool that can resolve an IP address to a country, US state, and even a city.
GitHub – geolocation-tool

Background

Ipinfo recently demonstrated that many VPN providers falsify IP‑geolocation data by feeding incorrect information to ARIN, RIPE, and Geo‑DB providers. Their accuracy comes from a large probe network that pings and traces most IP addresses on the internet, using latency and hop data (plus advanced algorithms) to infer the true physical location.

I wanted to see if a similar approach could be replicated on a smaller scale using Globalping, an open‑source, community‑powered project that lets users self‑host container‑based probes. The public network currently has > 3000 probes, enough to geolocate most IPs down to country or US‑state level.

How Globalping Works

Globalping provides a public API that can run network tests (ping, traceroute, etc.) from any of its probes. By specifying a location field, the API selects a set of probes that roughly match the desired region.

Globalping network map

Tool Design

The CLI follows three phases:

  1. Continent detection – ping a few probes per continent and pick the fastest.
  2. Country detection – run many measurements from probes in the winning continent and select the country with the lowest latency.
  3. US‑state detection – if the country is the United States, repeat the process with probes limited to the USA.

Implementation Details

  • Libraryglobalping-ts (TypeScript wrapper for the Globalping API).
  • Measurements – Initially used ICMP ping (2 packets) but many networks block ICMP. Switched to TCP‑based ping, then settled on traceroute because the last hop’s latency is usually in the same country as the target.
  • Probe selection – Use Globalping’s magic field to let the service pick pseudo‑random probes that match the requested location and limit.
  • Limits
    • Continent phase: 5 probes per continent (fast and accurate).
    • Country phase: default 50 probes (fits within the free‑tier limit of 250 tests / hour).
    • US‑state phase: also 50 probes.

Running the Tool

# Example: resolve your own home IP
geolocate 203.0.113.42

Phase 1 – Continent detection

Phase 1: Detecting continent...
  North America: 137.18 ms
  Europe: 32.39 ms
  Asia: 174.54 ms
  South America: 215.08 ms
  Oceania: 244.15 ms
  Africa: 156.83 ms

The tool selects Europe as the winning continent.

Phase 2 – Country detection (50 probes)

Phase 2: Detecting country...
  Measuring from 50 probes...

  [████████████████████████████████████████] 100.0%   50/50 - Best: PL (7.29 ms)

Top 3 Locations:
─────────────────────────────────────────────────
  1. Poland, EU                               7.29 ms
  2. Germany, EU                              13.42 ms
  3. Lithuania, EU                            17.65 ms

═══════════════════════════════════════════════════
                      SUMMARY
═══════════════════════════════════════════════════
  Location: Poland, EU
  Minimum Latency: 7.29 ms
  Confidence: Medium

Phase 3 – US‑state detection (example IP)

Phase 3: Detecting US state...
  Measuring from 50 probes...

  [████████████████████████████████████████] 100.0%   50/50 - Best: FL (0.45 ms)

Top 3 Locations:
─────────────────────────────────────────────────
  1. Florida, USA                             0.45 ms
  2. South Carolina, USA                      12.23 ms
  3. Georgia, USA                             15.01 ms

═══════════════════════════════════════════════════
                      SUMMARY
═══════════════════════════════════════════════════
  Location: Florida, United States
  Minimum Latency: 0.45 ms
  Confidence: Very High

Observations

  • Accuracy – With just 5 probes per continent and 50 probes for the final country/state step, the tool reliably matches ipinfo’s published locations.
  • Speed – The whole process finishes in a few seconds, well within the free‑tier API limits (250 tests / hour, 50 probes per measurement).
  • Limitations
    • Reliance on ICMP/UDP traceroute means results can be affected by networks that block these packets.
    • Only the last hop is examined; using multiple hops, TCP/UDP traceroute, or ASN data could improve confidence.
    • Border‑line IPs (e.g., between continents) may need more probes for a decisive result.

Future Improvements

  • Combine latency data with AS‑level WHOIS information and a weighted voting system.
  • Add support for TCP/UDP traceroute on various ports to handle ICMP‑blocked networks.
  • Expand analysis to the last few hops rather than a single hop.
  • Implement a “low‑certainty” flag that triggers a second round with more probes.

Getting Started

  1. Create a free Globalping accounthttps://dash.globalping.io/
  2. Generate an API token (optional) – gives up to 500 tests / hour.
  3. Clone the repository and install dependencies:
git clone https://github.com/jimaek/geolocation-tool.git
cd geolocation-tool
npm install
  1. Run the CLI (replace YOUR_TOKEN if you have one):
export GLOBALPING_TOKEN=YOUR_TOKEN   # optional
node dist/index.js 203.0.113.42

If you need more tests than the free quota, you can host your own probe (generates passive credits) or support the project via GitHub Sponsors.


This tool demonstrates that latency‑based probing, even with a modest number of globally distributed nodes, can provide surprisingly accurate IP geolocation without relying on potentially falsified public databases.

Back to Blog

Related posts

Read more »

Words and Letters: Take your side

'Below is the same content, reformatted as clean, readable Markdown while preserving the original structure, images, links, and descriptions.