diff --git a/.env.example b/.env.example index f1a4a31..d55c4bb 100644 --- a/.env.example +++ b/.env.example @@ -3,3 +3,5 @@ PUSHOVER_TOKEN= SSH_USERNAME= SSH_PASSWORD= ROUTER_HOSTNAME= +PRIMARY_DNS_SERVERS= +SECONDARY_DNS_SERVERS= diff --git a/src/dns.ts b/src/dns.ts index e62a8e3..5f43d37 100644 --- a/src/dns.ts +++ b/src/dns.ts @@ -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]}`); } diff --git a/src/env.ts b/src/env.ts index d277006..6771ca5 100644 --- a/src/env.ts +++ b/src/env.ts @@ -1,5 +1,10 @@ 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(), @@ -7,6 +12,8 @@ const env = z 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; diff --git a/src/ssh.ts b/src/ssh.ts index 4fa0791..3287271 100644 --- a/src/ssh.ts +++ b/src/ssh.ts @@ -31,11 +31,6 @@ export async function getSshConnection(): Promise { 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; } @@ -46,41 +41,27 @@ async function withShell( let stdout = ""; let stderr = ""; - await ssh.withShell( - async (channel) => { - return new Promise((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((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 }; }