Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions api/src/createLocalConfigFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async function getLocalIpWithRetries(retries: number): Promise<string> {
async function getLocalIp(): Promise<string> {
const dappmanagerHostnames = params.DAPPMANAGER_HOSTNAMES;
const getLocalIpUrls = dappmanagerHostnames.map(
(hostname) => `http://${hostname}${params.GET_INTERNAL_IP_ENDPOINT}`
(hostname) => `http://${hostname}${params.GET_INTERNAL_IP_ENDPOINT}`,
);

let errorMessages: string[] = [];
Expand All @@ -57,7 +57,7 @@ async function getLocalIp(): Promise<string> {
return localIp;
} catch (e) {
errorMessages.push(
`Local IP could not be fetched from ${url}: ${e.message}`
`Local IP could not be fetched from ${url}: ${e.message}`,
);
}
}
Expand All @@ -66,12 +66,12 @@ async function getLocalIp(): Promise<string> {

export function setLocalEndpoint(
configFile: string,
localEndpoint: string
localEndpoint: string,
): string {
return configFile
.split("\n")
.map((row) =>
row.startsWith("Endpoint =") ? `Endpoint = ${localEndpoint}` : row
row.startsWith("Endpoint =") ? `Endpoint = ${localEndpoint}` : row,
)
.join("\n");
}
Expand Down
12 changes: 9 additions & 3 deletions api/src/executables/getWireguardCredentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
createLocalConfigFile,
getRemoteConfigFilePath,
} from "../createLocalConfigFile";
import { setLocalhostConfig } from "../setLocalhostConfig";
import fs from "fs";

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

const isQr = process.argv.includes("--qr");
const isLocal = process.argv.includes("--local");
const isLocalhost = process.argv.includes("--localhost");

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

const config = isLocal
const baseConfig = isLocal
? await createLocalConfigFile(params.MASTER_ADMIN)
: fs.readFileSync(
getRemoteConfigFilePath(params.MASTER_ADMIN, "conf"),
"utf-8"
"utf-8",
);

// Mostly required to connect from within the same machine,
// especially when DAppNode is installed on macOS.
const config = isLocalhost ? setLocalhostConfig(baseConfig) : baseConfig;

const str = isQr
? // If rendering the QR fails, show error and the raw remoteTextCreds
await renderQrCode(config).catch((e) => {
Expand Down
8 changes: 8 additions & 0 deletions api/src/setLocalhostConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function setLocalhostConfig(configFile: string): string {
return configFile
.split("\n")
.map((row) =>
row.match(/^Endpoint\s*=/) ? "Endpoint = localhost:51820" : row,
)
.join("\n");
}
4 changes: 3 additions & 1 deletion api/test/createLocalConfigFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ AllowedIPs = 0.0.0.0/0`;

const localEndpoint = `192.168.1.45:51820`;

expect(setLocalEndpoint(remoteConfig, localEndpoint)).to.deep.equal(expextedLocalConfig);
expect(setLocalEndpoint(remoteConfig, localEndpoint)).to.deep.equal(
expextedLocalConfig,
);
});
});
43 changes: 43 additions & 0 deletions api/test/setLocalhostConfig.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import "mocha";
import { expect } from "chai";
import { setLocalhostConfig } from "../src/setLocalhostConfig";

describe("setLocalhostConfig", function () {
it("sets localhost endpoint", function () {
const remoteConfig = `[Interface]
Address = AX032NVGI2RIB4
PrivateKey = HIgo9xNzJMWLKASShiTqIybxZ0U3wGLiUeJ1PKf8ykw=
DNS = 172.33.1.2

[Peer]
PublicKey = gI6EdUSYvn8ugXOt8QQD6Yc+JyiZxIhp3GInSWRfWGE=
Endpoint = ff0239facf453517.dyndns.dappnode.io:51820
AllowedIPs = 0.0.0.0/0`;

const expectedLocalhostConfig = `[Interface]
Address = AX032NVGI2RIB4
PrivateKey = HIgo9xNzJMWLKASShiTqIybxZ0U3wGLiUeJ1PKf8ykw=
DNS = 172.33.1.2

[Peer]
PublicKey = gI6EdUSYvn8ugXOt8QQD6Yc+JyiZxIhp3GInSWRfWGE=
Endpoint = localhost:51820
AllowedIPs = 0.0.0.0/0`;

expect(setLocalhostConfig(remoteConfig)).to.deep.equal(
expectedLocalhostConfig,
);
});

it("replaces endpoint when spacing around equal sign varies", function () {
const remoteConfig = `[Peer]
Endpoint = ff0239facf453517.dyndns.dappnode.io:51820`;

const expectedLocalhostConfig = `[Peer]
Endpoint = localhost:51820`;

expect(setLocalhostConfig(remoteConfig)).to.deep.equal(
expectedLocalhostConfig,
);
});
});
1 change: 0 additions & 1 deletion root/defaults/peer.conf
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[Interface]
Address = ${CLIENT_IP}
PrivateKey = $(cat /config/${PEER_ID}/privatekey-${PEER_ID})
ListenPort = 51820
DNS = ${PEERDNS}

[Peer]
Expand Down