Skip to content

Commit

Permalink
Added environment variables for primary and secondary DNS servers
Browse files Browse the repository at this point in the history
  • Loading branch information
micthiesen committed Oct 18, 2024
1 parent e44ee76 commit 09fae2c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 58 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ PUSHOVER_TOKEN=
SSH_USERNAME=
SSH_PASSWORD=
ROUTER_HOSTNAME=
PRIMARY_DNS_SERVERS=
SECONDARY_DNS_SERVERS=
36 changes: 14 additions & 22 deletions src/dns.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,39 @@
import env from "./env";
import { parseDnsServers } from "./parsing";
import { getSshConnection, type SNodeSSH } from "./ssh";

let timeoutId: NodeJS.Timeout | null = null;
const RESTORE_DELAY = 1000 * 5;

const ALT_DNS_SERVERS: [string, string] = ["1.1.1.1", "1.0.0.1"];

export async function toggleDns(ssh: SNodeSSH) {
if (timeoutId) clearTimeout(timeoutId);

await ssh.shellSafe("echo $");
await ssh.shellSafe("echo $SHELL");
await ssh.shellSafe("echo $TERM");
await ssh.shellSafe("echo $-");
await ssh.shellSafe("shopt login_shell");

const originalDnsServers = parseDnsServers(
await ssh.shellSafe("show dns forwarding nameservers"),
await ssh.execSafe("cat /config/config.boot | grep 'dns-server'"),
);
console.log(`Found existing DNS servers: ${originalDnsServers}`);

console.log("Setting alternative DNS servers...");
await setDnsServers(ssh, ALT_DNS_SERVERS);
await setDnsServers(ssh, env.SECONDARY_DNS_SERVERS);

timeoutId = setTimeout(genRestoreDnsServers(originalDnsServers), RESTORE_DELAY);
timeoutId = setTimeout(restoreDnsServers, RESTORE_DELAY);
console.log(`Scheduled restore in ${RESTORE_DELAY}ms`);
}

function genRestoreDnsServers(originalDnsServers: [string, string]) {
return async () => {
console.log("Restoring original DNS servers...");
let ssh: SNodeSSH | undefined;
try {
ssh = await getSshConnection();
await setDnsServers(ssh, originalDnsServers);
} finally {
if (ssh) ssh.dispose();
}
};
async function restoreDnsServers() {
console.log("Restoring original DNS servers...");
let ssh: SNodeSSH | undefined;
try {
ssh = await getSshConnection();
await setDnsServers(ssh, env.PRIMARY_DNS_SERVERS);
} finally {
if (ssh) ssh.dispose();
}
}

async function setDnsServers(ssh: SNodeSSH, dns: [string, string]) {
console.log(`Setting DNS servers to ${dns}`);
await ssh.shellSafe("show service dns forwarding");
// ssh.execCommand(`configure set service dns forwarding name-server ${dns[0]}`);
// ssh.execCommand(`configure set service dns forwarding name-server ${dns[1]}`);
}
7 changes: 7 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { z } from "zod";

const dnsSchema = z
.string()
.transform((v) => v.split(","))
.pipe(z.tuple([z.string().ip({ version: "v4" }), z.string().ip({ version: "v4" })]));

const env = z
.object({
PUSHOVER_USER: z.string(),
PUSHOVER_TOKEN: z.string(),
SSH_USERNAME: z.string(),
SSH_PASSWORD: z.string(),
ROUTER_HOSTNAME: z.string().ip({ version: "v4" }),
PRIMARY_DNS_SERVERS: dnsSchema,
SECONDARY_DNS_SERVERS: dnsSchema,
})
.parse(process.env);
export default env;
53 changes: 17 additions & 36 deletions src/ssh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ export async function getSshConnection(): Promise<SNodeSSH> {
return result.stdout;
};

await ssh.execSafe("export PS1=true​");
await ssh.execSafe("export _OFR_CONFIGURE=ok​");
await ssh.execSafe("source ~/.bashrc");
await ssh.execSafe("alias");

return ssh;
}

Expand All @@ -46,41 +41,27 @@ async function withShell(
let stdout = "";
let stderr = "";

await ssh.withShell(
async (channel) => {
return new Promise<void>((resolve, reject) => {
channel.on("data", (data: Buffer) => {
stdout += data.toString();
});

channel.stderr.on("data", (data: Buffer) => {
stderr += data.toString();
});
await ssh.withShell(async (channel) => {
return new Promise<void>((resolve, reject) => {
channel.on("data", (data: Buffer) => {
stdout += data.toString();
});

channel.on("close", () => {
resolve();
});
channel.stderr.on("data", (data: Buffer) => {
stderr += data.toString();
});

channel.on("error", (err: any) => {
reject(err);
});
channel.on("close", () => {
resolve();
});

channel.write(`${command}\nexit\n`);
channel.on("error", (err: any) => {
reject(err);
});
},
{
modes: {
ECHO: 1, // Enable echoing
ICANON: 1, // Enable canonical input
ISIG: 1, // Enable signals like INTR, QUIT, SUSP
IEXTEN: 1, // Enable extensions
OPOST: 1, // Enable output processing
ONLCR: 1, // Translate newline to CR-NL
TTY_OP_ISPEED: 9600, // Input baud rate
TTY_OP_OSPEED: 9600, // Output baud rate
},
},
);

channel.write(`configure\n${command}\ncommit\nsave\nexit\nexit\n`);
});
});

return { stdout: extractCommandOutput(stdout), stderr };
}
Expand Down

0 comments on commit 09fae2c

Please sign in to comment.