File: libhosty.go

package info (click to toggle)
golang-github-areyoulazy-libhosty 1.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 176 kB
  • sloc: makefile: 2
file content (846 lines) | stat: -rw-r--r-- 21,572 bytes parent folder | download
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
//Package libhosty is a pure golang library to manipulate the hosts file
package libhosty

import (
	"io/ioutil"
	"net"
	"os"
	"regexp"
	"runtime"
	"strings"
	"sync"
)

const (
	//Version exposes library version
	Version = "2.0"
)

const (
	// defines default path for windows os
	windowsFilePath = "C:\\Windows\\System32\\drivers\\etc\\"

	// defines default path for linux os
	unixFilePath = "/etc/"

	// defines default filename
	hostsFileName = "hosts"
)

//LineType define a safe type for line type enumeration
type LineType int

const (
	//LineTypeUnknown defines unknown lines
	LineTypeUnknown LineType = 0

	//LineTypeEmpty defines empty lines
	LineTypeEmpty LineType = 10

	//LineTypeComment defines comment lines (starts with #)
	LineTypeComment LineType = 20

	//LineTypeAddress defines address lines (actual hosts lines)
	LineTypeAddress LineType = 30
)

//HostsFileConfig defines parameters to find hosts file.
// FilePath is the absolute path of the hosts file (filename included)
type HostsFileConfig struct {
	FilePath string
}

//HostsFileLine holds hosts file lines data
type HostsFileLine struct {
	//Number is the original line number
	Number int

	//LineType defines the line type
	Type LineType

	//Address is a net.IP representation of the address
	Address net.IP

	//Parts is a slice of the line splitted by '#'
	Parts []string

	//Hostnames is a slice of hostnames for the relative IP
	Hostnames []string

	//Raw is the raw representation of the line, as it is in the hosts file
	Raw string

	//Comment is the comment part of the line (if present in an ADDRESS line)
	Comment string

	//IsCommented to know if the current ADDRESS line is commented out (starts with '#')
	IsCommented bool

	//trimed is a trimed version (no spaces before and after) of the line
	trimed string
}

//HostsFile is a reference for the hosts file configuration and lines
type HostsFile struct {
	sync.Mutex

	//Config reference to a HostsConfig object
	Config *HostsFileConfig

	//HostsFileLines slice of HostsFileLine objects
	HostsFileLines []HostsFileLine
}

//InitWithConfig returns a new instance of a hostsfile.
// InitWithConfig is meant to be used with a custom conf file
// however InitWithConfig() will fallback to Init() if conf is nill
// You should use Init() to load hosts file from default location
func InitWithConfig(conf *HostsFileConfig) (*HostsFile, error) {
	var config *HostsFileConfig
	var err error

	if conf != nil {
		config = conf
	} else {
		return Init()
	}

	// allocate a new HostsFile object
	hf := &HostsFile{
		// use default configuration
		Config: config,

		// allocate a new slice of HostsFileLine objects
		HostsFileLines: make([]HostsFileLine, 0),
	}

	// parse the hosts file and load file lines
	hf.HostsFileLines, err = ParseHostsFile(hf.Config.FilePath)
	if err != nil {
		return nil, err
	}

	//return HostsFile
	return hf, nil
}

//Init returns a new instance of a hostsfile.
func Init() (*HostsFile, error) {
	// initialize hostsConfig
	config, err := NewHostsFileConfig("")
	if err != nil {
		return nil, err
	}

	// allocate a new HostsFile object
	hf := &HostsFile{
		// use default configuration
		Config: config,

		// allocate a new slice of HostsFileLine objects
		HostsFileLines: make([]HostsFileLine, 0),
	}

	// parse the hosts file and load file lines
	hf.HostsFileLines, err = ParseHostsFile(hf.Config.FilePath)
	if err != nil {
		return nil, err
	}

	//return HostsFile
	return hf, nil
}

