Skip to content

Commit 9fd4a67

Browse files
committed
Dedupe Docker DNS hostnames
Not a big deal, but this does dedupe the http-toolkit-host.docker.internal address that's used in every single container, and in the process avoids some warnings if that's used directly somewhere (and probably in other cases too).
1 parent 929b430 commit 9fd4a67

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/dns-server.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,18 @@ class DnsServer extends dns2.UDPServer {
3737

3838
private hostMaps: {
3939
[sourceId: string]: {
40-
[host: string]: string[] | undefined
40+
[host: string]: Set<string> | undefined
4141
}
4242
} = {};
4343

44-
setSourceHosts(sourceId: string, hosts: { [hostname: string]: string[] }) {
44+
setSourceHosts(sourceId: string, hosts: { [hostname: string]: Set<string> }) {
4545
this.hostMaps[sourceId] = hosts;
4646
}
4747

48-
private getHostAddresses(hostname: string) {
48+
private getHostAddresses(hostname: string): Set<string> {
4949
return _.flatMap(this.hostMaps, (hostMap) => hostMap[hostname])
50-
.filter(h => !!h) as string[];
50+
.filter(h => !!h)
51+
.reduce<Set<string>>((result, set) => new Set([...result!, ...set!]), new Set());
5152
}
5253

5354
handleQuery(request: dns2.DnsRequest, sendResponse: (response: dns2.DnsResponse) => void) {
@@ -59,7 +60,7 @@ class DnsServer extends dns2.UDPServer {
5960

6061
const answers = this.getHostAddresses(question.name);
6162

62-
if (answers.length > 1) {
63+
if (answers.size > 1) {
6364
console.log(`Multiple hosts in internal DNS for hostname ${question.name}: ${answers}`);
6465
}
6566

src/interceptors/docker/docker-networking.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class DockerNetworkMonitor {
135135
private docker: Docker,
136136
private proxyPort: number,
137137
private dockerEventStream: NodeJS.ReadableStream,
138-
private updateCallback: (aliases: { [hostname: string]: string[] }) => void
138+
private updateCallback: (aliases: { [hostname: string]: Set<string> }) => void
139139
) {
140140
dockerEventStream.on('data', this.onEvent);
141141

@@ -174,10 +174,10 @@ class DockerNetworkMonitor {
174174
// Merge the sets for the same hostname in multiple networks together, to create
175175
// a single mapping from hostnames to all possible addresses. This isn't great,
176176
// but hopefully in practice there will be few or zero conflicts regardless.
177-
return _.assignWith<{ [hostname: string]: string [] }>({},
177+
return _.assignWith<{ [hostname: string]: Set<string> }>({},
178178
...Object.values(this.aliasMappings),
179-
(existingIps: string[] = [], nextIps: Set<string> = new Set()) => {
180-
return [...existingIps, ...nextIps.values()];
179+
(existingIps: Set<string> = new Set(), nextIps: Set<string> = new Set()) => {
180+
return new Set([...existingIps, ...nextIps]);
181181
}
182182
);
183183
}

0 commit comments

Comments
 (0)