internetdata-fastapi 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,194 @@
1
+ """Official FastAPI and Starlette middleware for the InternetData API.
2
+
3
+ Looks up the visitor behind each request and hangs the answer off
4
+ ``request.state.internetdata``, where your endpoints can read it. Blocking is opt-in, for
5
+ every endpoint or, in FastAPI, for one with the :func:`block_if` dependency.
6
+
7
+ from fastapi import FastAPI
8
+ from internetdata_fastapi import InternetDataMiddleware
9
+
10
+ app = FastAPI()
11
+ app.add_middleware(InternetDataMiddleware, api_key=os.environ["INTERNETDATA_API_KEY"])
12
+
13
+ Pure ASGI, so this covers Starlette and Litestar too, not only FastAPI. The
14
+ framework-agnostic half lives in ``internetdata.middleware``.
15
+ """
16
+
17
+ from __future__ import annotations
18
+
19
+ from collections.abc import Awaitable, Callable
20
+ from typing import Any
21
+
22
+ from internetdata.middleware import (
23
+ AsyncCore,
24
+ Conditions,
25
+ Guard,
26
+ IpSelector,
27
+ Lookup,
28
+ MissingFieldAction,
29
+ Options,
30
+ RequestView,
31
+ Selectors,
32
+ bind_selectors,
33
+ )
34
+ from starlette.exceptions import HTTPException
35
+ from starlette.requests import Request
36
+ from starlette.responses import JSONResponse, Response
37
+ from starlette.types import ASGIApp, Receive, Scope, Send
38
+
39
+ __all__ = [
40
+ "InternetDataMiddleware",
41
+ "block_if",
42
+ "default_ip_selector",
43
+ "header_ip_selector",
44
+ "lookup",
45
+ "xff_ip_selector",
46
+ ]
47
+
48
+ __version__ = "1.0.0"
49
+
50
+ # Set on a request ``skip`` claimed, so block_if can tell it from one the middleware
51
+ # never saw.
52
+ _SKIPPED = "_internetdata_skipped"
53
+
54
+ _SELECTORS: Selectors[Request] = bind_selectors(
55
+ lambda request: RequestView(
56
+ header=lambda name: request.headers.get(name),
57
+ framework_ip=lambda: request.client.host if request.client else None,
58
+ )
59
+ )
60
+
61
+ #: ``request.client.host``. uvicorn reads proxy headers into it by default, but only
62
+ #: from a peer ``--forwarded-allow-ips`` trusts, which defaults to ``127.0.0.1,::1``.
63
+ #:
64
+ #: Behind a proxy anywhere else, every visitor looks like the proxy, usually a private
65
+ #: address that is answered locally, so nobody is flagged and the private-address
66
+ #: warning is the only sign. Pass ``--forwarded-allow-ips=<your proxy's address or
67
+ #: subnet>``, never ``*``, which takes the left-most ``X-Forwarded-For`` entry a visitor
68
+ #: can forge, or use :func:`header_ip_selector`.
69
+ default_ip_selector: IpSelector[Request] = _SELECTORS.default
70
+
71
+ #: An address from ``X-Forwarded-For``.
72
+ #:
73
+ #: The LEFT-MOST entry (``depth`` 0) is whatever the caller sent, because proxies
74
+ #: append to this header. It is only trustworthy when an edge you control overwrites
75
+ #: it. When you know how many proxies sit in front, count from the right:
76
+ #: ``xff_ip_selector(1)`` is the address your nearest proxy saw.
77
+ xff_ip_selector = _SELECTORS.xff
78
+
79
+ #: An address from a single-value header your edge writes -
80
+ #: ``header_ip_selector("CF-Connecting-IP")`` behind Cloudflare. Falls back to
81
+ #: ``request.client.host`` when the header is absent.
82
+ header_ip_selector = _SELECTORS.header
83
+
84
+
85
+ def lookup(request: Request) -> Lookup | None:
86
+ """What the middleware found out about this visitor.
87
+
88
+ None when the middleware has not run for this route, or when ``skip`` claimed it.
89
+ """
90
+ return getattr(request.state, "internetdata", None)
91
+
92
+
93
+ def block_if(
94
+ condition: Conditions,
95
+ *,
96
+ status_code: int = 403,
97
+ detail: Any = "access denied",
98
+ fail_closed: bool = False,
99
+ on_missing_field: MissingFieldAction = "warn",
100
+ on_warn: Callable[[str], None] | None = None,
101
+ ) -> Callable[[Request], Awaitable[Lookup | None]]:
102
+ """A FastAPI dependency refusing one endpoint to a visitor matching ``condition``.
103
+
104
+ The middleware's ``block_condition`` refuses on every endpoint; this refuses on the
105
+ path operations, routers or apps that depend on it, with an ``HTTPException`` of
106
+ ``status_code`` and ``detail``::
107
+
108
+ embargoed = block_if({"location": {"country": ["CU", "IR", "KP"]}})
109
+
110
+ @app.get("/checkout", dependencies=[Depends(embargoed)])
111
+ async def checkout(): ...
112
+
113
+ As a parameter it hands the endpoint the answer, or None for a request ``skip``
114
+ claimed. It judges the answer the middleware already attached, so the visitor is not
115
+ looked up again, and a member your plan does not serve is reported once, as the
116
+ middleware's own condition reports it. A condition that constrains nothing is
117
+ refused when the dependency is built.
118
+
119
+ A request ``skip`` claimed carries no answer and reaches the endpoint, and so does
120
+ one whose lookup failed unless you set ``fail_closed``. A request the middleware
121
+ never saw raises ``RuntimeError``: a check that silently never ran would be worse
122
+ than none.
123
+ """
124
+ guard = Guard(
125
+ condition,
126
+ fail_closed=fail_closed,
127
+ on_missing_field=on_missing_field,
128
+ on_warn=on_warn,
129
+ name="block_if",
130
+ )
131
+
132
+ async def dependency(request: Request) -> Lookup | None:
133
+ found = lookup(request)
134
+ if found is None:
135
+ if getattr(request.state, _SKIPPED, False):
136
+ return None
137
+ raise RuntimeError(
138
+ "internetdata: block_if found no answer on this request; add "
139
+ "InternetDataMiddleware to the app"
140
+ )
141
+ if guard.blocks(found):
142
+ raise HTTPException(status_code=status_code, detail=detail)
143
+ return found
144
+
145
+ return dependency
146
+
147
+
148
+ class InternetDataMiddleware:
149
+ """Look up the visitor, and optionally refuse the request.
150
+
151
+ Takes everything :class:`internetdata.middleware.Options` does, plus ``on_blocked``.
152
+
153
+ Written as raw ASGI rather than on ``BaseHTTPMiddleware``, which wraps the response
154
+ in a streaming bridge that breaks background tasks and server-sent events - a cost
155
+ no middleware that only reads the request should impose.
156
+ """
157
+
158
+ def __init__(
159
+ self,
160
+ app: ASGIApp,
161
+ *,
162
+ on_blocked: Callable[[Request, Lookup], Response | Awaitable[Response]] | None = None,
163
+ **options: Any,
164
+ ) -> None:
165
+ self.app = app
166
+ self._on_blocked = on_blocked or _refuse
167
+ self._core: AsyncCore[Request] = AsyncCore(Options(**options), default_ip_selector)
168
+
169
+ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
170
+ if scope["type"] != "http":
171
+ await self.app(scope, receive, send)
172
+ return
173
+ request = Request(scope, receive=receive)
174
+ found = await self._core.evaluate(request)
175
+ if found is None:
176
+ setattr(request.state, _SKIPPED, True)
177
+ await self.app(scope, receive, send)
178
+ return
179
+ # request.state is backed by scope["state"], so this reaches the Request the
180
+ # endpoint builds later over the same scope even though it is a different
181
+ # object.
182
+ request.state.internetdata = found
183
+ if found.blocked:
184
+ response = self._on_blocked(request, found)
185
+ if isinstance(response, Response):
186
+ await response(scope, receive, send)
187
+ else:
188
+ await (await response)(scope, receive, send)
189
+ return
190
+ await self.app(scope, receive, send)
191
+
192
+
193
+ def _refuse(_request: Request, _lookup: Lookup) -> Response:
194
+ return JSONResponse({"error": "access denied"}, status_code=403)
File without changes
@@ -0,0 +1,192 @@
1
+ Metadata-Version: 2.5
2
+ Name: internetdata-fastapi
3
+ Version: 1.0.0
4
+ Summary: Official FastAPI and Starlette middleware for the InternetData API. Look up the visitor behind every request: location, network, VPN, proxy, Tor and hosting.
5
+ Project-URL: Homepage, https://internetdata.io
6
+ Project-URL: Source, https://github.com/internetdata/sdk-python-fastapi
7
+ Author-email: Mslm Dev <support@internetdata.io>
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Keywords: asn,fastapi,geoblocking,hosting,internetdata,ip-geolocation,ip-intelligence,ip-lookup,middleware,proxy-detection,tor,vpn-detection
11
+ Classifier: Development Status :: 5 - Production/Stable
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Programming Language :: Python :: 3.14
18
+ Classifier: Topic :: Internet
19
+ Classifier: Topic :: Security
20
+ Classifier: Typing :: Typed
21
+ Requires-Python: >=3.11
22
+ Requires-Dist: internetdata<3,>=2.12.0
23
+ Requires-Dist: starlette>=0.37
24
+ Provides-Extra: dev
25
+ Requires-Dist: fastapi>=0.115; extra == 'dev'
26
+ Requires-Dist: mypy>=1.11; extra == 'dev'
27
+ Requires-Dist: pytest>=8; extra == 'dev'
28
+ Requires-Dist: ruff>=0.6; extra == 'dev'
29
+ Description-Content-Type: text/markdown
30
+
31
+ # [<img src="https://s3.internetdata.io/internetdata-public/brand/mark.svg" alt="InternetData" height="28"/>](https://internetdata.io/) InternetData FastAPI Middleware
32
+
33
+ [![PyPI](https://img.shields.io/pypi/v/internetdata-fastapi.svg)](https://pypi.org/project/internetdata-fastapi/)
34
+ [![license](https://img.shields.io/pypi/l/internetdata-fastapi.svg)](LICENSE)
35
+
36
+ The official [FastAPI](https://fastapi.tiangolo.com) and [Starlette](https://www.starlette.io) middleware for the [InternetData](https://internetdata.io) API.
37
+
38
+ 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 endpoints. Blocking is opt-in.
39
+
40
+ ## Getting Started
41
+
42
+ ```bash
43
+ pip install internetdata-fastapi
44
+ ```
45
+
46
+ Requires Python 3.11 or newer.
47
+
48
+ Without an API key a lookup is on Unlimited, the free plan: the country, the network and the company behind the address, Tor, relay, anycast, carrier and satellite space and the service flags, with no daily or monthly cap. Every lookup your app makes comes from your server's one address, though, so a busy site will see bursts refused. A paid plan answers every field, VPN, the proxy flags, hosting, CDN and the city among them; create a key in the [console](https://app.internetdata.io). A free account tries every field, 100 lookups a day.
49
+
50
+ ```python
51
+ from fastapi import FastAPI
52
+ from internetdata_fastapi import InternetDataMiddleware
53
+
54
+ app = FastAPI()
55
+ app.add_middleware(InternetDataMiddleware, api_key=os.environ.get("INTERNETDATA_API_KEY"))
56
+ ```
57
+
58
+ Pure ASGI, so this works on Starlette and Litestar too, not only FastAPI.
59
+
60
+ ```python
61
+ from fastapi import Request
62
+ from internetdata_fastapi import lookup
63
+
64
+
65
+ @app.get("/")
66
+ async def index(request: Request):
67
+ found = lookup(request)
68
+ result = found.result if found else None
69
+ return {"country": result.location.country if result else None}
70
+ ```
71
+
72
+ By default nothing is blocked. Every request gets an answer and your own code decides what it means: a currency to show, a sign-up to review, a page to keep from some countries. A request your `skip` claims gets no answer, and `lookup(request)` is `None` for it.
73
+
74
+ ## Blocking
75
+
76
+ Pass a `block_condition` and a matching request is answered with `403` and never reaches your endpoints.
77
+
78
+ ```python
79
+ app.add_middleware(
80
+ InternetDataMiddleware,
81
+ api_key=os.environ.get("INTERNETDATA_API_KEY"),
82
+ block_condition={"location": {"country": ["CU", "IR", "KP"]}},
83
+ )
84
+ ```
85
+
86
+ A condition is written in the shape of a result, keyed by the same names the API uses, 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 the first two of these:
87
+
88
+ ```python
89
+ {"asn": {"type": "hosting"}} # the network's type
90
+ [{"is_anycast": True}, {"is_satellite": True}] # anycast and satellite space
91
+ [{"is_hosting": True}, {"is_cdn": True}] # traffic from servers, not people
92
+ [{"is_vpn": True}, {"is_tor": True}, {"is_resproxy": True}] # anonymity: a list is OR
93
+ {"is_vpn": True, "vpn": {"provider": "nordvpn"}} # one provider
94
+ {"resproxy": {"hits": {"gte": 5}}} # a numeric threshold
95
+ ```
96
+
97
+ Values are matched by equality, strings without regard to case. A list means any-of. A dict of `gte`/`gt`/`lte`/`lt` compares numbers, and every bound you give must hold, so two of them are a range. Members set to `False` or `None` are ignored, so a condition states the signals you act on; one that constrains nothing would match every request, and is refused when the middleware is built rather than silently blocking all your traffic.
98
+
99
+ Replace the refusal with `on_blocked`, which takes the request and the answer and returns a response, or awaits one:
100
+
101
+ ```python
102
+ app.add_middleware(
103
+ InternetDataMiddleware,
104
+ api_key=os.environ.get("INTERNETDATA_API_KEY"),
105
+ block_condition={"location": {"country": ["CU", "IR", "KP"]}},
106
+ on_blocked=lambda request, found: JSONResponse({"error": "not available"}, status_code=451),
107
+ )
108
+ ```
109
+
110
+ ### Blocking one endpoint
111
+
112
+ `block_condition` applies to every endpoint. In FastAPI, refuse a visitor on some endpoints only with the `block_if` dependency, on a path operation, a router or the app:
113
+
114
+ ```python
115
+ from fastapi import Depends
116
+ from internetdata_fastapi import block_if
117
+
118
+ embargoed = block_if({"location": {"country": ["CU", "IR", "KP"]}})
119
+
120
+
121
+ @app.get("/checkout", dependencies=[Depends(embargoed)])
122
+ async def checkout(): ...
123
+ ```
124
+
125
+ A refused request gets an `HTTPException`, `403` with `{"detail": "access denied"}` unless you pass `status_code` and `detail`. Declared as a parameter, the dependency hands the endpoint the answer. `block_if` takes the same condition, `fail_closed` and `on_missing_field`. It judges the answer the middleware already attached, so a visitor is looked up once however many endpoints check them, and it warns once for a member your plan doesn't include. A request `skip` claimed reaches the endpoint. The middleware still has to be added: without it, the dependency raises `RuntimeError` instead of letting everyone through.
126
+
127
+ ## Where the client address comes from
128
+
129
+ This is the setting that decides whether any of the above works, and it is the one thing only you can get right.
130
+
131
+ By default the middleware uses `request.client.host`. uvicorn, and `fastapi run` with it, reads proxy headers by default, but only from a peer that `--forwarded-allow-ips` trusts, and that defaults to `127.0.0.1,::1` (or `$FORWARDED_ALLOW_IPS`). So behind a proxy on the same machine, the visitor's address arrives with no setup. Behind one anywhere else, such as nginx in its own container or a load balancer in a VPC, every visitor looks like the proxy. That address is usually a private one, which the middleware answers locally, as an address with no country: nothing is blocked, and the private-address warning below is the only sign. Name your proxy, by its address or its subnet: `--forwarded-allow-ips=172.28.5.0/28`. gunicorn's own `--forwarded-allow-ips` works the same way. Never pass `*`. It trusts every peer, so the left-most `X-Forwarded-For` entry wins, and that is whatever the visitor sent: someone who sends `X-Forwarded-For: 1.1.1.1` is looked up as `1.1.1.1`.
132
+
133
+ For an edge that writes the address into its own header, name the header:
134
+
135
+ ```python
136
+ from internetdata_fastapi import header_ip_selector
137
+
138
+ app.add_middleware(
139
+ InternetDataMiddleware,
140
+ ip_selector=header_ip_selector("CF-Connecting-IP"), # or True-Client-IP, or your own
141
+ )
142
+ ```
143
+
144
+ `xff_ip_selector()` 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: `xff_ip_selector(1)` is the address your nearest proxy saw.
145
+
146
+ Anything else, pass your own callable. It receives the request and returns an address.
147
+
148
+ If the address resolves to a private one, the middleware says so once through the `internetdata` logger. That is expected on localhost and is the signal to fix your configuration anywhere else.
149
+
150
+ ## When a lookup fails
151
+
152
+ The request is let through, and the reason is recorded on the answer's `error`. Our outage should not become yours, so a network failure, a rate limit or a rejected key all fail open. Pass `fail_closed=True` to block instead. Private addresses are answered locally and never fail, so this will not lock you out in development.
153
+
154
+ ## Cost and latency
155
+
156
+ Answers are cached 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 2.5 seconds by default and not retried: on a request path, failing open quickly beats holding a visitor while we try again. Both are adjustable with `timeout` and `retries`, and the cache through an `AsyncInternetData` client you build yourself and pass as `client`.
157
+
158
+ Skip what you do not care about:
159
+
160
+ ```python
161
+ app.add_middleware(
162
+ InternetDataMiddleware,
163
+ api_key=os.environ.get("INTERNETDATA_API_KEY"),
164
+ skip=lambda request: request.url.path.startswith("/static"),
165
+ )
166
+ ```
167
+
168
+ Beyond a few million distinct visitors a day, stop calling the API per request: look addresses up locally in [our databases](https://internetdata.io/databases) instead.
169
+
170
+ ## Absent is not false
171
+
172
+ Unlimited, with a key or without one, leaves out `is_vpn`, `is_hosting`, `is_cdn` and the four proxy flags, the detail behind them, and the paid members inside `location`, `asn`, `company` and `abuse`, such as `location.city`. A field your plan does not include is `None`, which means "not in your plan" rather than "checked, and no".
173
+
174
+ ```python
175
+ lookup(request).result.flagged("is_vpn") # False rather than None on a plan without it
176
+ ```
177
+
178
+ A `block_condition` naming a member your plan does not serve, `location.city` on Unlimited included, can never match, so the middleware warns once instead of failing silently. Pass `on_missing_field="raise"` to make it an error.
179
+
180
+ ## Other Libraries
181
+
182
+ 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.
183
+
184
+ ## About InternetData
185
+
186
+ Geolocation, anonymity, ownership and network data for IP addresses, AS numbers and domains, as an API and as databases you download and query yourself.
187
+
188
+ [<img src="https://s3.internetdata.io/internetdata-public/brand/mark.svg" alt="InternetData" height="64"/>](https://internetdata.io/)
189
+
190
+ ## License
191
+
192
+ This project is licensed under the [MIT License](LICENSE).
@@ -0,0 +1,6 @@
1
+ internetdata_fastapi/__init__.py,sha256=53CtdFc4a5Qezy1a0a2nKdiFat4Nw41vOurnd4QPzPk,7239
2
+ internetdata_fastapi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ internetdata_fastapi-1.0.0.dist-info/METADATA,sha256=h27-kF2KaP8wDz-D06H_cqOufjspFbymQAsHS0All-4,10954
4
+ internetdata_fastapi-1.0.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
5
+ internetdata_fastapi-1.0.0.dist-info/licenses/LICENSE,sha256=wglb3Uww0HrX4-4rYO9Xe4j8Sn9STPtK2M9Tt5NKTrQ,1065
6
+ internetdata_fastapi-1.0.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.33.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mslm Dev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.