//NewHostsFileConfig loads hosts file based on environment.
// NewHostsFileConfig initialize the default file path based
// on the OS or from a given location if a custom path is provided
func NewHostsFileConfig(path string) (*HostsFileConfig, error) {
	// allocate hostsConfig
	var hc *HostsFileConfig

	// ensure custom path exists
	// https://stackoverflow.com/questions/12518876/how-to-check-if-a-file-exists-in-go
	if fh, err := os.Stat(path); err == nil {
		// eusure custom path points to a file (not a directory)
		if !fh.IsDir() {
			hc = &HostsFileConfig{
				FilePath: path,
			}
		}
	} else {
		// check os to construct default path
		switch runtime.GOOS {
		case "windows":
			hc = &HostsFileConfig{
				FilePath: windowsFilePath + hostsFileName,
			}
		default:
			hc = &HostsFileConfig{
				FilePath: unixFilePath + hostsFileName,
			}
		}
	}

	return hc, nil
}

//GetHostsFileLines returns every address row
func (h *HostsFile) GetHostsFileLines() []*HostsFileLine {
	var hfl []*HostsFileLine

	for idx := range h.HostsFileLines {
		if h.HostsFileLines[idx].Type == LineTypeAddress {
			hfl = append(hfl, h.GetHostsFileLineByRow(idx))
		}
	}

	return hfl
}

//GetHostsFileLineByRow returns a ponter to the given HostsFileLine row
func (h *HostsFile) GetHostsFileLineByRow(row int) *HostsFileLine {
	return &h.HostsFileLines[row]
}

//GetHostsFileLineByIP returns the index of the line and a ponter to the given HostsFileLine line
func (h *HostsFile) GetHostsFileLineByIP(ip net.IP) (int, *HostsFileLine) {
	if ip == nil {
		return -1, nil
	}

	for idx := range h.HostsFileLines {
		if net.IP.Equal(ip, h.HostsFileLines[idx].Address) {
			return idx, &h.HostsFileLines[idx]
		}
	}

	return -1, nil
}

func (h *HostsFile) GetHostsFileLinesByIP(ip net.IP) []*HostsFileLine {
	if ip == nil {
		return nil
	}

	hfl := make([]*HostsFileLine, 0)

	for idx := range h.HostsFileLines {
		if net.IP.Equal(ip, h.HostsFileLines[idx].Address) {
			hfl = append(hfl, &h.HostsFileLines[idx])
		}
	}

	return hfl
}

//GetHostsFileLineByAddress returns the index of the line and a ponter to the given HostsFileLine line
func (h *HostsFile) GetHostsFileLineByAddress(address string) (int, *HostsFileLine) {
	ip := net.ParseIP(address)
	return h.GetHostsFileLineByIP(ip)
}

func (h *HostsFile) GetHostsFileLinesByAddress(address string) []*HostsFileLine {
	ip := net.ParseIP(address)
	return h.GetHostsFileLinesByIP(ip)
}

//GetHostsFileLineByHostname returns the index of the line and a ponter to the given HostsFileLine line
func (h *HostsFile) GetHostsFileLineByHostname(hostname string) (int, *HostsFileLine) {
	for idx := range h.HostsFileLines {
		for _, hn := range h.HostsFileLines[idx].Hostnames {
			if hn == hostname {
				return idx, &h.HostsFileLines[idx]
			}
		}
	}

	return -1, nil
}

func (h *HostsFile) GetHostsFileLinesByHostname(hostname string) []*HostsFileLine {
	hfl := make([]*HostsFileLine, 0)

	for idx := range h.HostsFileLines {
		for _, hn := range h.HostsFileLines[idx].Hostnames {
			if hn == hostname {
				hfl = append(hfl, &h.HostsFileLines[idx])
				continue
			}
		}
	}

	return hfl
}

func (h *HostsFile) GetHostsFileLinesByHostnameAsRegexp(hostname string) []*HostsFileLine {
	hfl := make([]*HostsFileLine, 0)

	reg := regexp.MustCompile(hostname)

	for idx := range h.HostsFileLines {
		for _, hn := range h.HostsFileLines[idx].Hostnames {
			if reg.MatchString(hn) {
				hfl = append(hfl, &h.HostsFileLines[idx])
				continue
			}
		}
	}

	return hfl
}

//RenderHostsFile render and returns the hosts file with the lineFormatter() routine
func (h *HostsFile) RenderHostsFile() string {
	// allocate a buffer for file lines
	var sliceBuffer []string

	// iterate HostsFileLines and popolate the buffer with formatted lines
	for _, l := range h.HostsFileLines {
		sliceBuffer = append(sliceBuffer, lineFormatter(l))
	}

	// strings.Join() prevent the last line from being a new blank line
	// as opposite to a for loop with fmt.Printf(buffer + '\n')
	return strings.Join(sliceBuffer, "\n")
}

