Skip to content

Commit 778a9a9

Browse files
committed
Improved dns IP resolution
1 parent 6563501 commit 778a9a9

2 files changed

Lines changed: 21 additions & 21 deletions

File tree

src/src/openvpn/openvpnConfig.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { shell, shellArgs } from "../utils/shell";
33
import { directoryIsEmptyOrEnoent } from "../utils/fs";
44
import { PKI_PATH, PROXY_ARP_PATH } from "../params";
55
import { logs } from "../logs";
6-
import { getDockerContainerIP } from "../utils/getDockerContainerIp";
6+
import { getContainerIP } from "../utils/getDockerContainerIp";
77

88
type OvpnGenConfigFlags = {
99
c: string; // Enable traffic among the clients connected to the VPN (Boolean, no value)
@@ -20,6 +20,7 @@ type OvpnGenConfigFlags = {
2020
* This function MUST be called before starting the openvpn binary
2121
*/
2222
export async function initalizeOpenVpnConfig(hostname: string): Promise<void> {
23+
const vpnContainerDomain = "vpn.dappnode";
2324
// Replicate environment used in entrypoint.sh
2425
const openVpnEnv = {
2526
OVPN_CN: hostname,
@@ -30,7 +31,7 @@ export async function initalizeOpenVpnConfig(hostname: string): Promise<void> {
3031
logs.info("Initializing OpenVPN configuration");
3132

3233
// Check current IP range
33-
const containerIp = getDockerContainerIP();
34+
const containerIp = await getContainerIP(vpnContainerDomain);
3435

3536
// If container IP is inside 172.33.0.0/16 --> generate credentials A
3637
if (containerIp && containerIp.startsWith("172.33.")) {
Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
import os from 'os';
1+
import { Resolver } from 'dns';
22
import { logs } from '../logs';
33

4-
export function getDockerContainerIP(): string | null {
5-
const networkInterfaces = os.networkInterfaces();
4+
export async function getContainerIP(containerName: string): Promise<string | null> {
65

7-
// Docker typically uses eth0 as the first network interface for bridge networks
8-
const eth0 = networkInterfaces['eth0'];
6+
const resolver = new Resolver();
97

10-
if (!eth0) {
11-
logs.error('Network interface eth0 not found.');
12-
return null;
13-
}
8+
// Use Docker's DNS server to resolve container name.
9+
resolver.setServers(['127.0.0.11']);
1410

15-
// Filter for IPv4 address
16-
const ipv4 = eth0.find(info => info.family === 'IPv4');
17-
18-
if (!ipv4) {
19-
console.error('IPv4 address for eth0 not found.');
20-
return null;
21-
}
22-
23-
return ipv4.address;
24-
}
11+
return new Promise((resolve) => {
12+
resolver.resolve4(containerName, (err, addresses) => {
13+
if (err) {
14+
logs.error(`Error resolving ${containerName} IP address: ${err}`);
15+
resolve(null);
16+
} else {
17+
// Resolve with the first address found (if any).
18+
logs.info(`Resolved ${containerName} IP addresses: ${addresses}`);
19+
resolve(addresses.length > 0 ? addresses[0] : null);
20+
}
21+
});
22+
});
23+
}

0 commit comments

Comments
 (0)