@@ -22,51 +22,28 @@ const HTTP_TOOLKIT_INJECTED_OVERRIDES_PATH = path.posix.join(HTTP_TOOLKIT_INJECT
22
22
const HTTP_TOOLKIT_INJECTED_CA_PATH = path . posix . join ( HTTP_TOOLKIT_INJECTED_PATH , 'ca.pem' ) ;
23
23
24
24
/**
25
- * The hostname that resolves to the host OS (i.e. generally: where HTTP Toolkit is running)
25
+ * Get the hostname that resolves to the host OS (i.e. generally: where HTTP Toolkit is running)
26
26
* from inside containers.
27
27
*
28
28
* In Docker for Windows & Mac, host.docker.internal is supported automatically:
29
29
* https://docs.docker.com/docker-for-windows/networking/#use-cases-and-workarounds
30
30
* https://docs.docker.com/docker-for-mac/networking/#use-cases-and-workarounds
31
31
*
32
- * On Linux this is _not_ supported, so we add it ourselves with (--add-host) .
32
+ * On Linux this is _not_ supported, and we need to be more clever .
33
33
*/
34
- export const DOCKER_HOST_HOSTNAME = "host.docker.internal" ;
35
-
36
- /**
37
- * To make the above hostname work on Linux, where it's not supported by default, we need to map it to the
38
- * host ip. This method works out the host IP to use to do so.
39
- */
40
- export const getDockerHostIp = (
34
+ export function getDockerHostAddress (
41
35
platform : typeof process . platform ,
42
- dockerVersion : { apiVersion : string } | { engineVersion : string } ,
43
36
containerMetadata ?: Docker . ContainerInspectInfo
44
- ) => {
45
- const semverVersion = semver . coerce (
46
- 'apiVersion' in dockerVersion
47
- ? dockerVersion . apiVersion
48
- : dockerVersion . engineVersion
49
- ) ;
50
-
51
- if ( platform !== 'linux' ) {
52
- // On non-linux platforms this method isn't necessary - host.docker.internal is always supported
53
- // so we can just use that.
54
- return DOCKER_HOST_HOSTNAME ;
55
- } else if (
56
- semver . satisfies (
57
- semverVersion ?? '0.0.0' ,
58
- 'apiVersion' in dockerVersion ? '>=1.41' : '>=20.10'
59
- )
60
- ) {
61
- // This is supported in Docker Engine 20.10, so always supported at least in API 1.41+
62
- // Special name defined in new Docker versions, that refers to the host gateway
63
- return 'host-gateway' ;
64
- } else if ( containerMetadata ) {
65
- // Old/Unknown Linux with known container: query the metadata, and if _that_ fails, use the default gateway IP.
66
- return containerMetadata . NetworkSettings . Gateway || "172.17.0.1" ;
37
+ ) {
38
+ if ( platform === 'win32' || platform === 'darwin' ) {
39
+ // On Docker Desktop, this alias always points to the host (outside the VM) IP:
40
+ return 'host.docker.internal' ;
67
41
} else {
68
- // Old/Unknown Linux without a container (e.g. during a build). Always use the default gateway IP:
69
- return "172.17.0.1" ;
42
+ // Elsewhere (Linux) we should be able to always use the gateway address. We avoid
43
+ // using ExtraHosts with host-gateway, because that uses /etc/hosts, and not all
44
+ // clients use that for resolution (some use _only_ DNS lookups). IPs avoid this.
45
+ return containerMetadata ?. NetworkSettings . Gateway
46
+ || "172.17.0.1" ;
70
47
}
71
48
}
72
49
@@ -134,7 +111,7 @@ export function transformContainerCreationConfig(
134
111
{ certPath : HTTP_TOOLKIT_INJECTED_CA_PATH } ,
135
112
envArrayToObject ( currentConfig . Env ) ,
136
113
{
137
- httpToolkitIp : DOCKER_HOST_HOSTNAME ,
114
+ httpToolkitHost : getDockerHostAddress ( process . platform ) ,
138
115
overridePath : HTTP_TOOLKIT_INJECTED_OVERRIDES_PATH ,
139
116
targetPlatform : 'linux'
140
117
}
@@ -160,18 +137,7 @@ export function transformContainerCreationConfig(
160
137
// Bind-mount the overrides directory into the container:
161
138
`${ OVERRIDES_DIR } :${ HTTP_TOOLKIT_INJECTED_OVERRIDES_PATH } :ro`
162
139
// ^ Both 'ro' - untrusted containers must not be able to mess with these!
163
- ] ,
164
- ...( process . platform === 'linux'
165
- // On Linux only, we need to add an explicit host to make host.docker.internal work:
166
- ? {
167
- ExtraHosts : [
168
- `${ DOCKER_HOST_HOSTNAME } :${ proxyHost } ` ,
169
- // Seems that first host wins conflicts, so we go before existing values
170
- ...( currentConfig . HostConfig ?. ExtraHosts ?? [ ] )
171
- ]
172
- }
173
- : { }
174
- )
140
+ ]
175
141
} ;
176
142
177
143
// Extend that config, injecting our custom overrides:
@@ -256,11 +222,7 @@ export async function restartAndInjectContainer(
256
222
}
257
223
} ) ;
258
224
259
- const proxyHost = getDockerHostIp (
260
- process . platform ,
261
- { engineVersion : ( await docker . version ( ) ) . Version } ,
262
- containerDetails
263
- ) ;
225
+ const proxyHost = getDockerHostAddress ( process . platform , containerDetails ) ;
264
226
265
227
// First we clone the continer, injecting our custom settings:
266
228
const newContainer = await docker . createContainer (
0 commit comments