Repository navigation
Expand file tree
/
Copy pathflushDnsCache.ts
More file actions
115 lines (99 loc) · 3.42 KB
/
Copy pathflushDnsCache.ts
File metadata and controls
115 lines (99 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* mcp/skills/flushDnsCache.ts — flush_dns_cache skill
*
* Clears the operating system DNS resolver cache. Useful after VPN
* connect/disconnect, network configuration changes, or when DNS resolution
* is returning stale or incorrect results.
*
* Platform strategy
* -----------------
* darwin `sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder`
* win32 PowerShell Clear-DnsClientCache
*
* Smoke test
* npx tsx -r dotenv/config mcp/skills/flushDnsCache.ts
*/
import * as os from "os";
import { exec } from "child_process";
import { promisify } from "util";
import { z } from "zod";
const execAsync = promisify(exec);
// -- Meta ---------------------------------------------------------------------
export const meta = {
name: "flush_dns_cache",
description:
"Clears the operating system DNS resolver cache. " +
"Use after VPN connect/disconnect, network configuration changes, or when " +
"DNS resolution is returning stale/incorrect results.",
riskLevel: "medium",
destructive: false,
requiresConsent: false,
supportsDryRun: false,
affectedScope: ["network", "system"],
auditRequired: true,
escalationHint: {
darwin: "sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder",
win32: "Clear-DnsClientCache # run from elevated PowerShell",
},
outputKeys: ["success","platform","command","error"],
schema: {} as Record<string, z.ZodTypeAny>,
} as const;
// -- Types --------------------------------------------------------------------
interface FlushResult {
success: boolean;
platform: string;
command: string;
error?: string;
}
// -- PowerShell helper --------------------------------------------------------
async function runPS(script: string): Promise<string> {
const encoded = Buffer.from(script, "utf16le").toString("base64");
const { stdout } = await execAsync(
`powershell.exe -NoProfile -NonInteractive -EncodedCommand ${encoded}`,
{ maxBuffer: 5 * 1024 * 1024 },
);
return stdout.trim();
}
// -- darwin implementation ----------------------------------------------------
async function flushDnsDarwin(): Promise<FlushResult> {
const command = "sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder";
try {
await execAsync(command, { shell: "/bin/bash" });
return { success: true, platform: "darwin", command };
} catch (err) {
return {
success: false,
platform: "darwin",
command,
error: (err as Error).message,
};
}
}
// -- win32 implementation -----------------------------------------------------
async function flushDnsWin32(): Promise<FlushResult> {
const command = "Clear-DnsClientCache";
try {
await runPS(command);
return { success: true, platform: "win32", command };
} catch (err) {
return {
success: false,
platform: "win32",
command,
error: (err as Error).message,
};
}
}
// -- Exported run function ----------------------------------------------------
export async function run(_args: Record<string, never> = {} as Record<string, never>) {
const platform = os.platform();
return platform === "win32"
? flushDnsWin32()
: flushDnsDarwin();
}
// -- CLI smoke test -----------------------------------------------------------
if (false) {
run({})
.then((r) => console.log(JSON.stringify(r, null, 2)))
.catch((err: Error) => { console.error(err.message); process.exit(1); });
}