2023-12-05 21:30:51 +00:00
|
|
|
package hosts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const readBufferSize = 8192
|
|
|
|
|
|
|
|
func NewHostsFile(filePath string) (*File, error) {
|
2023-12-06 15:09:55 +00:00
|
|
|
theHostFile := &File{
|
|
|
|
filePath: filePath,
|
|
|
|
}
|
|
|
|
err := theHostFile.ReadHostsFile()
|
|
|
|
if err == nil {
|
|
|
|
return theHostFile, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type File struct {
|
|
|
|
filePath string
|
|
|
|
Entries []Entry
|
|
|
|
lineEnding string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) ReadHostsFile() error {
|
|
|
|
f.Entries = nil
|
|
|
|
theFile, err := os.Open(f.filePath)
|
2023-12-05 21:30:51 +00:00
|
|
|
if err != nil {
|
2023-12-06 15:09:55 +00:00
|
|
|
return err
|
2023-12-05 21:30:51 +00:00
|
|
|
}
|
|
|
|
defer theFile.Close()
|
|
|
|
var lenIn int
|
2023-12-06 15:09:55 +00:00
|
|
|
f.lineEnding = ""
|
2023-12-05 21:30:51 +00:00
|
|
|
theCBuffer := ""
|
|
|
|
theBuffer := make([]byte, readBufferSize)
|
|
|
|
for err == nil {
|
|
|
|
lenIn, err = theFile.Read(theBuffer)
|
|
|
|
if lenIn > 0 {
|
|
|
|
theCBuffer += string(theBuffer[:lenIn])
|
2023-12-06 15:09:55 +00:00
|
|
|
if f.lineEnding == "" {
|
2023-12-05 21:30:51 +00:00
|
|
|
if strings.Contains(theCBuffer, "\r\n") {
|
2023-12-06 15:09:55 +00:00
|
|
|
f.lineEnding = "\r\n"
|
2023-12-05 21:30:51 +00:00
|
|
|
} else if strings.Contains(theCBuffer, "\r") {
|
2023-12-06 15:09:55 +00:00
|
|
|
f.lineEnding = "\r"
|
2023-12-05 21:30:51 +00:00
|
|
|
} else if strings.Contains(theCBuffer, "\n") {
|
2023-12-06 15:09:55 +00:00
|
|
|
f.lineEnding = "\n"
|
2023-12-05 21:30:51 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-06 15:09:55 +00:00
|
|
|
if f.lineEnding == "\r\n" {
|
2023-12-05 21:30:51 +00:00
|
|
|
strings.ReplaceAll(theCBuffer, "\r\n", "\n")
|
2023-12-06 15:09:55 +00:00
|
|
|
} else if f.lineEnding == "\r" {
|
2023-12-05 21:30:51 +00:00
|
|
|
strings.ReplaceAll(theCBuffer, "\r", "\n")
|
|
|
|
}
|
|
|
|
splt := strings.Split(theCBuffer, "\n")
|
|
|
|
for i := 0; i < len(splt)-1; i++ {
|
2023-12-06 15:09:55 +00:00
|
|
|
f.Entries = append(f.Entries, NewHostsEntry(splt[i]))
|
2023-12-05 21:30:51 +00:00
|
|
|
}
|
|
|
|
theCBuffer = splt[len(splt)-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != io.EOF {
|
2023-12-06 15:09:55 +00:00
|
|
|
return err
|
2023-12-05 21:30:51 +00:00
|
|
|
}
|
|
|
|
if theCBuffer != "" {
|
2023-12-06 15:09:55 +00:00
|
|
|
f.Entries = append(f.Entries, NewHostsEntry(theCBuffer))
|
2023-12-05 21:30:51 +00:00
|
|
|
}
|
2023-12-06 15:09:55 +00:00
|
|
|
return nil
|
2023-12-05 21:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f File) WriteHostsFile() error {
|
|
|
|
theFile, err := os.OpenFile(f.filePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer theFile.Close()
|
|
|
|
for _, entry := range f.Entries {
|
|
|
|
_, err = theFile.WriteString(entry.ToLine() + f.lineEnding)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f File) HasDomain(domain string) bool {
|
|
|
|
for _, entry := range f.Entries {
|
|
|
|
if entry.HasDomain(domain) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f File) indexDomainSingleton(domain string) int {
|
|
|
|
for i, entry := range f.Entries {
|
|
|
|
if len(entry.Domains) == 1 && entry.HasDomain(domain) {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f File) HasDomainSingleton(domain string) bool {
|
|
|
|
return f.indexDomainSingleton(domain) > -1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) OverwriteDomainSingleton(domain string, ipAddress string) {
|
|
|
|
idx := f.indexDomainSingleton(domain)
|
|
|
|
if idx == -1 {
|
|
|
|
f.Entries = append(f.Entries, Entry{
|
|
|
|
IPAddress: ipAddress,
|
|
|
|
Domains: []string{domain},
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
theEntry := f.Entries[idx]
|
|
|
|
theEntry.IPAddress = ipAddress
|
|
|
|
f.Entries = append(append(f.Entries[:idx], theEntry), f.Entries[idx+1:]...)
|
|
|
|
}
|
|
|
|
}
|