The /etc/hosts parsing and resolver library for golang

Overview

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
    
    opened by AkihiroSuda 6
  • 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.

    opened by RedL0tus 6
  • 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?

    opened by bootjp 4
  • 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"]
    
    opened by bsinou 2
  • 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 ':'

    opened by pzghost 1
Releases(releases/5.7.1)
Owner
J. Elliot Taylor
J. Elliot Taylor
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

Shulhan 28 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

null 0 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

Yves Junqueira 12 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

Miek Gieben 6.6k Dec 4, 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

Dave Cheney 192 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

Herbert Fischer 27 Sep 16, 2022
🐶 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

Karan Sharma 1k Nov 26, 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

Mohamed Badawi 0 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

Dimitri Herzog 2.1k Dec 1, 2022
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

Jean Ribes 0 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

happyDNS 7 May 24, 2022
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

Lupael 1 Nov 20, 2021
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

txn2: Kubernetes apps and system utilities. 277 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

null 11 Sep 3, 2021
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

Andrew Bashkatov 4 May 5, 2021
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

Gustavo 824 Nov 30, 2022
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

Kevin Burke 123 Nov 12, 2022
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

Antonio Ojea 4 Jun 23, 2022
🔎Sniffing and parsing mysql,redis,http,mongodb etc protocol. 抓包截取项目中的数据库请求并解析成相应的语句。

go-sniffer Capture mysql,redis,http,mongodb etc protocol... 抓包截取项目中的数据库请求并解析成相应的语句,如mysql协议会解析为sql语句,便于调试。 不要修改代码,直接嗅探项目中的数据请求。 中文使用说明 Support List: m

Four 1.7k Dec 3, 2022
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

null 1 Nov 15, 2021