-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlushDNS.php
More file actions
114 lines (100 loc) · 3.44 KB
/
Copy pathFlushDNS.php
File metadata and controls
114 lines (100 loc) · 3.44 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
<?php
namespace KnotsPHP\FlushDNS;
use KnotsPHP\FlushDNS\Exceptions\OperatingSystemNotSupported;
use KnotsPHP\System\Contracts\OperatingSystemContract;
use KnotsPHP\System\Enums\OperatingSystem;
use KnotsPHP\System\System;
class FlushDNS
{
/**
* Get the command to flush the DNS.
*/
public static function getCommand(): string
{
$os = OperatingSystem::current();
$system = System::os();
return match ($os) {
OperatingSystem::Windows => 'ipconfig /flushdns',
OperatingSystem::MacOS => self::getMacOSCommand($system),
OperatingSystem::Linux => self::getLinuxCommand($system),
default => throw new OperatingSystemNotSupported,
};
}
/**
* Get the cURL options to use.
*
* @return non-empty-array<int, mixed>
*/
public static function getCurlOpts(): array
{
return [
CURLOPT_DNS_CACHE_TIMEOUT => 0,
];
}
/**
* Check if the command needs elevation.
*/
public static function needsElevation(): bool
{
return match (OperatingSystem::current()) {
OperatingSystem::Windows => false,
OperatingSystem::MacOS => str_starts_with(self::getMacOSCommand(System::os()), 'sudo'),
OperatingSystem::Linux => str_starts_with(self::getLinuxCommand(System::os()), 'sudo'),
default => throw new OperatingSystemNotSupported,
};
}
/**
* Run the command to flush the DNS.
*/
public static function run(): bool
{
$command = self::getCommand();
exec($command, $output, $result_code);
return $result_code === 0;
}
/**
* Get the command for macOS.
*/
private static function getMacOSCommand(OperatingSystemContract $system): string
{
$version = array_map('intval', explode('.', $system->version()));
$major = $version[0];
$minor = $version[1];
if ($major === 10) {
if ($minor === 10) {
// 10.10 Yosemite
return 'sudo discoveryutil udnsflushcaches';
} elseif ($minor >= 7 && $minor <= 14) {
// From 10.7 Lion to 10.14 Mojave
return 'sudo killall -HUP mDNSResponder';
} elseif ($minor === 6) {
// 10.6 Snow Leopard
return 'sudo dscacheutil -flushcache';
} elseif ($minor === 5) {
// 10.5 Leopard
return 'sudo lookupd -flushcache';
} elseif ($minor === 4) {
// 10.4 Tiger
return 'lookupd -flushcache';
} elseif ($minor >= 15) {
// 10.15 Catalina and later
return 'sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder';
}
}
// Recent versions of macOS
return 'sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder';
}
/**
* Get the command for Linux.
*/
private static function getLinuxCommand(OperatingSystemContract $system): string
{
// Contributions are welcome to add other distributions
// Some examples of commands, we just need some way to check which service is running
// Some other commands to implement:
// sudo service dnsmasq restart
// sudo service nscd restart
// works on all modern and major distributions
return 'sudo systemd-resolve --flush-caches';
}
}