1
1
from django .http import HttpResponse , HttpResponseForbidden
2
2
from django .shortcuts import get_object_or_404
3
3
from django .conf import settings
4
+ import ipaddress
4
5
5
6
from .views import cache
6
7
from .models import Message , List
7
8
8
9
import json
9
10
10
11
12
+ def is_host_allowed (request ):
13
+ for ip_range in settings .API_CLIENTS :
14
+ if ipaddress .ip_address (request .META ['REMOTE_ADDR' ]) in ipaddress .ip_network (ip_range ):
15
+ return True
16
+ return False
17
+
18
+
11
19
@cache (hours = 4 )
12
20
def listinfo (request ):
13
21
if not settings .PUBLIC_ARCHIVES :
14
22
return HttpResponseForbidden ('No API access on private archives for now' )
15
23
16
- if not request . META [ 'REMOTE_ADDR' ] in settings . API_CLIENTS :
24
+ if not is_host_allowed ( request ) :
17
25
return HttpResponseForbidden ('Invalid host' )
18
26
19
27
resp = HttpResponse (content_type = 'application/json' )
@@ -33,7 +41,7 @@ def latest(request, listname):
33
41
if not settings .PUBLIC_ARCHIVES :
34
42
return HttpResponseForbidden ('No API access on private archives for now' )
35
43
36
- if not request . META [ 'REMOTE_ADDR' ] in settings . API_CLIENTS :
44
+ if not is_host_allowed ( request ) :
37
45
return HttpResponseForbidden ('Invalid host' )
38
46
39
47
# Return the latest <n> messages on this list.
@@ -94,7 +102,7 @@ def thread(request, msgid):
94
102
if not settings .PUBLIC_ARCHIVES :
95
103
return HttpResponseForbidden ('No API access on private archives for now' )
96
104
97
- if not request . META [ 'REMOTE_ADDR' ] in settings . API_CLIENTS :
105
+ if not is_host_allowed ( request ) :
98
106
return HttpResponseForbidden ('Invalid host' )
99
107
100
108
# Return metadata about a single thread. A list of all the emails
0 commit comments