19
19
from debug_toolbar .toolbar import DebugToolbar
20
20
from debug_toolbar .utils import clear_stack_trace_caches , is_processable_html_response
21
21
22
+ _resolved_gateway_ip = None
23
+
24
+
25
+ def _gateway_ip ():
26
+ global _resolved_gateway_ip
27
+ if _resolved_gateway_ip is None :
28
+ print (
29
+ "[django-debug-toolbar] Trying to configure determine the Docker gateay address for autoconfiguration... " ,
30
+ end = "" ,
31
+ )
32
+ try :
33
+ # This is a hack for docker installations. It attempts to look
34
+ # up the IP address of the docker host.
35
+ # This is not guaranteed to work.
36
+ _resolved_gateway_ip = (
37
+ # Convert the last segment of the IP address to be .1
38
+ "." .join (socket .gethostbyname ("host.docker.internal" ).rsplit ("." )[:- 1 ])
39
+ + ".1"
40
+ )
41
+ print ("Success." )
42
+ except socket .gaierror :
43
+ # It's fine if the lookup errored since they may not be using docker
44
+ _resolved_gateway_ip = "unresolvable"
45
+ print ("Not resolvable." )
46
+ return _resolved_gateway_ip
47
+
22
48
23
49
def show_toolbar (request ):
24
50
"""
@@ -32,20 +58,8 @@ def show_toolbar(request):
32
58
return True
33
59
34
60
# Test: Docker
35
- try :
36
- # This is a hack for docker installations. It attempts to look
37
- # up the IP address of the docker host.
38
- # This is not guaranteed to work.
39
- docker_ip = (
40
- # Convert the last segment of the IP address to be .1
41
- "." .join (socket .gethostbyname ("host.docker.internal" ).rsplit ("." )[:- 1 ])
42
- + ".1"
43
- )
44
- if request .META .get ("REMOTE_ADDR" ) == docker_ip :
45
- return True
46
- except socket .gaierror :
47
- # It's fine if the lookup errored since they may not be using docker
48
- pass
61
+ if request .META .get ("REMOTE_ADDR" ) == _gateway_ip ():
62
+ return True
49
63
50
64
# No test passed
51
65
return False
0 commit comments