Skip to content

Commit

Permalink
Check 'hosts' file records, during handling DNS requests
Browse files Browse the repository at this point in the history
This implementation use 'libhosty' lib to read and search for hosts file records. It reads 'hosts' file first and adds file watcher to track changes in 'hosts' file

Signed-off-by: Yevhen Vydolob <[email protected]>
  • Loading branch information
evidolob committed Sep 23, 2024
1 parent 05d2239 commit 7f9dc16
Show file tree
Hide file tree
Showing 17 changed files with 1,652 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22
require (
github.com/Microsoft/go-winio v0.6.2
github.com/apparentlymart/go-cidr v1.1.0
github.com/areYouLazy/libhosty v1.1.0
github.com/containers/winquit v1.1.0
github.com/coreos/stream-metadata-go v0.4.4
github.com/dustin/go-humanize v1.0.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERo
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
github.com/apparentlymart/go-cidr v1.1.0 h1:2mAhrMoF+nhXqxTzSZMUzDHkLjmIHC+Zzn4tdgBZjnU=
github.com/apparentlymart/go-cidr v1.1.0/go.mod h1:EBcsNrHc3zQeuaeCeCtQruQm+n9/YjEn/vI25Lg7Gwc=
github.com/areYouLazy/libhosty v1.1.0 h1:kO6UTk9z72cHW28A/V1kKi7C8iKQGqINiVGXp+05Eao=
github.com/areYouLazy/libhosty v1.1.0/go.mod h1:dV4ir3feRrTbWdcJ21mt3MeZlASg0sc8db6nimL9GOA=
github.com/armon/go-proxyproto v0.0.0-20210323213023-7e956b284f0a/go.mod h1:QmP9hvJ91BbJmGVGSbutW19IC0Q9phDCLGaomwTJbgU=
github.com/containers/winquit v1.1.0 h1:jArun04BNDQvt2W0Y78kh9TazN2EIEMG5Im6/JY7+pE=
github.com/containers/winquit v1.1.0/go.mod h1:PsPeZlnbkmGGIToMPHF1zhWjBUkd8aHjMOr/vFcPxw8=
Expand Down
28 changes: 28 additions & 0 deletions pkg/services/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package dns

import (
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"strings"
"sync"

"github.com/areYouLazy/libhosty"
"github.com/containers/gvisor-tap-vsock/pkg/types"
"github.com/miekg/dns"
log "github.com/sirupsen/logrus"
Expand All @@ -18,6 +20,7 @@ type dnsHandler struct {
zonesLock sync.RWMutex
udpClient *dns.Client
tcpClient *dns.Client
hostsFile HostsFile
nameserver string
}

Expand All @@ -28,11 +31,17 @@ func newDNSHandler(zones []types.Zone) (*dnsHandler, error) {
return nil, err
}

hostsFile, err := NewHostsFile("")
if err != nil {
return nil, err
}

return &dnsHandler{
zones: zones,
tcpClient: &dns.Client{Net: "tcp"},
udpClient: &dns.Client{Net: "udp"},
nameserver: net.JoinHostPort(nameserver, port),
hostsFile: hostsFile,
}, nil

}
Expand Down Expand Up @@ -98,6 +107,24 @@ func (h *dnsHandler) addLocalAnswers(m *dns.Msg, q dns.Question) bool {
m.Rcode = dns.RcodeNameError
return true
}
ip, err := h.hostsFile.LookupByHostname(q.Name)
if err != nil {
// ignore only ErrHostnameNotFound error
if !errors.Is(err, libhosty.ErrHostnameNotFound) {
log.Errorf("Error during looking in hosts file records: %v", err)
}
} else {
m.Answer = append(m.Answer, &dns.A{
Hdr: dns.RR_Header{
Name: q.Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 0,
},
A: ip,
})
return true
}
}
return false
}
Expand All @@ -106,6 +133,7 @@ func (h *dnsHandler) addAnswers(dnsClient *dns.Client, r *dns.Msg) *dns.Msg {
m := new(dns.Msg)
m.SetReply(r)
m.RecursionAvailable = true

for _, q := range m.Question {
if done := h.addLocalAnswers(m, q); done {
return m
Expand Down
94 changes: 94 additions & 0 deletions pkg/services/dns/hosts_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package dns

import (
"net"
"sync"

"github.com/areYouLazy/libhosty"
"github.com/fsnotify/fsnotify"
log "github.com/sirupsen/logrus"
)

type HostsFile interface {
LookupByHostname(name string) (net.IP, error)
}

type hosts struct {
hostsReadLock sync.RWMutex
hostsFilePath string
hostsFile *libhosty.HostsFile
}

// NewHostsFile Creates new HostsFile instance
// Pass ""(empty string) if you want to use default hosts file
func NewHostsFile(hostsPath string) (HostsFile, error) {
hostsFile, err := readHostsFile(hostsPath)
if err != nil {
return nil, err
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}

h := &hosts{
hostsFile: hostsFile,
hostsFilePath: hostsFile.Config.FilePath,
}
go func() {
h.startWatch(watcher)
}()
return h, nil
}

func (h *hosts) startWatch(w *fsnotify.Watcher) {
err := w.Add(h.hostsFilePath)
if err != nil {
log.Errorf("Hosts file adding watcher error:%s", err)
return
}
for {
select {
case err, ok := <-w.Errors:
if !ok {
return
}
log.Errorf("Hosts file watcher error:%s", err)
case event, ok := <-w.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
err := h.updateHostsFile()
if err != nil {
log.Errorf("Hosts file read error:%s", err)
return
}
}
}
}
}

func (h *hosts) LookupByHostname(name string) (net.IP, error) {
_, ip, err := h.hostsFile.LookupByHostname(name)
return ip, err
}

func (h *hosts) updateHostsFile() error {
h.hostsReadLock.RLock()
defer h.hostsReadLock.RUnlock()
newHosts, err := readHostsFile(h.hostsFilePath)
if err != nil {
return err
}
h.hostsFile = newHosts
return nil
}

func readHostsFile(hostsFilePath string) (*libhosty.HostsFile, error) {
config, err := libhosty.NewHostsFileConfig(hostsFilePath)
if err != nil {
return nil, err
}
return libhosty.InitWithConfig(config)
}
41 changes: 41 additions & 0 deletions pkg/services/dns/hosts_file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package dns

import (
"os"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestHostsFile(t *testing.T) {
hostsFile := filepath.Join(t.TempDir(), "hosts")
assert.NoError(t, os.WriteFile(hostsFile, []byte(`127.0.0.1 entry1`), 0600))

hosts, err := NewHostsFile(hostsFile)
assert.NoError(t, err)
ip, err := hosts.LookupByHostname("entry1")
assert.NoError(t, err)
assert.Equal(t, "127.0.0.1", ip.String())
}

func TestReloadingHostsFile(t *testing.T) {
hostsFile := filepath.Join(t.TempDir(), "hosts")
assert.NoError(t, os.WriteFile(hostsFile, []byte(`127.0.0.1 entry1`), 0600))

hosts, err := NewHostsFile(hostsFile)
time.Sleep(time.Second)
assert.NoError(t, err)
ip, err := hosts.LookupByHostname("entry1")
assert.NoError(t, err)
assert.Equal(t, "127.0.0.1", ip.String())

assert.NoError(t, os.WriteFile(hostsFile, []byte(`127.0.0.1 entry2 foobar`), 0600))
time.Sleep(time.Second)

ipBar, err := hosts.LookupByHostname("foobar")
assert.NoError(t, err)
assert.Equal(t, "127.0.0.1", ipBar.String())

}
5 changes: 5 additions & 0 deletions vendor/github.com/areYouLazy/libhosty/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7f9dc16

Please sign in to comment.