-
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.
Merge pull request #401 from evidolob/hosts-file-records
Check 'hosts' file records, during handling DNS requests
- Loading branch information
Showing
17 changed files
with
1,660 additions
and
3 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,97 @@ | ||
package dns | ||
|
||
import ( | ||
"net" | ||
"path/filepath" | ||
"sync" | ||
|
||
"github.com/areYouLazy/libhosty" | ||
"github.com/fsnotify/fsnotify" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type HostsFile 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 := &HostsFile{ | ||
hostsFile: hostsFile, | ||
hostsFilePath: hostsFile.Config.FilePath, | ||
} | ||
go func() { | ||
h.startWatch(watcher) | ||
}() | ||
return h, nil | ||
} | ||
|
||
func (h *HostsFile) startWatch(w *fsnotify.Watcher) { | ||
err := w.Add(filepath.Dir(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.Name == h.hostsFilePath && event.Op&fsnotify.Write == fsnotify.Write { | ||
err := h.updateHostsFile() | ||
if err != nil { | ||
log.Errorf("Hosts file read error:%s", err) | ||
return | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (h *HostsFile) LookupByHostname(name string) (net.IP, error) { | ||
h.hostsReadLock.RLock() | ||
defer h.hostsReadLock.RUnlock() | ||
|
||
_, ip, err := h.hostsFile.LookupByHostname(name) | ||
return ip, err | ||
} | ||
|
||
func (h *HostsFile) updateHostsFile() error { | ||
newHosts, err := readHostsFile(h.hostsFilePath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
h.hostsReadLock.Lock() | ||
defer h.hostsReadLock.Unlock() | ||
|
||
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.