//RenderHostsFileLine render and returns the given hosts line with the lineFormatter() routine
func (h *HostsFile) RenderHostsFileLine(row int) string {
	// iterate to find the row to render
	if len(h.HostsFileLines) > row {
		return lineFormatter(h.HostsFileLines[row])
	}

	return ""
}

//SaveHostsFile write hosts file to configured path.
// error is not nil if something goes wrong
func (h *HostsFile) SaveHostsFile() error {
	return h.SaveHostsFileAs(h.Config.FilePath)
}

//SaveHostsFileAs write hosts file to the given path.
// error is not nil if something goes wrong
func (h *HostsFile) SaveHostsFileAs(path string) error {
	// render the file as a byte slice
	dataBytes := []byte(h.RenderHostsFile())

	// write file to disk
	err := ioutil.WriteFile(path, dataBytes, 0644)
	if err != nil {
		return err
	}

	return nil
}

//RemoveHostsFileLineByRow remove row at given index from HostsFileLines
func (h *HostsFile) RemoveHostsFileLineByRow(row int) {
	// prevent out-of-index
	if row < len(h.HostsFileLines) {
		h.Lock()
		h.HostsFileLines = append(h.HostsFileLines[:row], h.HostsFileLines[row+1:]...)
		h.Unlock()
	}
}

func (h *HostsFile) RemoveHostsFileLineByIP(ip net.IP) {
	for idx := len(h.HostsFileLines) - 1; idx >= 0; idx-- {
		if net.IP.Equal(ip, h.HostsFileLines[idx].Address) {
			h.RemoveHostsFileLineByRow(idx)
			return
		}
	}
}

func (h *HostsFile) RemoveHostsFileLinesByIP(ip net.IP) {
	for idx := len(h.HostsFileLines) - 1; idx >= 0; idx-- {
		if net.IP.Equal(ip, h.HostsFileLines[idx].Address) {
			h.RemoveHostsFileLineByRow(idx)
		}
	}
}

func (h *HostsFile) RemoveHostsFileLineByAddress(address string) {
	ip := net.ParseIP(address)

	h.RemoveHostsFileLineByIP(ip)
}

func (h *HostsFile) RemoveHostsFileLinesByAddress(address string) {
	ip := net.ParseIP(address)

	h.RemoveHostsFileLinesByIP(ip)
}

func (h *HostsFile) RemoveHostsFileLineByHostname(hostname string) {
	for idx := len(h.HostsFileLines) - 1; idx >= 0; idx-- {
		if h.HostsFileLines[idx].Type == LineTypeAddress {
			for _, hn := range h.HostsFileLines[idx].Hostnames {
				if hn == hostname {
					h.RemoveHostsFileLineByRow(idx)
					return
				}
			}
		}
	}
}

func (h *HostsFile) RemoveHostsFileLinesByHostnameAsRegexp(hostname string) {
	reg := regexp.MustCompile(hostname)

	for idx := len(h.HostsFileLines) - 1; idx >= 0; idx-- {
		for _, hn := range h.HostsFileLines[idx].Hostnames {
			if reg.MatchString(hn) {
				h.RemoveHostsFileLineByRow(idx)
				continue
			}
		}
	}
}

func (h *HostsFile) RemoveHostsFileLinesByHostname(hostname string) {
	for idx := len(h.HostsFileLines) - 1; idx >= 0; idx-- {
		if h.HostsFileLines[idx].Type == LineTypeAddress {
			for _, hn := range h.HostsFileLines[idx].Hostnames {
				if hn == hostname {
					h.RemoveHostsFileLineByRow(idx)
					continue
				}
			}
		}
	}
}

//LookupByHostname check if the given fqdn exists.
// if yes, it returns the index of the address and the associated address.
// error is not nil if something goes wrong
func (h *HostsFile) LookupByHostname(hostname string) (int, net.IP, error) {
	for idx, hfl := range h.HostsFileLines {
		for _, hn := range hfl.Hostnames {
			if hn == hostname {
				return idx, h.HostsFileLines[idx].Address, nil
			}
		}
	}

	return -1, nil, ErrHostnameNotFound
}

