Skip to content

Commit 3881f9f

Browse files
authored
Merge pull request #38 from dappnode/pablo/add-localhost-flag
Add localhost configuration support and related tests
2 parents fa249cb + 6bab1e4 commit 3881f9f

6 files changed

Lines changed: 67 additions & 9 deletions

File tree

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

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

root/defaults/peer.conf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[Interface]
22
Address = ${CLIENT_IP}
33
PrivateKey = $(cat /config/${PEER_ID}/privatekey-${PEER_ID})
4-
ListenPort = 51820
54
DNS = ${PEERDNS}
65

76
[Peer]

0 commit comments

Comments
 (0)