hostess/hostess.go

132 lines
2.3 KiB
Go
Raw Normal View History

package hostess
2015-01-30 06:46:05 +00:00
import (
"errors"
2015-01-30 06:46:05 +00:00
"fmt"
"io/ioutil"
"os"
"regexp"
2015-01-30 06:46:05 +00:00
)
const default_osx = `
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
`
const default_linux = `
127.0.0.1 localhost
127.0.1.1 HOSTNAME
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
`
2015-01-30 06:46:05 +00:00
type Hostname struct {
Domain string
Ip string
Enabled bool
}
type Hostfile struct {
Path string
2015-01-30 07:38:57 +00:00
Hosts map[string]*Hostname
data string
2015-01-30 06:46:05 +00:00
}
func NewHostfile(path string) *Hostfile {
2015-01-30 07:38:57 +00:00
return &Hostfile{path, make(map[string]*Hostname), ""}
2015-01-30 06:46:05 +00:00
}
2015-01-30 07:38:57 +00:00
func (h *Hostfile) Read() string {
data, err := ioutil.ReadFile(h.Path)
2015-01-30 06:46:05 +00:00
if err != nil {
2015-01-30 07:38:57 +00:00
fmt.Println("Can't read ", h.Path)
2015-01-30 06:46:05 +00:00
os.Exit(1)
}
2015-01-30 07:38:57 +00:00
h.data = string(data)
return h.data
2015-01-30 06:46:05 +00:00
}
func Dump(hostnames []Hostname) string {
return ""
}
func DumpToFile(path string) {
2015-01-30 06:46:05 +00:00
}
var line_parser = regexp.MustCompile(``)
func parseLine(line string) (Hostname, error) {
// 1. Split on # to discard comments.
// 2. Split on first space to find the IP
// 3. Split remainder of line on whitespace to find
// domain names
// 4. Validate the IP (maybe -- could be ipv4 or ipv6)
hostname := Hostname{}
if false {
return hostname, errors.New("Can't parse hostname")
}
return hostname, nil
2015-01-30 06:46:05 +00:00
}
func Read(hostfile string) []Hostname {
2015-01-30 06:46:05 +00:00
var hosts = make([]Hostname, 0)
return hosts
}
func ReadFile(path string) {
}
2015-01-30 06:46:05 +00:00
func (h *Hostfile) Add(host Hostname) {
2015-01-30 07:38:57 +00:00
h.Hosts[host.Domain] = &host
2015-01-30 06:46:05 +00:00
}
2015-01-30 07:38:57 +00:00
func (h *Hostfile) Delete(domain string) {
delete(h.Hosts, domain)
2015-01-30 06:46:05 +00:00
}
2015-01-30 07:38:57 +00:00
func (h *Hostfile) Enable(domain string) {
_, ok := h.Hosts[domain]
if ok {
h.Hosts[domain].Enabled = true
}
2015-01-30 06:46:05 +00:00
}
2015-01-30 07:38:57 +00:00
func (h *Hostfile) Disable(domain string) {
_, ok := h.Hosts[domain]
if ok {
h.Hosts[domain].Enabled = false
}
2015-01-30 06:46:05 +00:00
}
func GetHostsPath() string {
2015-01-30 06:46:05 +00:00
path := os.Getenv("HOSTESS_FILE")
if path == "" {
path = "/etc/hosts"
}
return path
}
func Hostess() {
hostfile := NewHostfile(GetHostsPath())
2015-01-30 07:38:57 +00:00
hostfile.Read()
hostfile.Add(Hostname{"localhost", "127.0.0.1", true})
hostfile.Enable("localhost")
2015-01-30 06:46:05 +00:00
}