//AddHostsFileLineRaw add the given ip/fqdn/comment pair
// this is different from AddHostFileLine because it does not take care of duplicates
// this just append the new entry to the hosts file
func (h *HostsFile) AddHostsFileLineRaw(ipRaw, fqdnRaw, comment string) (int, *HostsFileLine, error) {
	// hostname to lowercase
	hostname := strings.ToLower(fqdnRaw)
	// parse ip to net.IP
	ip := net.ParseIP(ipRaw)

	// if we have a valid IP
	if ip != nil {
		// create a new hosts line
		hfl := HostsFileLine{
			Type:        LineTypeAddress,
			Address:     ip,
			Hostnames:   []string{hostname},
			Comment:     comment,
			IsCommented: false,
		}

		// append to hosts
		h.HostsFileLines = append(h.HostsFileLines, hfl)

		// get index
		idx := len(h.HostsFileLines) - 1

		// return created entry
		return idx, &h.HostsFileLines[idx], nil
	}

	// return error
	return -1, nil, ErrCannotParseIPAddress(ipRaw)
}

//AddHostsFileLine add the given ip/fqdn/comment pair, cleanup is done for previous entry.
// it returns the index of the edited (created) line and a pointer to the hostsfileline object.
// error is not nil if something goes wrong
func (h *HostsFile) AddHostsFileLine(ipRaw, fqdnRaw, comment string) (int, *HostsFileLine, error) {
	// hostname to lowercase
	hostname := strings.ToLower(fqdnRaw)
	// parse ip to net.IP
	ip := net.ParseIP(ipRaw)

	// if we have a valid IP
	if ip != nil {
		//check if we alredy have the fqdn
		if idx, addr, err := h.LookupByHostname(hostname); err == nil {
			//if actual ip is the same as the given one, we are done
			if net.IP.Equal(addr, ip) {
				// handle comment
				if comment != "" {
					// just replace the current comment with the new one
					h.HostsFileLines[idx].Comment = comment
				}
				return idx, &h.HostsFileLines[idx], nil
			}

			//if address is different, we need to remove the hostname from the previous entry
			for hostIdx, hn := range h.HostsFileLines[idx].Hostnames {
				if hn == hostname {
					if len(h.HostsFileLines[idx].Hostnames) > 1 {
						h.Lock()
						h.HostsFileLines[idx].Hostnames = append(h.HostsFileLines[idx].Hostnames[:hostIdx], h.HostsFileLines[idx].Hostnames[hostIdx+1:]...)
						h.Unlock()
					}

					//remove the line if there are no more hostnames (other than the actual one)
					if len(h.HostsFileLines[idx].Hostnames) <= 1 {
						h.RemoveHostsFileLineByRow(idx)
					}
				}
			}
		}

		//if we alredy have the address, just add the hostname to that line
		for idx, hfl := range h.HostsFileLines {
			if net.IP.Equal(hfl.Address, ip) {
				h.Lock()
				h.HostsFileLines[idx].Hostnames = append(h.HostsFileLines[idx].Hostnames, hostname)
				h.Unlock()

				// handle comment
				if comment != "" {
					// just replace the current comment with the new one
					h.HostsFileLines[idx].Comment = comment
				}

				// return edited entry
				return idx, &h.HostsFileLines[idx], nil
			}
		}

		// at this point we need to create new host line
		hfl := HostsFileLine{
			Type:        LineTypeAddress,
			Address:     ip,
			Hostnames:   []string{hostname},
			Comment:     comment,
			IsCommented: false,
		}

		// generate raw version of the line
		hfl.Raw = lineFormatter(hfl)

		// append to hosts
		h.HostsFileLines = append(h.HostsFileLines, hfl)

		// get index
		idx := len(h.HostsFileLines) - 1

		// return created entry
		return idx, &h.HostsFileLines[idx], nil
	}

	// return error
	return -1, nil, ErrCannotParseIPAddress(ipRaw)
}

//AddCommentFileLine adds a new line of type comment with the given comment.
// it returns the index of the edited (created) line and a pointer to the hostsfileline object.
// error is not nil if something goes wrong
func (h *HostsFile) AddCommentFileLine(comment string) (int, *HostsFileLine, error) {
	h.Lock()
	defer h.Unlock()

	hfl := HostsFileLine{
		Type:    LineTypeComment,
		Raw:     "# " + comment,
		Comment: comment,
	}

	hfl.Raw = lineFormatter(hfl)

	h.HostsFileLines = append(h.HostsFileLines, hfl)
	idx := len(h.HostsFileLines) - 1
	return idx, &h.HostsFileLines[idx], nil
}

