Skip to content

About

Official Fastify plugin for the InternetData API. Attach each visitor's IP data to the request, and block by country, anonymity or network.

Topics

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

InternetData InternetData Fastify Plugin

npm license

The official Fastify plugin for the InternetData API.

It looks up the visitor behind each request, where they are, which network they come from and whether it's a VPN, a proxy, Tor or a hosting server, and hands the answer to your handlers. Blocking is opt-in.

Getting Started

npm install internetdata-fastify

Requires Node.js 22 or newer and Fastify 4 or 5. TypeScript types are included.

Start with a free API key, no card needed: create one in the console and pass it in. A free key answers the basics on its organization's quota: the Tor, relay and anycast flags, the hostname, location to the country and asn without its type. Past the quota, each lookup fails as quota_exceeded until the quota resets (below). The Data plan answers every field, the company and its abuse contact, the VPN and proxy flags, the city, WHOIS and the services among them, at the volume you need (pricing).

import Fastify from 'fastify';
import { internetdata } from 'internetdata-fastify';

// trustProxy names the proxy whose X-Forwarded-For Fastify believes; see
// "Where the client address comes from" below.
const app = Fastify({ trustProxy: 'loopback' });

await app.register(internetdata, { apiKey: process.env.INTERNETDATA_API_KEY });

app.get('/', async (request) => {
    const { result } = request.internetdata;
    return `Hello, visitor from ${result?.location.country_name ?? 'somewhere'}`;
});

await app.listen({ port: 3000 });

By default nothing is blocked. Every request gets a request.internetdata and your own code decides what it means: a currency to show, a sign-up to review, a page to keep from some countries.

Blocking

Pass a blockCondition and a matching request is answered with 403 and never reaches your handlers.

await app.register(internetdata, {
    apiKey: process.env.INTERNETDATA_API_KEY,
    blockCondition: { location: { country: ['CU', 'IR', 'KP'] } },
});

A condition is written in the shape of a result, and only the members you name are considered, so it can refuse on anything in the answer. The country works on every plan, and so do isTor, isRelay and isAnycast; the others here need a paid plan:

blockCondition: { asn: { type: 'hosting' } }                          // the network's type
blockCondition: [{ isAnycast: true }, { isSatellite: true }]          // anycast and satellite space
blockCondition: { isBot: true }                                       // crawlers, monitors and scanners
blockCondition: [{ isHosting: true }, { isCdn: true }]                // traffic from servers, not people
blockCondition: [{ isVpn: true }, { isTor: true }, { isResproxy: true }]  // anonymity: a list is OR
blockCondition: { isIspproxy: true }                                  // an ISP proxy, apart from a datacenter proxy
blockCondition: { isVpn: true, vpn: { provider: 'nordvpn' } }         // one provider
blockCondition: { resproxy: { hits: { gte: 5 } } }                    // a numeric threshold

Values are matched by equality, strings without regard to case. An array means any-of. { gte, gt, lte, lt } compares numbers, and every bound you give must hold, so two of them are a range. A member the answer holds as a list matches when any one element does, every member of an object term on the same element: { web: { port: 443 } }, { pubproxy: { protocols: 'socks5' } }. Members you set to false or null are ignored, so a condition states the signals you act on; one that constrains nothing would match every request, and is refused when the plugin registers rather than silently blocking all your traffic.

Replace the refusal with onBlocked:

await app.register(internetdata, {
    apiKey: process.env.INTERNETDATA_API_KEY,
    blockCondition: { location: { country: ['CU', 'IR', 'KP'] } },
    onBlocked: (request, reply) => reply.status(451).send({ error: 'not available in your country' }),
});

onBlocked may be async; it is awaited, so the route handler never runs.

Let a verified crawler through

allowBots sends a request's own User-Agent with its lookup when it names a crawler, and a request whose answer's userAgent.verdict is verified, a crawler its operator vouches for such as Googlebot from Google's addresses, is never blocked, whatever blockCondition matches. The answer is still on request.internetdata:

await app.register(internetdata, {
    apiKey: process.env.INTERNETDATA_API_KEY,
    blockCondition: { isBot: true },
    allowBots: true,
});

A client claiming to be a crawler from an address its operator does not vouch for reads spoofed, so blockCondition: { userAgent: { verdict: 'spoofed' } } refuses a forged crawler. Any other agent, or none, is not sent, since its verdict could only be not_bot. One address is looked up with at most four crawler agents an hour: past those its lookup sends none, and a request passes as a crawler only where the answer's bot.verified is true. It is off by default, and then no agent is sent and the cache keeps one answer per address rather than one per address and agent. A verdict needs a paid plan: a served answer without one is reported once under onMissingField.

Where the client address comes from

This is the setting that decides whether any of the above works, and it is the one thing only you can get right.

By default the plugin uses request.ip, which is Fastify's own accessor. Fastify resolves request.ip to the socket peer unless you built the server with trustProxy. So if your app sits behind nginx, a load balancer, or a CDN and you have not set it, every visitor arrives wearing your proxy's address, and that is the address that gets looked up: every visitor gets your proxy's country and network, and if it is a hosting address, a hosting rule blocks everyone. nginx on the same machine is 127.0.0.1 and a load balancer usually has a private address, which the plugin answers locally without a lookup, as an address with no country, and logs a warning.

If you are behind a proxy you control, setting Fastify's own option to that proxy's address is the right fix and everything else here follows from it:

