The /etc/hosts parsing and resolver library for golang

Hostsfile

Documentation Build Status Report Card

The /etc/hosts parsing and resolver library for golang

Hostsfile is a go (golang) package for parsing hosts files and performing reverse IP -> hostname lookups which are file-based (e.g. via /etc/hosts).

This can be helpful to determine if there is a prettier (or known) hostname available for an IP address.

These lookups are "extremely inexpensive" compared to normal IP reverse DNS lookups because no network communication is required, as these lookups are all file-based! Naturally, the (obvious) tradeoff/downside is that this only works in cases where the IP mapping exists in the hostsfile.

Get it

go get -u github.com/jaytaylor/go-hostsfile

Usage

package main

import (
    "fmt"

    "github.com/jaytaylor/go-hostsfile"
)

func main() {
    res, err := hostsfile.ReverseLookup("127.0.0.1")
    if err != nil {
        panic(err)
    }
    fmt.Printf("%v maps 127.0.0.1 to the following names: %v", hostsfile.HostsPath, res)
}

Output:

/etc/hosts maps 127.0.0.1 to the following names: [localhost]

Supported Operating Systems

Tested and verified working on:

  • Linux
  • Mac OS X
  • Windows

It should also support Plan9, though admittedly I don't have a plan9 installation available to verify that one ;)

Unit-tests

Running the unit-tests is straightforward and standard:

go test

License

Permissive MIT license.

Contact

You are more than welcome to open issues and send pull requests if you find a bug or want a new feature.

If you appreciate this library please feel free to drop me a line and tell me! It's always nice to hear from people who have benefitted from my work.

Email: jay at (my github username).com

Twitter: @jtaylor

Comments
  • Support Go 1.18

    Support Go 1.18

    Fix:

    • containerd/nerdctl#900
    $ cd nerdctl
    $ go mod tidy
    github.com/containerd/nerdctl/pkg/dnsutil/hostsstore imports
            github.com/jaytaylor/go-hostsfile tested by
            github.com/jaytaylor/go-hostsfile.test imports
            ../go-hostsfile: "../go-hostsfile" is relative, but relative import paths are not supported in module mode
    
  • Use UNIX path on all OSs other than Windows and Plan9

    Use UNIX path on all OSs other than Windows and Plan9

    Previous settings will cause this library fail to build in other OSs including many BSD distributions (FreeBSD, OpenBSD, NetBSD, etc). I found this problem while trying to build it on my FreeBSD server.

    Since /etc/hosts is a pretty standard path for finding hosts file, it should be safe enough to set /etc/hosts as the default path.

  • support in-line cooments #5

    support in-line cooments #5

    Thank you for a very good library. When I looked at the issues, there were some that were not supported, so I sent a Pull Request.

    However, there are performance concerns with this implementation.

    It will be a bit complicated, but should I change the processing between the cases with in-line comment and without it?

  • In-line comments are not supported

    In-line comments are not supported

    Hello,

    First, thanks for the library, it is a nice helper.

    Yet, I have found a limitation and would kindly ask if you might give a look: it seems like in-line comments are not handled as expected. Typically, such lines in the /etc/hosts file (in GNU-Linux):

    127.0.0.1	localhost #my-other-host my-other-commented-host
    127.0.0.1	my.host # my-third-commented-host
    

    returns such an array:

    ["localhost", "#my-other-host", "my-other-commented-host", "my.host", "#", "my-third-commented-host"]
    

    where one would rather expect:

    ["localhost", "my.host"]
    
  • https:// should be removed

    https:// should be removed

    $ go get -u https://github.com/jaytaylor/go-hostsfile package https:/github.com/jaytaylor/go-hostsfile: https:/github.com/jaytaylor/go-hostsfile: invalid import path: malformed import path "https:/github.com/jaytaylor/go-hostsfile": invalid char ':'

Resolver (DNS) cache daemon.
Resolver (DNS) cache daemon.

RESCACHED(1) Manual Page NAME rescached - DNS resolver cache daemon. Table of Contents SYNOPSIS OPTIONS DESCRIPTION FEATURES BEHIND THE DNS HOW CACHE

Nov 17, 2022
A tool to solve DNS pollution of GitHub website. Query the real IP address of domain names such as github.com, and refresh the domain name setting of the system hosts file.