//AddEmptyFileLine adds a new line of type empty.
// it returns the index of the edited (created) line and a pointer to the hostsfileline object.
// error is not nil if something goes wrong
func (h *HostsFile) AddEmptyFileLine() (int, *HostsFileLine, error) {
	h.Lock()
	defer h.Unlock()

	hfl := HostsFileLine{
		Type: LineTypeEmpty,
		Raw:  "",
	}

	h.HostsFileLines = append(h.HostsFileLines, hfl)
	idx := len(h.HostsFileLines) - 1
	return idx, &h.HostsFileLines[idx], nil
}

//CommentHostsFileLineByRow set the IsCommented bit for the given row to true
func (h *HostsFile) CommentHostsFileLineByRow(row int) error {
	h.Lock()
	defer h.Unlock()

	if len(h.HostsFileLines) > row {
		if h.HostsFileLines[row].Type == LineTypeAddress {
			if !h.HostsFileLines[row].IsCommented {
				h.HostsFileLines[row].IsCommented = true

				h.HostsFileLines[row].Raw = h.RenderHostsFileLine(row)
				return nil
			}

			return ErrAlredyCommentedLine
		}

		return ErrNotAnAddressLine
	}

	return ErrUnknown
}

//CommentHostsFileLineByIP set the IsCommented bit for the given address to true
func (h *HostsFile) CommentHostsFileLineByIP(ip net.IP) error {
	h.Lock()
	defer h.Unlock()

	for idx := range h.HostsFileLines {
		if net.IP.Equal(ip, h.HostsFileLines[idx].Address) {
			if !h.HostsFileLines[idx].IsCommented {
				h.HostsFileLines[idx].IsCommented = true

				h.HostsFileLines[idx].Raw = h.RenderHostsFileLine(idx)
				return nil
			}

			return ErrAlredyCommentedLine
		}
	}

	return ErrAddressNotFound
}

func (h *HostsFile) CommentHostsFileLinesByIP(ip net.IP) {
	h.Lock()
	defer h.Unlock()

	for idx := range h.HostsFileLines {
		if net.IP.Equal(ip, h.HostsFileLines[idx].Address) {
			if !h.HostsFileLines[idx].IsCommented {
				h.HostsFileLines[idx].IsCommented = true

				h.HostsFileLines[idx].Raw = h.RenderHostsFileLine(idx)
			}
		}
	}
}

//CommentHostsFileLineByAddress set the IsCommented bit for the given address as string to true
func (h *HostsFile) CommentHostsFileLineByAddress(address string) error {
	ip := net.ParseIP(address)

	return h.CommentHostsFileLineByIP(ip)
}

func (h *HostsFile) CommentHostsFileLinesByAddress(address string) {
	ip := net.ParseIP(address)
	h.CommentHostsFileLinesByIP(ip)
}

//CommentHostsFileLineByHostname set the IsCommented bit for the given hostname to true
func (h *HostsFile) CommentHostsFileLineByHostname(hostname string) error {
	h.Lock()
	defer h.Unlock()

	for idx := range h.HostsFileLines {
		for _, hn := range h.HostsFileLines[idx].Hostnames {
			if hn == hostname {
				if !h.HostsFileLines[idx].IsCommented {
					h.HostsFileLines[idx].IsCommented = true

					h.HostsFileLines[idx].Raw = h.RenderHostsFileLine(idx)
					return nil
				}

				return ErrAlredyCommentedLine
			}
		}
	}

	return ErrHostnameNotFound
}

func (h *HostsFile) CommentHostsFileLinesByHostname(hostname string) {
	h.Lock()
	defer h.Unlock()

	for idx := range h.HostsFileLines {
		for _, hn := range h.HostsFileLines[idx].Hostnames {
			if hn == hostname {
				if !h.HostsFileLines[idx].IsCommented {
					h.HostsFileLines[idx].IsCommented = true

					h.HostsFileLines[idx].Raw = h.RenderHostsFileLine(idx)
				}
			}
		}
	}
}

