-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check 'hosts' file records, during handling DNS requests
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
Showing
17 changed files
with
1,652 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.