Skip to content

Commit

Permalink
Refactored DNS handling to simplify command execution and remove unus…
Browse files Browse the repository at this point in the history
…ed code
  • Loading branch information
micthiesen committed Oct 18, 2024
1 parent 09fae2c commit 96b4f3d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
13 changes: 4 additions & 9 deletions src/dns.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import env from "./env";
import { parseDnsServers } from "./parsing";
import { getSshConnection, type SNodeSSH } from "./ssh";

let timeoutId: NodeJS.Timeout | null = null;
Expand All @@ -8,11 +7,6 @@ const RESTORE_DELAY = 1000 * 5;
export async function toggleDns(ssh: SNodeSSH) {
if (timeoutId) clearTimeout(timeoutId);

const originalDnsServers = parseDnsServers(
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, env.SECONDARY_DNS_SERVERS);

Expand All @@ -33,7 +27,8 @@ async function restoreDnsServers() {

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]}`);
await ssh.configure([
"delete service dns forwarding name-server",
...dns.map((d) => `set service dns forwarding name-server ${d}`),
]);
}
14 changes: 7 additions & 7 deletions src/ssh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import env from "./env";

export type SNodeSSH = NodeSSH & {
execSafe: (command: string) => Promise<string>;
shellSafe: (command: string) => Promise<string>;
configure: (commands: string[]) => Promise<string>;
};

export async function getSshConnection(): Promise<SNodeSSH> {
Expand All @@ -22,9 +22,9 @@ export async function getSshConnection(): Promise<SNodeSSH> {
if (result.stdout) console.log(prefixBar(result.stdout));
return result.stdout;
};
ssh.shellSafe = async (command: string) => {
console.log(`>>> ${command}`);
const result = await withShell(ssh, command);
ssh.configure = async (commands: string[]) => {
for (const command of commands) console.log(`>>> ${command}`);
const result = await configure(ssh, commands);
if (result.stderr) console.warn(prefixBar(result.stderr));
if (result.stderr) throw new Error(`Error executing command: ${result.stderr}`);
if (result.stdout) console.log(prefixBar(result.stdout));
Expand All @@ -34,9 +34,9 @@ export async function getSshConnection(): Promise<SNodeSSH> {
return ssh;
}

async function withShell(
async function configure(
ssh: SNodeSSH,
command: string,
commands: string[],
): Promise<{ stdout: string; stderr: string }> {
let stdout = "";
let stderr = "";
Expand All @@ -59,7 +59,7 @@ async function withShell(
reject(err);
});

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

Expand Down

0 comments on commit 96b4f3d

Please sign in to comment.