func (h *HostsFile) CommentHostsFileLinesByHostnameAsRegexp(hostname string) {
	h.Lock()
	defer h.Unlock()

	reg := regexp.MustCompile(hostname)

	for idx := range h.HostsFileLines {
		for _, hn := range h.HostsFileLines[idx].Hostnames {
			if reg.MatchString(hn) {
				if !h.HostsFileLines[idx].IsCommented {
					h.HostsFileLines[idx].IsCommented = true

					h.HostsFileLines[idx].Raw = h.RenderHostsFileLine(idx)
					continue
				}
			}
		}
	}
}

//UncommentHostsFileLineByRow set the IsCommented bit for the given row to false
func (h *HostsFile) UncommentHostsFileLineByRow(row int) error {
	h.Lock()
	defer h.Unlock()

	if len(h.HostsFileLines) > row {
		if h.HostsFileLines[row].Type == LineTypeAddress {
			if h.HostsFileLines[row].IsCommented {
				h.HostsFileLines[row].IsCommented = false

				h.HostsFileLines[row].Raw = h.RenderHostsFileLine(row)
				return nil
			}

			return ErrAlredyUncommentedLine
		}

		return ErrNotAnAddressLine
	}

	return ErrUnknown
}

//UncommentHostsFileLineByIP set the IsCommented bit for the given address to false
func (h *HostsFile) UncommentHostsFileLineByIP(ip net.IP) error {
	h.Lock()
	defer h.Unlock()

	for idx, hfl := range h.HostsFileLines {
		if net.IP.Equal(ip, hfl.Address) {
			if h.HostsFileLines[idx].IsCommented {
				h.HostsFileLines[idx].IsCommented = false

				h.HostsFileLines[idx].Raw = h.RenderHostsFileLine(idx)
				return nil
			}

			return ErrAlredyUncommentedLine
		}
	}

	return ErrNotAnAddressLine
}

func (h *HostsFile) UncommentHostsFileLinesByIP(ip net.IP) {
	h.Lock()
	defer h.Unlock()

	for idx := range h.HostsFileLines {
		if net.IP.Equal(ip, h.HostsFileLines[idx].Address) {
			if h.HostsFileLines[idx].IsCommented {
				h.HostsFileLines[idx].IsCommented = false

				h.HostsFileLines[idx].Raw = h.RenderHostsFileLine(idx)
			}
		}
	}
}

//UncommentHostsFileLineByAddress set the IsCommented bit for the given address as string to false
func (h *HostsFile) UncommentHostsFileLineByAddress(address string) error {
	ip := net.ParseIP(address)

	return h.UncommentHostsFileLineByIP(ip)
}

func (h *HostsFile) UncommentHostsFileLinesByAddress(address string) {
	ip := net.ParseIP(address)
	h.UncommentHostsFileLinesByIP(ip)
}

//UncommentHostsFileLineByHostname set the IsCommented bit for the given hostname to false
func (h *HostsFile) UncommentHostsFileLineByHostname(hostname string) error {
	h.Lock()
	defer h.Unlock()

	for idx := range h.HostsFileLines {
		for _, hn := range h.HostsFileLines[idx].Hostnames {
			if hn == hostname {
				if h.HostsFileLines[idx].IsCommented {
					h.HostsFileLines[idx].IsCommented = false

					h.HostsFileLines[idx].Raw = h.RenderHostsFileLine(idx)
					return nil
				}

				return ErrAlredyUncommentedLine
			}
		}
	}

	return ErrHostnameNotFound
}

func (h *HostsFile) UncommentHostsFileLinesByHostname(hostname string) {
	h.Lock()
	defer h.Unlock()

	for idx := range h.HostsFileLines {
		for _, hn := range h.HostsFileLines[idx].Hostnames {
			if hn == hostname {
				if h.HostsFileLines[idx].IsCommented {
					h.HostsFileLines[idx].IsCommented = false

					h.HostsFileLines[idx].Raw = h.RenderHostsFileLine(idx)
				}
			}
		}
	}
}

func (h *HostsFile) UncommentHostsFileLinesByHostnameAsRegexp(hostname string) {
	h.Lock()
	defer h.Unlock()

	reg := regexp.MustCompile(hostname)

	for idx := range h.HostsFileLines {
		for _, hn := range h.HostsFileLines[idx].Hostnames {
			if reg.MatchString(hn) {
				if h.HostsFileLines[idx].IsCommented {
					h.HostsFileLines[idx].IsCommented = false

					h.HostsFileLines[idx].Raw = h.RenderHostsFileLine(idx)
					continue
				}
			}
		}
	}
}