-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added DNS toggling functionality with environment validation
- Loading branch information
1 parent
a71e36f
commit ca44a76
Showing
6 changed files
with
105 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
{ | ||
"files": { | ||
"ignore": ["dist"] | ||
}, | ||
"linter": { | ||
"enabled": true, | ||
"rules": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
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); | ||
|
||
const originalDnsServers = parseDnsServers(await ssh.execSafe("show")); | ||
console.log(`Found existing DNS servers: ${originalDnsServers}`); | ||
|
||
console.log("Setting alternative DNS servers..."); | ||
await setDnsServers(ssh, ALT_DNS_SERVERS); | ||
|
||
timeoutId = setTimeout(genRestoreDnsServers(originalDnsServers), 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 setDnsServers(ssh: SNodeSSH, dns: [string, string]) { | ||
console.log(`Setting DNS servers to ${dns}`); | ||
// ssh.execCommand(`configure set service dns forwarding name-server ${dns[0]}`); | ||
// ssh.execCommand(`configure set service dns forwarding name-server ${dns[1]}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { z } from "zod"; | ||
|
||
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" }), | ||
}) | ||
.parse(process.env); | ||
export default env; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { z } from "zod"; | ||
|
||
const dnsRegex = /dns-server\s+([\d.]+)/g; | ||
const dnsSchema = z | ||
.string() | ||
.transform((text) => Array.from(text.matchAll(dnsRegex)).map((match) => match[1])) | ||
.pipe(z.tuple([z.string().ip({ version: "v4" }), z.string().ip({ version: "v4" })])); | ||
|
||
export function parseDnsServers(text: string): [string, string] { | ||
return dnsSchema.parse(text); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { NodeSSH } from "node-ssh"; | ||
import env from "./env"; | ||
|
||
export type SNodeSSH = NodeSSH & { | ||
execSafe: (command: string) => Promise<string>; | ||
}; | ||
|
||
export async function getSshConnection(): Promise<SNodeSSH> { | ||
const ssh = new NodeSSH() as SNodeSSH; | ||
await ssh.connect({ | ||
host: env.ROUTER_HOSTNAME, | ||
username: env.SSH_USERNAME, | ||
password: env.SSH_PASSWORD, | ||
tryKeyboard: false, | ||
}); | ||
ssh.execSafe = async (command: string) => { | ||
console.log(`>>> ${command}`); | ||
const result = await ssh.execCommand(command); | ||
if (result.stderr) console.warn(prefixCaret(result.stderr)); | ||
if (result.stderr) throw new Error(`Error executing command: ${result.stderr}`); | ||
if (result.stdout) console.log(prefixCaret(result.stdout)); | ||
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; | ||
} | ||
|
||
function prefixCaret(text: string): string { | ||
return text.replace(/^/gm, "> "); | ||
} |