internetdata-django 1.0.0__tar.gz
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.
- internetdata_django-1.0.0/.github/workflows/ci.yml +25 -0
- internetdata_django-1.0.0/.github/workflows/release.yml +63 -0
- internetdata_django-1.0.0/.gitignore +17 -0
- internetdata_django-1.0.0/CHANGELOG.md +13 -0
- internetdata_django-1.0.0/LICENSE +21 -0
- internetdata_django-1.0.0/PKG-INFO +183 -0
- internetdata_django-1.0.0/README.md +154 -0
- internetdata_django-1.0.0/SECURITY.md +5 -0
- internetdata_django-1.0.0/pyproject.toml +53 -0
- internetdata_django-1.0.0/src/internetdata_django/__init__.py +199 -0
- internetdata_django-1.0.0/src/internetdata_django/py.typed +0 -0
- internetdata_django-1.0.0/testdata/bogons.json +86 -0
- internetdata_django-1.0.0/testdata/testdata.json +12449 -0
- internetdata_django-1.0.0/tests/test_middleware.py +492 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
test:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
strategy:
|
|
12
|
+
matrix:
|
|
13
|
+
python: ['3.11', '3.12', '3.13', '3.14']
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v7
|
|
16
|
+
- uses: actions/setup-python@v7
|
|
17
|
+
with:
|
|
18
|
+
python-version: ${{ matrix.python }}
|
|
19
|
+
cache: pip
|
|
20
|
+
cache-dependency-path: pyproject.toml
|
|
21
|
+
- run: pip install -e ".[dev]"
|
|
22
|
+
- run: ruff check .
|
|
23
|
+
- run: ruff format --check .
|
|
24
|
+
- run: mypy src
|
|
25
|
+
- run: pytest
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
name: Release
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags: ['v*']
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
publish:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
permissions:
|
|
11
|
+
contents: read
|
|
12
|
+
# PyPI trusted publishing exchanges this OIDC token for a short-lived upload
|
|
13
|
+
# token. No API token in a repository secret.
|
|
14
|
+
id-token: write
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v7
|
|
17
|
+
- uses: actions/setup-python@v7
|
|
18
|
+
with:
|
|
19
|
+
python-version: '3.13'
|
|
20
|
+
cache: pip
|
|
21
|
+
cache-dependency-path: pyproject.toml
|
|
22
|
+
# The tag only TRIGGERS this; PyPI publishes whatever the built artifact says.
|
|
23
|
+
# Without this check, tagging v1.0.1 against a 1.0.0 package silently
|
|
24
|
+
# republishes 1.0.0, or fails as "already exists" with a confusing message.
|
|
25
|
+
- name: Tag must match the package version
|
|
26
|
+
run: |
|
|
27
|
+
tag="${GITHUB_REF_NAME#v}"
|
|
28
|
+
pkg="$(sed -n 's/^__version__ = "\(.*\)"$/\1/p' src/internetdata_django/__init__.py)"
|
|
29
|
+
if [ "$tag" != "$pkg" ] ; then
|
|
30
|
+
echo "tag $tag does not match __version__ $pkg" >&2
|
|
31
|
+
exit 1
|
|
32
|
+
fi
|
|
33
|
+
- run: pip install -e ".[dev]" build
|
|
34
|
+
- run: pytest
|
|
35
|
+
- run: python -m build
|
|
36
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
|
37
|
+
|
|
38
|
+
# This version's section of CHANGELOG.md, as the tag's GitHub Release. It
|
|
39
|
+
# waits for the publish, so no release page names a version the registry
|
|
40
|
+
# cannot serve, and a re-run edits the release rather than failing on it.
|
|
41
|
+
github-release:
|
|
42
|
+
needs: publish
|
|
43
|
+
runs-on: ubuntu-latest
|
|
44
|
+
permissions:
|
|
45
|
+
contents: write
|
|
46
|
+
steps:
|
|
47
|
+
- uses: actions/checkout@v7
|
|
48
|
+
- name: GitHub Release from CHANGELOG.md
|
|
49
|
+
env:
|
|
50
|
+
GH_TOKEN: ${{ github.token }}
|
|
51
|
+
GH_REPO: ${{ github.repository }}
|
|
52
|
+
run: |
|
|
53
|
+
version="${GITHUB_REF_NAME#v}"
|
|
54
|
+
awk -v v="$version" '/^## /{ if (found) exit; found = ($2 == v); next } found' CHANGELOG.md > notes.md
|
|
55
|
+
if ! grep -q '^### ' notes.md ; then
|
|
56
|
+
echo "CHANGELOG.md has no section for ${version}" >&2
|
|
57
|
+
exit 1
|
|
58
|
+
fi
|
|
59
|
+
if gh release view "$GITHUB_REF_NAME" > /dev/null 2>&1 ; then
|
|
60
|
+
gh release edit "$GITHUB_REF_NAME" --notes-file notes.md
|
|
61
|
+
else
|
|
62
|
+
gh release create "$GITHUB_REF_NAME" --title "$GITHUB_REF_NAME" --notes-file notes.md --verify-tag
|
|
63
|
+
fi
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
__pycache__/
|
|
2
|
+
*.py[cod]
|
|
3
|
+
|
|
4
|
+
# Build output.
|
|
5
|
+
build/
|
|
6
|
+
dist/
|
|
7
|
+
*.egg-info/
|
|
8
|
+
|
|
9
|
+
# Virtualenvs, including any made to run codegen. Keep them out of the tree entirely.
|
|
10
|
+
.venv/
|
|
11
|
+
venv/
|
|
12
|
+
.venv-*/
|
|
13
|
+
|
|
14
|
+
# Tool caches.
|
|
15
|
+
.pytest_cache/
|
|
16
|
+
.mypy_cache/
|
|
17
|
+
.ruff_cache/
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
What each release changed for you, newest first. Each line is a commit's summary, linked to its full description and diff.
|
|
4
|
+
|
|
5
|
+
## 1.0.0 - 2026-11-14
|
|
6
|
+
|
|
7
|
+
### Features
|
|
8
|
+
|
|
9
|
+
- Attach the visitor's InternetData record to every request as `request.internetdata`: the location, the network and the company behind the address, and the anonymity and hosting flags your plan serves ([`c47a7c1`](https://github.com/internetdata/sdk-python-django/commit/c47a7c186f1b126400bbb6e1163069acafde33a4))
|
|
10
|
+
- Refuse a request with a `block_condition` in `settings.INTERNETDATA`, written in the shape of a result, by country, network type, anonymity or any other member, with 403 or an `on_blocked` of your own ([`c47a7c1`](https://github.com/internetdata/sdk-python-django/commit/c47a7c186f1b126400bbb6e1163069acafde33a4))
|
|
11
|
+
- Refuse one view with the `block_if` decorator, sync or async, judging the answer the middleware already attached ([`c47a7c1`](https://github.com/internetdata/sdk-python-django/commit/c47a7c186f1b126400bbb6e1163069acafde33a4))
|
|
12
|
+
- Choose where the client address comes from: `REMOTE_ADDR` by default, `xff_ip_selector`, `header_ip_selector` for a header your edge writes, or a callable of your own ([`c47a7c1`](https://github.com/internetdata/sdk-python-django/commit/c47a7c186f1b126400bbb6e1163069acafde33a4))
|
|
13
|
+
- Fail open on any lookup failure unless `fail_closed` is set, with a 2.5 s bound, no retry and a cache by default ([`c47a7c1`](https://github.com/internetdata/sdk-python-django/commit/c47a7c186f1b126400bbb6e1163069acafde33a4))
|
|
@@ -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.
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: internetdata-django
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Official Django 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-django
|
|
7
|
+
Author-email: Mslm Dev <support@internetdata.io>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: asn,django,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: django>=4.2
|
|
23
|
+
Requires-Dist: internetdata<3,>=2.12.0
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: mypy>=1.11; extra == 'dev'
|
|
26
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
27
|
+
Requires-Dist: ruff>=0.6; extra == 'dev'
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
|
|
30
|
+
# [<img src="https://s3.internetdata.io/internetdata-public/brand/mark.svg" alt="InternetData" height="28"/>](https://internetdata.io/) InternetData Django Middleware
|
|
31
|
+
|
|
32
|
+
[](https://pypi.org/project/internetdata-django/)
|
|
33
|
+
[](LICENSE)
|
|
34
|
+
|
|
35
|
+
The official [Django](https://www.djangoproject.com) middleware for the [InternetData](https://internetdata.io) API.
|
|
36
|
+
|
|
37
|
+
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 views. Blocking is opt-in.
|
|
38
|
+
|
|
39
|
+
## Getting Started
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
pip install internetdata-django
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Requires Python 3.11 or newer and Django 4.2 or newer.
|
|
46
|
+
|
|
47
|
+
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.
|
|
48
|
+
|
|
49
|
+
Add it to `MIDDLEWARE` and configure it with an `INTERNETDATA` dict:
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
# settings.py
|
|
53
|
+
MIDDLEWARE = [
|
|
54
|
+
# ...
|
|
55
|
+
"internetdata_django.InternetDataMiddleware",
|
|
56
|
+
]
|
|
57
|
+
|
|
58
|
+
INTERNETDATA = {"api_key": os.environ.get("INTERNETDATA_API_KEY")}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
def index(request):
|
|
63
|
+
result = request.internetdata.result
|
|
64
|
+
country = result.location.country_name if result else "somewhere"
|
|
65
|
+
return HttpResponse(f"Hello, visitor from {country}")
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
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.
|
|
69
|
+
|
|
70
|
+
A request your `skip` claims gets no answer, so it carries no `request.internetdata`. Wherever `skip` can apply, read the answer as `lookup(request)`, imported from `internetdata_django`, which is `None` for such a request.
|
|
71
|
+
|
|
72
|
+
## Blocking
|
|
73
|
+
|
|
74
|
+
Pass a `block_condition` and a matching request is answered with `403` and never reaches your views.
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
INTERNETDATA = {
|
|
78
|
+
"api_key": os.environ.get("INTERNETDATA_API_KEY"),
|
|
79
|
+
"block_condition": {"location": {"country": ["CU", "IR", "KP"]}},
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
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:
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
{"asn": {"type": "hosting"}} # the network's type
|
|
87
|
+
[{"is_anycast": True}, {"is_satellite": True}] # anycast and satellite space
|
|
88
|
+
[{"is_hosting": True}, {"is_cdn": True}] # traffic from servers, not people
|
|
89
|
+
[{"is_vpn": True}, {"is_tor": True}, {"is_resproxy": True}] # anonymity: a list is OR
|
|
90
|
+
{"is_vpn": True, "vpn": {"provider": "nordvpn"}} # one provider
|
|
91
|
+
{"resproxy": {"hits": {"gte": 5}}} # a numeric threshold
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
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.
|
|
95
|
+
|
|
96
|
+
Replace the refusal with `on_blocked`, which takes the request and the answer and returns a response:
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
INTERNETDATA = {
|
|
100
|
+
"api_key": os.environ.get("INTERNETDATA_API_KEY"),
|
|
101
|
+
"block_condition": {"location": {"country": ["CU", "IR", "KP"]}},
|
|
102
|
+
"on_blocked": lambda request, found: render(request, "unavailable.html", status=451),
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Blocking one view
|
|
107
|
+
|
|
108
|
+
`block_condition` applies to every view. To refuse a visitor on some views only, decorate them, sync or async:
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
from internetdata_django import block_if
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
@block_if({"location": {"country": ["CU", "IR", "KP"]}})
|
|
115
|
+
def checkout(request): ...
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
`block_if` takes the same condition, `on_blocked`, `fail_closed` and `on_missing_field`. It judges the answer the middleware already attached, so a visitor is looked up once however many views check them, and it warns once for a member your plan doesn't include. A request `skip` claimed reaches the view. The middleware still has to be installed: without it, a decorated view raises `ImproperlyConfigured` instead of letting everyone through.
|
|
119
|
+
|
|
120
|
+
## Where the client address comes from
|
|
121
|
+
|
|
122
|
+
This is the setting that decides whether any of the above works, and it is the one thing only you can get right.
|
|
123
|
+
|
|
124
|
+
By default the middleware uses `REMOTE_ADDR`, which is the socket peer. **Django deliberately does not read `X-Forwarded-For` for you**, because trusting a header without knowing your topology is unsafe. So if your app sits behind nginx, a load balancer, or a CDN, 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 middleware answers locally without a lookup, as an address with no country, and logs a warning.
|
|
125
|
+
|
|
126
|
+
For an edge that writes the address into its own header, name the header:
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
from internetdata_django import header_ip_selector
|
|
130
|
+
|
|
131
|
+
INTERNETDATA = {
|
|
132
|
+
"api_key": os.environ.get("INTERNETDATA_API_KEY"),
|
|
133
|
+
"ip_selector": header_ip_selector("CF-Connecting-IP"), # or True-Client-IP, or your own
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
`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.
|
|
138
|
+
|
|
139
|
+
Anything else, pass your own callable. It receives the request and returns an address.
|
|
140
|
+
|
|
141
|
+
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.
|
|
142
|
+
|
|
143
|
+
## When a lookup fails
|
|
144
|
+
|
|
145
|
+
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.
|
|
146
|
+
|
|
147
|
+
## Cost and latency
|
|
148
|
+
|
|
149
|
+
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 `InternetData` client you build yourself and pass as `client`.
|
|
150
|
+
|
|
151
|
+
Skip what you do not care about, or must never refuse:
|
|
152
|
+
|
|
153
|
+
```python
|
|
154
|
+
"skip": lambda request: request.path.startswith("/webhooks/"),
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
A webhook comes from its sender's servers, an address a hosting rule would refuse. Static files need no skip: `runserver`, WhiteNoise and a web server in front each answer them before this middleware runs.
|
|
158
|
+
|
|
159
|
+
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.
|
|
160
|
+
|
|
161
|
+
## Absent is not false
|
|
162
|
+
|
|
163
|
+
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".
|
|
164
|
+
|
|
165
|
+
```python
|
|
166
|
+
request.internetdata.result.flagged("is_vpn") # False rather than None on a plan without it
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
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.
|
|
170
|
+
|
|
171
|
+
## Other Libraries
|
|
172
|
+
|
|
173
|
+
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.
|
|
174
|
+
|
|
175
|
+
## About InternetData
|
|
176
|
+
|
|
177
|
+
Geolocation, anonymity, ownership and network data for IP addresses, AS numbers and domains, as an API and as databases you download and query yourself.
|
|
178
|
+
|
|
179
|
+
[<img src="https://s3.internetdata.io/internetdata-public/brand/mark.svg" alt="InternetData" height="64"/>](https://internetdata.io/)
|
|
180
|
+
|
|
181
|
+
## License
|
|
182
|
+
|
|
183
|
+
This project is licensed under the [MIT License](LICENSE).
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
# [<img src="https://s3.internetdata.io/internetdata-public/brand/mark.svg" alt="InternetData" height="28"/>](https://internetdata.io/) InternetData Django Middleware
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/internetdata-django/)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
|
|
6
|
+
The official [Django](https://www.djangoproject.com) middleware for the [InternetData](https://internetdata.io) API.
|
|
7
|
+
|
|
8
|
+
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 views. Blocking is opt-in.
|
|
9
|
+
|
|
10
|
+
## Getting Started
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pip install internetdata-django
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Requires Python 3.11 or newer and Django 4.2 or newer.
|
|
17
|
+
|
|
18
|
+
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.
|
|
19
|
+
|
|
20
|
+
Add it to `MIDDLEWARE` and configure it with an `INTERNETDATA` dict:
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
# settings.py
|
|
24
|
+
MIDDLEWARE = [
|
|
25
|
+
# ...
|
|
26
|
+
"internetdata_django.InternetDataMiddleware",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
INTERNETDATA = {"api_key": os.environ.get("INTERNETDATA_API_KEY")}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
def index(request):
|
|
34
|
+
result = request.internetdata.result
|
|
35
|
+
country = result.location.country_name if result else "somewhere"
|
|
36
|
+
return HttpResponse(f"Hello, visitor from {country}")
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
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.
|
|
40
|
+
|
|
41
|
+
A request your `skip` claims gets no answer, so it carries no `request.internetdata`. Wherever `skip` can apply, read the answer as `lookup(request)`, imported from `internetdata_django`, which is `None` for such a request.
|
|
42
|
+
|
|
43
|
+
## Blocking
|
|
44
|
+
|
|
45
|
+
Pass a `block_condition` and a matching request is answered with `403` and never reaches your views.
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
INTERNETDATA = {
|
|
49
|
+
"api_key": os.environ.get("INTERNETDATA_API_KEY"),
|
|
50
|
+
"block_condition": {"location": {"country": ["CU", "IR", "KP"]}},
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
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:
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
{"asn": {"type": "hosting"}} # the network's type
|
|
58
|
+
[{"is_anycast": True}, {"is_satellite": True}] # anycast and satellite space
|
|
59
|
+
[{"is_hosting": True}, {"is_cdn": True}] # traffic from servers, not people
|
|
60
|
+
[{"is_vpn": True}, {"is_tor": True}, {"is_resproxy": True}] # anonymity: a list is OR
|
|
61
|
+
{"is_vpn": True, "vpn": {"provider": "nordvpn"}} # one provider
|
|
62
|
+
{"resproxy": {"hits": {"gte": 5}}} # a numeric threshold
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
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.
|
|
66
|
+
|
|
67
|
+
Replace the refusal with `on_blocked`, which takes the request and the answer and returns a response:
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
INTERNETDATA = {
|
|
71
|
+
"api_key": os.environ.get("INTERNETDATA_API_KEY"),
|
|
72
|
+
"block_condition": {"location": {"country": ["CU", "IR", "KP"]}},
|
|
73
|
+
"on_blocked": lambda request, found: render(request, "unavailable.html", status=451),
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Blocking one view
|
|
78
|
+
|
|
79
|
+
`block_condition` applies to every view. To refuse a visitor on some views only, decorate them, sync or async:
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
from internetdata_django import block_if
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
@block_if({"location": {"country": ["CU", "IR", "KP"]}})
|
|
86
|
+
def checkout(request): ...
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
`block_if` takes the same condition, `on_blocked`, `fail_closed` and `on_missing_field`. It judges the answer the middleware already attached, so a visitor is looked up once however many views check them, and it warns once for a member your plan doesn't include. A request `skip` claimed reaches the view. The middleware still has to be installed: without it, a decorated view raises `ImproperlyConfigured` instead of letting everyone through.
|
|
90
|
+
|
|
91
|
+
## Where the client address comes from
|
|
92
|
+
|
|
93
|
+
This is the setting that decides whether any of the above works, and it is the one thing only you can get right.
|
|
94
|
+
|
|
95
|
+
By default the middleware uses `REMOTE_ADDR`, which is the socket peer. **Django deliberately does not read `X-Forwarded-For` for you**, because trusting a header without knowing your topology is unsafe. So if your app sits behind nginx, a load balancer, or a CDN, 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 middleware answers locally without a lookup, as an address with no country, and logs a warning.
|
|
96
|
+
|
|
97
|
+
For an edge that writes the address into its own header, name the header:
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
from internetdata_django import header_ip_selector
|
|
101
|
+
|
|
102
|
+
INTERNETDATA = {
|
|
103
|
+
"api_key": os.environ.get("INTERNETDATA_API_KEY"),
|
|
104
|
+
"ip_selector": header_ip_selector("CF-Connecting-IP"), # or True-Client-IP, or your own
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
`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.
|
|
109
|
+
|
|
110
|
+
Anything else, pass your own callable. It receives the request and returns an address.
|
|
111
|
+
|
|
112
|
+
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.
|
|
113
|
+
|
|
114
|
+
## When a lookup fails
|
|
115
|
+
|
|
116
|
+
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.
|
|
117
|
+
|
|
118
|
+
## Cost and latency
|
|
119
|
+
|
|
120
|
+
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 `InternetData` client you build yourself and pass as `client`.
|
|
121
|
+
|
|
122
|
+
Skip what you do not care about, or must never refuse:
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
"skip": lambda request: request.path.startswith("/webhooks/"),
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
A webhook comes from its sender's servers, an address a hosting rule would refuse. Static files need no skip: `runserver`, WhiteNoise and a web server in front each answer them before this middleware runs.
|
|
129
|
+
|
|
130
|
+
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.
|
|
131
|
+
|
|
132
|
+
## Absent is not false
|
|
133
|
+
|
|
134
|
+
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".
|
|
135
|
+
|
|
136
|
+
```python
|
|
137
|
+
request.internetdata.result.flagged("is_vpn") # False rather than None on a plan without it
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
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.
|
|
141
|
+
|
|
142
|
+
## Other Libraries
|
|
143
|
+
|
|
144
|
+
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.
|
|
145
|
+
|
|
146
|
+
## About InternetData
|
|
147
|
+
|
|
148
|
+
Geolocation, anonymity, ownership and network data for IP addresses, AS numbers and domains, as an API and as databases you download and query yourself.
|
|
149
|
+
|
|
150
|
+
[<img src="https://s3.internetdata.io/internetdata-public/brand/mark.svg" alt="InternetData" height="64"/>](https://internetdata.io/)
|
|
151
|
+
|
|
152
|
+
## License
|
|
153
|
+
|
|
154
|
+
This project is licensed under the [MIT License](LICENSE).
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# Security policy
|
|
2
|
+
|
|
3
|
+
Please report a security issue in this repository by email to support@internetdata.io, not in a public issue, pull request or discussion. Include what you found and the steps that reproduce it.
|
|
4
|
+
|
|
5
|
+
Keep any testing against the InternetData API to your own account and API key, and give us a reasonable chance to fix the issue before you publish it.
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.27"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "internetdata-django"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Official Django middleware for the InternetData API. Look up the visitor behind every request: location, network, VPN, proxy, Tor and hosting."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
requires-python = ">=3.11"
|
|
13
|
+
authors = [{ name = "Mslm Dev", email = "support@internetdata.io" }]
|
|
14
|
+
keywords = ["internetdata", "ip-geolocation", "ip-intelligence", "ip-lookup", "geoblocking", "vpn-detection", "proxy-detection", "tor", "hosting", "asn", "middleware", "django"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 5 - Production/Stable",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.11",
|
|
20
|
+
"Programming Language :: Python :: 3.12",
|
|
21
|
+
"Programming Language :: Python :: 3.13",
|
|
22
|
+
"Programming Language :: Python :: 3.14",
|
|
23
|
+
"Topic :: Internet",
|
|
24
|
+
"Topic :: Security",
|
|
25
|
+
"Typing :: Typed",
|
|
26
|
+
]
|
|
27
|
+
dependencies = ["internetdata>=2.12.0,<3", "django>=4.2"]
|
|
28
|
+
|
|
29
|
+
[project.urls]
|
|
30
|
+
Homepage = "https://internetdata.io"
|
|
31
|
+
Source = "https://github.com/internetdata/sdk-python-django"
|
|
32
|
+
|
|
33
|
+
[project.optional-dependencies]
|
|
34
|
+
dev = ["pytest>=8", "mypy>=1.11", "ruff>=0.6"]
|
|
35
|
+
|
|
36
|
+
[tool.hatch.build.targets.wheel]
|
|
37
|
+
packages = ["src/internetdata_django"]
|
|
38
|
+
|
|
39
|
+
[tool.hatch.version]
|
|
40
|
+
path = "src/internetdata_django/__init__.py"
|
|
41
|
+
|
|
42
|
+
[tool.ruff]
|
|
43
|
+
line-length = 96
|
|
44
|
+
|
|
45
|
+
[tool.mypy]
|
|
46
|
+
python_version = "3.11"
|
|
47
|
+
strict = true
|
|
48
|
+
|
|
49
|
+
# Django ships no py.typed marker and no inline stubs. django-stubs exists but pins a
|
|
50
|
+
# Django version range, which would make this package refuse a Django it works fine on.
|
|
51
|
+
[[tool.mypy.overrides]]
|
|
52
|
+
module = ["django.*"]
|
|
53
|
+
ignore_missing_imports = true
|