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: 6 additions & 2 deletions src/src/getAdminCredentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import retry from "async-retry";
import { getRpcCall } from "./api/getRpcCall";
import { API_PORT, NO_HOSTNAME_RETURNED_ERROR } from "./params";
import { renderQrCode } from "./utils/renderQrCode";
import { withHostname } from "./utils/withHostname";
import { VpnStatus } from "./types";

/* eslint-disable no-console */
Expand Down Expand Up @@ -47,6 +48,8 @@ class NotReadyError extends Error {

(async function(): Promise<void> {
try {
const localhost = process.argv.includes("--localhost");

console.log(
`Fetching DAppNode VPN credentials. It may take some time; use CTRL + C to stop`
);
Expand Down Expand Up @@ -81,14 +84,15 @@ class NotReadyError extends Error {
);

const { url } = await api.getMasterAdminCred();
const outputUrl = localhost ? withHostname(url, "localhost") : url;

// If rendering the QR fails, show the error and continue, the raw URL is consumable
console.log(`

${await renderQrCode(url).catch(e => e.stack)}
${await renderQrCode(outputUrl).catch(e => e.stack)}

To connect to your DAppNode scan the QR above or copy/paste link below into your browser:
${url}`);
${outputUrl}`);
} catch (e) {
// Exit process cleanly to prevent showing 'Unhandled rejection'
console.error(e);
Expand Down
9 changes: 9 additions & 0 deletions src/src/utils/withHostname.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function withHostname(rawUrl: string, hostname: string): string {
try {
const parsed = new URL(rawUrl);
parsed.hostname = hostname;
return parsed.toString();
} catch (e) {
return rawUrl;
}
}
17 changes: 14 additions & 3 deletions src/src/vpncli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import chalk from "chalk";
import prettyjson from "prettyjson";
import { getRpcCall } from "./api/getRpcCall";
import { API_PORT } from "./params";
import { withHostname } from "./utils/withHostname";

/* eslint-disable no-console */

Expand All @@ -25,6 +26,13 @@ const idArg: CommandBuilder<{}, { id: string }> = yargs =>
demandOption: true
});

const getArg: CommandBuilder<{}, { id: string; localhost: boolean }> = yargs =>
idArg(yargs).option("localhost", {
describe: "Print URL using localhost instead of the configured hostname",
type: "boolean",
default: false
});

yargs
.usage(`Usage: vpncli <command> [options]`)
.alias("h", "help")
Expand Down Expand Up @@ -57,10 +65,13 @@ yargs
.command({
command: "get <id>",
describe: "Generate device URL to download config file.",
builder: idArg,
handler: async ({ id }) => {
builder: getArg,
handler: async ({ id, localhost }) => {
const { url } = await api.getDeviceCredentials({ id });
console.log(chalk.green(`Credentials generated for ${id}:\n${url}`));
const outputUrl = localhost ? withHostname(url, "localhost") : url;
console.log(
chalk.green(`Credentials generated for ${id}:\n${outputUrl}`)
);
}
})
.command({
Expand Down
22 changes: 22 additions & 0 deletions src/test/utils/withHostname.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import "mocha";
import { expect } from "chai";

import { withHostname } from "../../src/utils/withHostname";

describe("utils > withHostname", () => {
it("Should replace hostname preserving port, path, query, and hash", () => {
const inputUrl =
"http://9442df98a3a59a65.dyndns.dappnode.io:8092/?id=xkmTGRv3sdu9XUuz#0P%2Blna33F5loAIUx13fgm3F7%2FRLFFvOigZDt9h2kcp8%3D";

const outputUrl = withHostname(inputUrl, "localhost");

expect(outputUrl).to.equal(
"http://localhost:8092/?id=xkmTGRv3sdu9XUuz#0P%2Blna33F5loAIUx13fgm3F7%2FRLFFvOigZDt9h2kcp8%3D"
);
});

it("Should return the raw input if it is not a valid URL", () => {
const inputUrl = "not-a-url";
expect(withHostname(inputUrl, "localhost")).to.equal(inputUrl);
});
});