2015-02-01 02:20:42 +00:00
|
|
|
package hostess
|
2015-01-30 06:46:05 +00:00
|
|
|
|
|
|
|
import (
|
2015-02-01 02:20:42 +00:00
|
|
|
"errors"
|
2015-01-30 06:46:05 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-02-01 02:20:42 +00:00
|
|
|
"regexp"
|
2015-01-30 06:46:05 +00:00
|
|
|
)
|
|
|
|
|
2015-01-31 05:45:20 +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
|
|
|
}
|
|
|
|
|
2015-02-01 03:23:05 +00:00
|
|
|
func Dump(hostnames []Hostname) string {
|
2015-02-01 02:20:42 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-02-01 03:23:05 +00:00
|
|
|
func DumpToFile(path string) {
|
2015-01-30 06:46:05 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-01 02:20:42 +00:00
|
|
|
var line_parser = regexp.MustCompile(``)
|
|
|
|
|
|
|
|
func parseLine(line string) (Hostname, error) {
|
2015-02-01 03:23:05 +00:00
|
|
|
// 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)
|
2015-02-01 02:20:42 +00:00
|
|
|
hostname := Hostname{}
|
|
|
|
if false {
|
|
|
|
return hostname, errors.New("Can't parse hostname")
|
|
|
|
}
|
|
|
|
return hostname, nil
|
2015-01-30 06:46:05 +00:00
|
|
|
}
|
|
|
|
|
2015-02-01 03:23:05 +00:00
|
|
|
func Read(hostfile string) []Hostname {
|
2015-01-30 06:46:05 +00:00
|
|
|
var hosts = make([]Hostname, 0)
|
|
|
|
return hosts
|
|
|
|
}
|
|
|
|
|
2015-02-01 03:23:05 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-02-01 02:20:42 +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
|
|
|
|
}
|
|
|
|
|
2015-02-01 02:20:42 +00:00
|
|
|
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
|
|
|
}
|