githubDNS Target A tool to solve DNS pollution of GitHub website. Query the real IP address of domain names such as github.com, and refresh the domain

Oct 14, 2021
A BitMessage headless client and library

bitz A BitMessage headless client and library. The initial focus is to implement the BitMessage protocol and provide a library that others can use. Sp

Nov 26, 2020
DNS library in Go

Alternative (more granular) approach to a DNS library Less is more. Complete and usable DNS library. All Resource Records are supported, including the

Dec 26, 2022
Multicast DNS library for Go

Introduction This package allows Go processes to publish multicast DNS style records onto their local network segment. For more information about mDNS

Oct 23, 2022
Simple application written in Go that combines two wordlists and a list of TLDs to form domain names and check if they are already registered.

Domainerator Domainerator was my first Go application. It combines two wordlists (prefixes and suffixes) and a list of TLDs to form domain names and c

Sep 16, 2022
🐶 Command-line DNS Client for Humans. Written in Golang
🐶 Command-line DNS Client for Humans. Written in Golang

doggo ?? Command-line DNS client for humans doggo is a modern command-line DNS client (like dig) written in Golang. It outputs information in a neat c

Dec 29, 2022
Goconf - The rudimentary golang config tool purpose is to config PHP servers via SSH

goconf The rudimentary golang config tool purpose is to config PHP servers via S

Jan 31, 2022
Fast and lightweight DNS proxy as ad-blocker for local network with many features

Blocky Blocky is a DNS proxy and ad-blocker for the local network written in Go with following features: Features Blocking - Blocking of DNS queries w

Jan 1, 2023
DNS server using miekg/dns offering dynamic subdomains, time-over-dns, and standard zone file support.

dns-go DNS server using miekg/dns offering dynamic subdomains, time-over-dns, and standard zone file support. dynamic subdomains web.myapp.192.168.1.1

Dec 14, 2021
Finally a simple, modern and open source interface for domain name.

happyDNS Finally a simple, modern and open source interface for domain name. It consists of a HTTP REST API written in Golang (primarily based on http

May 24, 2022
Free and open source, powerful network-wide ads & trackers blocking DNS server
Free and open source, powerful network-wide ads & trackers blocking DNS server

Privacy protection center for you and your devices Free and open source, powerful network-wide ads & trackers blocking DNS server. AdGuard.com | Wiki

Nov 20, 2021
Go library and CLI utility for /etc/hosts management.
Go library and CLI utility for /etc/hosts management.

Etc Hosts Management Utility & Go Library /etc/hosts Management It is easy to open your /etc/hosts file in text editor and add or remove entries. Howe

Nov 27, 2022
Hosty is a command-line utility that allows for fast inspection and editing of /etc/hosts-like files

Hosty Description Hosty is a command-line utility that allows for fast inspection and editing of /etc/hosts-like files. It is written in golang and us

Sep 3, 2021
Your dev tool to manage /etc/hosts like a pro!
Your dev tool to manage /etc/hosts like a pro!

hostctl This tool gives you more control over the use of your hosts file. You can have multiple profiles and switch them on/off as you need. Why? It i

Jan 2, 2023
go tool for working with /etc/hosts files

hostsfile This library, and the associated command line binary, will help you manipulate your /etc/hosts file. Both the library and the binary will le

Dec 30, 2022
ByNom is a Go package for parsing byte sequences, suitable for parsing text and binary data

ByNom is a Go package for parsing byte sequences. Its goal is to provide tools to build safe byte parsers without compromising the speed or memo

May 5, 2021
memresolver is an in-memory golang resolver that allows to override current golang Lookup func literals

mem-resolver memresolver is an in-memory golang resolver that allows to override current golang Lookup func literals How to use it Create your custom

Jun 23, 2022
Create cluster to run ingress controller and set the dns resolver
Create cluster to run ingress controller and set the dns resolver

kubebuilder-crd-dep-svc-ing create cluster to run ingress controller and set the dns resolver $ kind create cluster --config clust.yaml $ sudo

Nov 15, 2021
A GREAT GUI Offline Tool for manipulating/seeking resolver list of repique and dnscrypt proxy.
A GREAT GUI Offline Tool for manipulating/seeking resolver list of repique and dnscrypt proxy.

Intro A GUI Offline Tool for decrypting and manipulating *.md files used by repique and dnscrypt proxy It's targeted for creating your own DoT, DoH an

Nov 27, 2022