Skip to content
VPNDetection

Restrict content by country

Serve or refuse content by the visitor's country with VPNDetection, and close the gap that VPNs and Tor open in any country rule.

A rule that serves content only in some countries has a gap: a visitor in another country connects through a VPN server in an allowed one, and appears to be there. This guide combines the visitor's country with their connection type, so a VPN or Tor exit can't satisfy your rule.

How it works

For each request to restricted content, your server looks up the visitor's address. It serves the content only when the address's country is on your list and the address isn't a VPN or a Tor exit. Privacy relays such as iCloud Private Relay keep the user's real country, so they pass.

1. Check the country and the connection

Node.js
const ALLOWED = new Set(['US', 'CA', 'GB']);

export async function canServe(ip) {
    const r = await client.lookup(ip);
    if (r.isVpn || r.isTor) {
        return false;
    }
    return ALLOWED.has(r.location?.country ?? '');
}

The country and both flags are on the free plan.

2. Apply it as middleware

To protect a whole route, use a framework package with a block condition:

js
app.use('/watch', vpndetection({
    apiKey: process.env.VPNDETECTION_API_KEY,
    blockCondition: [{ isVpn: true }, { isTor: true }],
}));

Combine it with your country check in the handler, or in the middleware's own block handler.

3. Remember the decision for a session

Look the visitor up once when their session starts, and store the decision with the session. Each page view then costs no lookup, and the visitor's access doesn't change halfway through a visit. Look up again when the session ends or the visitor's address changes.

4. Handle travelers and relays

  • Travelers see what their current country allows, because the address says where they are now. Explain this in your message, so a subscriber abroad understands why content is missing.
  • Privacy relays keep the user's country, so iCloud Private Relay users pass a country rule as anyone else from their country does.
  • A failed lookup needs a decision. For content you're licensed to show only in some countries, fail closed: the framework packages have an option for it. For other content, letting the request through is usually right.

5. Explain the refusal

Tell visitors why the content isn't available, for example "This content isn't available in your country or over a VPN." A clear message prevents support requests from people who don't realize their VPN is on.

6. Test it

Connect to a VPN server in one of your allowed countries and open the restricted page. It should be refused. Disconnect, and the page should load if you're in an allowed country.

With a paid plan

Residential, mobile and datacenter proxies get around a country rule the same way a VPN does. A paid plan flags all three; add { isResproxy: true }, { isMobproxy: true } and { isDcproxy: true } to the block condition.

Next steps

To understand why relays pass this rule, read Handle privacy relays.