2023-11-17 20:05:21 +01:00
|
|
|
// hosts: utility for modifying the /etc/hosts file
|
|
|
|
package hosts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// HOSTS_FILE is the hosts file location
|
|
|
|
const HOSTS_FILE = "/etc/hosts"
|
|
|
|
|
|
|
|
const DOMAIN_HEADER = "#WG AUTO GENERATED HOSTS"
|
|
|
|
const DOMAIN_TRAILER = "#WG AUTO GENERATED HOSTS END"
|
|
|
|
|
2023-11-17 23:13:51 +01:00
|
|
|
type HostsEntry struct {
|
|
|
|
Alias string
|
|
|
|
Ip net.IP
|
|
|
|
}
|
|
|
|
|
2023-11-17 20:05:21 +01:00
|
|
|
// Generic interface to manipulate /etc/hosts file
|
|
|
|
type HostsManipulator interface {
|
|
|
|
// AddrAddr associates an aliasd with a given IP address
|
2023-11-17 23:13:51 +01:00
|
|
|
AddAddr(hosts ...HostsEntry)
|
2023-11-17 20:05:21 +01:00
|
|
|
// Remove deletes the entry from /etc/hosts
|
2023-11-17 23:13:51 +01:00
|
|
|
Remove(hosts ...HostsEntry)
|
2023-11-17 20:05:21 +01:00
|
|
|
// Writes the changes to /etc/hosts file
|
|
|
|
Write() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type HostsManipulatorImpl struct {
|
2023-11-17 23:13:51 +01:00
|
|
|
hosts map[string]HostsEntry
|
2023-11-17 20:05:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddAddr implements HostsManipulator.
|
2023-11-17 23:13:51 +01:00
|
|
|
func (m *HostsManipulatorImpl) AddAddr(hosts ...HostsEntry) {
|
|
|
|
changed := false
|
|
|
|
|
|
|
|
for _, host := range hosts {
|
|
|
|
prev, ok := m.hosts[host.Ip.String()]
|
|
|
|
|
|
|
|
if !ok || prev.Alias != host.Alias {
|
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
|
|
|
|
m.hosts[host.Ip.String()] = host
|
|
|
|
}
|
|
|
|
|
|
|
|
if changed {
|
|
|
|
m.Write()
|
|
|
|
}
|
2023-11-17 20:05:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove implements HostsManipulator.
|
2023-11-17 23:13:51 +01:00
|
|
|
func (m *HostsManipulatorImpl) Remove(hosts ...HostsEntry) {
|
|
|
|
lenBefore := len(m.hosts)
|
2023-11-17 20:05:21 +01:00
|
|
|
|
2023-11-17 23:13:51 +01:00
|
|
|
for _, host := range hosts {
|
|
|
|
delete(m.hosts, host.Alias)
|
|
|
|
}
|
|
|
|
|
|
|
|
if lenBefore != len(m.hosts) {
|
|
|
|
m.Write()
|
|
|
|
}
|
2023-11-17 20:05:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *HostsManipulatorImpl) removeHosts() string {
|
|
|
|
hostsFile, err := os.ReadFile(HOSTS_FILE)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
var contents strings.Builder
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(bytes.NewReader(hostsFile))
|
|
|
|
|
|
|
|
hostsSection := false
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-11-17 23:13:51 +01:00
|
|
|
if !hostsSection && strings.Contains(line, DOMAIN_HEADER) {
|
2023-11-17 20:05:21 +01:00
|
|
|
hostsSection = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !hostsSection {
|
|
|
|
contents.WriteString(line + "\n")
|
|
|
|
}
|
|
|
|
|
2023-11-17 23:13:51 +01:00
|
|
|
if hostsSection && strings.Contains(line, DOMAIN_TRAILER) {
|
2023-11-17 20:05:21 +01:00
|
|
|
hostsSection = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if scanner.Err() != nil && scanner.Err() != io.EOF {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return contents.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write implements HostsManipulator
|
|
|
|
func (m *HostsManipulatorImpl) Write() error {
|
|
|
|
contents := m.removeHosts()
|
|
|
|
|
|
|
|
var nextHosts strings.Builder
|
|
|
|
nextHosts.WriteString(contents)
|
|
|
|
|
2023-11-17 23:13:51 +01:00
|
|
|
nextHosts.WriteString(DOMAIN_HEADER + "\n")
|
2023-11-17 20:05:21 +01:00
|
|
|
|
2023-11-17 23:13:51 +01:00
|
|
|
for _, host := range m.hosts {
|
|
|
|
nextHosts.WriteString(fmt.Sprintf("%s\t%s\n", host.Ip.String(), host.Alias))
|
2023-11-17 20:05:21 +01:00
|
|
|
}
|
|
|
|
|
2023-11-17 23:13:51 +01:00
|
|
|
nextHosts.WriteString(DOMAIN_TRAILER + "\n")
|
2023-11-17 20:05:21 +01:00
|
|
|
return os.WriteFile(HOSTS_FILE, []byte(nextHosts.String()), 0644)
|
|
|
|
}
|
|
|
|
|
2023-11-17 23:13:51 +01:00
|
|
|
func NewHostsManipulator() HostsManipulator {
|
|
|
|
return &HostsManipulatorImpl{hosts: make(map[string]HostsEntry)}
|
2023-11-17 20:05:21 +01:00
|
|
|
}
|