keepcalculator

06 Numbers and Powers

Tor Metrics with PowerShell: Query Guide

Card No. 06·31137 · Updated · Standard formula, shown below

You can query Tor network metrics directly from PowerShell using the Tor Metrics REST API, which provides JSON data on relays, bridges, and network history. This guide shows you how to fetch and parse that data with built-in cmdlets like Invoke-RestMethod and ConvertFrom-Json, no extra modules required.

What Are Tor Metrics?

Tor Metrics is the official data service run by The Tor Project that collects and publishes statistics about the Tor network. It includes historical and real-time data on:

  • Relay bandwidth, uptime, and flags
  • Bridge usage and availability
  • Network consensus health
  • User estimates by country
  • Tor version distribution

All data is available through a public REST API at https://metrics.torproject.org/. The API returns JSON, which PowerShell can consume natively.

Why Use PowerShell?

PowerShell is already installed on every Windows machine and can be used on Linux and macOS. It has built-in cmdlets for making HTTP requests and parsing JSON, making it a quick way to pull and analyze Tor metrics without installing Python or using a browser. You can also schedule scripts to collect metrics over time, or combine them with other system administration tasks.

Getting Started: Basic API Calls

The Tor Metrics API has two main endpoints: torstatus for current relay data and bandwidth for historical bandwidth graphs. To fetch current relay status, use:

$relays = Invoke-RestMethod -Uri "https://metrics.torproject.org/rs/status.json"

This returns a JSON object containing an array of relays. You can then filter and display them:

$relays.relays | Where-Object { $_.flags -contains "Exit" } | Select-Object -First 5 | Format-Table nickname, bandwidth, uptime

For historical bandwidth data, use the bandwidth endpoint:

$bw = Invoke-RestMethod -Uri "https://metrics.torproject.org/bandwidth.json?start=2024-01-01&end=2024-01-31"

This returns a series of data points with timestamps and bandwidth values.

Parsing and Filtering Data

Once you have the JSON, PowerShell treats it as objects. You can use standard cmdlets like Where-Object, Sort-Object, and Select-Object to analyze the data. For example, to find the top 10 relays by bandwidth:

$relays.relays | Sort-Object bandwidth -Descending | Select-Object -First 10 | Format-Table nickname, bandwidth, country

To filter relays that support IPv6:

$relays.relays | Where-Object { $_.ipv6 } | Measure-Object | Select-Object Count

You can also export data to CSV for further analysis:

$relays.relays | Export-Csv -Path tor_relays.csv -NoTypeInformation

Handling API Limits and Errors

The Tor Metrics API has rate limits to prevent abuse. If you make too many requests, you may receive an HTTP 429 (Too Many Requests) or 403 (Forbidden). To avoid this, add a delay between requests:

Start-Sleep -Seconds 2

Also check the response status code to handle errors gracefully:

$response = Invoke-WebRequest -Uri "https://metrics.torproject.org/rs/status.json"
if ($response.StatusCode -eq 200) {
    $data = $response.Content | ConvertFrom-Json
} else {
    Write-Host "Error: $($response.StatusCode)"
}

Always use Invoke-WebRequest instead of Invoke-RestMethod if you need to inspect the raw response or headers.

Common Pitfalls and Misconceptions

A frequent mistake is confusing the torstatus endpoint with the deprecated rs endpoint. The current URL is https://metrics.torproject.org/rs/status.json, not the old https://torstatus.blutmagie.de/ which is no longer maintained. Also, the API returns data in JSON, not XML, so don’t try to parse it with [xml].

Another misconception is that the API provides real-time data. In practice, the status data is updated every hour, and bandwidth data is aggregated over 24-hour periods. For near-real-time data, you would need to use the Tor control protocol with a running Tor relay.

Finally, be aware that the API does not require an API key, but it does require a valid User-Agent header. PowerShell’s default User-Agent is fine, but if you’re behind a strict proxy, you may need to set it manually.

Alternatives and Extensions

If you need more advanced analysis, consider using Python with the requests library, or R with the httr package. The Tor Project also provides a web interface where you can manually explore the data. For automated monitoring, you could set up a scheduled task in Windows Task Scheduler to run a PowerShell script daily and log the results.

For those who want to work with raw consensus documents, the API also offers consensus.json and server-descriptor endpoints, which give more detailed data about each relay.

Frequently Asked Questions

Do I need to install anything to use Tor Metrics with PowerShell?

No, PowerShell comes with Invoke-RestMethod and ConvertFrom-Json built in. On Windows 10 and 11, PowerShell 5.1 is preinstalled, and PowerShell 7 is available for download if you want the latest features. No additional modules are required.

How often is the data updated?

The relay status data is updated every hour, while bandwidth history is aggregated daily. The API documentation states that the data is not real-time; it reflects the state of the network at the last hourly consensus.

Can I get metrics for a specific relay?

Yes, you can use the details endpoint with a relay fingerprint. For example, https://metrics.torproject.org/rs/details.json?fingerprint=ABCDEF... returns detailed information about that relay.

What if I get a 403 Forbidden error?

This usually means you’ve been rate-limited. Wait a few minutes and try again. If the problem persists, check that your User-Agent is not blocked, and avoid making more than one request per second.

Can I use these metrics to estimate Tor user numbers?

Yes, the API has a userstats endpoint that provides estimated user counts by country. You can query it with parameters like start and end dates, and it returns JSON with country and user counts.

Is it legal to query Tor metrics?

Yes, the Tor Project explicitly provides this API for public use. It is the same data shown on their website, and there are no restrictions on accessing it. However, be respectful of the rate limits to avoid putting load on their servers.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →