This repository has been archived by the owner on Jan 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_hosts_functions.cr
206 lines (193 loc) · 7.97 KB
/
update_hosts_functions.cr
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#require "http/client"
#require "uri"
#require "openssl"
require "io"
def get_os
if run_proc("uname", ["-a"]) == "error" # linux/mac/bsd fail
if run_proc("wmic", ["os", "get", "Caption", "/value"]) == "error" # windows fail
return "Unknown"
else # windows success
# Return windows version
return run_proc("wmic", ["os", "get", "Caption", "/value"]).strip.lchop("Caption=")
end
else # linux/mac/bsd success
# Linux, Mac, or BSDs
return run_proc("uname", ["-a"])
end
end
def run_proc(command, a)
stdout = IO::Memory.new
stderr = IO::Memory.new
begin
mini_proc = Process.run(command, args: a, output: stdout, error: stderr)
if mini_proc.success?
return stdout.to_s
else
return "error" #stderr.to_s
end
rescue ex
return "error"
end
end
def download_hosts(uri)
# no windows support right now
run_proc("curl", ["--output", "hosts", uri])
end
def copy_rw(source, dest)
File.open(source, "r") do |s|
File.open(dest, "w") do |d|
IO.copy s, d
end
end
end
# functions
def get_hosts_file(option, download_file, local_file)
case option
when "basic"
download_hosts "https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts"
when "everything"
download_hosts "https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/fakenews-gambling-porn-social/hosts"
when "download"
download_hosts download_file
when "local"
copy_rw(local_file, "hosts")
when "reset"
# NA
else
# NA
end
end
def write_hosts_file(operating_sys)
if operating_sys.includes?("Linux")
puts "INFO: Writing Linux hosts file"
copy_rw("hosts", "/etc/hosts")
elsif operating_sys.includes?("Windows")
puts "INFO: Writing Windows hosts file"
copy_rw("hosts", "C:\\Windows\\System32\\Drivers\\etc\\hosts")
elsif operating_sys.includes?("Darwin")
puts "INFO: Writing Mac hosts file"
copy_rw("hosts", "/etc/hosts")
elsif operating_sys.includes?("FreeBSD")
puts "INFO: Writing BSD hosts file"
copy_rw("hosts", "/etc/hosts")
else
puts "WARNING: Unrecognized OS detected, attempting to update /etc/hosts"
copy_rw("hosts", "/etc/hosts")
end
end
def write_reset_file(operating_sys)
default_lin_host = "# auto-generated host file by the update-hosts utility\n" +
"#\n" +
"# hosts This file describes a number of hostname-to-address\n" +
"# mappings for the TCP/IP subsystem. It is mostly\n" +
"# used at boot time, when no name servers are running.\n" +
"# On small systems, this file can be used instead of a\n" +
"# \"named\" name server.\n" +
"# Syntax:\n" +
"# \n" +
"# IP-Address Full-Qualified-Hostname Short-Hostname\n" +
"#\n" +
"\n" +
"127.0.0.1 localhost\n" +
"# fallback hostname used by NetworkManager\n" +
"127.0.0.1 localhost.localdomain\n" +
"\n" +
"# special IPv6 addresses\n" +
"::1 localhost ipv6-localhost ipv6-loopback\n" +
"\n" +
"fe00::0 ipv6-localnet\n" +
"\n" +
"ff00::0 ipv6-mcastprefix\n" +
"ff02::1 ipv6-allnodes\n" +
"ff02::2 ipv6-allrouters\n" +
"ff02::3 ipv6-allhosts\n"
default_win_host = "# auto-generated host file by the update-hosts utility\n" +
"#\n" +
"# DNS should handle localhost resolution but\n" +
"# you can remove the comment #s below to resolve on the local computer.\n" +
"# 127.0.0.1 localhost\n" +
"# ::1 localhost"
default_bsd_host = "# auto-generated host file by the update-hosts utility\n" +
"#\n" +
"# In the presence of the domain name service of NIS, this file may\n" +
"# not be consulted at all; see /etc/nsswitch.conf for the resolution order.\n" +
"#\n" +
"#\n" +
"::1 localhost localhost.my.domain\n" +
"127.0.0.1 localhost localhost.my.doman\n" +
"#\n"
default_mac_host = "# auto-generated host file by the update-hosts utility\n" +
"127.0.0.1 localhost\n" +
"255.255.255.255 broadcasthost\n" +
"::1 localhost\n" +
"fe80::1%lo0 localhost\n"
if operating_sys.includes?("Linux")
puts "INFO: Resetting Linux hosts file"
File.write("/etc/hosts", default_lin_host)
elsif operating_sys.includes?("Windows")
puts "INFO: Resetting Windows hosts file"
File.write("C:\\Windows\\System32\\Drivers\\etc\\hosts", default_win_host)
elsif operating_sys.includes?("Darwin")
puts "INFO: Resetting Mac hosts file"
File.write("/etc/hosts", default_mac_host)
elsif operating_sys.includes?("FreeBSD")
puts "INFO: Resetting BSD hosts file"
File.write("/etc/hosts", default_bsd_host)
else
puts "WARNING: Unrecognized OS detected, attempting to reset /etc/hosts with linux hosts file"
# operating_sys.chars.each do |c|
# puts "char: " + c + " unicode: " + c.unicode_escape
# end
File.write("/etc/hosts", default_lin_host)
end
end
# https://askubuntu.com/questions/1031439/am-i-running-networkmanager-or-networkd
def reset_network(operating_sys)
if operating_sys.includes?("Linux")
puts "INFO: Resetting Linux network"
if run_proc("systemctl", ["restart", "NetworkManager"]) == "error" # first try
if run_proc("systemctl", ["restart", "network"]) == "error" # second try
if run_proc("service", ["network-manager", "restart"]) == "error" # third
if run_proc("/etc/init.d/network", ["restart"]) == "error" # fourth
if run_proc("/etc/rc.d/rc.inet1", ["restart"]) == "error" # fifth
if run_proc("systemctl", ["restart", "wicd.service"]) == "error" # sixth
if run_proc("/etc/init.d/dns-clean", ["restart"]) == "error" # seventh
puts "WARNING: Failure to restart network, you may need to restart your network manually"
else
puts "INFO: Success restarting network with '/etc/init.d/dns-clean restart'" # 7
end
else
puts "INFO: Success restarting network with 'systemctl restart wicd.service'" # 6
end
else
puts "INFO: Success restarting network with '/etc/rc.d/rc.inet1 restart'" # 5
end
else
puts "INFO: Success restarting network with '/etc/init.d/network restart'" # 4
end
else
puts "INFO: Success restarting network with 'service network-manager restart'" # 3
end
else
puts "INFO: Success restarting network with 'systemctl restart network'" # 2
end
else
puts "INFO: Success restarting network with 'systemctl restart NetworkManager'" # 1
end
elsif operating_sys.includes?("Windows")
puts "INFO: Resetting Windows network"
run_proc("ipconfig ", ["/flushdns"])
elsif operating_sys.includes?("Darwin")
puts "INFO: Resetting network"
run_proc("dscacheutil", ["-flushcache"])
run_proc("killall", ["-HUP", "mDNSResponder"])
elsif operating_sys.includes?("FreeBSD")
puts "INFO: Resetting network"
run_proc("service", ["nscd", "restart"])
else
puts "WARNING: Unrecognized OS detected, you will need to reset your network manually if required"
# operating_sys.chars.each do |c|
# puts "char: " + c + " unicode: " + c.unicode_escape
# end
end
end