Skip to content

Commit e98529a

Browse files
Add localhost configuration support and related tests
- Implement setLocalhostConfig function to modify config for localhost usage. - Update getWireguardCredentials to handle --localhost flag. - Add tests for setLocalhostConfig to ensure correct functionality.
1 parent 4a07401 commit e98529a

File tree

5 files changed

+69
-8
lines changed

5 files changed

+69
-8
lines changed

api/src/createLocalConfigFile.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ async function getLocalIpWithRetries(retries: number): Promise<string> {
4343
async function getLocalIp(): Promise<string> {
4444
const dappmanagerHostnames = params.DAPPMANAGER_HOSTNAMES;
4545
const getLocalIpUrls = dappmanagerHostnames.map(
46-
(hostname) => `http://${hostname}${params.GET_INTERNAL_IP_ENDPOINT}`
46+
(hostname) => `http://${hostname}${params.GET_INTERNAL_IP_ENDPOINT}`,
4747
);
4848

4949
let errorMessages: string[] = [];
@@ -57,7 +57,7 @@ async function getLocalIp(): Promise<string> {
5757
return localIp;
5858
} catch (e) {
5959
errorMessages.push(
60-
`Local IP could not be fetched from ${url}: ${e.message}`
60+
`Local IP could not be fetched from ${url}: ${e.message}`,
6161
);
6262
}
6363
}
@@ -66,12 +66,12 @@ async function getLocalIp(): Promise<string> {
6666

6767
export function setLocalEndpoint(
6868
configFile: string,
69-
localEndpoint: string
69+
localEndpoint: string,
7070
): string {
7171
return configFile
7272
.split("\n")
7373
.map((row) =>
74-
row.startsWith("Endpoint =") ? `Endpoint = ${localEndpoint}` : row
74+
row.startsWith("Endpoint =") ? `Endpoint = ${localEndpoint}` : row,
7575
)
7676
.join("\n");
7777
}

api/src/executables/getWireguardCredentials.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
createLocalConfigFile,
1212
getRemoteConfigFilePath,
1313
} from "../createLocalConfigFile";
14+
import { setLocalhostConfig } from "../setLocalhostConfig";
1415
import fs from "fs";
1516

1617
(async function (): Promise<void> {
@@ -23,20 +24,25 @@ import fs from "fs";
2324

2425
const isQr = process.argv.includes("--qr");
2526
const isLocal = process.argv.includes("--local");
27+
const isLocalhost = process.argv.includes("--localhost");
2628

2729
const configName = isLocal ? "local" : "remote";
2830
const configFormat = isQr ? "qr" : "text";
2931
console.log(
30-
`Preparing ${configName} ${configFormat} Wireguard credentials; use CTRL + C to stop`
32+
`Preparing ${configName} ${configFormat} Wireguard credentials; use CTRL + C to stop`,
3133
);
3234

33-
const config = isLocal
35+
const baseConfig = isLocal
3436
? await createLocalConfigFile(params.MASTER_ADMIN)
3537
: fs.readFileSync(
3638
getRemoteConfigFilePath(params.MASTER_ADMIN, "conf"),
37-
"utf-8"
39+
"utf-8",
3840
);
3941

42+
// Mostly required to connect from within the same machine,
43+
// especially when DAppNode is installed on macOS.
44+
const config = isLocalhost ? setLocalhostConfig(baseConfig) : baseConfig;
45+
4046
const str = isQr
4147
? // If rendering the QR fails, show error and the raw remoteTextCreds
4248
await renderQrCode(config).catch((e) => {

api/src/setLocalhostConfig.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function setLocalhostConfig(configFile: string): string {
2+
return configFile
3+
.split("\n")
4+
.filter((row) => !row.includes("ListenPort"))
5+
.map((row) =>
6+
row.match(/^Endpoint\s*=/) ? "Endpoint = localhost:51820" : row,
7+
)
8+
.join("\n");
9+
}

api/test/createLocalConfigFile.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ AllowedIPs = 0.0.0.0/0`;
2828

2929
const localEndpoint = `192.168.1.45:51820`;
3030

31-
expect(setLocalEndpoint(remoteConfig, localEndpoint)).to.deep.equal(expextedLocalConfig);
31+
expect(setLocalEndpoint(remoteConfig, localEndpoint)).to.deep.equal(
32+
expextedLocalConfig,
33+
);
3234
});
3335
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import "mocha";
2+
import { expect } from "chai";
3+
import { setLocalhostConfig } from "../src/setLocalhostConfig";
4+
5+
describe("setLocalhostConfig", function () {
6+
it("removes ListenPort and sets localhost endpoint", function () {
7+
const remoteConfig = `[Interface]
8+
Address = AX032NVGI2RIB4
9+
PrivateKey = HIgo9xNzJMWLKASShiTqIybxZ0U3wGLiUeJ1PKf8ykw=
10+
ListenPort = 51820
11+
DNS = 172.33.1.2
12+
13+
[Peer]
14+
PublicKey = gI6EdUSYvn8ugXOt8QQD6Yc+JyiZxIhp3GInSWRfWGE=
15+
Endpoint = ff0239facf453517.dyndns.dappnode.io:51820
16+
AllowedIPs = 0.0.0.0/0`;
17+
18+
const expectedLocalhostConfig = `[Interface]
19+
Address = AX032NVGI2RIB4
20+
PrivateKey = HIgo9xNzJMWLKASShiTqIybxZ0U3wGLiUeJ1PKf8ykw=
21+
DNS = 172.33.1.2
22+
23+
[Peer]
24+
PublicKey = gI6EdUSYvn8ugXOt8QQD6Yc+JyiZxIhp3GInSWRfWGE=
25+
Endpoint = localhost:51820
26+
AllowedIPs = 0.0.0.0/0`;
27+
28+
expect(setLocalhostConfig(remoteConfig)).to.deep.equal(
29+
expectedLocalhostConfig,
30+
);
31+
});
32+
33+
it("replaces endpoint when spacing around equal sign varies", function () {
34+
const remoteConfig = `[Peer]
35+
Endpoint = ff0239facf453517.dyndns.dappnode.io:51820`;
36+
37+
const expectedLocalhostConfig = `[Peer]
38+
Endpoint = localhost:51820`;
39+
40+
expect(setLocalhostConfig(remoteConfig)).to.deep.equal(
41+
expectedLocalhostConfig,
42+
);
43+
});
44+
});

0 commit comments

Comments
 (0)