-
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 environment variables for primary and secondary DNS servers
- Loading branch information
1 parent
e44ee76
commit 09fae2c
Showing
4 changed files
with
40 additions
and
58 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 |
---|---|---|
|
@@ -3,3 +3,5 @@ PUSHOVER_TOKEN= | |
SSH_USERNAME= | ||
SSH_PASSWORD= | ||
ROUTER_HOSTNAME= | ||
PRIMARY_DNS_SERVERS= | ||
SECONDARY_DNS_SERVERS= |
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,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]}`); | ||
} |
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,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; |
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