-
Notifications
You must be signed in to change notification settings - Fork 231
/
hosts.go
162 lines (147 loc) · 3.61 KB
/
hosts.go
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
package util
import (
"bufio"
"fmt"
"github.com/rs/zerolog/log"
"os"
"path/filepath"
)
const ktHostsEscapeBegin = "# Kt Hosts Begin"
const ktHostsEscapeEnd = "# Kt Hosts End"
// DropHosts ...
func DropHosts() {
lines, err := loadHostsFile()
if err != nil {
log.Error().Msgf("Failed to load hosts file: %s", err.Error())
return
}
linesAfterDrop, err := dropHosts(lines)
if err != nil {
log.Error().Msgf("Failed to parse hosts file: %s", err.Error())
return
}
if len(linesAfterDrop) < len(lines) {
err = updateHostsFile(linesAfterDrop)
if err != nil {
log.Error().Msgf("Failed to update hosts file, you may require %s permission: %s",
getAdminUserName(), err.Error())
return
}
log.Info().Msgf("Drop hosts successful")
}
}
// DumpHosts DumpToHosts
func DumpHosts(hostsMap map[string]string) {
lines, err := loadHostsFile()
if err != nil {
log.Error().Msgf("Failed to load hosts file: %s", err.Error())
return
}
linesBeforeDump, err := dropHosts(lines)
if err != nil {
log.Error().Msgf("Failed to parse hosts file: %s", err.Error())
return
}
err = updateHostsFile(mergeLines(linesBeforeDump, dumpHosts(hostsMap)))
if err != nil {
log.Warn().Msgf("Unable to update hosts file, you may need %s permission.", getAdminUserName())
log.Debug().Msg(err.Error())
} else {
log.Info().Msg("Dump hosts successful")
}
}
func dropHosts(rawLines []string) ([]string, error) {
escapeBegin := -1
escapeEnd := -1
for i, l := range rawLines {
if l == ktHostsEscapeBegin {
escapeBegin = i
} else if l == ktHostsEscapeEnd {
escapeEnd = i
}
}
if escapeEnd < escapeBegin {
return nil, fmt.Errorf("invalid hosts file: escapeBegin=%d, escapeEnd=%d", escapeBegin, escapeEnd)
}
if escapeBegin >= 0 && escapeEnd > 0 {
linesAfterDrop := make([]string, len(rawLines)-(escapeEnd-escapeBegin+1))
if escapeBegin > 0 {
copy(linesAfterDrop[0:escapeBegin], rawLines[0:escapeBegin])
}
if escapeEnd < len(rawLines)-1 {
copy(linesAfterDrop[escapeBegin:], rawLines[escapeEnd+1:])
}
return linesAfterDrop, nil
} else {
return rawLines, nil
}
}
func dumpHosts(hostsMap map[string]string) []string {
var lines []string
lines = append(lines, ktHostsEscapeBegin)
for host, ip := range hostsMap {
lines = append(lines, fmt.Sprintf("%s %s", ip, host))
}
lines = append(lines, ktHostsEscapeEnd)
return lines
}
func mergeLines(linesBefore []string, linesAfter []string) []string {
lines := make([]string, len(linesBefore)+len(linesAfter)+2)
posBegin := len(linesBefore)
if posBegin > 0 {
copy(lines[0:posBegin], linesBefore[:])
}
if len(linesAfter) > 0 {
copy(lines[posBegin+1:len(lines)-1], linesAfter[:])
}
return lines
}
func loadHostsFile() ([]string, error) {
var lines []string
file, err := os.Open(getHostsPath())
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err = scanner.Err(); err != nil {
return nil, err
}
return lines, nil
}
func updateHostsFile(lines []string) error {
file, err := os.Create(getHostsPath())
if err != nil {
return err
}
w := bufio.NewWriter(file)
continualEmptyLine := false
for _, l := range lines {
if continualEmptyLine && l == "" {
continue
}
continualEmptyLine = l == ""
fmt.Fprintf(w, "%s%s", l, eol)
}
err = w.Flush()
if err != nil {
return err
}
return nil
}
func getHostsPath() string {
if os.Getenv("HOSTS_PATH") == "" {
return os.ExpandEnv(filepath.FromSlash(hostsFilePath))
} else {
return os.Getenv("HOSTS_PATH")
}
}
func getAdminUserName() string {
if IsWindows() {
return "administrator"
}
return "root"
}