-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathtxeh.go
More file actions
693 lines (570 loc) · 18.9 KB
/
txeh.go
File metadata and controls
693 lines (570 loc) · 18.9 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
// Package txeh provides /etc/hosts file management capabilities.
// It offers a simple interface for adding, removing, and querying hostname-to-IP mappings
// with thread-safe operations and cross-platform support (Linux, macOS, Windows).
package txeh
import (
"errors"
"fmt"
"net"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
)
// Line type constants for HostFileLine.
const (
UNKNOWN = 0 // Unknown line type.
EMPTY = 10 // Empty line.
COMMENT = 20 // Comment line starting with #.
ADDRESS = 30 // Address line with IP and hostnames.
)
// DefaultMaxHostsPerLineWindows is the default maximum number of hostnames per line on Windows.
// Windows has a limitation where lines with more than ~9 hostnames may not resolve correctly.
const DefaultMaxHostsPerLineWindows = 9
// osWindows is the runtime.GOOS value for Windows.
const osWindows = "windows"
// IPFamily represents the IP address family (IPv4 or IPv6).
type IPFamily int64
// IP address family constants.
const (
IPFamilyV4 IPFamily = iota // IPv4 address family.
IPFamilyV6 // IPv6 address family.
)
// HostsConfig contains configuration for reading and writing hosts files.
type HostsConfig struct {
ReadFilePath string
WriteFilePath string
// RawText for input. If RawText is set ReadFilePath, WriteFilePath are ignored. Use RenderHostsFile rather
// than save to get the results.
RawText *string
// MaxHostsPerLine limits the number of hostnames per line when adding hosts.
// This is useful for Windows which has a limitation of ~9 hostnames per line.
// Values:
// 0 = auto-detect (Windows: 9, others: unlimited)
// -1 = force unlimited (no limit)
// >0 = explicit limit
MaxHostsPerLine int
// AutoFlush triggers a DNS cache flush after every successful Save/SaveAs.
// Flush failures are returned as *FlushError, distinguishable via errors.As.
AutoFlush bool
}
// Hosts represents a parsed hosts file with thread-safe operations.
type Hosts struct {
mu sync.Mutex
*HostsConfig
hostFileLines HostFileLines
}
// AddressLocations maps an address to its location in the HFL.
type AddressLocations map[string]int
// HostLocations maps a hostname to an original line number.
type HostLocations map[string]int
// HostFileLines is a slice of HostFileLine entries.
type HostFileLines []HostFileLine
// HostFileLine represents a single line in a hosts file.
type HostFileLine struct {
OriginalLineNum int
LineType int
Address string
Parts []string
Hostnames []string
Raw string
Trimmed string
Comment string
}
// NewHostsDefault returns a hosts object with default configuration.
func NewHostsDefault() (*Hosts, error) {
return NewHosts(&HostsConfig{})
}
// NewHosts returns a new hosts object.
func NewHosts(hc *HostsConfig) (*Hosts, error) {
h := &Hosts{HostsConfig: hc}
h.mu.Lock()
defer h.mu.Unlock()
defaultHostsFile := "/etc/hosts"
if runtime.GOOS == osWindows {
defaultHostsFile = winDefaultHostsFile()
}
if h.ReadFilePath == "" && h.RawText == nil {
h.ReadFilePath = defaultHostsFile
}
if h.WriteFilePath == "" && h.RawText == nil {
h.WriteFilePath = h.ReadFilePath
}
if h.RawText != nil {
hfl, err := ParseHostsFromString(*h.RawText)
if err != nil {
return nil, err
}
h.hostFileLines = hfl
return h, nil
}
hfl, err := ParseHosts(h.ReadFilePath)
if err != nil {
return nil, err
}
h.hostFileLines = hfl
return h, nil
}
// Save renders and writes the hosts file to the configured write path.
func (h *Hosts) Save() error {
return h.SaveAs(h.WriteFilePath)
}
// SaveAs saves rendered hosts file to the filename specified.
func (h *Hosts) SaveAs(fileName string) error {
if h.RawText != nil {
return errors.New("cannot call Save or SaveAs with RawText. Use RenderHostsFile to return a string")
}
hfData := []byte(h.RenderHostsFile())
h.mu.Lock()
defer h.mu.Unlock()
err := os.WriteFile(filepath.Clean(fileName), hfData, 0o644) // #nosec G306 -- hosts file must be world-readable (0644) for DNS resolution
if err != nil {
return fmt.Errorf("write hosts file %s: %w", fileName, err)
}
if h.AutoFlush {
if flushErr := FlushDNSCache(); flushErr != nil {
return flushErr
}
}
return nil
}
// Reload re-reads the hosts file from disk and replaces the in-memory state.
// This is part of the public API for consumers who manage long-lived Hosts instances.
func (h *Hosts) Reload() error {
if h.RawText != nil {
return errors.New("cannot call Reload with RawText")
}
h.mu.Lock()
defer h.mu.Unlock()
hfl, err := ParseHosts(h.ReadFilePath)
if err != nil {
return err
}
h.hostFileLines = hfl
return nil
}
// RemoveAddresses removes all entries (lines) with the provided address.
func (h *Hosts) RemoveAddresses(addresses []string) {
for _, address := range addresses {
if h.RemoveFirstAddress(address) {
h.RemoveAddress(address)
}
}
}
// RemoveAddress removes all entries (lines) with the provided address.
func (h *Hosts) RemoveAddress(address string) {
if h.RemoveFirstAddress(address) {
h.RemoveAddress(address)
}
}
// RemoveFirstAddress removes the first entry (line) found with the provided address.
func (h *Hosts) RemoveFirstAddress(address string) bool {
h.mu.Lock()
defer h.mu.Unlock()
for hflIdx := range h.hostFileLines {
if address == h.hostFileLines[hflIdx].Address {
h.hostFileLines = removeHFLElement(h.hostFileLines, hflIdx)
return true
}
}
return false
}
// RemoveCIDRs Remove CIDR Range (Classless inter-domain routing)
// examples:
//
// 127.1.0.0/16 = 127.1.0.0 -> 127.1.255.255
// 127.1.27.0/24 = 127.1.27.0 -> 127.1.27.255
func (h *Hosts) RemoveCIDRs(cidrs []string) error {
addresses := make([]string, 0)
// loop through all the CIDR ranges (we probably have less ranges than IPs)
for _, cidr := range cidrs {
_, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
return fmt.Errorf("parse CIDR %s: %w", cidr, err)
}
hfLines := h.GetHostFileLines()
for _, hfl := range hfLines {
ip := net.ParseIP(hfl.Address)
if ip != nil {
if ipnet.Contains(ip) {
addresses = append(addresses, hfl.Address)
}
}
}
}
h.RemoveAddresses(addresses)
return nil
}
// RemoveHosts removes all hostname entries of the provided host slice.
func (h *Hosts) RemoveHosts(hosts []string) {
for _, host := range hosts {
if h.RemoveFirstHost(host) {
h.RemoveHost(host)
}
}
}
// RemoveHost removes all hostname entries of provided host.
func (h *Hosts) RemoveHost(host string) {
if h.RemoveFirstHost(host) {
h.RemoveHost(host)
}
}
// RemoveFirstHost removes the first hostname entry found and returns true if successful.
func (h *Hosts) RemoveFirstHost(host string) bool {
host = strings.TrimSpace(strings.ToLower(host))
h.mu.Lock()
defer h.mu.Unlock()
for hflIdx := range h.hostFileLines {
for hidx, hst := range h.hostFileLines[hflIdx].Hostnames {
if hst == host {
h.hostFileLines[hflIdx].Hostnames = removeStringElement(h.hostFileLines[hflIdx].Hostnames, hidx)
// remove the address line if empty
if len(h.hostFileLines[hflIdx].Hostnames) < 1 {
h.hostFileLines = removeHFLElement(h.hostFileLines, hflIdx)
}
return true
}
}
}
return false
}
// RemoveByComments removes all host entries that have any of the specified comments.
// This removes entire lines where the comment matches.
// This is part of the public API for bulk comment-based cleanup (e.g., kubefwd teardown).
func (h *Hosts) RemoveByComments(comments []string) {
for _, comment := range comments {
h.RemoveByComment(comment)
}
}
// RemoveByComment removes all host entries that have the specified comment.
// This removes entire lines where the comment matches.
func (h *Hosts) RemoveByComment(comment string) {
h.mu.Lock()
defer h.mu.Unlock()
comment = strings.TrimSpace(comment)
var newLines HostFileLines
for _, hfl := range h.hostFileLines {
if hfl.Comment != comment {
newLines = append(newLines, hfl)
}
}
h.hostFileLines = newLines
}
// AddHosts adds an array of hosts to the first matching address it finds
// or creates the address and adds the hosts.
func (h *Hosts) AddHosts(address string, hosts []string) {
for _, hst := range hosts {
h.AddHost(address, hst)
}
}
// AddHostsWithComment adds an array of hosts to an address with a comment.
// All hosts will share the same comment. If the address already exists with
// the same comment, hosts are appended to that line (respecting MaxHostsPerLine).
// If the address exists with a different comment, a new line is created.
func (h *Hosts) AddHostsWithComment(address string, hosts []string, comment string) {
for _, hst := range hosts {
h.AddHostWithComment(address, hst, comment)
}
}
// AddHost adds a host to an address and removes the host
// from any existing address it may be associated with.
func (h *Hosts) AddHost(addressRaw, hostRaw string) {
h.addHostWithComment(addressRaw, hostRaw, "")
}
// AddHostWithComment adds a host to an address with an inline comment.
// The comment will appear after the hostnames on the line (e.g., "127.0.0.1 host # comment").
// If the address already exists with the same comment, the host is appended to that line
// (respecting MaxHostsPerLine). If the address exists with a different comment, a new line
// is created with the specified comment.
func (h *Hosts) AddHostWithComment(addressRaw, hostRaw, comment string) {
h.addHostWithComment(addressRaw, hostRaw, comment)
}
// addHostWithComment is the internal implementation that handles both
// commented and non-commented host additions.
func (h *Hosts) addHostWithComment(addressRaw, hostRaw, comment string) { //nolint:revive // internal method mirrors public API naming
host := strings.TrimSpace(strings.ToLower(hostRaw))
address := strings.TrimSpace(strings.ToLower(addressRaw))
// Normalize comment: trim spaces, but don't add/remove the # prefix
// (the # is handled during rendering)
comment = strings.TrimSpace(comment)
addressIP := net.ParseIP(address)
if addressIP == nil {
return
}
ipFamily := IPFamilyV4
if addressIP.To4() == nil {
ipFamily = IPFamilyV6
}
h.mu.Lock()
defer h.mu.Unlock()
// does the host already exist
ok, exAdd, hflIdx := h.hostAddressLookupLocked(host, ipFamily)
if ok {
if address == exAdd {
return // already at correct address
}
// hostname is at a different address, remove it from there
h.removeHostFromLineLocked(hflIdx, host, address)
}
// Get the effective max hosts per line limit
maxPerLine := h.getEffectiveMaxHostsPerLine()
// if the address exists with matching comment, add it to that line if there's room
for i, hfl := range h.hostFileLines {
if hfl.Address == address && hfl.Comment == comment {
// Check if this line has room (0 means unlimited)
if maxPerLine <= 0 || len(h.hostFileLines[i].Hostnames) < maxPerLine {
h.hostFileLines[i].Hostnames = append(h.hostFileLines[i].Hostnames, host)
return
}
// This line is full, continue looking for another line with the same address and comment
}
}
// No existing line with matching address and comment found (or all are full), create a new line
hfl := HostFileLine{
LineType: ADDRESS,
Address: address,
Hostnames: []string{host},
Comment: comment,
}
h.hostFileLines = append(h.hostFileLines, hfl)
}
// ListHostsByIP returns a list of hostnames associated with a given IP address.
func (h *Hosts) ListHostsByIP(address string) []string {
h.mu.Lock()
defer h.mu.Unlock()
var hosts []string
for _, hsl := range h.hostFileLines {
if hsl.Address == address {
hosts = append(hosts, hsl.Hostnames...)
}
}
return hosts
}
// ListAddressesByHost returns a list of IPs associated with a given hostname.
func (h *Hosts) ListAddressesByHost(hostname string, exact bool) [][]string {
h.mu.Lock()
defer h.mu.Unlock()
var addresses [][]string
for _, hsl := range h.hostFileLines {
for _, hst := range hsl.Hostnames {
if hst == hostname {
addresses = append(addresses, []string{hsl.Address, hst})
}
if !exact && hst != hostname && strings.Contains(hst, hostname) {
addresses = append(addresses, []string{hsl.Address, hst})
}
}
}
return addresses
}
// ListHostsByCIDR returns a list of IPs and hostnames associated with a given CIDR.
func (h *Hosts) ListHostsByCIDR(cidr string) [][]string {
h.mu.Lock()
defer h.mu.Unlock()
var ipHosts [][]string
_, subnet, err := net.ParseCIDR(cidr)
if err != nil {
return ipHosts
}
for _, hsl := range h.hostFileLines {
if subnet.Contains(net.ParseIP(hsl.Address)) {
for _, hst := range hsl.Hostnames {
ipHosts = append(ipHosts, []string{hsl.Address, hst})
}
}
}
return ipHosts
}
// ListHostsByComment returns all hostnames on lines with the given comment.
func (h *Hosts) ListHostsByComment(comment string) []string {
h.mu.Lock()
defer h.mu.Unlock()
comment = strings.TrimSpace(comment)
var hosts []string
for _, hsl := range h.hostFileLines {
if hsl.Comment == comment {
hosts = append(hosts, hsl.Hostnames...)
}
}
return hosts
}
// HostAddressLookup returns true if the host is found, the address string,
// and the index of the host file line. This is part of the public API for
// consumers that need direct address lookups by IP family.
func (h *Hosts) HostAddressLookup(host string, ipFamily IPFamily) (found bool, address string, idx int) {
h.mu.Lock()
defer h.mu.Unlock()
return h.hostAddressLookupLocked(host, ipFamily)
}
// hostAddressLookupLocked is the internal version that assumes the lock is already held.
func (h *Hosts) hostAddressLookupLocked(host string, ipFamily IPFamily) (found bool, address string, idx int) {
host = strings.ToLower(strings.TrimSpace(host))
for i, hfl := range h.hostFileLines {
for _, hn := range hfl.Hostnames {
ipAddr := net.ParseIP(hfl.Address)
if ipAddr == nil || hn != host {
continue
}
if ipFamily == IPFamilyV4 && ipAddr.To4() != nil {
return true, hfl.Address, i
}
if ipFamily == IPFamilyV6 && ipAddr.To4() == nil {
return true, hfl.Address, i
}
}
}
return false, "", 0
}
// RenderHostsFile returns the hosts file content as a formatted string.
func (h *Hosts) RenderHostsFile() string {
h.mu.Lock()
defer h.mu.Unlock()
var sb strings.Builder
for _, hfl := range h.hostFileLines {
sb.WriteString(lineFormatter(hfl))
sb.WriteByte('\n')
}
return sb.String()
}
// GetHostFileLines returns a copy of all parsed host file lines.
func (h *Hosts) GetHostFileLines() HostFileLines {
h.mu.Lock()
defer h.mu.Unlock()
// Return a copy to prevent external modification of internal state
result := make(HostFileLines, len(h.hostFileLines))
copy(result, h.hostFileLines)
return result
}
// getEffectiveMaxHostsPerLine returns the effective maximum hosts per line.
// Returns 0 for unlimited.
func (h *Hosts) getEffectiveMaxHostsPerLine() int {
if h.HostsConfig == nil {
// No config, use auto-detect
if runtime.GOOS == osWindows {
return DefaultMaxHostsPerLineWindows
}
return 0
}
if h.MaxHostsPerLine > 0 {
// Explicit limit set
return h.MaxHostsPerLine
}
if h.MaxHostsPerLine < 0 {
// Explicitly unlimited
return 0
}
// MaxHostsPerLine == 0: auto-detect based on OS
if runtime.GOOS == osWindows {
return DefaultMaxHostsPerLineWindows
}
return 0
}
// ParseHosts reads and parses a hosts file from the given path.
func ParseHosts(path string) ([]HostFileLine, error) {
input, err := os.ReadFile(filepath.Clean(path))
if err != nil {
return nil, fmt.Errorf("read hosts file %s: %w", path, err)
}
return ParseHostsFromString(string(input))
}
// ParseHostsFromString parses hosts file content from a string.
func ParseHostsFromString(input string) ([]HostFileLine, error) {
inputNormalized := strings.ReplaceAll(input, "\r\n", "\n")
dataLines := strings.Split(inputNormalized, "\n")
// remove extra blank line at end that does not exist in /etc/hosts file
// only if the file ended with a newline (last element is empty)
if len(dataLines) > 0 && dataLines[len(dataLines)-1] == "" {
dataLines = dataLines[:len(dataLines)-1]
}
hostFileLines := make([]HostFileLine, len(dataLines))
// trim leading and trailing whitespace
for i, l := range dataLines {
curLine := &hostFileLines[i]
curLine.OriginalLineNum = i
curLine.Raw = l
// trim line
curLine.Trimmed = strings.TrimSpace(l)
// check for comment
if strings.HasPrefix(curLine.Trimmed, "#") {
curLine.LineType = COMMENT
continue
}
if curLine.Trimmed == "" {
curLine.LineType = EMPTY
continue
}
curLineSplit := strings.SplitN(curLine.Trimmed, "#", 2)
if len(curLineSplit) > 1 {
curLine.Comment = strings.TrimSpace(curLineSplit[1])
}
curLine.Trimmed = curLineSplit[0]
curLine.Parts = strings.Fields(curLine.Trimmed)
if len(curLine.Parts) > 1 {
curLine.LineType = ADDRESS
curLine.Address = strings.ToLower(curLine.Parts[0])
// lower case all
for _, p := range curLine.Parts[1:] {
curLine.Hostnames = append(curLine.Hostnames, strings.ToLower(p))
}
continue
}
// if we can't figure out what this line is
// at this point mark it as unknown
curLine.LineType = UNKNOWN
}
return hostFileLines, nil
}
// removeStringElement removes an element of a string slice.
func removeStringElement(slice []string, s int) []string {
return append(slice[:s], slice[s+1:]...)
}
// removeHFLElement removes an element of a HostFileLine slice.
func removeHFLElement(slice []HostFileLine, s int) []HostFileLine {
return append(slice[:s], slice[s+1:]...)
}
// removeHostFromLineLocked removes a hostname from a specific line and cleans up empty lines.
// Must be called with lock held. For localhost addresses, this is a no-op since
// the same hostname can exist at multiple localhost addresses.
func (h *Hosts) removeHostFromLineLocked(hflIdx int, host, newAddress string) {
if isLocalhost(newAddress) {
return
}
for hidx, hst := range h.hostFileLines[hflIdx].Hostnames {
if hst != host {
continue
}
h.hostFileLines[hflIdx].Hostnames = removeStringElement(h.hostFileLines[hflIdx].Hostnames, hidx)
if len(h.hostFileLines[hflIdx].Hostnames) == 0 {
h.hostFileLines = removeHFLElement(h.hostFileLines, hflIdx)
}
return
}
}
// lineFormatter formats a single host file line as a string.
func lineFormatter(hfl HostFileLine) string {
if hfl.LineType < ADDRESS {
return hfl.Raw
}
if hfl.Comment != "" {
return fmt.Sprintf("%-16s %s # %s", hfl.Address, strings.Join(hfl.Hostnames, " "), hfl.Comment)
}
return fmt.Sprintf("%-16s %s", hfl.Address, strings.Join(hfl.Hostnames, " "))
}
// ipLocalhost is a regex pattern for IPv4 or IPv6 loopback range.
const ipLocalhost = `((127\.(\d{1,3}\.){2}\d{1,3})|(::1)$)`
var localhostIPRegexp = regexp.MustCompile(ipLocalhost)
func isLocalhost(address string) bool {
return localhostIPRegexp.MatchString(address)
}
// winDefaultHostsFile returns the default hosts file path for Windows.
// It tries to use the SystemRoot environment variable. If that is not set,
// it falls back to C:\Windows\System32\Drivers\etc\hosts.
func winDefaultHostsFile() string {
if r := os.Getenv("SystemRoot"); r != "" {
return filepath.Join(r, "System32", "drivers", "etc", "hosts")
}
// fallback to C:\
return `C:\Windows\System32\drivers\etc\hosts`
}