-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathU4R.ps1
More file actions
106 lines (95 loc) · 2.73 KB
/
U4R.ps1
File metadata and controls
106 lines (95 loc) · 2.73 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
using module ".\core\Autoloader.ps1"
Param([string]$M = "")
function Main() {
if ($M -eq "") {
ShowMenu
return
}
$mode = ResolveModeByChoice -choice $M
if ($null -eq $mode) {
Write-Host "Invalid mode ""$M"".`nAvailable:`n 1 - $MODE_UNBLOCK`n 2 - $MODE_BLOCK`n 3 - $MODE_BOTH`n 4 - $MODE_DOWNLOAD" -ForegroundColor Red
exit $CODE_ERROR_MODE_CHOISE
}
ProcessHosts -mode $mode
}
function ShowMenu() {
while ($true) {
Clear-Host
[LogoRenderer]::show()
[MenuRenderer]::show()
$choice = Read-Host "`n- Enter your choice"
if ($choice -eq $CHOISE_QUIT) {
ProcessExit
return
}
$mode = ResolveModeByChoice -choice $choice
if ($null -eq $mode) {
continue
}
ProcessHosts -mode $mode
}
}
function ResolveModeByChoice([String]$choice) {
switch ($choice) {
"1" { return $MODE_UNBLOCK }
"2" { return $MODE_BLOCK }
"3" { return $MODE_BOTH }
"4" { return $MODE_DOWNLOAD }
Default { return $null }
}
}
function ProcessHosts([String]$mode) {
Clear-Host
[LogoRenderer]::show()
DownloadHosts
if ($mode -ne $MODE_DOWNLOAD) {
ConvertHosts -mode $mode
}
UnloadModules
Done
exit $CODE_SUCCESS
}
function ProcessExit() {
UnloadModules
exit $CODE_SUCCESS
}
function DownloadHosts() {
Write-Host
Write-Host "- Downloading hosts file..."
try {
[Downloader]::download($HOSTS_SRC, $HOSTS_DST)
} catch {
Write-Host " Hosts file download failed!" -ForegroundColor Red
Write-Host " $( $_.Exception.Message )" -ForegroundColor Red
exit $CODE_ERROR_DOWNLOAD
}
Write-Host " Hosts file downloaded to `"$( $HOSTS_DST )`"" -ForegroundColor Green
}
function ConvertHosts([String]$mode) {
Write-Host
Write-Host "- Converting hosts file..."
try {
[HostsConverter]::new($HOSTS_DST, $STATIC_DNS_DST, $mode).convert()
} catch {
Write-Host " Hosts file conversion failed!" -ForegroundColor Red
Write-Host " $( $_.Exception.Message )" -ForegroundColor Red
exit $CODE_ERROR_CONVERSION
}
Write-Host " Hosts file converted to `"$( $STATIC_DNS_DST )`"" -ForegroundColor Green
}
function UnloadModules() {
Write-Host
try {
Import-Module "$PSScriptRoot\core\Unloader.ps1" -Force -ErrorAction Stop -Scope Global
} catch {
Write-Host " Unload modules failed!" -ForegroundColor Red
Write-Host " $( $_.Exception.Message )" -ForegroundColor Red
exit $CODE_ERROR_UNLOAD
}
}
function Done() {
Write-Host
Write-Host " Done! Press Any key to exit..." -ForegroundColor Cyan
Read-Host
}
Main