const app = Fastify({ trustProxy: 'loopback' });    // a proxy on the same machine, over 127.0.0.1 or ::1
const app = Fastify({ trustProxy: '10.0.0.0/8' });  // or your load balancer's subnet

Name your proxies rather than setting true. With true, request.ip is the left-most X-Forwarded-For entry, and the visitor writes that one: nginx, AWS's Application Load Balancer and Cloudflare all append to the header rather than replace it, so a visitor who sends X-Forwarded-For: 1.1.1.1 is looked up as 1.1.1.1. Keep true only behind an edge you control that overwrites X-Forwarded-For.

For an edge that writes the address into its own header, name the header:

import { headerIpSelector } from 'internetdata-fastify';

await app.register(internetdata, {
    apiKey: process.env.INTERNETDATA_API_KEY,
    ipSelector: headerIpSelector('CF-Connecting-IP'),  // or True-Client-IP, or your own
});

xffIpSelector() reads X-Forwarded-For directly. Be aware that the left-most entry is whatever the caller sent, because proxies append to that header, so it's only trustworthy when an edge you control overwrites it. If you know how many proxies sit in front, count from the right instead: xffIpSelector({ depth: 1 }) is the address your nearest proxy saw.

Anything else, pass your own function. It receives the Fastify request and returns an address:

ipSelector: (request) => request.headers['x-real-ip'] ?? request.ip,

If the address resolves to a private one, the plugin says so once on console.warn. That is expected on localhost and is the signal to fix your configuration anywhere else.

When a lookup fails

The request is let through, and the reason is recorded on request.internetdata.error. Our outage should not become yours, so a network failure, a rate limit or a rejected key all fail open.

app.get('/', async (request) => {
    const { result, error } = request.internetdata;
    if (error) {
        request.log.warn({ kind: error.kind }, 'internetdata unavailable');
    }
    return `Hello, visitor from ${result?.location.country_name ?? 'somewhere'}`;
});

Pass failClosed: true to block instead. Private addresses are answered locally and never fail, so this will not lock you out in development.

A key whose organization has spent its quota is refused with a 429, which the client library raises as quota_exceeded, so the middleware treats it as a failed lookup: the request goes through with request.internetdata.error.kind quota_exceeded and no request.internetdata.result, and failClosed: true refuses it. An answer whose capReached is true (none is today) fails the same way, with its basics on request.internetdata.error.result and an error-level log naming quota_exceeded on console.error (or the onError you pass) once a minute at most:

app.get('/', async (request) => {
    const { result, error } = request.internetdata;
    if (error?.kind === 'quota_exceeded') {
        return 'Hello, visitor';   // the quota is spent until it resets
    }
    return `Hello, visitor from ${result?.location.country_name ?? 'somewhere'}`;
});

Cost and latency

Answers are cached per plugin registration for an hour, so a returning visitor costs nothing, and private addresses never leave the process. A cache miss is one request to our API, bounded at 2500 ms by default and not retried: on a request path, failing open quickly beats holding a visitor while we try again. Both are adjustable, along with the cache itself:

await app.register(internetdata, {
    apiKey: KEY, timeoutMs: 1000, retries: 1, cache: { max: 50000, ttlMs: 600000 },
});

Register it inside a scope to confine it to those routes, or skip what you do not care about:

await app.register(async (scope) => {
    await scope.register(internetdata, { apiKey: KEY });
    scope.get('/checkout', handler);
});

await app.register(internetdata, {
    apiKey: KEY, skip: (request) => request.url.startsWith('/static'),
});

If you already hold an InternetData client, pass it as client and the plugin will share it rather than building a second cache.

A registration nested in a scope that already has one runs after it, and its answer replaces the outer one on the routes it covers. Give both the same client and the second lookup is a cache hit, so this attaches the answer on every account route and blocks only at checkout, for one request per visitor:

const client = new InternetData({ apiKey: KEY });  // from '@internetdata/internetdata'

await app.register(async (account) => {
    await account.register(internetdata, { client });
    account.post('/signup', signupHandler);

    await account.register(async (checkout) => {
        await checkout.register(internetdata, { client, blockCondition: { location: { country: ['CU', 'IR', 'KP'] } } });
        checkout.post('/checkout', checkoutHandler);
    });
});

Beyond a few million distinct visitors a day, stop calling the API per request: look addresses up locally in our databases instead.

Absent is not false

The basics, what a free key answers, leave out isVpn, isHosting, isCdn, the five proxy flags, isBot, isCarrier, isSatellite and the five service flags, the detail behind them, userAgent, company and abuse, and the paid members inside location and asn, such as location.city and asn.type. A field the answer leaves out is undefined, which means "not in this answer" rather than "checked, and no": every one reads that way, the flags, company and abuse included, and result.raw has a member only when the API sent it.

request.internetdata.result?.isVpn ?? false   // when you only want the flag

A blockCondition naming a member your plan does not serve, location.city or a member of company included, can never match, so the plugin warns once instead of failing silently. Set onMissingField: 'throw' to make it an error.

Other Libraries

There are official InternetData client libraries available for many languages including PHP, Python, Go, Java, and Ruby. See our GitHub at https://github.com/internetdata for more.

About InternetData

Geolocation, anonymity, ownership, network, service and domain data for any IP address, AS or domain, by API or as databases you download.

InternetData

License

This project is licensed under the MIT License.

About

Official Fastify plugin for the InternetData API. Attach each visitor's IP data to the request, and block by country, anonymity or network.

Topics

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages